aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/tty/serial
diff options
context:
space:
mode:
authorGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
committerGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
commitc71c03bda1e86c9d5198c5d83f712e695c4f2a1e (patch)
treeecb166cb3e2b7e2adb3b5e292245fefd23381ac8 /drivers/tty/serial
parentea53c912f8a86a8567697115b6a0d8152beee5c8 (diff)
parent6a00f206debf8a5c8899055726ad127dbeeed098 (diff)
Merge branch 'mpi-master' into wip-k-fmlpwip-k-fmlp
Conflicts: litmus/sched_cedf.c
Diffstat (limited to 'drivers/tty/serial')
-rw-r--r--drivers/tty/serial/21285.c511
-rw-r--r--drivers/tty/serial/68328serial.c1448
-rw-r--r--drivers/tty/serial/68328serial.h187
-rw-r--r--drivers/tty/serial/68360serial.c2979
-rw-r--r--drivers/tty/serial/8250.c3424
-rw-r--r--drivers/tty/serial/8250.h79
-rw-r--r--drivers/tty/serial/8250_accent.c45
-rw-r--r--drivers/tty/serial/8250_acorn.c141
-rw-r--r--drivers/tty/serial/8250_boca.c59
-rw-r--r--drivers/tty/serial/8250_early.c287
-rw-r--r--drivers/tty/serial/8250_exar_st16c554.c50
-rw-r--r--drivers/tty/serial/8250_fourport.c51
-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.c56
-rw-r--r--drivers/tty/serial/8250_mca.c61
-rw-r--r--drivers/tty/serial/8250_pci.c3956
-rw-r--r--drivers/tty/serial/8250_pnp.c521
-rw-r--r--drivers/tty/serial/Kconfig1638
-rw-r--r--drivers/tty/serial/Makefile98
-rw-r--r--drivers/tty/serial/altera_jtaguart.c516
-rw-r--r--drivers/tty/serial/altera_uart.c661
-rw-r--r--drivers/tty/serial/amba-pl010.c823
-rw-r--r--drivers/tty/serial/amba-pl011.c2027
-rw-r--r--drivers/tty/serial/apbuart.c696
-rw-r--r--drivers/tty/serial/apbuart.h64
-rw-r--r--drivers/tty/serial/atmel_serial.c1828
-rw-r--r--drivers/tty/serial/bcm63xx_uart.c901
-rw-r--r--drivers/tty/serial/bfin_5xx.c1597
-rw-r--r--drivers/tty/serial/bfin_sport_uart.c934
-rw-r--r--drivers/tty/serial/bfin_sport_uart.h86
-rw-r--r--drivers/tty/serial/clps711x.c577
-rw-r--r--drivers/tty/serial/cpm_uart/Makefile11
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart.h145
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_core.c1440
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_cpm1.c136
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_cpm1.h32
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_cpm2.c172
-rw-r--r--drivers/tty/serial/cpm_uart/cpm_uart_cpm2.h32
-rw-r--r--drivers/tty/serial/crisv10.c4572
-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.c1414
-rw-r--r--drivers/tty/serial/ifx6x60.h129
-rw-r--r--drivers/tty/serial/imx.c1379
-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/lantiq.c756
-rw-r--r--drivers/tty/serial/m32r_sio.c1193
-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.c1470
-rw-r--r--drivers/tty/serial/mpc52xx_uart.c1524
-rw-r--r--drivers/tty/serial/mpsc.c2159
-rw-r--r--drivers/tty/serial/mrst_max3110.c920
-rw-r--r--drivers/tty/serial/mrst_max3110.h60
-rw-r--r--drivers/tty/serial/msm_serial.c966
-rw-r--r--drivers/tty/serial/msm_serial.h187
-rw-r--r--drivers/tty/serial/msm_serial_hs.c1880
-rw-r--r--drivers/tty/serial/msm_smd_tty.c235
-rw-r--r--drivers/tty/serial/mux.c633
-rw-r--r--drivers/tty/serial/mxs-auart.c798
-rw-r--r--drivers/tty/serial/netx-serial.c748
-rw-r--r--drivers/tty/serial/nwpserial.c477
-rw-r--r--drivers/tty/serial/of_serial.c206
-rw-r--r--drivers/tty/serial/omap-serial.c1362
-rw-r--r--drivers/tty/serial/pch_uart.c1612
-rw-r--r--drivers/tty/serial/pmac_zilog.c2206
-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.c877
-rw-r--r--drivers/tty/serial/s3c2400.c105
-rw-r--r--drivers/tty/serial/s3c2410.c117
-rw-r--r--drivers/tty/serial/s3c2412.c151
-rw-r--r--drivers/tty/serial/s3c2440.c180
-rw-r--r--drivers/tty/serial/s3c24a0.c117
-rw-r--r--drivers/tty/serial/s3c6400.c151
-rw-r--r--drivers/tty/serial/s5pv210.c161
-rw-r--r--drivers/tty/serial/sa1100.c916
-rw-r--r--drivers/tty/serial/samsung.c1486
-rw-r--r--drivers/tty/serial/samsung.h119
-rw-r--r--drivers/tty/serial/sb1250-duart.c974
-rw-r--r--drivers/tty/serial/sc26xx.c757
-rw-r--r--drivers/tty/serial/serial_core.c2504
-rw-r--r--drivers/tty/serial/serial_cs.c870
-rw-r--r--drivers/tty/serial/serial_ks8695.c703
-rw-r--r--drivers/tty/serial/serial_txx9.c1342
-rw-r--r--drivers/tty/serial/sh-sci.c2083
-rw-r--r--drivers/tty/serial/sh-sci.h468
-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.c654
-rw-r--r--drivers/tty/serial/ucc_uart.c1539
-rw-r--r--drivers/tty/serial/vr41xx_siu.c978
-rw-r--r--drivers/tty/serial/vt8500_serial.c646
-rw-r--r--drivers/tty/serial/xilinx_uartps.c1113
-rw-r--r--drivers/tty/serial/zs.c1304
-rw-r--r--drivers/tty/serial/zs.h284
123 files changed, 102053 insertions, 0 deletions
diff --git a/drivers/tty/serial/21285.c b/drivers/tty/serial/21285.c
new file mode 100644
index 000000000000..1b37626e8f13
--- /dev/null
+++ b/drivers/tty/serial/21285.c
@@ -0,0 +1,511 @@
1/*
2 * Driver for the serial port on the 21285 StrongArm-110 core logic chip.
3 *
4 * Based on drivers/char/serial.c
5 */
6#include <linux/module.h>
7#include <linux/tty.h>
8#include <linux/ioport.h>
9#include <linux/init.h>
10#include <linux/console.h>
11#include <linux/device.h>
12#include <linux/tty_flip.h>
13#include <linux/serial_core.h>
14#include <linux/serial.h>
15#include <linux/io.h>
16
17#include <asm/irq.h>
18#include <asm/mach-types.h>
19#include <asm/hardware/dec21285.h>
20#include <mach/hardware.h>
21
22#define BAUD_BASE (mem_fclk_21285/64)
23
24#define SERIAL_21285_NAME "ttyFB"
25#define SERIAL_21285_MAJOR 204
26#define SERIAL_21285_MINOR 4
27
28#define RXSTAT_DUMMY_READ 0x80000000
29#define RXSTAT_FRAME (1 << 0)
30#define RXSTAT_PARITY (1 << 1)
31#define RXSTAT_OVERRUN (1 << 2)
32#define RXSTAT_ANYERR (RXSTAT_FRAME|RXSTAT_PARITY|RXSTAT_OVERRUN)
33
34#define H_UBRLCR_BREAK (1 << 0)
35#define H_UBRLCR_PARENB (1 << 1)
36#define H_UBRLCR_PAREVN (1 << 2)
37#define H_UBRLCR_STOPB (1 << 3)
38#define H_UBRLCR_FIFO (1 << 4)
39
40static const char serial21285_name[] = "Footbridge UART";
41
42#define tx_enabled(port) ((port)->unused[0])
43#define rx_enabled(port) ((port)->unused[1])
44
45/*
46 * The documented expression for selecting the divisor is:
47 * BAUD_BASE / baud - 1
48 * However, typically BAUD_BASE is not divisible by baud, so
49 * we want to select the divisor that gives us the minimum
50 * error. Therefore, we want:
51 * int(BAUD_BASE / baud - 0.5) ->
52 * int(BAUD_BASE / baud - (baud >> 1) / baud) ->
53 * int((BAUD_BASE - (baud >> 1)) / baud)
54 */
55
56static void serial21285_stop_tx(struct uart_port *port)
57{
58 if (tx_enabled(port)) {
59 disable_irq_nosync(IRQ_CONTX);
60 tx_enabled(port) = 0;
61 }
62}
63
64static void serial21285_start_tx(struct uart_port *port)
65{
66 if (!tx_enabled(port)) {
67 enable_irq(IRQ_CONTX);
68 tx_enabled(port) = 1;
69 }
70}
71
72static void serial21285_stop_rx(struct uart_port *port)
73{
74 if (rx_enabled(port)) {
75 disable_irq_nosync(IRQ_CONRX);
76 rx_enabled(port) = 0;
77 }
78}
79
80static void serial21285_enable_ms(struct uart_port *port)
81{
82}
83
84static irqreturn_t serial21285_rx_chars(int irq, void *dev_id)
85{
86 struct uart_port *port = dev_id;
87 struct tty_struct *tty = port->state->port.tty;
88 unsigned int status, ch, flag, rxs, max_count = 256;
89
90 status = *CSR_UARTFLG;
91 while (!(status & 0x10) && max_count--) {
92 ch = *CSR_UARTDR;
93 flag = TTY_NORMAL;
94 port->icount.rx++;
95
96 rxs = *CSR_RXSTAT | RXSTAT_DUMMY_READ;
97 if (unlikely(rxs & RXSTAT_ANYERR)) {
98 if (rxs & RXSTAT_PARITY)
99 port->icount.parity++;
100 else if (rxs & RXSTAT_FRAME)
101 port->icount.frame++;
102 if (rxs & RXSTAT_OVERRUN)
103 port->icount.overrun++;
104
105 rxs &= port->read_status_mask;
106
107 if (rxs & RXSTAT_PARITY)
108 flag = TTY_PARITY;
109 else if (rxs & RXSTAT_FRAME)
110 flag = TTY_FRAME;
111 }
112
113 uart_insert_char(port, rxs, RXSTAT_OVERRUN, ch, flag);
114
115 status = *CSR_UARTFLG;
116 }
117 tty_flip_buffer_push(tty);
118
119 return IRQ_HANDLED;
120}
121
122static irqreturn_t serial21285_tx_chars(int irq, void *dev_id)
123{
124 struct uart_port *port = dev_id;
125 struct circ_buf *xmit = &port->state->xmit;
126 int count = 256;
127
128 if (port->x_char) {
129 *CSR_UARTDR = port->x_char;
130 port->icount.tx++;
131 port->x_char = 0;
132 goto out;
133 }
134 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
135 serial21285_stop_tx(port);
136 goto out;
137 }
138
139 do {
140 *CSR_UARTDR = xmit->buf[xmit->tail];
141 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
142 port->icount.tx++;
143 if (uart_circ_empty(xmit))
144 break;
145 } while (--count > 0 && !(*CSR_UARTFLG & 0x20));
146
147 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
148 uart_write_wakeup(port);
149
150 if (uart_circ_empty(xmit))
151 serial21285_stop_tx(port);
152
153 out:
154 return IRQ_HANDLED;
155}
156
157static unsigned int serial21285_tx_empty(struct uart_port *port)
158{
159 return (*CSR_UARTFLG & 8) ? 0 : TIOCSER_TEMT;
160}
161
162/* no modem control lines */
163static unsigned int serial21285_get_mctrl(struct uart_port *port)
164{
165 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
166}
167
168static void serial21285_set_mctrl(struct uart_port *port, unsigned int mctrl)
169{
170}
171
172static void serial21285_break_ctl(struct uart_port *port, int break_state)
173{
174 unsigned long flags;
175 unsigned int h_lcr;
176
177 spin_lock_irqsave(&port->lock, flags);
178 h_lcr = *CSR_H_UBRLCR;
179 if (break_state)
180 h_lcr |= H_UBRLCR_BREAK;
181 else
182 h_lcr &= ~H_UBRLCR_BREAK;
183 *CSR_H_UBRLCR = h_lcr;
184 spin_unlock_irqrestore(&port->lock, flags);
185}
186
187static int serial21285_startup(struct uart_port *port)
188{
189 int ret;
190
191 tx_enabled(port) = 1;
192 rx_enabled(port) = 1;
193
194 ret = request_irq(IRQ_CONRX, serial21285_rx_chars, 0,
195 serial21285_name, port);
196 if (ret == 0) {
197 ret = request_irq(IRQ_CONTX, serial21285_tx_chars, 0,
198 serial21285_name, port);
199 if (ret)
200 free_irq(IRQ_CONRX, port);
201 }
202
203 return ret;
204}
205
206static void serial21285_shutdown(struct uart_port *port)
207{
208 free_irq(IRQ_CONTX, port);
209 free_irq(IRQ_CONRX, port);
210}
211
212static void
213serial21285_set_termios(struct uart_port *port, struct ktermios *termios,
214 struct ktermios *old)
215{
216 unsigned long flags;
217 unsigned int baud, quot, h_lcr, b;
218
219 /*
220 * We don't support modem control lines.
221 */
222 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
223 termios->c_cflag |= CLOCAL;
224
225 /*
226 * We don't support BREAK character recognition.
227 */
228 termios->c_iflag &= ~(IGNBRK | BRKINT);
229
230 /*
231 * Ask the core to calculate the divisor for us.
232 */
233 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
234 quot = uart_get_divisor(port, baud);
235 b = port->uartclk / (16 * quot);
236 tty_termios_encode_baud_rate(termios, b, b);
237
238 switch (termios->c_cflag & CSIZE) {
239 case CS5:
240 h_lcr = 0x00;
241 break;
242 case CS6:
243 h_lcr = 0x20;
244 break;
245 case CS7:
246 h_lcr = 0x40;
247 break;
248 default: /* CS8 */
249 h_lcr = 0x60;
250 break;
251 }
252
253 if (termios->c_cflag & CSTOPB)
254 h_lcr |= H_UBRLCR_STOPB;
255 if (termios->c_cflag & PARENB) {
256 h_lcr |= H_UBRLCR_PARENB;
257 if (!(termios->c_cflag & PARODD))
258 h_lcr |= H_UBRLCR_PAREVN;
259 }
260
261 if (port->fifosize)
262 h_lcr |= H_UBRLCR_FIFO;
263
264 spin_lock_irqsave(&port->lock, flags);
265
266 /*
267 * Update the per-port timeout.
268 */
269 uart_update_timeout(port, termios->c_cflag, baud);
270
271 /*
272 * Which character status flags are we interested in?
273 */
274 port->read_status_mask = RXSTAT_OVERRUN;
275 if (termios->c_iflag & INPCK)
276 port->read_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
277
278 /*
279 * Which character status flags should we ignore?
280 */
281 port->ignore_status_mask = 0;
282 if (termios->c_iflag & IGNPAR)
283 port->ignore_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
284 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
285 port->ignore_status_mask |= RXSTAT_OVERRUN;
286
287 /*
288 * Ignore all characters if CREAD is not set.
289 */
290 if ((termios->c_cflag & CREAD) == 0)
291 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
292
293 quot -= 1;
294
295 *CSR_UARTCON = 0;
296 *CSR_L_UBRLCR = quot & 0xff;
297 *CSR_M_UBRLCR = (quot >> 8) & 0x0f;
298 *CSR_H_UBRLCR = h_lcr;
299 *CSR_UARTCON = 1;
300
301 spin_unlock_irqrestore(&port->lock, flags);
302}
303
304static const char *serial21285_type(struct uart_port *port)
305{
306 return port->type == PORT_21285 ? "DC21285" : NULL;
307}
308
309static void serial21285_release_port(struct uart_port *port)
310{
311 release_mem_region(port->mapbase, 32);
312}
313
314static int serial21285_request_port(struct uart_port *port)
315{
316 return request_mem_region(port->mapbase, 32, serial21285_name)
317 != NULL ? 0 : -EBUSY;
318}
319
320static void serial21285_config_port(struct uart_port *port, int flags)
321{
322 if (flags & UART_CONFIG_TYPE && serial21285_request_port(port) == 0)
323 port->type = PORT_21285;
324}
325
326/*
327 * verify the new serial_struct (for TIOCSSERIAL).
328 */
329static int serial21285_verify_port(struct uart_port *port, struct serial_struct *ser)
330{
331 int ret = 0;
332 if (ser->type != PORT_UNKNOWN && ser->type != PORT_21285)
333 ret = -EINVAL;
334 if (ser->irq != NO_IRQ)
335 ret = -EINVAL;
336 if (ser->baud_base != port->uartclk / 16)
337 ret = -EINVAL;
338 return ret;
339}
340
341static struct uart_ops serial21285_ops = {
342 .tx_empty = serial21285_tx_empty,
343 .get_mctrl = serial21285_get_mctrl,
344 .set_mctrl = serial21285_set_mctrl,
345 .stop_tx = serial21285_stop_tx,
346 .start_tx = serial21285_start_tx,
347 .stop_rx = serial21285_stop_rx,
348 .enable_ms = serial21285_enable_ms,
349 .break_ctl = serial21285_break_ctl,
350 .startup = serial21285_startup,
351 .shutdown = serial21285_shutdown,
352 .set_termios = serial21285_set_termios,
353 .type = serial21285_type,
354 .release_port = serial21285_release_port,
355 .request_port = serial21285_request_port,
356 .config_port = serial21285_config_port,
357 .verify_port = serial21285_verify_port,
358};
359
360static struct uart_port serial21285_port = {
361 .mapbase = 0x42000160,
362 .iotype = UPIO_MEM,
363 .irq = NO_IRQ,
364 .fifosize = 16,
365 .ops = &serial21285_ops,
366 .flags = UPF_BOOT_AUTOCONF,
367};
368
369static void serial21285_setup_ports(void)
370{
371 serial21285_port.uartclk = mem_fclk_21285 / 4;
372}
373
374#ifdef CONFIG_SERIAL_21285_CONSOLE
375static void serial21285_console_putchar(struct uart_port *port, int ch)
376{
377 while (*CSR_UARTFLG & 0x20)
378 barrier();
379 *CSR_UARTDR = ch;
380}
381
382static void
383serial21285_console_write(struct console *co, const char *s,
384 unsigned int count)
385{
386 uart_console_write(&serial21285_port, s, count, serial21285_console_putchar);
387}
388
389static void __init
390serial21285_get_options(struct uart_port *port, int *baud,
391 int *parity, int *bits)
392{
393 if (*CSR_UARTCON == 1) {
394 unsigned int tmp;
395
396 tmp = *CSR_H_UBRLCR;
397 switch (tmp & 0x60) {
398 case 0x00:
399 *bits = 5;
400 break;
401 case 0x20:
402 *bits = 6;
403 break;
404 case 0x40:
405 *bits = 7;
406 break;
407 default:
408 case 0x60:
409 *bits = 8;
410 break;
411 }
412
413 if (tmp & H_UBRLCR_PARENB) {
414 *parity = 'o';
415 if (tmp & H_UBRLCR_PAREVN)
416 *parity = 'e';
417 }
418
419 tmp = *CSR_L_UBRLCR | (*CSR_M_UBRLCR << 8);
420
421 *baud = port->uartclk / (16 * (tmp + 1));
422 }
423}
424
425static int __init serial21285_console_setup(struct console *co, char *options)
426{
427 struct uart_port *port = &serial21285_port;
428 int baud = 9600;
429 int bits = 8;
430 int parity = 'n';
431 int flow = 'n';
432
433 if (machine_is_personal_server())
434 baud = 57600;
435
436 /*
437 * Check whether an invalid uart number has been specified, and
438 * if so, search for the first available port that does have
439 * console support.
440 */
441 if (options)
442 uart_parse_options(options, &baud, &parity, &bits, &flow);
443 else
444 serial21285_get_options(port, &baud, &parity, &bits);
445
446 return uart_set_options(port, co, baud, parity, bits, flow);
447}
448
449static struct uart_driver serial21285_reg;
450
451static struct console serial21285_console =
452{
453 .name = SERIAL_21285_NAME,
454 .write = serial21285_console_write,
455 .device = uart_console_device,
456 .setup = serial21285_console_setup,
457 .flags = CON_PRINTBUFFER,
458 .index = -1,
459 .data = &serial21285_reg,
460};
461
462static int __init rs285_console_init(void)
463{
464 serial21285_setup_ports();
465 register_console(&serial21285_console);
466 return 0;
467}
468console_initcall(rs285_console_init);
469
470#define SERIAL_21285_CONSOLE &serial21285_console
471#else
472#define SERIAL_21285_CONSOLE NULL
473#endif
474
475static struct uart_driver serial21285_reg = {
476 .owner = THIS_MODULE,
477 .driver_name = "ttyFB",
478 .dev_name = "ttyFB",
479 .major = SERIAL_21285_MAJOR,
480 .minor = SERIAL_21285_MINOR,
481 .nr = 1,
482 .cons = SERIAL_21285_CONSOLE,
483};
484
485static int __init serial21285_init(void)
486{
487 int ret;
488
489 printk(KERN_INFO "Serial: 21285 driver\n");
490
491 serial21285_setup_ports();
492
493 ret = uart_register_driver(&serial21285_reg);
494 if (ret == 0)
495 uart_add_one_port(&serial21285_reg, &serial21285_port);
496
497 return ret;
498}
499
500static void __exit serial21285_exit(void)
501{
502 uart_remove_one_port(&serial21285_reg, &serial21285_port);
503 uart_unregister_driver(&serial21285_reg);
504}
505
506module_init(serial21285_init);
507module_exit(serial21285_exit);
508
509MODULE_LICENSE("GPL");
510MODULE_DESCRIPTION("Intel Footbridge (21285) serial driver");
511MODULE_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..e0a77540b8ca
--- /dev/null
+++ b/drivers/tty/serial/68328serial.c
@@ -0,0 +1,1448 @@
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->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(0);
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->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->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
396static int startup(struct m68k_serial * info)
397{
398 m68328_uart *uart = &uart_addr[info->line];
399 unsigned long flags;
400
401 if (info->flags & S_INITIALIZED)
402 return 0;
403
404 if (!info->xmit_buf) {
405 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
406 if (!info->xmit_buf)
407 return -ENOMEM;
408 }
409
410 local_irq_save(flags);
411
412 /*
413 * Clear the FIFO buffers and disable them
414 * (they will be reenabled in change_speed())
415 */
416
417 uart->ustcnt = USTCNT_UEN;
418 info->xmit_fifo_size = 1;
419 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
420 (void)uart->urx.w;
421
422 /*
423 * Finally, enable sequencing and interrupts
424 */
425#ifdef USE_INTS
426 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
427 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
428#else
429 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
430#endif
431
432 if (info->tty)
433 clear_bit(TTY_IO_ERROR, &info->tty->flags);
434 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
435
436 /*
437 * and set the speed of the serial port
438 */
439
440 change_speed(info);
441
442 info->flags |= S_INITIALIZED;
443 local_irq_restore(flags);
444 return 0;
445}
446
447/*
448 * This routine will shutdown a serial port; interrupts are disabled, and
449 * DTR is dropped if the hangup on close termio flag is on.
450 */
451static void shutdown(struct m68k_serial * info)
452{
453 m68328_uart *uart = &uart_addr[info->line];
454 unsigned long flags;
455
456 uart->ustcnt = 0; /* All off! */
457 if (!(info->flags & S_INITIALIZED))
458 return;
459
460 local_irq_save(flags);
461
462 if (info->xmit_buf) {
463 free_page((unsigned long) info->xmit_buf);
464 info->xmit_buf = 0;
465 }
466
467 if (info->tty)
468 set_bit(TTY_IO_ERROR, &info->tty->flags);
469
470 info->flags &= ~S_INITIALIZED;
471 local_irq_restore(flags);
472}
473
474struct {
475 int divisor, prescale;
476}
477#ifndef CONFIG_M68VZ328
478 hw_baud_table[18] = {
479 {0,0}, /* 0 */
480 {0,0}, /* 50 */
481 {0,0}, /* 75 */
482 {0,0}, /* 110 */
483 {0,0}, /* 134 */
484 {0,0}, /* 150 */
485 {0,0}, /* 200 */
486 {7,0x26}, /* 300 */
487 {6,0x26}, /* 600 */
488 {5,0x26}, /* 1200 */
489 {0,0}, /* 1800 */
490 {4,0x26}, /* 2400 */
491 {3,0x26}, /* 4800 */
492 {2,0x26}, /* 9600 */
493 {1,0x26}, /* 19200 */
494 {0,0x26}, /* 38400 */
495 {1,0x38}, /* 57600 */
496 {0,0x38}, /* 115200 */
497};
498#else
499 hw_baud_table[18] = {
500 {0,0}, /* 0 */
501 {0,0}, /* 50 */
502 {0,0}, /* 75 */
503 {0,0}, /* 110 */
504 {0,0}, /* 134 */
505 {0,0}, /* 150 */
506 {0,0}, /* 200 */
507 {0,0}, /* 300 */
508 {7,0x26}, /* 600 */
509 {6,0x26}, /* 1200 */
510 {0,0}, /* 1800 */
511 {5,0x26}, /* 2400 */
512 {4,0x26}, /* 4800 */
513 {3,0x26}, /* 9600 */
514 {2,0x26}, /* 19200 */
515 {1,0x26}, /* 38400 */
516 {0,0x26}, /* 57600 */
517 {1,0x38}, /* 115200 */
518};
519#endif
520/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
521
522/*
523 * This routine is called to set the UART divisor registers to match
524 * the specified baud rate for a serial port.
525 */
526static void change_speed(struct m68k_serial *info)
527{
528 m68328_uart *uart = &uart_addr[info->line];
529 unsigned short port;
530 unsigned short ustcnt;
531 unsigned cflag;
532 int i;
533
534 if (!info->tty || !info->tty->termios)
535 return;
536 cflag = info->tty->termios->c_cflag;
537 if (!(port = info->port))
538 return;
539
540 ustcnt = uart->ustcnt;
541 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
542
543 i = cflag & CBAUD;
544 if (i & CBAUDEX) {
545 i = (i & ~CBAUDEX) + B38400;
546 }
547
548 info->baud = baud_table[i];
549 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
550 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
551
552 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
553
554 if ((cflag & CSIZE) == CS8)
555 ustcnt |= USTCNT_8_7;
556
557 if (cflag & CSTOPB)
558 ustcnt |= USTCNT_STOP;
559
560 if (cflag & PARENB)
561 ustcnt |= USTCNT_PARITYEN;
562 if (cflag & PARODD)
563 ustcnt |= USTCNT_ODD_EVEN;
564
565#ifdef CONFIG_SERIAL_68328_RTS_CTS
566 if (cflag & CRTSCTS) {
567 uart->utx.w &= ~ UTX_NOCTS;
568 } else {
569 uart->utx.w |= UTX_NOCTS;
570 }
571#endif
572
573 ustcnt |= USTCNT_TXEN;
574
575 uart->ustcnt = ustcnt;
576 return;
577}
578
579/*
580 * Fair output driver allows a process to speak.
581 */
582static void rs_fair_output(void)
583{
584 int left; /* Output no more than that */
585 unsigned long flags;
586 struct m68k_serial *info = &m68k_soft[0];
587 char c;
588
589 if (info == 0) return;
590 if (info->xmit_buf == 0) return;
591
592 local_irq_save(flags);
593 left = info->xmit_cnt;
594 while (left != 0) {
595 c = info->xmit_buf[info->xmit_tail];
596 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
597 info->xmit_cnt--;
598 local_irq_restore(flags);
599
600 rs_put_char(c);
601
602 local_irq_save(flags);
603 left = min(info->xmit_cnt, left-1);
604 }
605
606 /* Last character is being transmitted now (hopefully). */
607 udelay(5);
608
609 local_irq_restore(flags);
610 return;
611}
612
613/*
614 * m68k_console_print is registered for printk.
615 */
616void console_print_68328(const char *p)
617{
618 char c;
619
620 while((c=*(p++)) != 0) {
621 if(c == '\n')
622 rs_put_char('\r');
623 rs_put_char(c);
624 }
625
626 /* Comment this if you want to have a strict interrupt-driven output */
627 rs_fair_output();
628
629 return;
630}
631
632static void rs_set_ldisc(struct tty_struct *tty)
633{
634 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
635
636 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
637 return;
638
639 info->is_cons = (tty->termios->c_line == N_TTY);
640
641 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
642}
643
644static void rs_flush_chars(struct tty_struct *tty)
645{
646 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
647 m68328_uart *uart = &uart_addr[info->line];
648 unsigned long flags;
649
650 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
651 return;
652#ifndef USE_INTS
653 for(;;) {
654#endif
655
656 /* Enable transmitter */
657 local_irq_save(flags);
658
659 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
660 !info->xmit_buf) {
661 local_irq_restore(flags);
662 return;
663 }
664
665#ifdef USE_INTS
666 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
667#else
668 uart->ustcnt |= USTCNT_TXEN;
669#endif
670
671#ifdef USE_INTS
672 if (uart->utx.w & UTX_TX_AVAIL) {
673#else
674 if (1) {
675#endif
676 /* Send char */
677 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
678 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
679 info->xmit_cnt--;
680 }
681
682#ifndef USE_INTS
683 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
684 }
685#endif
686 local_irq_restore(flags);
687}
688
689extern void console_printn(const char * b, int count);
690
691static int rs_write(struct tty_struct * tty,
692 const unsigned char *buf, int count)
693{
694 int c, total = 0;
695 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
696 m68328_uart *uart = &uart_addr[info->line];
697 unsigned long flags;
698
699 if (serial_paranoia_check(info, tty->name, "rs_write"))
700 return 0;
701
702 if (!tty || !info->xmit_buf)
703 return 0;
704
705 local_save_flags(flags);
706 while (1) {
707 local_irq_disable();
708 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
709 SERIAL_XMIT_SIZE - info->xmit_head));
710 local_irq_restore(flags);
711
712 if (c <= 0)
713 break;
714
715 memcpy(info->xmit_buf + info->xmit_head, buf, c);
716
717 local_irq_disable();
718 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
719 info->xmit_cnt += c;
720 local_irq_restore(flags);
721 buf += c;
722 count -= c;
723 total += c;
724 }
725
726 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
727 /* Enable transmitter */
728 local_irq_disable();
729#ifndef USE_INTS
730 while(info->xmit_cnt) {
731#endif
732
733 uart->ustcnt |= USTCNT_TXEN;
734#ifdef USE_INTS
735 uart->ustcnt |= USTCNT_TX_INTR_MASK;
736#else
737 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
738#endif
739 if (uart->utx.w & UTX_TX_AVAIL) {
740 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
741 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
742 info->xmit_cnt--;
743 }
744
745#ifndef USE_INTS
746 }
747#endif
748 local_irq_restore(flags);
749 }
750
751 return total;
752}
753
754static int rs_write_room(struct tty_struct *tty)
755{
756 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
757 int ret;
758
759 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
760 return 0;
761 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
762 if (ret < 0)
763 ret = 0;
764 return ret;
765}
766
767static int rs_chars_in_buffer(struct tty_struct *tty)
768{
769 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
770
771 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
772 return 0;
773 return info->xmit_cnt;
774}
775
776static void rs_flush_buffer(struct tty_struct *tty)
777{
778 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
779 unsigned long flags;
780
781 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
782 return;
783 local_irq_save(flags);
784 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
785 local_irq_restore(flags);
786 tty_wakeup(tty);
787}
788
789/*
790 * ------------------------------------------------------------
791 * rs_throttle()
792 *
793 * This routine is called by the upper-layer tty layer to signal that
794 * incoming characters should be throttled.
795 * ------------------------------------------------------------
796 */
797static void rs_throttle(struct tty_struct * tty)
798{
799 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
800
801 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
802 return;
803
804 if (I_IXOFF(tty))
805 info->x_char = STOP_CHAR(tty);
806
807 /* Turn off RTS line (do this atomic) */
808}
809
810static void rs_unthrottle(struct tty_struct * tty)
811{
812 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
813
814 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
815 return;
816
817 if (I_IXOFF(tty)) {
818 if (info->x_char)
819 info->x_char = 0;
820 else
821 info->x_char = START_CHAR(tty);
822 }
823
824 /* Assert RTS line (do this atomic) */
825}
826
827/*
828 * ------------------------------------------------------------
829 * rs_ioctl() and friends
830 * ------------------------------------------------------------
831 */
832
833static int get_serial_info(struct m68k_serial * info,
834 struct serial_struct * retinfo)
835{
836 struct serial_struct tmp;
837
838 if (!retinfo)
839 return -EFAULT;
840 memset(&tmp, 0, sizeof(tmp));
841 tmp.type = info->type;
842 tmp.line = info->line;
843 tmp.port = info->port;
844 tmp.irq = info->irq;
845 tmp.flags = info->flags;
846 tmp.baud_base = info->baud_base;
847 tmp.close_delay = info->close_delay;
848 tmp.closing_wait = info->closing_wait;
849 tmp.custom_divisor = info->custom_divisor;
850 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
851 return -EFAULT;
852
853 return 0;
854}
855
856static int set_serial_info(struct m68k_serial * info,
857 struct serial_struct * new_info)
858{
859 struct serial_struct new_serial;
860 struct m68k_serial old_info;
861 int retval = 0;
862
863 if (!new_info)
864 return -EFAULT;
865 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
866 return -EFAULT;
867 old_info = *info;
868
869 if (!capable(CAP_SYS_ADMIN)) {
870 if ((new_serial.baud_base != info->baud_base) ||
871 (new_serial.type != info->type) ||
872 (new_serial.close_delay != info->close_delay) ||
873 ((new_serial.flags & ~S_USR_MASK) !=
874 (info->flags & ~S_USR_MASK)))
875 return -EPERM;
876 info->flags = ((info->flags & ~S_USR_MASK) |
877 (new_serial.flags & S_USR_MASK));
878 info->custom_divisor = new_serial.custom_divisor;
879 goto check_and_exit;
880 }
881
882 if (info->count > 1)
883 return -EBUSY;
884
885 /*
886 * OK, past this point, all the error checking has been done.
887 * At this point, we start making changes.....
888 */
889
890 info->baud_base = new_serial.baud_base;
891 info->flags = ((info->flags & ~S_FLAGS) |
892 (new_serial.flags & S_FLAGS));
893 info->type = new_serial.type;
894 info->close_delay = new_serial.close_delay;
895 info->closing_wait = new_serial.closing_wait;
896
897check_and_exit:
898 retval = startup(info);
899 return retval;
900}
901
902/*
903 * get_lsr_info - get line status register info
904 *
905 * Purpose: Let user call ioctl() to get info when the UART physically
906 * is emptied. On bus types like RS485, the transmitter must
907 * release the bus after transmitting. This must be done when
908 * the transmit shift register is empty, not be done when the
909 * transmit holding register is empty. This functionality
910 * allows an RS485 driver to be written in user space.
911 */
912static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
913{
914#ifdef CONFIG_SERIAL_68328_RTS_CTS
915 m68328_uart *uart = &uart_addr[info->line];
916#endif
917 unsigned char status;
918 unsigned long flags;
919
920 local_irq_save(flags);
921#ifdef CONFIG_SERIAL_68328_RTS_CTS
922 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
923#else
924 status = 0;
925#endif
926 local_irq_restore(flags);
927 return put_user(status, value);
928}
929
930/*
931 * This routine sends a break character out the serial port.
932 */
933static void send_break(struct m68k_serial * info, unsigned int duration)
934{
935 m68328_uart *uart = &uart_addr[info->line];
936 unsigned long flags;
937 if (!info->port)
938 return;
939 local_irq_save(flags);
940#ifdef USE_INTS
941 uart->utx.w |= UTX_SEND_BREAK;
942 msleep_interruptible(duration);
943 uart->utx.w &= ~UTX_SEND_BREAK;
944#endif
945 local_irq_restore(flags);
946}
947
948static int rs_ioctl(struct tty_struct *tty,
949 unsigned int cmd, unsigned long arg)
950{
951 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
952 int retval;
953
954 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
955 return -ENODEV;
956
957 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
958 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
959 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
960 if (tty->flags & (1 << TTY_IO_ERROR))
961 return -EIO;
962 }
963
964 switch (cmd) {
965 case TCSBRK: /* SVID version: non-zero arg --> no break */
966 retval = tty_check_change(tty);
967 if (retval)
968 return retval;
969 tty_wait_until_sent(tty, 0);
970 if (!arg)
971 send_break(info, 250); /* 1/4 second */
972 return 0;
973 case TCSBRKP: /* support for POSIX tcsendbreak() */
974 retval = tty_check_change(tty);
975 if (retval)
976 return retval;
977 tty_wait_until_sent(tty, 0);
978 send_break(info, arg ? arg*(100) : 250);
979 return 0;
980 case TIOCGSERIAL:
981 return get_serial_info(info,
982 (struct serial_struct *) arg);
983 case TIOCSSERIAL:
984 return set_serial_info(info,
985 (struct serial_struct *) arg);
986 case TIOCSERGETLSR: /* Get line status register */
987 return get_lsr_info(info, (unsigned int *) arg);
988 case TIOCSERGSTRUCT:
989 if (copy_to_user((struct m68k_serial *) arg,
990 info, sizeof(struct m68k_serial)))
991 return -EFAULT;
992 return 0;
993 default:
994 return -ENOIOCTLCMD;
995 }
996 return 0;
997}
998
999static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1000{
1001 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1002
1003 change_speed(info);
1004
1005 if ((old_termios->c_cflag & CRTSCTS) &&
1006 !(tty->termios->c_cflag & CRTSCTS)) {
1007 tty->hw_stopped = 0;
1008 rs_start(tty);
1009 }
1010
1011}
1012
1013/*
1014 * ------------------------------------------------------------
1015 * rs_close()
1016 *
1017 * This routine is called when the serial port gets closed. First, we
1018 * wait for the last remaining data to be sent. Then, we unlink its
1019 * S structure from the interrupt chain if necessary, and we free
1020 * that IRQ if nothing is left in the chain.
1021 * ------------------------------------------------------------
1022 */
1023static void rs_close(struct tty_struct *tty, struct file * filp)
1024{
1025 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1026 m68328_uart *uart = &uart_addr[info->line];
1027 unsigned long flags;
1028
1029 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1030 return;
1031
1032 local_irq_save(flags);
1033
1034 if (tty_hung_up_p(filp)) {
1035 local_irq_restore(flags);
1036 return;
1037 }
1038
1039 if ((tty->count == 1) && (info->count != 1)) {
1040 /*
1041 * Uh, oh. tty->count is 1, which means that the tty
1042 * structure will be freed. Info->count should always
1043 * be one in these conditions. If it's greater than
1044 * one, we've got real problems, since it means the
1045 * serial port won't be shutdown.
1046 */
1047 printk("rs_close: bad serial port count; tty->count is 1, "
1048 "info->count is %d\n", info->count);
1049 info->count = 1;
1050 }
1051 if (--info->count < 0) {
1052 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1053 info->line, info->count);
1054 info->count = 0;
1055 }
1056 if (info->count) {
1057 local_irq_restore(flags);
1058 return;
1059 }
1060 info->flags |= S_CLOSING;
1061 /*
1062 * Now we wait for the transmit buffer to clear; and we notify
1063 * the line discipline to only process XON/XOFF characters.
1064 */
1065 tty->closing = 1;
1066 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1067 tty_wait_until_sent(tty, info->closing_wait);
1068 /*
1069 * At this point we stop accepting input. To do this, we
1070 * disable the receive line status interrupts, and tell the
1071 * interrupt driver to stop checking the data ready bit in the
1072 * line status register.
1073 */
1074
1075 uart->ustcnt &= ~USTCNT_RXEN;
1076 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1077
1078 shutdown(info);
1079 rs_flush_buffer(tty);
1080
1081 tty_ldisc_flush(tty);
1082 tty->closing = 0;
1083 info->event = 0;
1084 info->tty = NULL;
1085#warning "This is not and has never been valid so fix it"
1086#if 0
1087 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1088 if (tty->ldisc.close)
1089 (tty->ldisc.close)(tty);
1090 tty->ldisc = ldiscs[N_TTY];
1091 tty->termios->c_line = N_TTY;
1092 if (tty->ldisc.open)
1093 (tty->ldisc.open)(tty);
1094 }
1095#endif
1096 if (info->blocked_open) {
1097 if (info->close_delay) {
1098 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1099 }
1100 wake_up_interruptible(&info->open_wait);
1101 }
1102 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1103 wake_up_interruptible(&info->close_wait);
1104 local_irq_restore(flags);
1105}
1106
1107/*
1108 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1109 */
1110void rs_hangup(struct tty_struct *tty)
1111{
1112 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1113
1114 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1115 return;
1116
1117 rs_flush_buffer(tty);
1118 shutdown(info);
1119 info->event = 0;
1120 info->count = 0;
1121 info->flags &= ~S_NORMAL_ACTIVE;
1122 info->tty = NULL;
1123 wake_up_interruptible(&info->open_wait);
1124}
1125
1126/*
1127 * ------------------------------------------------------------
1128 * rs_open() and friends
1129 * ------------------------------------------------------------
1130 */
1131static int block_til_ready(struct tty_struct *tty, struct file * filp,
1132 struct m68k_serial *info)
1133{
1134 DECLARE_WAITQUEUE(wait, current);
1135 int retval;
1136 int do_clocal = 0;
1137
1138 /*
1139 * If the device is in the middle of being closed, then block
1140 * until it's done, and then try again.
1141 */
1142 if (info->flags & S_CLOSING) {
1143 interruptible_sleep_on(&info->close_wait);
1144#ifdef SERIAL_DO_RESTART
1145 if (info->flags & S_HUP_NOTIFY)
1146 return -EAGAIN;
1147 else
1148 return -ERESTARTSYS;
1149#else
1150 return -EAGAIN;
1151#endif
1152 }
1153
1154 /*
1155 * If non-blocking mode is set, or the port is not enabled,
1156 * then make the check up front and then exit.
1157 */
1158 if ((filp->f_flags & O_NONBLOCK) ||
1159 (tty->flags & (1 << TTY_IO_ERROR))) {
1160 info->flags |= S_NORMAL_ACTIVE;
1161 return 0;
1162 }
1163
1164 if (tty->termios->c_cflag & CLOCAL)
1165 do_clocal = 1;
1166
1167 /*
1168 * Block waiting for the carrier detect and the line to become
1169 * free (i.e., not in use by the callout). While we are in
1170 * this loop, info->count is dropped by one, so that
1171 * rs_close() knows when to free things. We restore it upon
1172 * exit, either normal or abnormal.
1173 */
1174 retval = 0;
1175 add_wait_queue(&info->open_wait, &wait);
1176
1177 info->count--;
1178 info->blocked_open++;
1179 while (1) {
1180 local_irq_disable();
1181 m68k_rtsdtr(info, 1);
1182 local_irq_enable();
1183 current->state = TASK_INTERRUPTIBLE;
1184 if (tty_hung_up_p(filp) ||
1185 !(info->flags & S_INITIALIZED)) {
1186#ifdef SERIAL_DO_RESTART
1187 if (info->flags & S_HUP_NOTIFY)
1188 retval = -EAGAIN;
1189 else
1190 retval = -ERESTARTSYS;
1191#else
1192 retval = -EAGAIN;
1193#endif
1194 break;
1195 }
1196 if (!(info->flags & S_CLOSING) && do_clocal)
1197 break;
1198 if (signal_pending(current)) {
1199 retval = -ERESTARTSYS;
1200 break;
1201 }
1202 tty_unlock();
1203 schedule();
1204 tty_lock();
1205 }
1206 current->state = TASK_RUNNING;
1207 remove_wait_queue(&info->open_wait, &wait);
1208 if (!tty_hung_up_p(filp))
1209 info->count++;
1210 info->blocked_open--;
1211
1212 if (retval)
1213 return retval;
1214 info->flags |= S_NORMAL_ACTIVE;
1215 return 0;
1216}
1217
1218/*
1219 * This routine is called whenever a serial port is opened. It
1220 * enables interrupts for a serial port, linking in its S structure into
1221 * the IRQ chain. It also performs the serial-specific
1222 * initialization for the tty structure.
1223 */
1224int rs_open(struct tty_struct *tty, struct file * filp)
1225{
1226 struct m68k_serial *info;
1227 int retval, line;
1228
1229 line = tty->index;
1230
1231 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1232 return -ENODEV;
1233
1234 info = &m68k_soft[line];
1235
1236 if (serial_paranoia_check(info, tty->name, "rs_open"))
1237 return -ENODEV;
1238
1239 info->count++;
1240 tty->driver_data = info;
1241 info->tty = tty;
1242
1243 /*
1244 * Start up serial port
1245 */
1246 retval = startup(info);
1247 if (retval)
1248 return retval;
1249
1250 return block_til_ready(tty, filp, info);
1251}
1252
1253/* Finally, routines used to initialize the serial driver. */
1254
1255static void show_serial_version(void)
1256{
1257 printk("MC68328 serial driver version 1.00\n");
1258}
1259
1260static const struct tty_operations rs_ops = {
1261 .open = rs_open,
1262 .close = rs_close,
1263 .write = rs_write,
1264 .flush_chars = rs_flush_chars,
1265 .write_room = rs_write_room,
1266 .chars_in_buffer = rs_chars_in_buffer,
1267 .flush_buffer = rs_flush_buffer,
1268 .ioctl = rs_ioctl,
1269 .throttle = rs_throttle,
1270 .unthrottle = rs_unthrottle,
1271 .set_termios = rs_set_termios,
1272 .stop = rs_stop,
1273 .start = rs_start,
1274 .hangup = rs_hangup,
1275 .set_ldisc = rs_set_ldisc,
1276};
1277
1278/* rs_init inits the driver */
1279static int __init
1280rs68328_init(void)
1281{
1282 int flags, i;
1283 struct m68k_serial *info;
1284
1285 serial_driver = alloc_tty_driver(NR_PORTS);
1286 if (!serial_driver)
1287 return -ENOMEM;
1288
1289 show_serial_version();
1290
1291 /* Initialize the tty_driver structure */
1292 /* SPARC: Not all of this is exactly right for us. */
1293
1294 serial_driver->name = "ttyS";
1295 serial_driver->major = TTY_MAJOR;
1296 serial_driver->minor_start = 64;
1297 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1298 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1299 serial_driver->init_termios = tty_std_termios;
1300 serial_driver->init_termios.c_cflag =
1301 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1302 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1303 tty_set_operations(serial_driver, &rs_ops);
1304
1305 if (tty_register_driver(serial_driver)) {
1306 put_tty_driver(serial_driver);
1307 printk(KERN_ERR "Couldn't register serial driver\n");
1308 return -ENOMEM;
1309 }
1310
1311 local_irq_save(flags);
1312
1313 for(i=0;i<NR_PORTS;i++) {
1314
1315 info = &m68k_soft[i];
1316 info->magic = SERIAL_MAGIC;
1317 info->port = (int) &uart_addr[i];
1318 info->tty = NULL;
1319 info->irq = uart_irqs[i];
1320 info->custom_divisor = 16;
1321 info->close_delay = 50;
1322 info->closing_wait = 3000;
1323 info->x_char = 0;
1324 info->event = 0;
1325 info->count = 0;
1326 info->blocked_open = 0;
1327 INIT_WORK(&info->tqueue, do_softint);
1328 init_waitqueue_head(&info->open_wait);
1329 init_waitqueue_head(&info->close_wait);
1330 info->line = i;
1331 info->is_cons = 1; /* Means shortcuts work */
1332
1333 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1334 info->port, info->irq);
1335 printk(" is a builtin MC68328 UART\n");
1336
1337#ifdef CONFIG_M68VZ328
1338 if (i > 0 )
1339 PJSEL &= 0xCF; /* PSW enable second port output */
1340#endif
1341
1342 if (request_irq(uart_irqs[i],
1343 rs_interrupt,
1344 IRQF_DISABLED,
1345 "M68328_UART", info))
1346 panic("Unable to attach 68328 serial interrupt\n");
1347 }
1348 local_irq_restore(flags);
1349 return 0;
1350}
1351
1352module_init(rs68328_init);
1353
1354
1355
1356static void m68328_set_baud(void)
1357{
1358 unsigned short ustcnt;
1359 int i;
1360
1361 ustcnt = USTCNT;
1362 USTCNT = ustcnt & ~USTCNT_TXEN;
1363
1364again:
1365 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1366 if (baud_table[i] == m68328_console_baud)
1367 break;
1368 if (i >= ARRAY_SIZE(baud_table)) {
1369 m68328_console_baud = 9600;
1370 goto again;
1371 }
1372
1373 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1374 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1375 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1376 ustcnt |= USTCNT_8_7;
1377 ustcnt |= USTCNT_TXEN;
1378 USTCNT = ustcnt;
1379 m68328_console_initted = 1;
1380 return;
1381}
1382
1383
1384int m68328_console_setup(struct console *cp, char *arg)
1385{
1386 int i, n = CONSOLE_BAUD_RATE;
1387
1388 if (!cp)
1389 return(-1);
1390
1391 if (arg)
1392 n = simple_strtoul(arg,NULL,0);
1393
1394 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1395 if (baud_table[i] == n)
1396 break;
1397 if (i < ARRAY_SIZE(baud_table)) {
1398 m68328_console_baud = n;
1399 m68328_console_cbaud = 0;
1400 if (i > 15) {
1401 m68328_console_cbaud |= CBAUDEX;
1402 i -= 15;
1403 }
1404 m68328_console_cbaud |= i;
1405 }
1406
1407 m68328_set_baud(); /* make sure baud rate changes */
1408 return(0);
1409}
1410
1411
1412static struct tty_driver *m68328_console_device(struct console *c, int *index)
1413{
1414 *index = c->index;
1415 return serial_driver;
1416}
1417
1418
1419void m68328_console_write (struct console *co, const char *str,
1420 unsigned int count)
1421{
1422 if (!m68328_console_initted)
1423 m68328_set_baud();
1424 while (count--) {
1425 if (*str == '\n')
1426 rs_put_char('\r');
1427 rs_put_char( *str++ );
1428 }
1429}
1430
1431
1432static struct console m68328_driver = {
1433 .name = "ttyS",
1434 .write = m68328_console_write,
1435 .device = m68328_console_device,
1436 .setup = m68328_console_setup,
1437 .flags = CON_PRINTBUFFER,
1438 .index = -1,
1439};
1440
1441
1442static int __init m68328_console_init(void)
1443{
1444 register_console(&m68328_driver);
1445 return 0;
1446}
1447
1448console_initcall(m68328_console_init);
diff --git a/drivers/tty/serial/68328serial.h b/drivers/tty/serial/68328serial.h
new file mode 100644
index 000000000000..8c9c3c0745db
--- /dev/null
+++ b/drivers/tty/serial/68328serial.h
@@ -0,0 +1,187 @@
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 wait_queue_head_t open_wait;
163 wait_queue_head_t close_wait;
164};
165
166
167#define SERIAL_MAGIC 0x5301
168
169/*
170 * The size of the serial xmit buffer is 1 page, or 4096 bytes
171 */
172#define SERIAL_XMIT_SIZE 4096
173
174/*
175 * Events are used to schedule things to happen at timer-interrupt
176 * time, instead of at rs interrupt time.
177 */
178#define RS_EVENT_WRITE_WAKEUP 0
179
180/*
181 * Define the number of ports supported and their irqs.
182 */
183#define NR_PORTS 1
184#define UART_IRQ_DEFNS {UART_IRQ_NUM}
185
186#endif /* __KERNEL__ */
187#endif /* !(_MC683XX_SERIAL_H) */
diff --git a/drivers/tty/serial/68360serial.c b/drivers/tty/serial/68360serial.c
new file mode 100644
index 000000000000..0a3e8787ed50
--- /dev/null
+++ b/drivers/tty/serial/68360serial.c
@@ -0,0 +1,2979 @@
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)
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,
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,
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 .get_icount = rs_360_get_icount,
2432};
2433
2434static int __init rs_360_init(void)
2435{
2436 struct serial_state * state;
2437 ser_info_t *info;
2438 void *mem_addr;
2439 uint dp_addr, iobits;
2440 int i, j, idx;
2441 ushort chan;
2442 QUICC_BD *bdp;
2443 volatile QUICC *cp;
2444 volatile struct smc_regs *sp;
2445 volatile struct smc_uart_pram *up;
2446 volatile struct scc_regs *scp;
2447 volatile struct uart_pram *sup;
2448 /* volatile immap_t *immap; */
2449
2450 serial_driver = alloc_tty_driver(NR_PORTS);
2451 if (!serial_driver)
2452 return -1;
2453
2454 show_serial_version();
2455
2456 serial_driver->name = "ttyS";
2457 serial_driver->major = TTY_MAJOR;
2458 serial_driver->minor_start = 64;
2459 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2460 serial_driver->subtype = SERIAL_TYPE_NORMAL;
2461 serial_driver->init_termios = tty_std_termios;
2462 serial_driver->init_termios.c_cflag =
2463 baud_idx | CS8 | CREAD | HUPCL | CLOCAL;
2464 serial_driver->flags = TTY_DRIVER_REAL_RAW;
2465 tty_set_operations(serial_driver, &rs_360_ops);
2466
2467 if (tty_register_driver(serial_driver))
2468 panic("Couldn't register serial driver\n");
2469
2470 cp = pquicc; /* Get pointer to Communication Processor */
2471 /* immap = (immap_t *)IMAP_ADDR; */ /* and to internal registers */
2472
2473
2474 /* Configure SCC2, SCC3, and SCC4 instead of port A parallel I/O.
2475 */
2476 /* The "standard" configuration through the 860.
2477 */
2478/* immap->im_ioport.iop_papar |= 0x00fc; */
2479/* immap->im_ioport.iop_padir &= ~0x00fc; */
2480/* immap->im_ioport.iop_paodr &= ~0x00fc; */
2481 cp->pio_papar |= 0x00fc;
2482 cp->pio_padir &= ~0x00fc;
2483 /* cp->pio_paodr &= ~0x00fc; */
2484
2485
2486 /* Since we don't yet do modem control, connect the port C pins
2487 * as general purpose I/O. This will assert CTS and CD for the
2488 * SCC ports.
2489 */
2490 /* FIXME: see 360um p.7-365 and 860um p.34-12
2491 * I can't make sense of these bits - mleslie*/
2492/* immap->im_ioport.iop_pcdir |= 0x03c6; */
2493/* immap->im_ioport.iop_pcpar &= ~0x03c6; */
2494
2495/* cp->pio_pcdir |= 0x03c6; */
2496/* cp->pio_pcpar &= ~0x03c6; */
2497
2498
2499
2500 /* Connect SCC2 and SCC3 to NMSI. Connect BRG3 to SCC2 and
2501 * BRG4 to SCC3.
2502 */
2503 cp->si_sicr &= ~0x00ffff00;
2504 cp->si_sicr |= 0x001b1200;
2505
2506#ifdef CONFIG_PP04
2507 /* Frequentis PP04 forced to RS-232 until we know better.
2508 * Port C 12 and 13 low enables RS-232 on SCC3 and SCC4.
2509 */
2510 immap->im_ioport.iop_pcdir |= 0x000c;
2511 immap->im_ioport.iop_pcpar &= ~0x000c;
2512 immap->im_ioport.iop_pcdat &= ~0x000c;
2513
2514 /* This enables the TX driver.
2515 */
2516 cp->cp_pbpar &= ~0x6000;
2517 cp->cp_pbdat &= ~0x6000;
2518#endif
2519
2520 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2521 state->magic = SSTATE_MAGIC;
2522 state->line = i;
2523 state->type = PORT_UNKNOWN;
2524 state->custom_divisor = 0;
2525 state->close_delay = 5*HZ/10;
2526 state->closing_wait = 30*HZ;
2527 state->icount.cts = state->icount.dsr =
2528 state->icount.rng = state->icount.dcd = 0;
2529 state->icount.rx = state->icount.tx = 0;
2530 state->icount.frame = state->icount.parity = 0;
2531 state->icount.overrun = state->icount.brk = 0;
2532 printk(KERN_INFO "ttyS%d at irq 0x%02x is an %s\n",
2533 i, (unsigned int)(state->irq),
2534 (state->smc_scc_num & NUM_IS_SCC) ? "SCC" : "SMC");
2535
2536#ifdef CONFIG_SERIAL_CONSOLE
2537 /* If we just printed the message on the console port, and
2538 * we are about to initialize it for general use, we have
2539 * to wait a couple of character times for the CR/NL to
2540 * make it out of the transmit buffer.
2541 */
2542 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2543 mdelay(8);
2544
2545
2546/* idx = PORT_NUM(info->state->smc_scc_num); */
2547/* if (info->state->smc_scc_num & NUM_IS_SCC) */
2548/* chan = scc_chan_map[idx]; */
2549/* else */
2550/* chan = smc_chan_map[idx]; */
2551
2552/* cp->cp_cr = mk_cr_cmd(chan, CPM_CR_STOP_TX) | CPM_CR_FLG; */
2553/* while (cp->cp_cr & CPM_CR_FLG); */
2554
2555#endif
2556 /* info = kmalloc(sizeof(ser_info_t), GFP_KERNEL); */
2557 info = &quicc_ser_info[i];
2558 if (info) {
2559 memset (info, 0, sizeof(ser_info_t));
2560 info->magic = SERIAL_MAGIC;
2561 info->line = i;
2562 info->flags = state->flags;
2563 INIT_WORK(&info->tqueue, do_softint, info);
2564 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
2565 init_waitqueue_head(&info->open_wait);
2566 init_waitqueue_head(&info->close_wait);
2567 info->state = state;
2568 state->info = (struct async_struct *)info;
2569
2570 /* We need to allocate a transmit and receive buffer
2571 * descriptors from dual port ram, and a character
2572 * buffer area from host mem.
2573 */
2574 dp_addr = m360_cpm_dpalloc(sizeof(QUICC_BD) * RX_NUM_FIFO);
2575
2576 /* Allocate space for FIFOs in the host memory.
2577 * (for now this is from a static array of buffers :(
2578 */
2579 /* mem_addr = m360_cpm_hostalloc(RX_NUM_FIFO * RX_BUF_SIZE); */
2580 /* mem_addr = kmalloc (RX_NUM_FIFO * RX_BUF_SIZE, GFP_BUFFER); */
2581 mem_addr = &rx_buf_pool[i * RX_NUM_FIFO * RX_BUF_SIZE];
2582
2583 /* Set the physical address of the host memory
2584 * buffers in the buffer descriptors, and the
2585 * virtual address for us to work with.
2586 */
2587 bdp = (QUICC_BD *)((uint)pquicc + dp_addr);
2588 info->rx_cur = info->rx_bd_base = bdp;
2589
2590 /* initialize rx buffer descriptors */
2591 for (j=0; j<(RX_NUM_FIFO-1); j++) {
2592 bdp->buf = &rx_buf_pool[(i * RX_NUM_FIFO + j ) * RX_BUF_SIZE];
2593 bdp->status = BD_SC_EMPTY | BD_SC_INTRPT;
2594 mem_addr += RX_BUF_SIZE;
2595 bdp++;
2596 }
2597 bdp->buf = &rx_buf_pool[(i * RX_NUM_FIFO + j ) * RX_BUF_SIZE];
2598 bdp->status = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
2599
2600
2601 idx = PORT_NUM(info->state->smc_scc_num);
2602 if (info->state->smc_scc_num & NUM_IS_SCC) {
2603
2604#if defined (CONFIG_UCQUICC) && 1
2605 /* set the transceiver mode to RS232 */
2606 sipex_mode_bits &= ~(uint)SIPEX_MODE(idx,0x0f); /* clear current mode */
2607 sipex_mode_bits |= (uint)SIPEX_MODE(idx,0x02);
2608 *(uint *)_periph_base = sipex_mode_bits;
2609 /* printk ("sipex bits = 0x%08x\n", sipex_mode_bits); */
2610#endif
2611 }
2612
2613 dp_addr = m360_cpm_dpalloc(sizeof(QUICC_BD) * TX_NUM_FIFO);
2614
2615 /* Allocate space for FIFOs in the host memory.
2616 */
2617 /* mem_addr = m360_cpm_hostalloc(TX_NUM_FIFO * TX_BUF_SIZE); */
2618 /* mem_addr = kmalloc (TX_NUM_FIFO * TX_BUF_SIZE, GFP_BUFFER); */
2619 mem_addr = &tx_buf_pool[i * TX_NUM_FIFO * TX_BUF_SIZE];
2620
2621 /* Set the physical address of the host memory
2622 * buffers in the buffer descriptors, and the
2623 * virtual address for us to work with.
2624 */
2625 /* bdp = (QUICC_BD *)&cp->cp_dpmem[dp_addr]; */
2626 bdp = (QUICC_BD *)((uint)pquicc + dp_addr);
2627 info->tx_cur = info->tx_bd_base = (QUICC_BD *)bdp;
2628
2629 /* initialize tx buffer descriptors */
2630 for (j=0; j<(TX_NUM_FIFO-1); j++) {
2631 bdp->buf = &tx_buf_pool[(i * TX_NUM_FIFO + j ) * TX_BUF_SIZE];
2632 bdp->status = BD_SC_INTRPT;
2633 mem_addr += TX_BUF_SIZE;
2634 bdp++;
2635 }
2636 bdp->buf = &tx_buf_pool[(i * TX_NUM_FIFO + j ) * TX_BUF_SIZE];
2637 bdp->status = (BD_SC_WRAP | BD_SC_INTRPT);
2638
2639 if (info->state->smc_scc_num & NUM_IS_SCC) {
2640 scp = &pquicc->scc_regs[idx];
2641 sup = &pquicc->pram[info->state->port].scc.pscc.u;
2642 sup->rbase = dp_addr;
2643 sup->tbase = dp_addr;
2644
2645 /* Set up the uart parameters in the
2646 * parameter ram.
2647 */
2648 sup->rfcr = SMC_EB;
2649 sup->tfcr = SMC_EB;
2650
2651 /* Set this to 1 for now, so we get single
2652 * character interrupts. Using idle character
2653 * time requires some additional tuning.
2654 */
2655 sup->mrblr = 1;
2656 sup->max_idl = 0;
2657 sup->brkcr = 1;
2658 sup->parec = 0;
2659 sup->frmer = 0;
2660 sup->nosec = 0;
2661 sup->brkec = 0;
2662 sup->uaddr1 = 0;
2663 sup->uaddr2 = 0;
2664 sup->toseq = 0;
2665 {
2666 int i;
2667 for (i=0;i<8;i++)
2668 sup->cc[i] = 0x8000;
2669 }
2670 sup->rccm = 0xc0ff;
2671
2672 /* Send the CPM an initialize command.
2673 */
2674 chan = scc_chan_map[idx];
2675
2676 /* execute the INIT RX & TX PARAMS command for this channel. */
2677 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
2678 while (cp->cp_cr & CPM_CR_FLG);
2679
2680 /* Set UART mode, 8 bit, no parity, one stop.
2681 * Enable receive and transmit.
2682 */
2683 scp->scc_gsmr.w.high = 0;
2684 scp->scc_gsmr.w.low =
2685 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2686
2687 /* Disable all interrupts and clear all pending
2688 * events.
2689 */
2690 scp->scc_sccm = 0;
2691 scp->scc_scce = 0xffff;
2692 scp->scc_dsr = 0x7e7e;
2693 scp->scc_psmr = 0x3000;
2694
2695 /* If the port is the console, enable Rx and Tx.
2696 */
2697#ifdef CONFIG_SERIAL_CONSOLE
2698 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2699 scp->scc_gsmr.w.low |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2700#endif
2701 }
2702 else {
2703 /* Configure SMCs Tx/Rx instead of port B
2704 * parallel I/O.
2705 */
2706 up = &pquicc->pram[info->state->port].scc.pothers.idma_smc.psmc.u;
2707 up->rbase = dp_addr;
2708
2709 iobits = 0xc0 << (idx * 4);
2710 cp->pip_pbpar |= iobits;
2711 cp->pip_pbdir &= ~iobits;
2712 cp->pip_pbodr &= ~iobits;
2713
2714
2715 /* Connect the baud rate generator to the
2716 * SMC based upon index in rs_table. Also
2717 * make sure it is connected to NMSI.
2718 */
2719 cp->si_simode &= ~(0xffff << (idx * 16));
2720 cp->si_simode |= (i << ((idx * 16) + 12));
2721
2722 up->tbase = dp_addr;
2723
2724 /* Set up the uart parameters in the
2725 * parameter ram.
2726 */
2727 up->rfcr = SMC_EB;
2728 up->tfcr = SMC_EB;
2729
2730 /* Set this to 1 for now, so we get single
2731 * character interrupts. Using idle character
2732 * time requires some additional tuning.
2733 */
2734 up->mrblr = 1;
2735 up->max_idl = 0;
2736 up->brkcr = 1;
2737
2738 /* Send the CPM an initialize command.
2739 */
2740 chan = smc_chan_map[idx];
2741
2742 cp->cp_cr = mk_cr_cmd(chan,
2743 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2744#ifdef CONFIG_SERIAL_CONSOLE
2745 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2746 printk("");
2747#endif
2748 while (cp->cp_cr & CPM_CR_FLG);
2749
2750 /* Set UART mode, 8 bit, no parity, one stop.
2751 * Enable receive and transmit.
2752 */
2753 sp = &cp->smc_regs[idx];
2754 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
2755
2756 /* Disable all interrupts and clear all pending
2757 * events.
2758 */
2759 sp->smc_smcm = 0;
2760 sp->smc_smce = 0xff;
2761
2762 /* If the port is the console, enable Rx and Tx.
2763 */
2764#ifdef CONFIG_SERIAL_CONSOLE
2765 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2766 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
2767#endif
2768 }
2769
2770 /* Install interrupt handler.
2771 */
2772 /* cpm_install_handler(IRQ_MACHSPEC | state->irq, rs_360_interrupt, info); */
2773 /*request_irq(IRQ_MACHSPEC | state->irq, rs_360_interrupt, */
2774 request_irq(state->irq, rs_360_interrupt,
2775 IRQ_FLG_LOCK, "ttyS", (void *)info);
2776
2777 /* Set up the baud rate generator.
2778 */
2779 m360_cpm_setbrg(i, baud_table[baud_idx]);
2780
2781 }
2782 }
2783
2784 return 0;
2785}
2786module_init(rs_360_init);
2787
2788/* This must always be called before the rs_360_init() function, otherwise
2789 * it blows away the port control information.
2790 */
2791//static int __init serial_console_setup( struct console *co, char *options)
2792int serial_console_setup( struct console *co, char *options)
2793{
2794 struct serial_state *ser;
2795 uint mem_addr, dp_addr, bidx, idx, iobits;
2796 ushort chan;
2797 QUICC_BD *bdp;
2798 volatile QUICC *cp;
2799 volatile struct smc_regs *sp;
2800 volatile struct scc_regs *scp;
2801 volatile struct smc_uart_pram *up;
2802 volatile struct uart_pram *sup;
2803
2804/* mleslie TODO:
2805 * add something to the 68k bootloader to store a desired initial console baud rate */
2806
2807/* bd_t *bd; */ /* a board info struct used by EPPC-bug */
2808/* bd = (bd_t *)__res; */
2809
2810 for (bidx = 0; bidx < (sizeof(baud_table) / sizeof(int)); bidx++)
2811 /* if (bd->bi_baudrate == baud_table[bidx]) */
2812 if (CONSOLE_BAUDRATE == baud_table[bidx])
2813 break;
2814
2815 /* co->cflag = CREAD|CLOCAL|bidx|CS8; */
2816 baud_idx = bidx;
2817
2818 ser = rs_table + CONFIG_SERIAL_CONSOLE_PORT;
2819
2820 cp = pquicc; /* Get pointer to Communication Processor */
2821
2822 idx = PORT_NUM(ser->smc_scc_num);
2823 if (ser->smc_scc_num & NUM_IS_SCC) {
2824
2825 /* TODO: need to set up SCC pin assignment etc. here */
2826
2827 }
2828 else {
2829 iobits = 0xc0 << (idx * 4);
2830 cp->pip_pbpar |= iobits;
2831 cp->pip_pbdir &= ~iobits;
2832 cp->pip_pbodr &= ~iobits;
2833
2834 /* Connect the baud rate generator to the
2835 * SMC based upon index in rs_table. Also
2836 * make sure it is connected to NMSI.
2837 */
2838 cp->si_simode &= ~(0xffff << (idx * 16));
2839 cp->si_simode |= (idx << ((idx * 16) + 12));
2840 }
2841
2842 /* When we get here, the CPM has been reset, so we need
2843 * to configure the port.
2844 * We need to allocate a transmit and receive buffer descriptor
2845 * from dual port ram, and a character buffer area from host mem.
2846 */
2847
2848 /* Allocate space for two buffer descriptors in the DP ram.
2849 */
2850 dp_addr = m360_cpm_dpalloc(sizeof(QUICC_BD) * CONSOLE_NUM_FIFO);
2851
2852 /* Allocate space for two 2 byte FIFOs in the host memory.
2853 */
2854 /* mem_addr = m360_cpm_hostalloc(8); */
2855 mem_addr = (uint)console_fifos;
2856
2857
2858 /* Set the physical address of the host memory buffers in
2859 * the buffer descriptors.
2860 */
2861 /* bdp = (QUICC_BD *)&cp->cp_dpmem[dp_addr]; */
2862 bdp = (QUICC_BD *)((uint)pquicc + dp_addr);
2863 bdp->buf = (char *)mem_addr;
2864 (bdp+1)->buf = (char *)(mem_addr+4);
2865
2866 /* For the receive, set empty and wrap.
2867 * For transmit, set wrap.
2868 */
2869 bdp->status = BD_SC_EMPTY | BD_SC_WRAP;
2870 (bdp+1)->status = BD_SC_WRAP;
2871
2872 /* Set up the uart parameters in the parameter ram.
2873 */
2874 if (ser->smc_scc_num & NUM_IS_SCC) {
2875 scp = &cp->scc_regs[idx];
2876 /* sup = (scc_uart_t *)&cp->cp_dparam[ser->port]; */
2877 sup = &pquicc->pram[ser->port].scc.pscc.u;
2878
2879 sup->rbase = dp_addr;
2880 sup->tbase = dp_addr + sizeof(QUICC_BD);
2881
2882 /* Set up the uart parameters in the
2883 * parameter ram.
2884 */
2885 sup->rfcr = SMC_EB;
2886 sup->tfcr = SMC_EB;
2887
2888 /* Set this to 1 for now, so we get single
2889 * character interrupts. Using idle character
2890 * time requires some additional tuning.
2891 */
2892 sup->mrblr = 1;
2893 sup->max_idl = 0;
2894 sup->brkcr = 1;
2895 sup->parec = 0;
2896 sup->frmer = 0;
2897 sup->nosec = 0;
2898 sup->brkec = 0;
2899 sup->uaddr1 = 0;
2900 sup->uaddr2 = 0;
2901 sup->toseq = 0;
2902 {
2903 int i;
2904 for (i=0;i<8;i++)
2905 sup->cc[i] = 0x8000;
2906 }
2907 sup->rccm = 0xc0ff;
2908
2909 /* Send the CPM an initialize command.
2910 */
2911 chan = scc_chan_map[idx];
2912
2913 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
2914 while (cp->cp_cr & CPM_CR_FLG);
2915
2916 /* Set UART mode, 8 bit, no parity, one stop.
2917 * Enable receive and transmit.
2918 */
2919 scp->scc_gsmr.w.high = 0;
2920 scp->scc_gsmr.w.low =
2921 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2922
2923 /* Disable all interrupts and clear all pending
2924 * events.
2925 */
2926 scp->scc_sccm = 0;
2927 scp->scc_scce = 0xffff;
2928 scp->scc_dsr = 0x7e7e;
2929 scp->scc_psmr = 0x3000;
2930
2931 scp->scc_gsmr.w.low |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2932
2933 }
2934 else {
2935 /* up = (smc_uart_t *)&cp->cp_dparam[ser->port]; */
2936 up = &pquicc->pram[ser->port].scc.pothers.idma_smc.psmc.u;
2937
2938 up->rbase = dp_addr; /* Base of receive buffer desc. */
2939 up->tbase = dp_addr+sizeof(QUICC_BD); /* Base of xmt buffer desc. */
2940 up->rfcr = SMC_EB;
2941 up->tfcr = SMC_EB;
2942
2943 /* Set this to 1 for now, so we get single character interrupts.
2944 */
2945 up->mrblr = 1; /* receive buffer length */
2946 up->max_idl = 0; /* wait forever for next char */
2947
2948 /* Send the CPM an initialize command.
2949 */
2950 chan = smc_chan_map[idx];
2951 cp->cp_cr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
2952 while (cp->cp_cr & CPM_CR_FLG);
2953
2954 /* Set UART mode, 8 bit, no parity, one stop.
2955 * Enable receive and transmit.
2956 */
2957 sp = &cp->smc_regs[idx];
2958 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
2959
2960 /* And finally, enable Rx and Tx.
2961 */
2962 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
2963 }
2964
2965 /* Set up the baud rate generator.
2966 */
2967 /* m360_cpm_setbrg((ser - rs_table), bd->bi_baudrate); */
2968 m360_cpm_setbrg((ser - rs_table), CONSOLE_BAUDRATE);
2969
2970 return 0;
2971}
2972
2973/*
2974 * Local variables:
2975 * c-indent-level: 4
2976 * c-basic-offset: 4
2977 * tab-width: 4
2978 * End:
2979 */
diff --git a/drivers/tty/serial/8250.c b/drivers/tty/serial/8250.c
new file mode 100644
index 000000000000..b4129f53fb1b
--- /dev/null
+++ b/drivers/tty/serial/8250.c
@@ -0,0 +1,3424 @@
1/*
2 * Driver for 8250/16550-type serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * A note about mapbase / membase
14 *
15 * mapbase is the physical address of the IO port.
16 * membase is an 'ioremapped' cookie.
17 */
18
19#if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
20#define SUPPORT_SYSRQ
21#endif
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/ioport.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/sysrq.h>
29#include <linux/delay.h>
30#include <linux/platform_device.h>
31#include <linux/tty.h>
32#include <linux/ratelimit.h>
33#include <linux/tty_flip.h>
34#include <linux/serial_reg.h>
35#include <linux/serial_core.h>
36#include <linux/serial.h>
37#include <linux/serial_8250.h>
38#include <linux/nmi.h>
39#include <linux/mutex.h>
40#include <linux/slab.h>
41
42#include <asm/io.h>
43#include <asm/irq.h>
44
45#include "8250.h"
46
47#ifdef CONFIG_SPARC
48#include "suncore.h"
49#endif
50
51/*
52 * Configuration:
53 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
54 * is unsafe when used on edge-triggered interrupts.
55 */
56static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
57
58static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
59
60static struct uart_driver serial8250_reg;
61
62static int serial_index(struct uart_port *port)
63{
64 return (serial8250_reg.minor - 64) + port->line;
65}
66
67static unsigned int skip_txen_test; /* force skip of txen test at init time */
68
69/*
70 * Debugging.
71 */
72#if 0
73#define DEBUG_AUTOCONF(fmt...) printk(fmt)
74#else
75#define DEBUG_AUTOCONF(fmt...) do { } while (0)
76#endif
77
78#if 0
79#define DEBUG_INTR(fmt...) printk(fmt)
80#else
81#define DEBUG_INTR(fmt...) do { } while (0)
82#endif
83
84#define PASS_LIMIT 256
85
86#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
87
88
89/*
90 * We default to IRQ0 for the "no irq" hack. Some
91 * machine types want others as well - they're free
92 * to redefine this in their header file.
93 */
94#define is_real_interrupt(irq) ((irq) != 0)
95
96#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
97#define CONFIG_SERIAL_DETECT_IRQ 1
98#endif
99#ifdef CONFIG_SERIAL_8250_MANY_PORTS
100#define CONFIG_SERIAL_MANY_PORTS 1
101#endif
102
103/*
104 * HUB6 is always on. This will be removed once the header
105 * files have been cleaned.
106 */
107#define CONFIG_HUB6 1
108
109#include <asm/serial.h>
110/*
111 * SERIAL_PORT_DFNS tells us about built-in ports that have no
112 * standard enumeration mechanism. Platforms that can find all
113 * serial ports via mechanisms like ACPI or PCI need not supply it.
114 */
115#ifndef SERIAL_PORT_DFNS
116#define SERIAL_PORT_DFNS
117#endif
118
119static const struct old_serial_port old_serial_port[] = {
120 SERIAL_PORT_DFNS /* defined in asm/serial.h */
121};
122
123#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
124
125#ifdef CONFIG_SERIAL_8250_RSA
126
127#define PORT_RSA_MAX 4
128static unsigned long probe_rsa[PORT_RSA_MAX];
129static unsigned int probe_rsa_count;
130#endif /* CONFIG_SERIAL_8250_RSA */
131
132struct uart_8250_port {
133 struct uart_port port;
134 struct timer_list timer; /* "no irq" timer */
135 struct list_head list; /* ports on this IRQ */
136 unsigned short capabilities; /* port capabilities */
137 unsigned short bugs; /* port bugs */
138 unsigned int tx_loadsz; /* transmit fifo load size */
139 unsigned char acr;
140 unsigned char ier;
141 unsigned char lcr;
142 unsigned char mcr;
143 unsigned char mcr_mask; /* mask of user bits */
144 unsigned char mcr_force; /* mask of forced bits */
145 unsigned char cur_iotype; /* Running I/O type */
146
147 /*
148 * Some bits in registers are cleared on a read, so they must
149 * be saved whenever the register is read but the bits will not
150 * be immediately processed.
151 */
152#define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS
153 unsigned char lsr_saved_flags;
154#define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
155 unsigned char msr_saved_flags;
156};
157
158struct irq_info {
159 struct hlist_node node;
160 int irq;
161 spinlock_t lock; /* Protects list not the hash */
162 struct list_head *head;
163};
164
165#define NR_IRQ_HASH 32 /* Can be adjusted later */
166static struct hlist_head irq_lists[NR_IRQ_HASH];
167static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
168
169/*
170 * Here we define the default xmit fifo size used for each type of UART.
171 */
172static const struct serial8250_config uart_config[] = {
173 [PORT_UNKNOWN] = {
174 .name = "unknown",
175 .fifo_size = 1,
176 .tx_loadsz = 1,
177 },
178 [PORT_8250] = {
179 .name = "8250",
180 .fifo_size = 1,
181 .tx_loadsz = 1,
182 },
183 [PORT_16450] = {
184 .name = "16450",
185 .fifo_size = 1,
186 .tx_loadsz = 1,
187 },
188 [PORT_16550] = {
189 .name = "16550",
190 .fifo_size = 1,
191 .tx_loadsz = 1,
192 },
193 [PORT_16550A] = {
194 .name = "16550A",
195 .fifo_size = 16,
196 .tx_loadsz = 16,
197 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
198 .flags = UART_CAP_FIFO,
199 },
200 [PORT_CIRRUS] = {
201 .name = "Cirrus",
202 .fifo_size = 1,
203 .tx_loadsz = 1,
204 },
205 [PORT_16650] = {
206 .name = "ST16650",
207 .fifo_size = 1,
208 .tx_loadsz = 1,
209 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
210 },
211 [PORT_16650V2] = {
212 .name = "ST16650V2",
213 .fifo_size = 32,
214 .tx_loadsz = 16,
215 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
216 UART_FCR_T_TRIG_00,
217 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
218 },
219 [PORT_16750] = {
220 .name = "TI16750",
221 .fifo_size = 64,
222 .tx_loadsz = 64,
223 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
224 UART_FCR7_64BYTE,
225 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
226 },
227 [PORT_STARTECH] = {
228 .name = "Startech",
229 .fifo_size = 1,
230 .tx_loadsz = 1,
231 },
232 [PORT_16C950] = {
233 .name = "16C950/954",
234 .fifo_size = 128,
235 .tx_loadsz = 128,
236 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
237 /* UART_CAP_EFR breaks billionon CF bluetooth card. */
238 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
239 },
240 [PORT_16654] = {
241 .name = "ST16654",
242 .fifo_size = 64,
243 .tx_loadsz = 32,
244 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
245 UART_FCR_T_TRIG_10,
246 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
247 },
248 [PORT_16850] = {
249 .name = "XR16850",
250 .fifo_size = 128,
251 .tx_loadsz = 128,
252 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
253 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
254 },
255 [PORT_RSA] = {
256 .name = "RSA",
257 .fifo_size = 2048,
258 .tx_loadsz = 2048,
259 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
260 .flags = UART_CAP_FIFO,
261 },
262 [PORT_NS16550A] = {
263 .name = "NS16550A",
264 .fifo_size = 16,
265 .tx_loadsz = 16,
266 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
267 .flags = UART_CAP_FIFO | UART_NATSEMI,
268 },
269 [PORT_XSCALE] = {
270 .name = "XScale",
271 .fifo_size = 32,
272 .tx_loadsz = 32,
273 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
274 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
275 },
276 [PORT_RM9000] = {
277 .name = "RM9000",
278 .fifo_size = 16,
279 .tx_loadsz = 16,
280 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
281 .flags = UART_CAP_FIFO,
282 },
283 [PORT_OCTEON] = {
284 .name = "OCTEON",
285 .fifo_size = 64,
286 .tx_loadsz = 64,
287 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
288 .flags = UART_CAP_FIFO,
289 },
290 [PORT_AR7] = {
291 .name = "AR7",
292 .fifo_size = 16,
293 .tx_loadsz = 16,
294 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
295 .flags = UART_CAP_FIFO | UART_CAP_AFE,
296 },
297 [PORT_U6_16550A] = {
298 .name = "U6_16550A",
299 .fifo_size = 64,
300 .tx_loadsz = 64,
301 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
302 .flags = UART_CAP_FIFO | UART_CAP_AFE,
303 },
304 [PORT_TEGRA] = {
305 .name = "Tegra",
306 .fifo_size = 32,
307 .tx_loadsz = 8,
308 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
309 UART_FCR_T_TRIG_01,
310 .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
311 },
312};
313
314#if defined(CONFIG_MIPS_ALCHEMY)
315
316/* Au1x00 UART hardware has a weird register layout */
317static const u8 au_io_in_map[] = {
318 [UART_RX] = 0,
319 [UART_IER] = 2,
320 [UART_IIR] = 3,
321 [UART_LCR] = 5,
322 [UART_MCR] = 6,
323 [UART_LSR] = 7,
324 [UART_MSR] = 8,
325};
326
327static const u8 au_io_out_map[] = {
328 [UART_TX] = 1,
329 [UART_IER] = 2,
330 [UART_FCR] = 4,
331 [UART_LCR] = 5,
332 [UART_MCR] = 6,
333};
334
335/* sane hardware needs no mapping */
336static inline int map_8250_in_reg(struct uart_port *p, int offset)
337{
338 if (p->iotype != UPIO_AU)
339 return offset;
340 return au_io_in_map[offset];
341}
342
343static inline int map_8250_out_reg(struct uart_port *p, int offset)
344{
345 if (p->iotype != UPIO_AU)
346 return offset;
347 return au_io_out_map[offset];
348}
349
350#elif defined(CONFIG_SERIAL_8250_RM9K)
351
352static const u8
353 regmap_in[8] = {
354 [UART_RX] = 0x00,
355 [UART_IER] = 0x0c,
356 [UART_IIR] = 0x14,
357 [UART_LCR] = 0x1c,
358 [UART_MCR] = 0x20,
359 [UART_LSR] = 0x24,
360 [UART_MSR] = 0x28,
361 [UART_SCR] = 0x2c
362 },
363 regmap_out[8] = {
364 [UART_TX] = 0x04,
365 [UART_IER] = 0x0c,
366 [UART_FCR] = 0x18,
367 [UART_LCR] = 0x1c,
368 [UART_MCR] = 0x20,
369 [UART_LSR] = 0x24,
370 [UART_MSR] = 0x28,
371 [UART_SCR] = 0x2c
372 };
373
374static inline int map_8250_in_reg(struct uart_port *p, int offset)
375{
376 if (p->iotype != UPIO_RM9000)
377 return offset;
378 return regmap_in[offset];
379}
380
381static inline int map_8250_out_reg(struct uart_port *p, int offset)
382{
383 if (p->iotype != UPIO_RM9000)
384 return offset;
385 return regmap_out[offset];
386}
387
388#else
389
390/* sane hardware needs no mapping */
391#define map_8250_in_reg(up, offset) (offset)
392#define map_8250_out_reg(up, offset) (offset)
393
394#endif
395
396static unsigned int hub6_serial_in(struct uart_port *p, int offset)
397{
398 offset = map_8250_in_reg(p, offset) << p->regshift;
399 outb(p->hub6 - 1 + offset, p->iobase);
400 return inb(p->iobase + 1);
401}
402
403static void hub6_serial_out(struct uart_port *p, int offset, int value)
404{
405 offset = map_8250_out_reg(p, offset) << p->regshift;
406 outb(p->hub6 - 1 + offset, p->iobase);
407 outb(value, p->iobase + 1);
408}
409
410static unsigned int mem_serial_in(struct uart_port *p, int offset)
411{
412 offset = map_8250_in_reg(p, offset) << p->regshift;
413 return readb(p->membase + offset);
414}
415
416static void mem_serial_out(struct uart_port *p, int offset, int value)
417{
418 offset = map_8250_out_reg(p, offset) << p->regshift;
419 writeb(value, p->membase + offset);
420}
421
422static void mem32_serial_out(struct uart_port *p, int offset, int value)
423{
424 offset = map_8250_out_reg(p, offset) << p->regshift;
425 writel(value, p->membase + offset);
426}
427
428static unsigned int mem32_serial_in(struct uart_port *p, int offset)
429{
430 offset = map_8250_in_reg(p, offset) << p->regshift;
431 return readl(p->membase + offset);
432}
433
434static unsigned int au_serial_in(struct uart_port *p, int offset)
435{
436 offset = map_8250_in_reg(p, offset) << p->regshift;
437 return __raw_readl(p->membase + offset);
438}
439
440static void au_serial_out(struct uart_port *p, int offset, int value)
441{
442 offset = map_8250_out_reg(p, offset) << p->regshift;
443 __raw_writel(value, p->membase + offset);
444}
445
446static unsigned int tsi_serial_in(struct uart_port *p, int offset)
447{
448 unsigned int tmp;
449 offset = map_8250_in_reg(p, offset) << p->regshift;
450 if (offset == UART_IIR) {
451 tmp = readl(p->membase + (UART_IIR & ~3));
452 return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
453 } else
454 return readb(p->membase + offset);
455}
456
457static void tsi_serial_out(struct uart_port *p, int offset, int value)
458{
459 offset = map_8250_out_reg(p, offset) << p->regshift;
460 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
461 writeb(value, p->membase + offset);
462}
463
464/* Save the LCR value so it can be re-written when a Busy Detect IRQ occurs. */
465static inline void dwapb_save_out_value(struct uart_port *p, int offset,
466 int value)
467{
468 struct uart_8250_port *up =
469 container_of(p, struct uart_8250_port, port);
470
471 if (offset == UART_LCR)
472 up->lcr = value;
473}
474
475/* Read the IER to ensure any interrupt is cleared before returning from ISR. */
476static inline void dwapb_check_clear_ier(struct uart_port *p, int offset)
477{
478 if (offset == UART_TX || offset == UART_IER)
479 p->serial_in(p, UART_IER);
480}
481
482static void dwapb_serial_out(struct uart_port *p, int offset, int value)
483{
484 int save_offset = offset;
485 offset = map_8250_out_reg(p, offset) << p->regshift;
486 dwapb_save_out_value(p, save_offset, value);
487 writeb(value, p->membase + offset);
488 dwapb_check_clear_ier(p, save_offset);
489}
490
491static void dwapb32_serial_out(struct uart_port *p, int offset, int value)
492{
493 int save_offset = offset;
494 offset = map_8250_out_reg(p, offset) << p->regshift;
495 dwapb_save_out_value(p, save_offset, value);
496 writel(value, p->membase + offset);
497 dwapb_check_clear_ier(p, save_offset);
498}
499
500static unsigned int io_serial_in(struct uart_port *p, int offset)
501{
502 offset = map_8250_in_reg(p, offset) << p->regshift;
503 return inb(p->iobase + offset);
504}
505
506static void io_serial_out(struct uart_port *p, int offset, int value)
507{
508 offset = map_8250_out_reg(p, offset) << p->regshift;
509 outb(value, p->iobase + offset);
510}
511
512static void set_io_from_upio(struct uart_port *p)
513{
514 struct uart_8250_port *up =
515 container_of(p, struct uart_8250_port, port);
516 switch (p->iotype) {
517 case UPIO_HUB6:
518 p->serial_in = hub6_serial_in;
519 p->serial_out = hub6_serial_out;
520 break;
521
522 case UPIO_MEM:
523 p->serial_in = mem_serial_in;
524 p->serial_out = mem_serial_out;
525 break;
526
527 case UPIO_RM9000:
528 case UPIO_MEM32:
529 p->serial_in = mem32_serial_in;
530 p->serial_out = mem32_serial_out;
531 break;
532
533 case UPIO_AU:
534 p->serial_in = au_serial_in;
535 p->serial_out = au_serial_out;
536 break;
537
538 case UPIO_TSI:
539 p->serial_in = tsi_serial_in;
540 p->serial_out = tsi_serial_out;
541 break;
542
543 case UPIO_DWAPB:
544 p->serial_in = mem_serial_in;
545 p->serial_out = dwapb_serial_out;
546 break;
547
548 case UPIO_DWAPB32:
549 p->serial_in = mem32_serial_in;
550 p->serial_out = dwapb32_serial_out;
551 break;
552
553 default:
554 p->serial_in = io_serial_in;
555 p->serial_out = io_serial_out;
556 break;
557 }
558 /* Remember loaded iotype */
559 up->cur_iotype = p->iotype;
560}
561
562static void
563serial_out_sync(struct uart_8250_port *up, int offset, int value)
564{
565 struct uart_port *p = &up->port;
566 switch (p->iotype) {
567 case UPIO_MEM:
568 case UPIO_MEM32:
569 case UPIO_AU:
570 case UPIO_DWAPB:
571 case UPIO_DWAPB32:
572 p->serial_out(p, offset, value);
573 p->serial_in(p, UART_LCR); /* safe, no side-effects */
574 break;
575 default:
576 p->serial_out(p, offset, value);
577 }
578}
579
580#define serial_in(up, offset) \
581 (up->port.serial_in(&(up)->port, (offset)))
582#define serial_out(up, offset, value) \
583 (up->port.serial_out(&(up)->port, (offset), (value)))
584/*
585 * We used to support using pause I/O for certain machines. We
586 * haven't supported this for a while, but just in case it's badly
587 * needed for certain old 386 machines, I've left these #define's
588 * in....
589 */
590#define serial_inp(up, offset) serial_in(up, offset)
591#define serial_outp(up, offset, value) serial_out(up, offset, value)
592
593/* Uart divisor latch read */
594static inline int _serial_dl_read(struct uart_8250_port *up)
595{
596 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
597}
598
599/* Uart divisor latch write */
600static inline void _serial_dl_write(struct uart_8250_port *up, int value)
601{
602 serial_outp(up, UART_DLL, value & 0xff);
603 serial_outp(up, UART_DLM, value >> 8 & 0xff);
604}
605
606#if defined(CONFIG_MIPS_ALCHEMY)
607/* Au1x00 haven't got a standard divisor latch */
608static int serial_dl_read(struct uart_8250_port *up)
609{
610 if (up->port.iotype == UPIO_AU)
611 return __raw_readl(up->port.membase + 0x28);
612 else
613 return _serial_dl_read(up);
614}
615
616static void serial_dl_write(struct uart_8250_port *up, int value)
617{
618 if (up->port.iotype == UPIO_AU)
619 __raw_writel(value, up->port.membase + 0x28);
620 else
621 _serial_dl_write(up, value);
622}
623#elif defined(CONFIG_SERIAL_8250_RM9K)
624static int serial_dl_read(struct uart_8250_port *up)
625{
626 return (up->port.iotype == UPIO_RM9000) ?
627 (((__raw_readl(up->port.membase + 0x10) << 8) |
628 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
629 _serial_dl_read(up);
630}
631
632static void serial_dl_write(struct uart_8250_port *up, int value)
633{
634 if (up->port.iotype == UPIO_RM9000) {
635 __raw_writel(value, up->port.membase + 0x08);
636 __raw_writel(value >> 8, up->port.membase + 0x10);
637 } else {
638 _serial_dl_write(up, value);
639 }
640}
641#else
642#define serial_dl_read(up) _serial_dl_read(up)
643#define serial_dl_write(up, value) _serial_dl_write(up, value)
644#endif
645
646/*
647 * For the 16C950
648 */
649static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
650{
651 serial_out(up, UART_SCR, offset);
652 serial_out(up, UART_ICR, value);
653}
654
655static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
656{
657 unsigned int value;
658
659 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
660 serial_out(up, UART_SCR, offset);
661 value = serial_in(up, UART_ICR);
662 serial_icr_write(up, UART_ACR, up->acr);
663
664 return value;
665}
666
667/*
668 * FIFO support.
669 */
670static void serial8250_clear_fifos(struct uart_8250_port *p)
671{
672 if (p->capabilities & UART_CAP_FIFO) {
673 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
674 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
675 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
676 serial_outp(p, UART_FCR, 0);
677 }
678}
679
680/*
681 * IER sleep support. UARTs which have EFRs need the "extended
682 * capability" bit enabled. Note that on XR16C850s, we need to
683 * reset LCR to write to IER.
684 */
685static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
686{
687 if (p->capabilities & UART_CAP_SLEEP) {
688 if (p->capabilities & UART_CAP_EFR) {
689 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
690 serial_outp(p, UART_EFR, UART_EFR_ECB);
691 serial_outp(p, UART_LCR, 0);
692 }
693 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
694 if (p->capabilities & UART_CAP_EFR) {
695 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_B);
696 serial_outp(p, UART_EFR, 0);
697 serial_outp(p, UART_LCR, 0);
698 }
699 }
700}
701
702#ifdef CONFIG_SERIAL_8250_RSA
703/*
704 * Attempts to turn on the RSA FIFO. Returns zero on failure.
705 * We set the port uart clock rate if we succeed.
706 */
707static int __enable_rsa(struct uart_8250_port *up)
708{
709 unsigned char mode;
710 int result;
711
712 mode = serial_inp(up, UART_RSA_MSR);
713 result = mode & UART_RSA_MSR_FIFO;
714
715 if (!result) {
716 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
717 mode = serial_inp(up, UART_RSA_MSR);
718 result = mode & UART_RSA_MSR_FIFO;
719 }
720
721 if (result)
722 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
723
724 return result;
725}
726
727static void enable_rsa(struct uart_8250_port *up)
728{
729 if (up->port.type == PORT_RSA) {
730 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
731 spin_lock_irq(&up->port.lock);
732 __enable_rsa(up);
733 spin_unlock_irq(&up->port.lock);
734 }
735 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
736 serial_outp(up, UART_RSA_FRR, 0);
737 }
738}
739
740/*
741 * Attempts to turn off the RSA FIFO. Returns zero on failure.
742 * It is unknown why interrupts were disabled in here. However,
743 * the caller is expected to preserve this behaviour by grabbing
744 * the spinlock before calling this function.
745 */
746static void disable_rsa(struct uart_8250_port *up)
747{
748 unsigned char mode;
749 int result;
750
751 if (up->port.type == PORT_RSA &&
752 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
753 spin_lock_irq(&up->port.lock);
754
755 mode = serial_inp(up, UART_RSA_MSR);
756 result = !(mode & UART_RSA_MSR_FIFO);
757
758 if (!result) {
759 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
760 mode = serial_inp(up, UART_RSA_MSR);
761 result = !(mode & UART_RSA_MSR_FIFO);
762 }
763
764 if (result)
765 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
766 spin_unlock_irq(&up->port.lock);
767 }
768}
769#endif /* CONFIG_SERIAL_8250_RSA */
770
771/*
772 * This is a quickie test to see how big the FIFO is.
773 * It doesn't work at all the time, more's the pity.
774 */
775static int size_fifo(struct uart_8250_port *up)
776{
777 unsigned char old_fcr, old_mcr, old_lcr;
778 unsigned short old_dl;
779 int count;
780
781 old_lcr = serial_inp(up, UART_LCR);
782 serial_outp(up, UART_LCR, 0);
783 old_fcr = serial_inp(up, UART_FCR);
784 old_mcr = serial_inp(up, UART_MCR);
785 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
786 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
787 serial_outp(up, UART_MCR, UART_MCR_LOOP);
788 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
789 old_dl = serial_dl_read(up);
790 serial_dl_write(up, 0x0001);
791 serial_outp(up, UART_LCR, 0x03);
792 for (count = 0; count < 256; count++)
793 serial_outp(up, UART_TX, count);
794 mdelay(20);/* FIXME - schedule_timeout */
795 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
796 (count < 256); count++)
797 serial_inp(up, UART_RX);
798 serial_outp(up, UART_FCR, old_fcr);
799 serial_outp(up, UART_MCR, old_mcr);
800 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
801 serial_dl_write(up, old_dl);
802 serial_outp(up, UART_LCR, old_lcr);
803
804 return count;
805}
806
807/*
808 * Read UART ID using the divisor method - set DLL and DLM to zero
809 * and the revision will be in DLL and device type in DLM. We
810 * preserve the device state across this.
811 */
812static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
813{
814 unsigned char old_dll, old_dlm, old_lcr;
815 unsigned int id;
816
817 old_lcr = serial_inp(p, UART_LCR);
818 serial_outp(p, UART_LCR, UART_LCR_CONF_MODE_A);
819
820 old_dll = serial_inp(p, UART_DLL);
821 old_dlm = serial_inp(p, UART_DLM);
822
823 serial_outp(p, UART_DLL, 0);
824 serial_outp(p, UART_DLM, 0);
825
826 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
827
828 serial_outp(p, UART_DLL, old_dll);
829 serial_outp(p, UART_DLM, old_dlm);
830 serial_outp(p, UART_LCR, old_lcr);
831
832 return id;
833}
834
835/*
836 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
837 * When this function is called we know it is at least a StarTech
838 * 16650 V2, but it might be one of several StarTech UARTs, or one of
839 * its clones. (We treat the broken original StarTech 16650 V1 as a
840 * 16550, and why not? Startech doesn't seem to even acknowledge its
841 * existence.)
842 *
843 * What evil have men's minds wrought...
844 */
845static void autoconfig_has_efr(struct uart_8250_port *up)
846{
847 unsigned int id1, id2, id3, rev;
848
849 /*
850 * Everything with an EFR has SLEEP
851 */
852 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
853
854 /*
855 * First we check to see if it's an Oxford Semiconductor UART.
856 *
857 * If we have to do this here because some non-National
858 * Semiconductor clone chips lock up if you try writing to the
859 * LSR register (which serial_icr_read does)
860 */
861
862 /*
863 * Check for Oxford Semiconductor 16C950.
864 *
865 * EFR [4] must be set else this test fails.
866 *
867 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
868 * claims that it's needed for 952 dual UART's (which are not
869 * recommended for new designs).
870 */
871 up->acr = 0;
872 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
873 serial_out(up, UART_EFR, UART_EFR_ECB);
874 serial_out(up, UART_LCR, 0x00);
875 id1 = serial_icr_read(up, UART_ID1);
876 id2 = serial_icr_read(up, UART_ID2);
877 id3 = serial_icr_read(up, UART_ID3);
878 rev = serial_icr_read(up, UART_REV);
879
880 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
881
882 if (id1 == 0x16 && id2 == 0xC9 &&
883 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
884 up->port.type = PORT_16C950;
885
886 /*
887 * Enable work around for the Oxford Semiconductor 952 rev B
888 * chip which causes it to seriously miscalculate baud rates
889 * when DLL is 0.
890 */
891 if (id3 == 0x52 && rev == 0x01)
892 up->bugs |= UART_BUG_QUOT;
893 return;
894 }
895
896 /*
897 * We check for a XR16C850 by setting DLL and DLM to 0, and then
898 * reading back DLL and DLM. The chip type depends on the DLM
899 * value read back:
900 * 0x10 - XR16C850 and the DLL contains the chip revision.
901 * 0x12 - XR16C2850.
902 * 0x14 - XR16C854.
903 */
904 id1 = autoconfig_read_divisor_id(up);
905 DEBUG_AUTOCONF("850id=%04x ", id1);
906
907 id2 = id1 >> 8;
908 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
909 up->port.type = PORT_16850;
910 return;
911 }
912
913 /*
914 * It wasn't an XR16C850.
915 *
916 * We distinguish between the '654 and the '650 by counting
917 * how many bytes are in the FIFO. I'm using this for now,
918 * since that's the technique that was sent to me in the
919 * serial driver update, but I'm not convinced this works.
920 * I've had problems doing this in the past. -TYT
921 */
922 if (size_fifo(up) == 64)
923 up->port.type = PORT_16654;
924 else
925 up->port.type = PORT_16650V2;
926}
927
928/*
929 * We detected a chip without a FIFO. Only two fall into
930 * this category - the original 8250 and the 16450. The
931 * 16450 has a scratch register (accessible with LCR=0)
932 */
933static void autoconfig_8250(struct uart_8250_port *up)
934{
935 unsigned char scratch, status1, status2;
936
937 up->port.type = PORT_8250;
938
939 scratch = serial_in(up, UART_SCR);
940 serial_outp(up, UART_SCR, 0xa5);
941 status1 = serial_in(up, UART_SCR);
942 serial_outp(up, UART_SCR, 0x5a);
943 status2 = serial_in(up, UART_SCR);
944 serial_outp(up, UART_SCR, scratch);
945
946 if (status1 == 0xa5 && status2 == 0x5a)
947 up->port.type = PORT_16450;
948}
949
950static int broken_efr(struct uart_8250_port *up)
951{
952 /*
953 * Exar ST16C2550 "A2" devices incorrectly detect as
954 * having an EFR, and report an ID of 0x0201. See
955 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
956 */
957 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
958 return 1;
959
960 return 0;
961}
962
963static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
964{
965 unsigned char status;
966
967 status = serial_in(up, 0x04); /* EXCR2 */
968#define PRESL(x) ((x) & 0x30)
969 if (PRESL(status) == 0x10) {
970 /* already in high speed mode */
971 return 0;
972 } else {
973 status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
974 status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
975 serial_outp(up, 0x04, status);
976 }
977 return 1;
978}
979
980/*
981 * We know that the chip has FIFOs. Does it have an EFR? The
982 * EFR is located in the same register position as the IIR and
983 * we know the top two bits of the IIR are currently set. The
984 * EFR should contain zero. Try to read the EFR.
985 */
986static void autoconfig_16550a(struct uart_8250_port *up)
987{
988 unsigned char status1, status2;
989 unsigned int iersave;
990
991 up->port.type = PORT_16550A;
992 up->capabilities |= UART_CAP_FIFO;
993
994 /*
995 * Check for presence of the EFR when DLAB is set.
996 * Only ST16C650V1 UARTs pass this test.
997 */
998 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
999 if (serial_in(up, UART_EFR) == 0) {
1000 serial_outp(up, UART_EFR, 0xA8);
1001 if (serial_in(up, UART_EFR) != 0) {
1002 DEBUG_AUTOCONF("EFRv1 ");
1003 up->port.type = PORT_16650;
1004 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
1005 } else {
1006 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
1007 }
1008 serial_outp(up, UART_EFR, 0);
1009 return;
1010 }
1011
1012 /*
1013 * Maybe it requires 0xbf to be written to the LCR.
1014 * (other ST16C650V2 UARTs, TI16C752A, etc)
1015 */
1016 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
1017 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
1018 DEBUG_AUTOCONF("EFRv2 ");
1019 autoconfig_has_efr(up);
1020 return;
1021 }
1022
1023 /*
1024 * Check for a National Semiconductor SuperIO chip.
1025 * Attempt to switch to bank 2, read the value of the LOOP bit
1026 * from EXCR1. Switch back to bank 0, change it in MCR. Then
1027 * switch back to bank 2, read it from EXCR1 again and check
1028 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
1029 */
1030 serial_outp(up, UART_LCR, 0);
1031 status1 = serial_in(up, UART_MCR);
1032 serial_outp(up, UART_LCR, 0xE0);
1033 status2 = serial_in(up, 0x02); /* EXCR1 */
1034
1035 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1036 serial_outp(up, UART_LCR, 0);
1037 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
1038 serial_outp(up, UART_LCR, 0xE0);
1039 status2 = serial_in(up, 0x02); /* EXCR1 */
1040 serial_outp(up, UART_LCR, 0);
1041 serial_outp(up, UART_MCR, status1);
1042
1043 if ((status2 ^ status1) & UART_MCR_LOOP) {
1044 unsigned short quot;
1045
1046 serial_outp(up, UART_LCR, 0xE0);
1047
1048 quot = serial_dl_read(up);
1049 quot <<= 3;
1050
1051 if (ns16550a_goto_highspeed(up))
1052 serial_dl_write(up, quot);
1053
1054 serial_outp(up, UART_LCR, 0);
1055
1056 up->port.uartclk = 921600*16;
1057 up->port.type = PORT_NS16550A;
1058 up->capabilities |= UART_NATSEMI;
1059 return;
1060 }
1061 }
1062
1063 /*
1064 * No EFR. Try to detect a TI16750, which only sets bit 5 of
1065 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1066 * Try setting it with and without DLAB set. Cheap clones
1067 * set bit 5 without DLAB set.
1068 */
1069 serial_outp(up, UART_LCR, 0);
1070 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1071 status1 = serial_in(up, UART_IIR) >> 5;
1072 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1073 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_A);
1074 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1075 status2 = serial_in(up, UART_IIR) >> 5;
1076 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1077 serial_outp(up, UART_LCR, 0);
1078
1079 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1080
1081 if (status1 == 6 && status2 == 7) {
1082 up->port.type = PORT_16750;
1083 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1084 return;
1085 }
1086
1087 /*
1088 * Try writing and reading the UART_IER_UUE bit (b6).
1089 * If it works, this is probably one of the Xscale platform's
1090 * internal UARTs.
1091 * We're going to explicitly set the UUE bit to 0 before
1092 * trying to write and read a 1 just to make sure it's not
1093 * already a 1 and maybe locked there before we even start start.
1094 */
1095 iersave = serial_in(up, UART_IER);
1096 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
1097 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1098 /*
1099 * OK it's in a known zero state, try writing and reading
1100 * without disturbing the current state of the other bits.
1101 */
1102 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
1103 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1104 /*
1105 * It's an Xscale.
1106 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1107 */
1108 DEBUG_AUTOCONF("Xscale ");
1109 up->port.type = PORT_XSCALE;
1110 up->capabilities |= UART_CAP_UUE;
1111 return;
1112 }
1113 } else {
1114 /*
1115 * If we got here we couldn't force the IER_UUE bit to 0.
1116 * Log it and continue.
1117 */
1118 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1119 }
1120 serial_outp(up, UART_IER, iersave);
1121
1122 /*
1123 * We distinguish between 16550A and U6 16550A by counting
1124 * how many bytes are in the FIFO.
1125 */
1126 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1127 up->port.type = PORT_U6_16550A;
1128 up->capabilities |= UART_CAP_AFE;
1129 }
1130}
1131
1132/*
1133 * This routine is called by rs_init() to initialize a specific serial
1134 * port. It determines what type of UART chip this serial port is
1135 * using: 8250, 16450, 16550, 16550A. The important question is
1136 * whether or not this UART is a 16550A or not, since this will
1137 * determine whether or not we can use its FIFO features or not.
1138 */
1139static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
1140{
1141 unsigned char status1, scratch, scratch2, scratch3;
1142 unsigned char save_lcr, save_mcr;
1143 unsigned long flags;
1144
1145 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
1146 return;
1147
1148 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
1149 serial_index(&up->port), up->port.iobase, up->port.membase);
1150
1151 /*
1152 * We really do need global IRQs disabled here - we're going to
1153 * be frobbing the chips IRQ enable register to see if it exists.
1154 */
1155 spin_lock_irqsave(&up->port.lock, flags);
1156
1157 up->capabilities = 0;
1158 up->bugs = 0;
1159
1160 if (!(up->port.flags & UPF_BUGGY_UART)) {
1161 /*
1162 * Do a simple existence test first; if we fail this,
1163 * there's no point trying anything else.
1164 *
1165 * 0x80 is used as a nonsense port to prevent against
1166 * false positives due to ISA bus float. The
1167 * assumption is that 0x80 is a non-existent port;
1168 * which should be safe since include/asm/io.h also
1169 * makes this assumption.
1170 *
1171 * Note: this is safe as long as MCR bit 4 is clear
1172 * and the device is in "PC" mode.
1173 */
1174 scratch = serial_inp(up, UART_IER);
1175 serial_outp(up, UART_IER, 0);
1176#ifdef __i386__
1177 outb(0xff, 0x080);
1178#endif
1179 /*
1180 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1181 * 16C754B) allow only to modify them if an EFR bit is set.
1182 */
1183 scratch2 = serial_inp(up, UART_IER) & 0x0f;
1184 serial_outp(up, UART_IER, 0x0F);
1185#ifdef __i386__
1186 outb(0, 0x080);
1187#endif
1188 scratch3 = serial_inp(up, UART_IER) & 0x0f;
1189 serial_outp(up, UART_IER, scratch);
1190 if (scratch2 != 0 || scratch3 != 0x0F) {
1191 /*
1192 * We failed; there's nothing here
1193 */
1194 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1195 scratch2, scratch3);
1196 goto out;
1197 }
1198 }
1199
1200 save_mcr = serial_in(up, UART_MCR);
1201 save_lcr = serial_in(up, UART_LCR);
1202
1203 /*
1204 * Check to see if a UART is really there. Certain broken
1205 * internal modems based on the Rockwell chipset fail this
1206 * test, because they apparently don't implement the loopback
1207 * test mode. So this test is skipped on the COM 1 through
1208 * COM 4 ports. This *should* be safe, since no board
1209 * manufacturer would be stupid enough to design a board
1210 * that conflicts with COM 1-4 --- we hope!
1211 */
1212 if (!(up->port.flags & UPF_SKIP_TEST)) {
1213 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1214 status1 = serial_inp(up, UART_MSR) & 0xF0;
1215 serial_outp(up, UART_MCR, save_mcr);
1216 if (status1 != 0x90) {
1217 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1218 status1);
1219 goto out;
1220 }
1221 }
1222
1223 /*
1224 * We're pretty sure there's a port here. Lets find out what
1225 * type of port it is. The IIR top two bits allows us to find
1226 * out if it's 8250 or 16450, 16550, 16550A or later. This
1227 * determines what we test for next.
1228 *
1229 * We also initialise the EFR (if any) to zero for later. The
1230 * EFR occupies the same register location as the FCR and IIR.
1231 */
1232 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
1233 serial_outp(up, UART_EFR, 0);
1234 serial_outp(up, UART_LCR, 0);
1235
1236 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1237 scratch = serial_in(up, UART_IIR) >> 6;
1238
1239 DEBUG_AUTOCONF("iir=%d ", scratch);
1240
1241 switch (scratch) {
1242 case 0:
1243 autoconfig_8250(up);
1244 break;
1245 case 1:
1246 up->port.type = PORT_UNKNOWN;
1247 break;
1248 case 2:
1249 up->port.type = PORT_16550;
1250 break;
1251 case 3:
1252 autoconfig_16550a(up);
1253 break;
1254 }
1255
1256#ifdef CONFIG_SERIAL_8250_RSA
1257 /*
1258 * Only probe for RSA ports if we got the region.
1259 */
1260 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1261 int i;
1262
1263 for (i = 0 ; i < probe_rsa_count; ++i) {
1264 if (probe_rsa[i] == up->port.iobase &&
1265 __enable_rsa(up)) {
1266 up->port.type = PORT_RSA;
1267 break;
1268 }
1269 }
1270 }
1271#endif
1272
1273 serial_outp(up, UART_LCR, save_lcr);
1274
1275 if (up->capabilities != uart_config[up->port.type].flags) {
1276 printk(KERN_WARNING
1277 "ttyS%d: detected caps %08x should be %08x\n",
1278 serial_index(&up->port), up->capabilities,
1279 uart_config[up->port.type].flags);
1280 }
1281
1282 up->port.fifosize = uart_config[up->port.type].fifo_size;
1283 up->capabilities = uart_config[up->port.type].flags;
1284 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1285
1286 if (up->port.type == PORT_UNKNOWN)
1287 goto out;
1288
1289 /*
1290 * Reset the UART.
1291 */
1292#ifdef CONFIG_SERIAL_8250_RSA
1293 if (up->port.type == PORT_RSA)
1294 serial_outp(up, UART_RSA_FRR, 0);
1295#endif
1296 serial_outp(up, UART_MCR, save_mcr);
1297 serial8250_clear_fifos(up);
1298 serial_in(up, UART_RX);
1299 if (up->capabilities & UART_CAP_UUE)
1300 serial_outp(up, UART_IER, UART_IER_UUE);
1301 else
1302 serial_outp(up, UART_IER, 0);
1303
1304 out:
1305 spin_unlock_irqrestore(&up->port.lock, flags);
1306 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1307}
1308
1309static void autoconfig_irq(struct uart_8250_port *up)
1310{
1311 unsigned char save_mcr, save_ier;
1312 unsigned char save_ICP = 0;
1313 unsigned int ICP = 0;
1314 unsigned long irqs;
1315 int irq;
1316
1317 if (up->port.flags & UPF_FOURPORT) {
1318 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1319 save_ICP = inb_p(ICP);
1320 outb_p(0x80, ICP);
1321 (void) inb_p(ICP);
1322 }
1323
1324 /* forget possible initially masked and pending IRQ */
1325 probe_irq_off(probe_irq_on());
1326 save_mcr = serial_inp(up, UART_MCR);
1327 save_ier = serial_inp(up, UART_IER);
1328 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1329
1330 irqs = probe_irq_on();
1331 serial_outp(up, UART_MCR, 0);
1332 udelay(10);
1333 if (up->port.flags & UPF_FOURPORT) {
1334 serial_outp(up, UART_MCR,
1335 UART_MCR_DTR | UART_MCR_RTS);
1336 } else {
1337 serial_outp(up, UART_MCR,
1338 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1339 }
1340 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1341 (void)serial_inp(up, UART_LSR);
1342 (void)serial_inp(up, UART_RX);
1343 (void)serial_inp(up, UART_IIR);
1344 (void)serial_inp(up, UART_MSR);
1345 serial_outp(up, UART_TX, 0xFF);
1346 udelay(20);
1347 irq = probe_irq_off(irqs);
1348
1349 serial_outp(up, UART_MCR, save_mcr);
1350 serial_outp(up, UART_IER, save_ier);
1351
1352 if (up->port.flags & UPF_FOURPORT)
1353 outb_p(save_ICP, ICP);
1354
1355 up->port.irq = (irq > 0) ? irq : 0;
1356}
1357
1358static inline void __stop_tx(struct uart_8250_port *p)
1359{
1360 if (p->ier & UART_IER_THRI) {
1361 p->ier &= ~UART_IER_THRI;
1362 serial_out(p, UART_IER, p->ier);
1363 }
1364}
1365
1366static void serial8250_stop_tx(struct uart_port *port)
1367{
1368 struct uart_8250_port *up =
1369 container_of(port, struct uart_8250_port, port);
1370
1371 __stop_tx(up);
1372
1373 /*
1374 * We really want to stop the transmitter from sending.
1375 */
1376 if (up->port.type == PORT_16C950) {
1377 up->acr |= UART_ACR_TXDIS;
1378 serial_icr_write(up, UART_ACR, up->acr);
1379 }
1380}
1381
1382static void transmit_chars(struct uart_8250_port *up);
1383
1384static void serial8250_start_tx(struct uart_port *port)
1385{
1386 struct uart_8250_port *up =
1387 container_of(port, struct uart_8250_port, port);
1388
1389 if (!(up->ier & UART_IER_THRI)) {
1390 up->ier |= UART_IER_THRI;
1391 serial_out(up, UART_IER, up->ier);
1392
1393 if (up->bugs & UART_BUG_TXEN) {
1394 unsigned char lsr;
1395 lsr = serial_in(up, UART_LSR);
1396 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1397 if ((up->port.type == PORT_RM9000) ?
1398 (lsr & UART_LSR_THRE) :
1399 (lsr & UART_LSR_TEMT))
1400 transmit_chars(up);
1401 }
1402 }
1403
1404 /*
1405 * Re-enable the transmitter if we disabled it.
1406 */
1407 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1408 up->acr &= ~UART_ACR_TXDIS;
1409 serial_icr_write(up, UART_ACR, up->acr);
1410 }
1411}
1412
1413static void serial8250_stop_rx(struct uart_port *port)
1414{
1415 struct uart_8250_port *up =
1416 container_of(port, struct uart_8250_port, port);
1417
1418 up->ier &= ~UART_IER_RLSI;
1419 up->port.read_status_mask &= ~UART_LSR_DR;
1420 serial_out(up, UART_IER, up->ier);
1421}
1422
1423static void serial8250_enable_ms(struct uart_port *port)
1424{
1425 struct uart_8250_port *up =
1426 container_of(port, struct uart_8250_port, port);
1427
1428 /* no MSR capabilities */
1429 if (up->bugs & UART_BUG_NOMSR)
1430 return;
1431
1432 up->ier |= UART_IER_MSI;
1433 serial_out(up, UART_IER, up->ier);
1434}
1435
1436/*
1437 * Clear the Tegra rx fifo after a break
1438 *
1439 * FIXME: This needs to become a port specific callback once we have a
1440 * framework for this
1441 */
1442static void clear_rx_fifo(struct uart_8250_port *up)
1443{
1444 unsigned int status, tmout = 10000;
1445 do {
1446 status = serial_in(up, UART_LSR);
1447 if (status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS))
1448 status = serial_in(up, UART_RX);
1449 else
1450 break;
1451 if (--tmout == 0)
1452 break;
1453 udelay(1);
1454 } while (1);
1455}
1456
1457static void
1458receive_chars(struct uart_8250_port *up, unsigned int *status)
1459{
1460 struct tty_struct *tty = up->port.state->port.tty;
1461 unsigned char ch, lsr = *status;
1462 int max_count = 256;
1463 char flag;
1464
1465 do {
1466 if (likely(lsr & UART_LSR_DR))
1467 ch = serial_inp(up, UART_RX);
1468 else
1469 /*
1470 * Intel 82571 has a Serial Over Lan device that will
1471 * set UART_LSR_BI without setting UART_LSR_DR when
1472 * it receives a break. To avoid reading from the
1473 * receive buffer without UART_LSR_DR bit set, we
1474 * just force the read character to be 0
1475 */
1476 ch = 0;
1477
1478 flag = TTY_NORMAL;
1479 up->port.icount.rx++;
1480
1481 lsr |= up->lsr_saved_flags;
1482 up->lsr_saved_flags = 0;
1483
1484 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1485 /*
1486 * For statistics only
1487 */
1488 if (lsr & UART_LSR_BI) {
1489 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1490 up->port.icount.brk++;
1491 /*
1492 * If tegra port then clear the rx fifo to
1493 * accept another break/character.
1494 */
1495 if (up->port.type == PORT_TEGRA)
1496 clear_rx_fifo(up);
1497
1498 /*
1499 * We do the SysRQ and SAK checking
1500 * here because otherwise the break
1501 * may get masked by ignore_status_mask
1502 * or read_status_mask.
1503 */
1504 if (uart_handle_break(&up->port))
1505 goto ignore_char;
1506 } else if (lsr & UART_LSR_PE)
1507 up->port.icount.parity++;
1508 else if (lsr & UART_LSR_FE)
1509 up->port.icount.frame++;
1510 if (lsr & UART_LSR_OE)
1511 up->port.icount.overrun++;
1512
1513 /*
1514 * Mask off conditions which should be ignored.
1515 */
1516 lsr &= up->port.read_status_mask;
1517
1518 if (lsr & UART_LSR_BI) {
1519 DEBUG_INTR("handling break....");
1520 flag = TTY_BREAK;
1521 } else if (lsr & UART_LSR_PE)
1522 flag = TTY_PARITY;
1523 else if (lsr & UART_LSR_FE)
1524 flag = TTY_FRAME;
1525 }
1526 if (uart_handle_sysrq_char(&up->port, ch))
1527 goto ignore_char;
1528
1529 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1530
1531ignore_char:
1532 lsr = serial_inp(up, UART_LSR);
1533 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
1534 spin_unlock(&up->port.lock);
1535 tty_flip_buffer_push(tty);
1536 spin_lock(&up->port.lock);
1537 *status = lsr;
1538}
1539
1540static void transmit_chars(struct uart_8250_port *up)
1541{
1542 struct circ_buf *xmit = &up->port.state->xmit;
1543 int count;
1544
1545 if (up->port.x_char) {
1546 serial_outp(up, UART_TX, up->port.x_char);
1547 up->port.icount.tx++;
1548 up->port.x_char = 0;
1549 return;
1550 }
1551 if (uart_tx_stopped(&up->port)) {
1552 serial8250_stop_tx(&up->port);
1553 return;
1554 }
1555 if (uart_circ_empty(xmit)) {
1556 __stop_tx(up);
1557 return;
1558 }
1559
1560 count = up->tx_loadsz;
1561 do {
1562 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1563 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1564 up->port.icount.tx++;
1565 if (uart_circ_empty(xmit))
1566 break;
1567 } while (--count > 0);
1568
1569 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1570 uart_write_wakeup(&up->port);
1571
1572 DEBUG_INTR("THRE...");
1573
1574 if (uart_circ_empty(xmit))
1575 __stop_tx(up);
1576}
1577
1578static unsigned int check_modem_status(struct uart_8250_port *up)
1579{
1580 unsigned int status = serial_in(up, UART_MSR);
1581
1582 status |= up->msr_saved_flags;
1583 up->msr_saved_flags = 0;
1584 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1585 up->port.state != NULL) {
1586 if (status & UART_MSR_TERI)
1587 up->port.icount.rng++;
1588 if (status & UART_MSR_DDSR)
1589 up->port.icount.dsr++;
1590 if (status & UART_MSR_DDCD)
1591 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1592 if (status & UART_MSR_DCTS)
1593 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
1594
1595 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
1596 }
1597
1598 return status;
1599}
1600
1601/*
1602 * This handles the interrupt from one port.
1603 */
1604static void serial8250_handle_port(struct uart_8250_port *up)
1605{
1606 unsigned int status;
1607 unsigned long flags;
1608
1609 spin_lock_irqsave(&up->port.lock, flags);
1610
1611 status = serial_inp(up, UART_LSR);
1612
1613 DEBUG_INTR("status = %x...", status);
1614
1615 if (status & (UART_LSR_DR | UART_LSR_BI))
1616 receive_chars(up, &status);
1617 check_modem_status(up);
1618 if (status & UART_LSR_THRE)
1619 transmit_chars(up);
1620
1621 spin_unlock_irqrestore(&up->port.lock, flags);
1622}
1623
1624/*
1625 * This is the serial driver's interrupt routine.
1626 *
1627 * Arjan thinks the old way was overly complex, so it got simplified.
1628 * Alan disagrees, saying that need the complexity to handle the weird
1629 * nature of ISA shared interrupts. (This is a special exception.)
1630 *
1631 * In order to handle ISA shared interrupts properly, we need to check
1632 * that all ports have been serviced, and therefore the ISA interrupt
1633 * line has been de-asserted.
1634 *
1635 * This means we need to loop through all ports. checking that they
1636 * don't have an interrupt pending.
1637 */
1638static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
1639{
1640 struct irq_info *i = dev_id;
1641 struct list_head *l, *end = NULL;
1642 int pass_counter = 0, handled = 0;
1643
1644 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1645
1646 spin_lock(&i->lock);
1647
1648 l = i->head;
1649 do {
1650 struct uart_8250_port *up;
1651 unsigned int iir;
1652
1653 up = list_entry(l, struct uart_8250_port, list);
1654
1655 iir = serial_in(up, UART_IIR);
1656 if (!(iir & UART_IIR_NO_INT)) {
1657 serial8250_handle_port(up);
1658
1659 handled = 1;
1660
1661 end = NULL;
1662 } else if ((up->port.iotype == UPIO_DWAPB ||
1663 up->port.iotype == UPIO_DWAPB32) &&
1664 (iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
1665 /* The DesignWare APB UART has an Busy Detect (0x07)
1666 * interrupt meaning an LCR write attempt occurred while the
1667 * UART was busy. The interrupt must be cleared by reading
1668 * the UART status register (USR) and the LCR re-written. */
1669 unsigned int status;
1670 status = *(volatile u32 *)up->port.private_data;
1671 serial_out(up, UART_LCR, up->lcr);
1672
1673 handled = 1;
1674
1675 end = NULL;
1676 } else if (end == NULL)
1677 end = l;
1678
1679 l = l->next;
1680
1681 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1682 /* If we hit this, we're dead. */
1683 printk_ratelimited(KERN_ERR
1684 "serial8250: too much work for irq%d\n", irq);
1685 break;
1686 }
1687 } while (l != end);
1688
1689 spin_unlock(&i->lock);
1690
1691 DEBUG_INTR("end.\n");
1692
1693 return IRQ_RETVAL(handled);
1694}
1695
1696/*
1697 * To support ISA shared interrupts, we need to have one interrupt
1698 * handler that ensures that the IRQ line has been deasserted
1699 * before returning. Failing to do this will result in the IRQ
1700 * line being stuck active, and, since ISA irqs are edge triggered,
1701 * no more IRQs will be seen.
1702 */
1703static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1704{
1705 spin_lock_irq(&i->lock);
1706
1707 if (!list_empty(i->head)) {
1708 if (i->head == &up->list)
1709 i->head = i->head->next;
1710 list_del(&up->list);
1711 } else {
1712 BUG_ON(i->head != &up->list);
1713 i->head = NULL;
1714 }
1715 spin_unlock_irq(&i->lock);
1716 /* List empty so throw away the hash node */
1717 if (i->head == NULL) {
1718 hlist_del(&i->node);
1719 kfree(i);
1720 }
1721}
1722
1723static int serial_link_irq_chain(struct uart_8250_port *up)
1724{
1725 struct hlist_head *h;
1726 struct hlist_node *n;
1727 struct irq_info *i;
1728 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
1729
1730 mutex_lock(&hash_mutex);
1731
1732 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1733
1734 hlist_for_each(n, h) {
1735 i = hlist_entry(n, struct irq_info, node);
1736 if (i->irq == up->port.irq)
1737 break;
1738 }
1739
1740 if (n == NULL) {
1741 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1742 if (i == NULL) {
1743 mutex_unlock(&hash_mutex);
1744 return -ENOMEM;
1745 }
1746 spin_lock_init(&i->lock);
1747 i->irq = up->port.irq;
1748 hlist_add_head(&i->node, h);
1749 }
1750 mutex_unlock(&hash_mutex);
1751
1752 spin_lock_irq(&i->lock);
1753
1754 if (i->head) {
1755 list_add(&up->list, i->head);
1756 spin_unlock_irq(&i->lock);
1757
1758 ret = 0;
1759 } else {
1760 INIT_LIST_HEAD(&up->list);
1761 i->head = &up->list;
1762 spin_unlock_irq(&i->lock);
1763 irq_flags |= up->port.irqflags;
1764 ret = request_irq(up->port.irq, serial8250_interrupt,
1765 irq_flags, "serial", i);
1766 if (ret < 0)
1767 serial_do_unlink(i, up);
1768 }
1769
1770 return ret;
1771}
1772
1773static void serial_unlink_irq_chain(struct uart_8250_port *up)
1774{
1775 struct irq_info *i;
1776 struct hlist_node *n;
1777 struct hlist_head *h;
1778
1779 mutex_lock(&hash_mutex);
1780
1781 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1782
1783 hlist_for_each(n, h) {
1784 i = hlist_entry(n, struct irq_info, node);
1785 if (i->irq == up->port.irq)
1786 break;
1787 }
1788
1789 BUG_ON(n == NULL);
1790 BUG_ON(i->head == NULL);
1791
1792 if (list_empty(i->head))
1793 free_irq(up->port.irq, i);
1794
1795 serial_do_unlink(i, up);
1796 mutex_unlock(&hash_mutex);
1797}
1798
1799/*
1800 * This function is used to handle ports that do not have an
1801 * interrupt. This doesn't work very well for 16450's, but gives
1802 * barely passable results for a 16550A. (Although at the expense
1803 * of much CPU overhead).
1804 */
1805static void serial8250_timeout(unsigned long data)
1806{
1807 struct uart_8250_port *up = (struct uart_8250_port *)data;
1808 unsigned int iir;
1809
1810 iir = serial_in(up, UART_IIR);
1811 if (!(iir & UART_IIR_NO_INT))
1812 serial8250_handle_port(up);
1813 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
1814}
1815
1816static void serial8250_backup_timeout(unsigned long data)
1817{
1818 struct uart_8250_port *up = (struct uart_8250_port *)data;
1819 unsigned int iir, ier = 0, lsr;
1820 unsigned long flags;
1821
1822 /*
1823 * Must disable interrupts or else we risk racing with the interrupt
1824 * based handler.
1825 */
1826 if (is_real_interrupt(up->port.irq)) {
1827 ier = serial_in(up, UART_IER);
1828 serial_out(up, UART_IER, 0);
1829 }
1830
1831 iir = serial_in(up, UART_IIR);
1832
1833 /*
1834 * This should be a safe test for anyone who doesn't trust the
1835 * IIR bits on their UART, but it's specifically designed for
1836 * the "Diva" UART used on the management processor on many HP
1837 * ia64 and parisc boxes.
1838 */
1839 spin_lock_irqsave(&up->port.lock, flags);
1840 lsr = serial_in(up, UART_LSR);
1841 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1842 spin_unlock_irqrestore(&up->port.lock, flags);
1843 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
1844 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
1845 (lsr & UART_LSR_THRE)) {
1846 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1847 iir |= UART_IIR_THRI;
1848 }
1849
1850 if (!(iir & UART_IIR_NO_INT))
1851 serial8250_handle_port(up);
1852
1853 if (is_real_interrupt(up->port.irq))
1854 serial_out(up, UART_IER, ier);
1855
1856 /* Standard timer interval plus 0.2s to keep the port running */
1857 mod_timer(&up->timer,
1858 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
1859}
1860
1861static unsigned int serial8250_tx_empty(struct uart_port *port)
1862{
1863 struct uart_8250_port *up =
1864 container_of(port, struct uart_8250_port, port);
1865 unsigned long flags;
1866 unsigned int lsr;
1867
1868 spin_lock_irqsave(&up->port.lock, flags);
1869 lsr = serial_in(up, UART_LSR);
1870 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1871 spin_unlock_irqrestore(&up->port.lock, flags);
1872
1873 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
1874}
1875
1876static unsigned int serial8250_get_mctrl(struct uart_port *port)
1877{
1878 struct uart_8250_port *up =
1879 container_of(port, struct uart_8250_port, port);
1880 unsigned int status;
1881 unsigned int ret;
1882
1883 status = check_modem_status(up);
1884
1885 ret = 0;
1886 if (status & UART_MSR_DCD)
1887 ret |= TIOCM_CAR;
1888 if (status & UART_MSR_RI)
1889 ret |= TIOCM_RNG;
1890 if (status & UART_MSR_DSR)
1891 ret |= TIOCM_DSR;
1892 if (status & UART_MSR_CTS)
1893 ret |= TIOCM_CTS;
1894 return ret;
1895}
1896
1897static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1898{
1899 struct uart_8250_port *up =
1900 container_of(port, struct uart_8250_port, port);
1901 unsigned char mcr = 0;
1902
1903 if (mctrl & TIOCM_RTS)
1904 mcr |= UART_MCR_RTS;
1905 if (mctrl & TIOCM_DTR)
1906 mcr |= UART_MCR_DTR;
1907 if (mctrl & TIOCM_OUT1)
1908 mcr |= UART_MCR_OUT1;
1909 if (mctrl & TIOCM_OUT2)
1910 mcr |= UART_MCR_OUT2;
1911 if (mctrl & TIOCM_LOOP)
1912 mcr |= UART_MCR_LOOP;
1913
1914 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1915
1916 serial_out(up, UART_MCR, mcr);
1917}
1918
1919static void serial8250_break_ctl(struct uart_port *port, int break_state)
1920{
1921 struct uart_8250_port *up =
1922 container_of(port, struct uart_8250_port, port);
1923 unsigned long flags;
1924
1925 spin_lock_irqsave(&up->port.lock, flags);
1926 if (break_state == -1)
1927 up->lcr |= UART_LCR_SBC;
1928 else
1929 up->lcr &= ~UART_LCR_SBC;
1930 serial_out(up, UART_LCR, up->lcr);
1931 spin_unlock_irqrestore(&up->port.lock, flags);
1932}
1933
1934/*
1935 * Wait for transmitter & holding register to empty
1936 */
1937static void wait_for_xmitr(struct uart_8250_port *up, int bits)
1938{
1939 unsigned int status, tmout = 10000;
1940
1941 /* Wait up to 10ms for the character(s) to be sent. */
1942 for (;;) {
1943 status = serial_in(up, UART_LSR);
1944
1945 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
1946
1947 if ((status & bits) == bits)
1948 break;
1949 if (--tmout == 0)
1950 break;
1951 udelay(1);
1952 }
1953
1954 /* Wait up to 1s for flow control if necessary */
1955 if (up->port.flags & UPF_CONS_FLOW) {
1956 unsigned int tmout;
1957 for (tmout = 1000000; tmout; tmout--) {
1958 unsigned int msr = serial_in(up, UART_MSR);
1959 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1960 if (msr & UART_MSR_CTS)
1961 break;
1962 udelay(1);
1963 touch_nmi_watchdog();
1964 }
1965 }
1966}
1967
1968#ifdef CONFIG_CONSOLE_POLL
1969/*
1970 * Console polling routines for writing and reading from the uart while
1971 * in an interrupt or debug context.
1972 */
1973
1974static int serial8250_get_poll_char(struct uart_port *port)
1975{
1976 struct uart_8250_port *up =
1977 container_of(port, struct uart_8250_port, port);
1978 unsigned char lsr = serial_inp(up, UART_LSR);
1979
1980 if (!(lsr & UART_LSR_DR))
1981 return NO_POLL_CHAR;
1982
1983 return serial_inp(up, UART_RX);
1984}
1985
1986
1987static void serial8250_put_poll_char(struct uart_port *port,
1988 unsigned char c)
1989{
1990 unsigned int ier;
1991 struct uart_8250_port *up =
1992 container_of(port, struct uart_8250_port, port);
1993
1994 /*
1995 * First save the IER then disable the interrupts
1996 */
1997 ier = serial_in(up, UART_IER);
1998 if (up->capabilities & UART_CAP_UUE)
1999 serial_out(up, UART_IER, UART_IER_UUE);
2000 else
2001 serial_out(up, UART_IER, 0);
2002
2003 wait_for_xmitr(up, BOTH_EMPTY);
2004 /*
2005 * Send the character out.
2006 * If a LF, also do CR...
2007 */
2008 serial_out(up, UART_TX, c);
2009 if (c == 10) {
2010 wait_for_xmitr(up, BOTH_EMPTY);
2011 serial_out(up, UART_TX, 13);
2012 }
2013
2014 /*
2015 * Finally, wait for transmitter to become empty
2016 * and restore the IER
2017 */
2018 wait_for_xmitr(up, BOTH_EMPTY);
2019 serial_out(up, UART_IER, ier);
2020}
2021
2022#endif /* CONFIG_CONSOLE_POLL */
2023
2024static int serial8250_startup(struct uart_port *port)
2025{
2026 struct uart_8250_port *up =
2027 container_of(port, struct uart_8250_port, port);
2028 unsigned long flags;
2029 unsigned char lsr, iir;
2030 int retval;
2031
2032 up->port.fifosize = uart_config[up->port.type].fifo_size;
2033 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
2034 up->capabilities = uart_config[up->port.type].flags;
2035 up->mcr = 0;
2036
2037 if (up->port.iotype != up->cur_iotype)
2038 set_io_from_upio(port);
2039
2040 if (up->port.type == PORT_16C950) {
2041 /* Wake up and initialize UART */
2042 up->acr = 0;
2043 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
2044 serial_outp(up, UART_EFR, UART_EFR_ECB);
2045 serial_outp(up, UART_IER, 0);
2046 serial_outp(up, UART_LCR, 0);
2047 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2048 serial_outp(up, UART_LCR, 0xBF);
2049 serial_outp(up, UART_EFR, UART_EFR_ECB);
2050 serial_outp(up, UART_LCR, 0);
2051 }
2052
2053#ifdef CONFIG_SERIAL_8250_RSA
2054 /*
2055 * If this is an RSA port, see if we can kick it up to the
2056 * higher speed clock.
2057 */
2058 enable_rsa(up);
2059#endif
2060
2061 /*
2062 * Clear the FIFO buffers and disable them.
2063 * (they will be reenabled in set_termios())
2064 */
2065 serial8250_clear_fifos(up);
2066
2067 /*
2068 * Clear the interrupt registers.
2069 */
2070 (void) serial_inp(up, UART_LSR);
2071 (void) serial_inp(up, UART_RX);
2072 (void) serial_inp(up, UART_IIR);
2073 (void) serial_inp(up, UART_MSR);
2074
2075 /*
2076 * At this point, there's no way the LSR could still be 0xff;
2077 * if it is, then bail out, because there's likely no UART
2078 * here.
2079 */
2080 if (!(up->port.flags & UPF_BUGGY_UART) &&
2081 (serial_inp(up, UART_LSR) == 0xff)) {
2082 printk(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2083 serial_index(&up->port));
2084 return -ENODEV;
2085 }
2086
2087 /*
2088 * For a XR16C850, we need to set the trigger levels
2089 */
2090 if (up->port.type == PORT_16850) {
2091 unsigned char fctr;
2092
2093 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
2094
2095 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2096 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2097 serial_outp(up, UART_TRG, UART_TRG_96);
2098 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2099 serial_outp(up, UART_TRG, UART_TRG_96);
2100
2101 serial_outp(up, UART_LCR, 0);
2102 }
2103
2104 if (is_real_interrupt(up->port.irq)) {
2105 unsigned char iir1;
2106 /*
2107 * Test for UARTs that do not reassert THRE when the
2108 * transmitter is idle and the interrupt has already
2109 * been cleared. Real 16550s should always reassert
2110 * this interrupt whenever the transmitter is idle and
2111 * the interrupt is enabled. Delays are necessary to
2112 * allow register changes to become visible.
2113 */
2114 spin_lock_irqsave(&up->port.lock, flags);
2115 if (up->port.irqflags & IRQF_SHARED)
2116 disable_irq_nosync(up->port.irq);
2117
2118 wait_for_xmitr(up, UART_LSR_THRE);
2119 serial_out_sync(up, UART_IER, UART_IER_THRI);
2120 udelay(1); /* allow THRE to set */
2121 iir1 = serial_in(up, UART_IIR);
2122 serial_out(up, UART_IER, 0);
2123 serial_out_sync(up, UART_IER, UART_IER_THRI);
2124 udelay(1); /* allow a working UART time to re-assert THRE */
2125 iir = serial_in(up, UART_IIR);
2126 serial_out(up, UART_IER, 0);
2127
2128 if (up->port.irqflags & IRQF_SHARED)
2129 enable_irq(up->port.irq);
2130 spin_unlock_irqrestore(&up->port.lock, flags);
2131
2132 /*
2133 * If the interrupt is not reasserted, setup a timer to
2134 * kick the UART on a regular basis.
2135 */
2136 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
2137 up->bugs |= UART_BUG_THRE;
2138 pr_debug("ttyS%d - using backup timer\n",
2139 serial_index(port));
2140 }
2141 }
2142
2143 /*
2144 * The above check will only give an accurate result the first time
2145 * the port is opened so this value needs to be preserved.
2146 */
2147 if (up->bugs & UART_BUG_THRE) {
2148 up->timer.function = serial8250_backup_timeout;
2149 up->timer.data = (unsigned long)up;
2150 mod_timer(&up->timer, jiffies +
2151 uart_poll_timeout(port) + HZ / 5);
2152 }
2153
2154 /*
2155 * If the "interrupt" for this port doesn't correspond with any
2156 * hardware interrupt, we use a timer-based system. The original
2157 * driver used to do this with IRQ0.
2158 */
2159 if (!is_real_interrupt(up->port.irq)) {
2160 up->timer.data = (unsigned long)up;
2161 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
2162 } else {
2163 retval = serial_link_irq_chain(up);
2164 if (retval)
2165 return retval;
2166 }
2167
2168 /*
2169 * Now, initialize the UART
2170 */
2171 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
2172
2173 spin_lock_irqsave(&up->port.lock, flags);
2174 if (up->port.flags & UPF_FOURPORT) {
2175 if (!is_real_interrupt(up->port.irq))
2176 up->port.mctrl |= TIOCM_OUT1;
2177 } else
2178 /*
2179 * Most PC uarts need OUT2 raised to enable interrupts.
2180 */
2181 if (is_real_interrupt(up->port.irq))
2182 up->port.mctrl |= TIOCM_OUT2;
2183
2184 serial8250_set_mctrl(&up->port, up->port.mctrl);
2185
2186 /* Serial over Lan (SoL) hack:
2187 Intel 8257x Gigabit ethernet chips have a
2188 16550 emulation, to be used for Serial Over Lan.
2189 Those chips take a longer time than a normal
2190 serial device to signalize that a transmission
2191 data was queued. Due to that, the above test generally
2192 fails. One solution would be to delay the reading of
2193 iir. However, this is not reliable, since the timeout
2194 is variable. So, let's just don't test if we receive
2195 TX irq. This way, we'll never enable UART_BUG_TXEN.
2196 */
2197 if (skip_txen_test || up->port.flags & UPF_NO_TXEN_TEST)
2198 goto dont_test_tx_en;
2199
2200 /*
2201 * Do a quick test to see if we receive an
2202 * interrupt when we enable the TX irq.
2203 */
2204 serial_outp(up, UART_IER, UART_IER_THRI);
2205 lsr = serial_in(up, UART_LSR);
2206 iir = serial_in(up, UART_IIR);
2207 serial_outp(up, UART_IER, 0);
2208
2209 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2210 if (!(up->bugs & UART_BUG_TXEN)) {
2211 up->bugs |= UART_BUG_TXEN;
2212 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2213 serial_index(port));
2214 }
2215 } else {
2216 up->bugs &= ~UART_BUG_TXEN;
2217 }
2218
2219dont_test_tx_en:
2220 spin_unlock_irqrestore(&up->port.lock, flags);
2221
2222 /*
2223 * Clear the interrupt registers again for luck, and clear the
2224 * saved flags to avoid getting false values from polling
2225 * routines or the previous session.
2226 */
2227 serial_inp(up, UART_LSR);
2228 serial_inp(up, UART_RX);
2229 serial_inp(up, UART_IIR);
2230 serial_inp(up, UART_MSR);
2231 up->lsr_saved_flags = 0;
2232 up->msr_saved_flags = 0;
2233
2234 /*
2235 * Finally, enable interrupts. Note: Modem status interrupts
2236 * are set via set_termios(), which will be occurring imminently
2237 * anyway, so we don't enable them here.
2238 */
2239 up->ier = UART_IER_RLSI | UART_IER_RDI;
2240 serial_outp(up, UART_IER, up->ier);
2241
2242 if (up->port.flags & UPF_FOURPORT) {
2243 unsigned int icp;
2244 /*
2245 * Enable interrupts on the AST Fourport board
2246 */
2247 icp = (up->port.iobase & 0xfe0) | 0x01f;
2248 outb_p(0x80, icp);
2249 (void) inb_p(icp);
2250 }
2251
2252 return 0;
2253}
2254
2255static void serial8250_shutdown(struct uart_port *port)
2256{
2257 struct uart_8250_port *up =
2258 container_of(port, struct uart_8250_port, port);
2259 unsigned long flags;
2260
2261 /*
2262 * Disable interrupts from this port
2263 */
2264 up->ier = 0;
2265 serial_outp(up, UART_IER, 0);
2266
2267 spin_lock_irqsave(&up->port.lock, flags);
2268 if (up->port.flags & UPF_FOURPORT) {
2269 /* reset interrupts on the AST Fourport board */
2270 inb((up->port.iobase & 0xfe0) | 0x1f);
2271 up->port.mctrl |= TIOCM_OUT1;
2272 } else
2273 up->port.mctrl &= ~TIOCM_OUT2;
2274
2275 serial8250_set_mctrl(&up->port, up->port.mctrl);
2276 spin_unlock_irqrestore(&up->port.lock, flags);
2277
2278 /*
2279 * Disable break condition and FIFOs
2280 */
2281 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
2282 serial8250_clear_fifos(up);
2283
2284#ifdef CONFIG_SERIAL_8250_RSA
2285 /*
2286 * Reset the RSA board back to 115kbps compat mode.
2287 */
2288 disable_rsa(up);
2289#endif
2290
2291 /*
2292 * Read data port to reset things, and then unlink from
2293 * the IRQ chain.
2294 */
2295 (void) serial_in(up, UART_RX);
2296
2297 del_timer_sync(&up->timer);
2298 up->timer.function = serial8250_timeout;
2299 if (is_real_interrupt(up->port.irq))
2300 serial_unlink_irq_chain(up);
2301}
2302
2303static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2304{
2305 unsigned int quot;
2306
2307 /*
2308 * Handle magic divisors for baud rates above baud_base on
2309 * SMSC SuperIO chips.
2310 */
2311 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2312 baud == (port->uartclk/4))
2313 quot = 0x8001;
2314 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2315 baud == (port->uartclk/8))
2316 quot = 0x8002;
2317 else
2318 quot = uart_get_divisor(port, baud);
2319
2320 return quot;
2321}
2322
2323void
2324serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2325 struct ktermios *old)
2326{
2327 struct uart_8250_port *up =
2328 container_of(port, struct uart_8250_port, port);
2329 unsigned char cval, fcr = 0;
2330 unsigned long flags;
2331 unsigned int baud, quot;
2332
2333 switch (termios->c_cflag & CSIZE) {
2334 case CS5:
2335 cval = UART_LCR_WLEN5;
2336 break;
2337 case CS6:
2338 cval = UART_LCR_WLEN6;
2339 break;
2340 case CS7:
2341 cval = UART_LCR_WLEN7;
2342 break;
2343 default:
2344 case CS8:
2345 cval = UART_LCR_WLEN8;
2346 break;
2347 }
2348
2349 if (termios->c_cflag & CSTOPB)
2350 cval |= UART_LCR_STOP;
2351 if (termios->c_cflag & PARENB)
2352 cval |= UART_LCR_PARITY;
2353 if (!(termios->c_cflag & PARODD))
2354 cval |= UART_LCR_EPAR;
2355#ifdef CMSPAR
2356 if (termios->c_cflag & CMSPAR)
2357 cval |= UART_LCR_SPAR;
2358#endif
2359
2360 /*
2361 * Ask the core to calculate the divisor for us.
2362 */
2363 baud = uart_get_baud_rate(port, termios, old,
2364 port->uartclk / 16 / 0xffff,
2365 port->uartclk / 16);
2366 quot = serial8250_get_divisor(port, baud);
2367
2368 /*
2369 * Oxford Semi 952 rev B workaround
2370 */
2371 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2372 quot++;
2373
2374 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
2375 if (baud < 2400)
2376 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2377 else
2378 fcr = uart_config[up->port.type].fcr;
2379 }
2380
2381 /*
2382 * MCR-based auto flow control. When AFE is enabled, RTS will be
2383 * deasserted when the receive FIFO contains more characters than
2384 * the trigger, or the MCR RTS bit is cleared. In the case where
2385 * the remote UART is not using CTS auto flow control, we must
2386 * have sufficient FIFO entries for the latency of the remote
2387 * UART to respond. IOW, at least 32 bytes of FIFO.
2388 */
2389 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
2390 up->mcr &= ~UART_MCR_AFE;
2391 if (termios->c_cflag & CRTSCTS)
2392 up->mcr |= UART_MCR_AFE;
2393 }
2394
2395 /*
2396 * Ok, we're now changing the port state. Do it with
2397 * interrupts disabled.
2398 */
2399 spin_lock_irqsave(&up->port.lock, flags);
2400
2401 /*
2402 * Update the per-port timeout.
2403 */
2404 uart_update_timeout(port, termios->c_cflag, baud);
2405
2406 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2407 if (termios->c_iflag & INPCK)
2408 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2409 if (termios->c_iflag & (BRKINT | PARMRK))
2410 up->port.read_status_mask |= UART_LSR_BI;
2411
2412 /*
2413 * Characteres to ignore
2414 */
2415 up->port.ignore_status_mask = 0;
2416 if (termios->c_iflag & IGNPAR)
2417 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2418 if (termios->c_iflag & IGNBRK) {
2419 up->port.ignore_status_mask |= UART_LSR_BI;
2420 /*
2421 * If we're ignoring parity and break indicators,
2422 * ignore overruns too (for real raw support).
2423 */
2424 if (termios->c_iflag & IGNPAR)
2425 up->port.ignore_status_mask |= UART_LSR_OE;
2426 }
2427
2428 /*
2429 * ignore all characters if CREAD is not set
2430 */
2431 if ((termios->c_cflag & CREAD) == 0)
2432 up->port.ignore_status_mask |= UART_LSR_DR;
2433
2434 /*
2435 * CTS flow control flag and modem status interrupts
2436 */
2437 up->ier &= ~UART_IER_MSI;
2438 if (!(up->bugs & UART_BUG_NOMSR) &&
2439 UART_ENABLE_MS(&up->port, termios->c_cflag))
2440 up->ier |= UART_IER_MSI;
2441 if (up->capabilities & UART_CAP_UUE)
2442 up->ier |= UART_IER_UUE;
2443 if (up->capabilities & UART_CAP_RTOIE)
2444 up->ier |= UART_IER_RTOIE;
2445
2446 serial_out(up, UART_IER, up->ier);
2447
2448 if (up->capabilities & UART_CAP_EFR) {
2449 unsigned char efr = 0;
2450 /*
2451 * TI16C752/Startech hardware flow control. FIXME:
2452 * - TI16C752 requires control thresholds to be set.
2453 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2454 */
2455 if (termios->c_cflag & CRTSCTS)
2456 efr |= UART_EFR_CTS;
2457
2458 serial_outp(up, UART_LCR, UART_LCR_CONF_MODE_B);
2459 serial_outp(up, UART_EFR, efr);
2460 }
2461
2462#ifdef CONFIG_ARCH_OMAP
2463 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2464 if (cpu_is_omap1510() && is_omap_port(up)) {
2465 if (baud == 115200) {
2466 quot = 1;
2467 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2468 } else
2469 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2470 }
2471#endif
2472
2473 if (up->capabilities & UART_NATSEMI) {
2474 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2475 serial_outp(up, UART_LCR, 0xe0);
2476 } else {
2477 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2478 }
2479
2480 serial_dl_write(up, quot);
2481
2482 /*
2483 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2484 * is written without DLAB set, this mode will be disabled.
2485 */
2486 if (up->port.type == PORT_16750)
2487 serial_outp(up, UART_FCR, fcr);
2488
2489 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2490 up->lcr = cval; /* Save LCR */
2491 if (up->port.type != PORT_16750) {
2492 if (fcr & UART_FCR_ENABLE_FIFO) {
2493 /* emulated UARTs (Lucent Venus 167x) need two steps */
2494 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2495 }
2496 serial_outp(up, UART_FCR, fcr); /* set fcr */
2497 }
2498 serial8250_set_mctrl(&up->port, up->port.mctrl);
2499 spin_unlock_irqrestore(&up->port.lock, flags);
2500 /* Don't rewrite B0 */
2501 if (tty_termios_baud_rate(termios))
2502 tty_termios_encode_baud_rate(termios, baud, baud);
2503}
2504EXPORT_SYMBOL(serial8250_do_set_termios);
2505
2506static void
2507serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2508 struct ktermios *old)
2509{
2510 if (port->set_termios)
2511 port->set_termios(port, termios, old);
2512 else
2513 serial8250_do_set_termios(port, termios, old);
2514}
2515
2516static void
2517serial8250_set_ldisc(struct uart_port *port, int new)
2518{
2519 if (new == N_PPS) {
2520 port->flags |= UPF_HARDPPS_CD;
2521 serial8250_enable_ms(port);
2522 } else
2523 port->flags &= ~UPF_HARDPPS_CD;
2524}
2525
2526
2527void serial8250_do_pm(struct uart_port *port, unsigned int state,
2528 unsigned int oldstate)
2529{
2530 struct uart_8250_port *p =
2531 container_of(port, struct uart_8250_port, port);
2532
2533 serial8250_set_sleep(p, state != 0);
2534}
2535EXPORT_SYMBOL(serial8250_do_pm);
2536
2537static void
2538serial8250_pm(struct uart_port *port, unsigned int state,
2539 unsigned int oldstate)
2540{
2541 if (port->pm)
2542 port->pm(port, state, oldstate);
2543 else
2544 serial8250_do_pm(port, state, oldstate);
2545}
2546
2547static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2548{
2549 if (pt->port.iotype == UPIO_AU)
2550 return 0x1000;
2551#ifdef CONFIG_ARCH_OMAP
2552 if (is_omap_port(pt))
2553 return 0x16 << pt->port.regshift;
2554#endif
2555 return 8 << pt->port.regshift;
2556}
2557
2558/*
2559 * Resource handling.
2560 */
2561static int serial8250_request_std_resource(struct uart_8250_port *up)
2562{
2563 unsigned int size = serial8250_port_size(up);
2564 int ret = 0;
2565
2566 switch (up->port.iotype) {
2567 case UPIO_AU:
2568 case UPIO_TSI:
2569 case UPIO_MEM32:
2570 case UPIO_MEM:
2571 case UPIO_DWAPB:
2572 case UPIO_DWAPB32:
2573 if (!up->port.mapbase)
2574 break;
2575
2576 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2577 ret = -EBUSY;
2578 break;
2579 }
2580
2581 if (up->port.flags & UPF_IOREMAP) {
2582 up->port.membase = ioremap_nocache(up->port.mapbase,
2583 size);
2584 if (!up->port.membase) {
2585 release_mem_region(up->port.mapbase, size);
2586 ret = -ENOMEM;
2587 }
2588 }
2589 break;
2590
2591 case UPIO_HUB6:
2592 case UPIO_PORT:
2593 if (!request_region(up->port.iobase, size, "serial"))
2594 ret = -EBUSY;
2595 break;
2596 }
2597 return ret;
2598}
2599
2600static void serial8250_release_std_resource(struct uart_8250_port *up)
2601{
2602 unsigned int size = serial8250_port_size(up);
2603
2604 switch (up->port.iotype) {
2605 case UPIO_AU:
2606 case UPIO_TSI:
2607 case UPIO_MEM32:
2608 case UPIO_MEM:
2609 case UPIO_DWAPB:
2610 case UPIO_DWAPB32:
2611 if (!up->port.mapbase)
2612 break;
2613
2614 if (up->port.flags & UPF_IOREMAP) {
2615 iounmap(up->port.membase);
2616 up->port.membase = NULL;
2617 }
2618
2619 release_mem_region(up->port.mapbase, size);
2620 break;
2621
2622 case UPIO_HUB6:
2623 case UPIO_PORT:
2624 release_region(up->port.iobase, size);
2625 break;
2626 }
2627}
2628
2629static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2630{
2631 unsigned long start = UART_RSA_BASE << up->port.regshift;
2632 unsigned int size = 8 << up->port.regshift;
2633 int ret = -EINVAL;
2634
2635 switch (up->port.iotype) {
2636 case UPIO_HUB6:
2637 case UPIO_PORT:
2638 start += up->port.iobase;
2639 if (request_region(start, size, "serial-rsa"))
2640 ret = 0;
2641 else
2642 ret = -EBUSY;
2643 break;
2644 }
2645
2646 return ret;
2647}
2648
2649static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2650{
2651 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2652 unsigned int size = 8 << up->port.regshift;
2653
2654 switch (up->port.iotype) {
2655 case UPIO_HUB6:
2656 case UPIO_PORT:
2657 release_region(up->port.iobase + offset, size);
2658 break;
2659 }
2660}
2661
2662static void serial8250_release_port(struct uart_port *port)
2663{
2664 struct uart_8250_port *up =
2665 container_of(port, struct uart_8250_port, port);
2666
2667 serial8250_release_std_resource(up);
2668 if (up->port.type == PORT_RSA)
2669 serial8250_release_rsa_resource(up);
2670}
2671
2672static int serial8250_request_port(struct uart_port *port)
2673{
2674 struct uart_8250_port *up =
2675 container_of(port, struct uart_8250_port, port);
2676 int ret = 0;
2677
2678 ret = serial8250_request_std_resource(up);
2679 if (ret == 0 && up->port.type == PORT_RSA) {
2680 ret = serial8250_request_rsa_resource(up);
2681 if (ret < 0)
2682 serial8250_release_std_resource(up);
2683 }
2684
2685 return ret;
2686}
2687
2688static void serial8250_config_port(struct uart_port *port, int flags)
2689{
2690 struct uart_8250_port *up =
2691 container_of(port, struct uart_8250_port, port);
2692 int probeflags = PROBE_ANY;
2693 int ret;
2694
2695 /*
2696 * Find the region that we can probe for. This in turn
2697 * tells us whether we can probe for the type of port.
2698 */
2699 ret = serial8250_request_std_resource(up);
2700 if (ret < 0)
2701 return;
2702
2703 ret = serial8250_request_rsa_resource(up);
2704 if (ret < 0)
2705 probeflags &= ~PROBE_RSA;
2706
2707 if (up->port.iotype != up->cur_iotype)
2708 set_io_from_upio(port);
2709
2710 if (flags & UART_CONFIG_TYPE)
2711 autoconfig(up, probeflags);
2712
2713 /* if access method is AU, it is a 16550 with a quirk */
2714 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
2715 up->bugs |= UART_BUG_NOMSR;
2716
2717 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2718 autoconfig_irq(up);
2719
2720 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2721 serial8250_release_rsa_resource(up);
2722 if (up->port.type == PORT_UNKNOWN)
2723 serial8250_release_std_resource(up);
2724}
2725
2726static int
2727serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2728{
2729 if (ser->irq >= nr_irqs || ser->irq < 0 ||
2730 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2731 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2732 ser->type == PORT_STARTECH)
2733 return -EINVAL;
2734 return 0;
2735}
2736
2737static const char *
2738serial8250_type(struct uart_port *port)
2739{
2740 int type = port->type;
2741
2742 if (type >= ARRAY_SIZE(uart_config))
2743 type = 0;
2744 return uart_config[type].name;
2745}
2746
2747static struct uart_ops serial8250_pops = {
2748 .tx_empty = serial8250_tx_empty,
2749 .set_mctrl = serial8250_set_mctrl,
2750 .get_mctrl = serial8250_get_mctrl,
2751 .stop_tx = serial8250_stop_tx,
2752 .start_tx = serial8250_start_tx,
2753 .stop_rx = serial8250_stop_rx,
2754 .enable_ms = serial8250_enable_ms,
2755 .break_ctl = serial8250_break_ctl,
2756 .startup = serial8250_startup,
2757 .shutdown = serial8250_shutdown,
2758 .set_termios = serial8250_set_termios,
2759 .set_ldisc = serial8250_set_ldisc,
2760 .pm = serial8250_pm,
2761 .type = serial8250_type,
2762 .release_port = serial8250_release_port,
2763 .request_port = serial8250_request_port,
2764 .config_port = serial8250_config_port,
2765 .verify_port = serial8250_verify_port,
2766#ifdef CONFIG_CONSOLE_POLL
2767 .poll_get_char = serial8250_get_poll_char,
2768 .poll_put_char = serial8250_put_poll_char,
2769#endif
2770};
2771
2772static struct uart_8250_port serial8250_ports[UART_NR];
2773
2774static void (*serial8250_isa_config)(int port, struct uart_port *up,
2775 unsigned short *capabilities);
2776
2777void serial8250_set_isa_configurator(
2778 void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
2779{
2780 serial8250_isa_config = v;
2781}
2782EXPORT_SYMBOL(serial8250_set_isa_configurator);
2783
2784static void __init serial8250_isa_init_ports(void)
2785{
2786 struct uart_8250_port *up;
2787 static int first = 1;
2788 int i, irqflag = 0;
2789
2790 if (!first)
2791 return;
2792 first = 0;
2793
2794 for (i = 0; i < nr_uarts; i++) {
2795 struct uart_8250_port *up = &serial8250_ports[i];
2796
2797 up->port.line = i;
2798 spin_lock_init(&up->port.lock);
2799
2800 init_timer(&up->timer);
2801 up->timer.function = serial8250_timeout;
2802
2803 /*
2804 * ALPHA_KLUDGE_MCR needs to be killed.
2805 */
2806 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2807 up->mcr_force = ALPHA_KLUDGE_MCR;
2808
2809 up->port.ops = &serial8250_pops;
2810 }
2811
2812 if (share_irqs)
2813 irqflag = IRQF_SHARED;
2814
2815 for (i = 0, up = serial8250_ports;
2816 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
2817 i++, up++) {
2818 up->port.iobase = old_serial_port[i].port;
2819 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
2820 up->port.irqflags = old_serial_port[i].irqflags;
2821 up->port.uartclk = old_serial_port[i].baud_base * 16;
2822 up->port.flags = old_serial_port[i].flags;
2823 up->port.hub6 = old_serial_port[i].hub6;
2824 up->port.membase = old_serial_port[i].iomem_base;
2825 up->port.iotype = old_serial_port[i].io_type;
2826 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2827 set_io_from_upio(&up->port);
2828 up->port.irqflags |= irqflag;
2829 if (serial8250_isa_config != NULL)
2830 serial8250_isa_config(i, &up->port, &up->capabilities);
2831
2832 }
2833}
2834
2835static void
2836serial8250_init_fixed_type_port(struct uart_8250_port *up, unsigned int type)
2837{
2838 up->port.type = type;
2839 up->port.fifosize = uart_config[type].fifo_size;
2840 up->capabilities = uart_config[type].flags;
2841 up->tx_loadsz = uart_config[type].tx_loadsz;
2842}
2843
2844static void __init
2845serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2846{
2847 int i;
2848
2849 for (i = 0; i < nr_uarts; i++) {
2850 struct uart_8250_port *up = &serial8250_ports[i];
2851 up->cur_iotype = 0xFF;
2852 }
2853
2854 serial8250_isa_init_ports();
2855
2856 for (i = 0; i < nr_uarts; i++) {
2857 struct uart_8250_port *up = &serial8250_ports[i];
2858
2859 up->port.dev = dev;
2860
2861 if (up->port.flags & UPF_FIXED_TYPE)
2862 serial8250_init_fixed_type_port(up, up->port.type);
2863
2864 uart_add_one_port(drv, &up->port);
2865 }
2866}
2867
2868#ifdef CONFIG_SERIAL_8250_CONSOLE
2869
2870static void serial8250_console_putchar(struct uart_port *port, int ch)
2871{
2872 struct uart_8250_port *up =
2873 container_of(port, struct uart_8250_port, port);
2874
2875 wait_for_xmitr(up, UART_LSR_THRE);
2876 serial_out(up, UART_TX, ch);
2877}
2878
2879/*
2880 * Print a string to the serial port trying not to disturb
2881 * any possible real use of the port...
2882 *
2883 * The console_lock must be held when we get here.
2884 */
2885static void
2886serial8250_console_write(struct console *co, const char *s, unsigned int count)
2887{
2888 struct uart_8250_port *up = &serial8250_ports[co->index];
2889 unsigned long flags;
2890 unsigned int ier;
2891 int locked = 1;
2892
2893 touch_nmi_watchdog();
2894
2895 local_irq_save(flags);
2896 if (up->port.sysrq) {
2897 /* serial8250_handle_port() already took the lock */
2898 locked = 0;
2899 } else if (oops_in_progress) {
2900 locked = spin_trylock(&up->port.lock);
2901 } else
2902 spin_lock(&up->port.lock);
2903
2904 /*
2905 * First save the IER then disable the interrupts
2906 */
2907 ier = serial_in(up, UART_IER);
2908
2909 if (up->capabilities & UART_CAP_UUE)
2910 serial_out(up, UART_IER, UART_IER_UUE);
2911 else
2912 serial_out(up, UART_IER, 0);
2913
2914 uart_console_write(&up->port, s, count, serial8250_console_putchar);
2915
2916 /*
2917 * Finally, wait for transmitter to become empty
2918 * and restore the IER
2919 */
2920 wait_for_xmitr(up, BOTH_EMPTY);
2921 serial_out(up, UART_IER, ier);
2922
2923 /*
2924 * The receive handling will happen properly because the
2925 * receive ready bit will still be set; it is not cleared
2926 * on read. However, modem control will not, we must
2927 * call it if we have saved something in the saved flags
2928 * while processing with interrupts off.
2929 */
2930 if (up->msr_saved_flags)
2931 check_modem_status(up);
2932
2933 if (locked)
2934 spin_unlock(&up->port.lock);
2935 local_irq_restore(flags);
2936}
2937
2938static int __init serial8250_console_setup(struct console *co, char *options)
2939{
2940 struct uart_port *port;
2941 int baud = 9600;
2942 int bits = 8;
2943 int parity = 'n';
2944 int flow = 'n';
2945
2946 /*
2947 * Check whether an invalid uart number has been specified, and
2948 * if so, search for the first available port that does have
2949 * console support.
2950 */
2951 if (co->index >= nr_uarts)
2952 co->index = 0;
2953 port = &serial8250_ports[co->index].port;
2954 if (!port->iobase && !port->membase)
2955 return -ENODEV;
2956
2957 if (options)
2958 uart_parse_options(options, &baud, &parity, &bits, &flow);
2959
2960 return uart_set_options(port, co, baud, parity, bits, flow);
2961}
2962
2963static int serial8250_console_early_setup(void)
2964{
2965 return serial8250_find_port_for_earlycon();
2966}
2967
2968static struct console serial8250_console = {
2969 .name = "ttyS",
2970 .write = serial8250_console_write,
2971 .device = uart_console_device,
2972 .setup = serial8250_console_setup,
2973 .early_setup = serial8250_console_early_setup,
2974 .flags = CON_PRINTBUFFER | CON_ANYTIME,
2975 .index = -1,
2976 .data = &serial8250_reg,
2977};
2978
2979static int __init serial8250_console_init(void)
2980{
2981 if (nr_uarts > UART_NR)
2982 nr_uarts = UART_NR;
2983
2984 serial8250_isa_init_ports();
2985 register_console(&serial8250_console);
2986 return 0;
2987}
2988console_initcall(serial8250_console_init);
2989
2990int serial8250_find_port(struct uart_port *p)
2991{
2992 int line;
2993 struct uart_port *port;
2994
2995 for (line = 0; line < nr_uarts; line++) {
2996 port = &serial8250_ports[line].port;
2997 if (uart_match_port(p, port))
2998 return line;
2999 }
3000 return -ENODEV;
3001}
3002
3003#define SERIAL8250_CONSOLE &serial8250_console
3004#else
3005#define SERIAL8250_CONSOLE NULL
3006#endif
3007
3008static struct uart_driver serial8250_reg = {
3009 .owner = THIS_MODULE,
3010 .driver_name = "serial",
3011 .dev_name = "ttyS",
3012 .major = TTY_MAJOR,
3013 .minor = 64,
3014 .cons = SERIAL8250_CONSOLE,
3015};
3016
3017/*
3018 * early_serial_setup - early registration for 8250 ports
3019 *
3020 * Setup an 8250 port structure prior to console initialisation. Use
3021 * after console initialisation will cause undefined behaviour.
3022 */
3023int __init early_serial_setup(struct uart_port *port)
3024{
3025 struct uart_port *p;
3026
3027 if (port->line >= ARRAY_SIZE(serial8250_ports))
3028 return -ENODEV;
3029
3030 serial8250_isa_init_ports();
3031 p = &serial8250_ports[port->line].port;
3032 p->iobase = port->iobase;
3033 p->membase = port->membase;
3034 p->irq = port->irq;
3035 p->irqflags = port->irqflags;
3036 p->uartclk = port->uartclk;
3037 p->fifosize = port->fifosize;
3038 p->regshift = port->regshift;
3039 p->iotype = port->iotype;
3040 p->flags = port->flags;
3041 p->mapbase = port->mapbase;
3042 p->private_data = port->private_data;
3043 p->type = port->type;
3044 p->line = port->line;
3045
3046 set_io_from_upio(p);
3047 if (port->serial_in)
3048 p->serial_in = port->serial_in;
3049 if (port->serial_out)
3050 p->serial_out = port->serial_out;
3051
3052 return 0;
3053}
3054
3055/**
3056 * serial8250_suspend_port - suspend one serial port
3057 * @line: serial line number
3058 *
3059 * Suspend one serial port.
3060 */
3061void serial8250_suspend_port(int line)
3062{
3063 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
3064}
3065
3066/**
3067 * serial8250_resume_port - resume one serial port
3068 * @line: serial line number
3069 *
3070 * Resume one serial port.
3071 */
3072void serial8250_resume_port(int line)
3073{
3074 struct uart_8250_port *up = &serial8250_ports[line];
3075
3076 if (up->capabilities & UART_NATSEMI) {
3077 /* Ensure it's still in high speed mode */
3078 serial_outp(up, UART_LCR, 0xE0);
3079
3080 ns16550a_goto_highspeed(up);
3081
3082 serial_outp(up, UART_LCR, 0);
3083 up->port.uartclk = 921600*16;
3084 }
3085 uart_resume_port(&serial8250_reg, &up->port);
3086}
3087
3088/*
3089 * Register a set of serial devices attached to a platform device. The
3090 * list is terminated with a zero flags entry, which means we expect
3091 * all entries to have at least UPF_BOOT_AUTOCONF set.
3092 */
3093static int __devinit serial8250_probe(struct platform_device *dev)
3094{
3095 struct plat_serial8250_port *p = dev->dev.platform_data;
3096 struct uart_port port;
3097 int ret, i, irqflag = 0;
3098
3099 memset(&port, 0, sizeof(struct uart_port));
3100
3101 if (share_irqs)
3102 irqflag = IRQF_SHARED;
3103
3104 for (i = 0; p && p->flags != 0; p++, i++) {
3105 port.iobase = p->iobase;
3106 port.membase = p->membase;
3107 port.irq = p->irq;
3108 port.irqflags = p->irqflags;
3109 port.uartclk = p->uartclk;
3110 port.regshift = p->regshift;
3111 port.iotype = p->iotype;
3112 port.flags = p->flags;
3113 port.mapbase = p->mapbase;
3114 port.hub6 = p->hub6;
3115 port.private_data = p->private_data;
3116 port.type = p->type;
3117 port.serial_in = p->serial_in;
3118 port.serial_out = p->serial_out;
3119 port.set_termios = p->set_termios;
3120 port.pm = p->pm;
3121 port.dev = &dev->dev;
3122 port.irqflags |= irqflag;
3123 ret = serial8250_register_port(&port);
3124 if (ret < 0) {
3125 dev_err(&dev->dev, "unable to register port at index %d "
3126 "(IO%lx MEM%llx IRQ%d): %d\n", i,
3127 p->iobase, (unsigned long long)p->mapbase,
3128 p->irq, ret);
3129 }
3130 }
3131 return 0;
3132}
3133
3134/*
3135 * Remove serial ports registered against a platform device.
3136 */
3137static int __devexit serial8250_remove(struct platform_device *dev)
3138{
3139 int i;
3140
3141 for (i = 0; i < nr_uarts; i++) {
3142 struct uart_8250_port *up = &serial8250_ports[i];
3143
3144 if (up->port.dev == &dev->dev)
3145 serial8250_unregister_port(i);
3146 }
3147 return 0;
3148}
3149
3150static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
3151{
3152 int i;
3153
3154 for (i = 0; i < UART_NR; i++) {
3155 struct uart_8250_port *up = &serial8250_ports[i];
3156
3157 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3158 uart_suspend_port(&serial8250_reg, &up->port);
3159 }
3160
3161 return 0;
3162}
3163
3164static int serial8250_resume(struct platform_device *dev)
3165{
3166 int i;
3167
3168 for (i = 0; i < UART_NR; i++) {
3169 struct uart_8250_port *up = &serial8250_ports[i];
3170
3171 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3172 serial8250_resume_port(i);
3173 }
3174
3175 return 0;
3176}
3177
3178static struct platform_driver serial8250_isa_driver = {
3179 .probe = serial8250_probe,
3180 .remove = __devexit_p(serial8250_remove),
3181 .suspend = serial8250_suspend,
3182 .resume = serial8250_resume,
3183 .driver = {
3184 .name = "serial8250",
3185 .owner = THIS_MODULE,
3186 },
3187};
3188
3189/*
3190 * This "device" covers _all_ ISA 8250-compatible serial devices listed
3191 * in the table in include/asm/serial.h
3192 */
3193static struct platform_device *serial8250_isa_devs;
3194
3195/*
3196 * serial8250_register_port and serial8250_unregister_port allows for
3197 * 16x50 serial ports to be configured at run-time, to support PCMCIA
3198 * modems and PCI multiport cards.
3199 */
3200static DEFINE_MUTEX(serial_mutex);
3201
3202static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
3203{
3204 int i;
3205
3206 /*
3207 * First, find a port entry which matches.
3208 */
3209 for (i = 0; i < nr_uarts; i++)
3210 if (uart_match_port(&serial8250_ports[i].port, port))
3211 return &serial8250_ports[i];
3212
3213 /*
3214 * We didn't find a matching entry, so look for the first
3215 * free entry. We look for one which hasn't been previously
3216 * used (indicated by zero iobase).
3217 */
3218 for (i = 0; i < nr_uarts; i++)
3219 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
3220 serial8250_ports[i].port.iobase == 0)
3221 return &serial8250_ports[i];
3222
3223 /*
3224 * That also failed. Last resort is to find any entry which
3225 * doesn't have a real port associated with it.
3226 */
3227 for (i = 0; i < nr_uarts; i++)
3228 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
3229 return &serial8250_ports[i];
3230
3231 return NULL;
3232}
3233
3234/**
3235 * serial8250_register_port - register a serial port
3236 * @port: serial port template
3237 *
3238 * Configure the serial port specified by the request. If the
3239 * port exists and is in use, it is hung up and unregistered
3240 * first.
3241 *
3242 * The port is then probed and if necessary the IRQ is autodetected
3243 * If this fails an error is returned.
3244 *
3245 * On success the port is ready to use and the line number is returned.
3246 */
3247int serial8250_register_port(struct uart_port *port)
3248{
3249 struct uart_8250_port *uart;
3250 int ret = -ENOSPC;
3251
3252 if (port->uartclk == 0)
3253 return -EINVAL;
3254
3255 mutex_lock(&serial_mutex);
3256
3257 uart = serial8250_find_match_or_unused(port);
3258 if (uart) {
3259 uart_remove_one_port(&serial8250_reg, &uart->port);
3260
3261 uart->port.iobase = port->iobase;
3262 uart->port.membase = port->membase;
3263 uart->port.irq = port->irq;
3264 uart->port.irqflags = port->irqflags;
3265 uart->port.uartclk = port->uartclk;
3266 uart->port.fifosize = port->fifosize;
3267 uart->port.regshift = port->regshift;
3268 uart->port.iotype = port->iotype;
3269 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
3270 uart->port.mapbase = port->mapbase;
3271 uart->port.private_data = port->private_data;
3272 if (port->dev)
3273 uart->port.dev = port->dev;
3274
3275 if (port->flags & UPF_FIXED_TYPE)
3276 serial8250_init_fixed_type_port(uart, port->type);
3277
3278 set_io_from_upio(&uart->port);
3279 /* Possibly override default I/O functions. */
3280 if (port->serial_in)
3281 uart->port.serial_in = port->serial_in;
3282 if (port->serial_out)
3283 uart->port.serial_out = port->serial_out;
3284 /* Possibly override set_termios call */
3285 if (port->set_termios)
3286 uart->port.set_termios = port->set_termios;
3287 if (port->pm)
3288 uart->port.pm = port->pm;
3289
3290 if (serial8250_isa_config != NULL)
3291 serial8250_isa_config(0, &uart->port,
3292 &uart->capabilities);
3293
3294 ret = uart_add_one_port(&serial8250_reg, &uart->port);
3295 if (ret == 0)
3296 ret = uart->port.line;
3297 }
3298 mutex_unlock(&serial_mutex);
3299
3300 return ret;
3301}
3302EXPORT_SYMBOL(serial8250_register_port);
3303
3304/**
3305 * serial8250_unregister_port - remove a 16x50 serial port at runtime
3306 * @line: serial line number
3307 *
3308 * Remove one serial port. This may not be called from interrupt
3309 * context. We hand the port back to the our control.
3310 */
3311void serial8250_unregister_port(int line)
3312{
3313 struct uart_8250_port *uart = &serial8250_ports[line];
3314
3315 mutex_lock(&serial_mutex);
3316 uart_remove_one_port(&serial8250_reg, &uart->port);
3317 if (serial8250_isa_devs) {
3318 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3319 uart->port.type = PORT_UNKNOWN;
3320 uart->port.dev = &serial8250_isa_devs->dev;
3321 uart->capabilities = uart_config[uart->port.type].flags;
3322 uart_add_one_port(&serial8250_reg, &uart->port);
3323 } else {
3324 uart->port.dev = NULL;
3325 }
3326 mutex_unlock(&serial_mutex);
3327}
3328EXPORT_SYMBOL(serial8250_unregister_port);
3329
3330static int __init serial8250_init(void)
3331{
3332 int ret;
3333
3334 if (nr_uarts > UART_NR)
3335 nr_uarts = UART_NR;
3336
3337 printk(KERN_INFO "Serial: 8250/16550 driver, "
3338 "%d ports, IRQ sharing %sabled\n", nr_uarts,
3339 share_irqs ? "en" : "dis");
3340
3341#ifdef CONFIG_SPARC
3342 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3343#else
3344 serial8250_reg.nr = UART_NR;
3345 ret = uart_register_driver(&serial8250_reg);
3346#endif
3347 if (ret)
3348 goto out;
3349
3350 serial8250_isa_devs = platform_device_alloc("serial8250",
3351 PLAT8250_DEV_LEGACY);
3352 if (!serial8250_isa_devs) {
3353 ret = -ENOMEM;
3354 goto unreg_uart_drv;
3355 }
3356
3357 ret = platform_device_add(serial8250_isa_devs);
3358 if (ret)
3359 goto put_dev;
3360
3361 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3362
3363 ret = platform_driver_register(&serial8250_isa_driver);
3364 if (ret == 0)
3365 goto out;
3366
3367 platform_device_del(serial8250_isa_devs);
3368put_dev:
3369 platform_device_put(serial8250_isa_devs);
3370unreg_uart_drv:
3371#ifdef CONFIG_SPARC
3372 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3373#else
3374 uart_unregister_driver(&serial8250_reg);
3375#endif
3376out:
3377 return ret;
3378}
3379
3380static void __exit serial8250_exit(void)
3381{
3382 struct platform_device *isa_dev = serial8250_isa_devs;
3383
3384 /*
3385 * This tells serial8250_unregister_port() not to re-register
3386 * the ports (thereby making serial8250_isa_driver permanently
3387 * in use.)
3388 */
3389 serial8250_isa_devs = NULL;
3390
3391 platform_driver_unregister(&serial8250_isa_driver);
3392 platform_device_unregister(isa_dev);
3393
3394#ifdef CONFIG_SPARC
3395 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3396#else
3397 uart_unregister_driver(&serial8250_reg);
3398#endif
3399}
3400
3401module_init(serial8250_init);
3402module_exit(serial8250_exit);
3403
3404EXPORT_SYMBOL(serial8250_suspend_port);
3405EXPORT_SYMBOL(serial8250_resume_port);
3406
3407MODULE_LICENSE("GPL");
3408MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
3409
3410module_param(share_irqs, uint, 0644);
3411MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3412 " (unsafe)");
3413
3414module_param(nr_uarts, uint, 0644);
3415MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3416
3417module_param(skip_txen_test, uint, 0644);
3418MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
3419
3420#ifdef CONFIG_SERIAL_8250_RSA
3421module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3422MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3423#endif
3424MODULE_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..6edf4a6a22d4
--- /dev/null
+++ b/drivers/tty/serial/8250.h
@@ -0,0 +1,79 @@
1/*
2 * Driver for 8250/16550-type serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/serial_8250.h>
15
16struct old_serial_port {
17 unsigned int uart;
18 unsigned int baud_base;
19 unsigned int port;
20 unsigned int irq;
21 unsigned int flags;
22 unsigned char hub6;
23 unsigned char io_type;
24 unsigned char *iomem_base;
25 unsigned short iomem_reg_shift;
26 unsigned long irqflags;
27};
28
29/*
30 * This replaces serial_uart_config in include/linux/serial.h
31 */
32struct serial8250_config {
33 const char *name;
34 unsigned short fifo_size;
35 unsigned short tx_loadsz;
36 unsigned char fcr;
37 unsigned int flags;
38};
39
40#define UART_CAP_FIFO (1 << 8) /* UART has FIFO */
41#define UART_CAP_EFR (1 << 9) /* UART has EFR */
42#define UART_CAP_SLEEP (1 << 10) /* UART has IER sleep */
43#define UART_CAP_AFE (1 << 11) /* MCR-based hw flow control */
44#define UART_CAP_UUE (1 << 12) /* UART needs IER bit 6 set (Xscale) */
45#define UART_CAP_RTOIE (1 << 13) /* UART needs IER bit 4 set (Xscale, Tegra) */
46
47#define UART_BUG_QUOT (1 << 0) /* UART has buggy quot LSB */
48#define UART_BUG_TXEN (1 << 1) /* UART has buggy TX IIR status */
49#define UART_BUG_NOMSR (1 << 2) /* UART has buggy MSR status bits (Au1x00) */
50#define UART_BUG_THRE (1 << 3) /* UART has buggy THRE reassertion */
51
52#define PROBE_RSA (1 << 0)
53#define PROBE_ANY (~0)
54
55#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
56
57#ifdef CONFIG_SERIAL_8250_SHARE_IRQ
58#define SERIAL8250_SHARE_IRQS 1
59#else
60#define SERIAL8250_SHARE_IRQS 0
61#endif
62
63#if defined(__alpha__) && !defined(CONFIG_PCI)
64/*
65 * Digital did something really horribly wrong with the OUT1 and OUT2
66 * lines on at least some ALPHA's. The failure mode is that if either
67 * is cleared, the machine locks up with endless interrupts.
68 */
69#define ALPHA_KLUDGE_MCR (UART_MCR_OUT2 | UART_MCR_OUT1)
70#elif defined(CONFIG_SBC8560)
71/*
72 * WindRiver did something similarly broken on their SBC8560 board. The
73 * UART tristates its IRQ output while OUT2 is clear, but they pulled
74 * the interrupt line _up_ instead of down, so if we register the IRQ
75 * while the UART is in that state, we die in an IRQ storm. */
76#define ALPHA_KLUDGE_MCR (UART_MCR_OUT2)
77#else
78#define ALPHA_KLUDGE_MCR 0
79#endif
diff --git a/drivers/tty/serial/8250_accent.c b/drivers/tty/serial/8250_accent.c
new file mode 100644
index 000000000000..34b51c651192
--- /dev/null
+++ b/drivers/tty/serial/8250_accent.c
@@ -0,0 +1,45 @@
1/*
2 * Copyright (C) 2005 Russell King.
3 * Data taken from include/asm-i386/serial.h
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#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/serial_8250.h>
12
13#define PORT(_base,_irq) \
14 { \
15 .iobase = _base, \
16 .irq = _irq, \
17 .uartclk = 1843200, \
18 .iotype = UPIO_PORT, \
19 .flags = UPF_BOOT_AUTOCONF, \
20 }
21
22static struct plat_serial8250_port accent_data[] = {
23 PORT(0x330, 4),
24 PORT(0x338, 4),
25 { },
26};
27
28static struct platform_device accent_device = {
29 .name = "serial8250",
30 .id = PLAT8250_DEV_ACCENT,
31 .dev = {
32 .platform_data = accent_data,
33 },
34};
35
36static int __init accent_init(void)
37{
38 return platform_device_register(&accent_device);
39}
40
41module_init(accent_init);
42
43MODULE_AUTHOR("Russell King");
44MODULE_DESCRIPTION("8250 serial probe module for Accent Async cards");
45MODULE_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..d125dc107985
--- /dev/null
+++ b/drivers/tty/serial/8250_boca.c
@@ -0,0 +1,59 @@
1/*
2 * Copyright (C) 2005 Russell King.
3 * Data taken from include/asm-i386/serial.h
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#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/serial_8250.h>
12
13#define PORT(_base,_irq) \
14 { \
15 .iobase = _base, \
16 .irq = _irq, \
17 .uartclk = 1843200, \
18 .iotype = UPIO_PORT, \
19 .flags = UPF_BOOT_AUTOCONF, \
20 }
21
22static struct plat_serial8250_port boca_data[] = {
23 PORT(0x100, 12),
24 PORT(0x108, 12),
25 PORT(0x110, 12),
26 PORT(0x118, 12),
27 PORT(0x120, 12),
28 PORT(0x128, 12),
29 PORT(0x130, 12),
30 PORT(0x138, 12),
31 PORT(0x140, 12),
32 PORT(0x148, 12),
33 PORT(0x150, 12),
34 PORT(0x158, 12),
35 PORT(0x160, 12),
36 PORT(0x168, 12),
37 PORT(0x170, 12),
38 PORT(0x178, 12),
39 { },
40};
41
42static struct platform_device boca_device = {
43 .name = "serial8250",
44 .id = PLAT8250_DEV_BOCA,
45 .dev = {
46 .platform_data = boca_data,
47 },
48};
49
50static int __init boca_init(void)
51{
52 return platform_device_register(&boca_device);
53}
54
55module_init(boca_init);
56
57MODULE_AUTHOR("Russell King");
58MODULE_DESCRIPTION("8250 serial probe module for Boca cards");
59MODULE_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..bf53aabf9b5e
--- /dev/null
+++ b/drivers/tty/serial/8250_exar_st16c554.c
@@ -0,0 +1,50 @@
1/*
2 * Written by Paul B Schroeder < pschroeder "at" uplogix "dot" com >
3 * Based on 8250_boca.
4 *
5 * Copyright (C) 2005 Russell King.
6 * Data taken from include/asm-i386/serial.h
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#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/serial_8250.h>
15
16#define PORT(_base,_irq) \
17 { \
18 .iobase = _base, \
19 .irq = _irq, \
20 .uartclk = 1843200, \
21 .iotype = UPIO_PORT, \
22 .flags = UPF_BOOT_AUTOCONF, \
23 }
24
25static struct plat_serial8250_port exar_data[] = {
26 PORT(0x100, 5),
27 PORT(0x108, 5),
28 PORT(0x110, 5),
29 PORT(0x118, 5),
30 { },
31};
32
33static struct platform_device exar_device = {
34 .name = "serial8250",
35 .id = PLAT8250_DEV_EXAR_ST16C554,
36 .dev = {
37 .platform_data = exar_data,
38 },
39};
40
41static int __init exar_init(void)
42{
43 return platform_device_register(&exar_device);
44}
45
46module_init(exar_init);
47
48MODULE_AUTHOR("Paul B Schroeder");
49MODULE_DESCRIPTION("8250 serial probe module for Exar cards");
50MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_fourport.c b/drivers/tty/serial/8250_fourport.c
new file mode 100644
index 000000000000..be1582609626
--- /dev/null
+++ b/drivers/tty/serial/8250_fourport.c
@@ -0,0 +1,51 @@
1/*
2 * Copyright (C) 2005 Russell King.
3 * Data taken from include/asm-i386/serial.h
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#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/serial_8250.h>
12
13#define PORT(_base,_irq) \
14 { \
15 .iobase = _base, \
16 .irq = _irq, \
17 .uartclk = 1843200, \
18 .iotype = UPIO_PORT, \
19 .flags = UPF_BOOT_AUTOCONF | UPF_FOURPORT, \
20 }
21
22static struct plat_serial8250_port fourport_data[] = {
23 PORT(0x1a0, 9),
24 PORT(0x1a8, 9),
25 PORT(0x1b0, 9),
26 PORT(0x1b8, 9),
27 PORT(0x2a0, 5),
28 PORT(0x2a8, 5),
29 PORT(0x2b0, 5),
30 PORT(0x2b8, 5),
31 { },
32};
33
34static struct platform_device fourport_device = {
35 .name = "serial8250",
36 .id = PLAT8250_DEV_FOURPORT,
37 .dev = {
38 .platform_data = fourport_data,
39 },
40};
41
42static int __init fourport_init(void)
43{
44 return platform_device_register(&fourport_device);
45}
46
47module_init(fourport_init);
48
49MODULE_AUTHOR("Russell King");
50MODULE_DESCRIPTION("8250 serial probe module for AST Fourport cards");
51MODULE_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..a5c778e83de0
--- /dev/null
+++ b/drivers/tty/serial/8250_hub6.c
@@ -0,0 +1,56 @@
1/*
2 * Copyright (C) 2005 Russell King.
3 * Data taken from include/asm-i386/serial.h
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#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/serial_8250.h>
12
13#define HUB6(card,port) \
14 { \
15 .iobase = 0x302, \
16 .irq = 3, \
17 .uartclk = 1843200, \
18 .iotype = UPIO_HUB6, \
19 .flags = UPF_BOOT_AUTOCONF, \
20 .hub6 = (card) << 6 | (port) << 3 | 1, \
21 }
22
23static struct plat_serial8250_port hub6_data[] = {
24 HUB6(0, 0),
25 HUB6(0, 1),
26 HUB6(0, 2),
27 HUB6(0, 3),
28 HUB6(0, 4),
29 HUB6(0, 5),
30 HUB6(1, 0),
31 HUB6(1, 1),
32 HUB6(1, 2),
33 HUB6(1, 3),
34 HUB6(1, 4),
35 HUB6(1, 5),
36 { },
37};
38
39static struct platform_device hub6_device = {
40 .name = "serial8250",
41 .id = PLAT8250_DEV_HUB6,
42 .dev = {
43 .platform_data = hub6_data,
44 },
45};
46
47static int __init hub6_init(void)
48{
49 return platform_device_register(&hub6_device);
50}
51
52module_init(hub6_init);
53
54MODULE_AUTHOR("Russell King");
55MODULE_DESCRIPTION("8250 serial probe module for Hub6 cards");
56MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_mca.c b/drivers/tty/serial/8250_mca.c
new file mode 100644
index 000000000000..d20abf04541e
--- /dev/null
+++ b/drivers/tty/serial/8250_mca.c
@@ -0,0 +1,61 @@
1/*
2 * Copyright (C) 2005 Russell King.
3 * Data taken from include/asm-i386/serial.h
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#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/mca.h>
12#include <linux/serial_8250.h>
13
14/*
15 * FIXME: Should we be doing AUTO_IRQ here?
16 */
17#ifdef CONFIG_SERIAL_8250_DETECT_IRQ
18#define MCA_FLAGS UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_AUTO_IRQ
19#else
20#define MCA_FLAGS UPF_BOOT_AUTOCONF | UPF_SKIP_TEST
21#endif
22
23#define PORT(_base,_irq) \
24 { \
25 .iobase = _base, \
26 .irq = _irq, \
27 .uartclk = 1843200, \
28 .iotype = UPIO_PORT, \
29 .flags = MCA_FLAGS, \
30 }
31
32static struct plat_serial8250_port mca_data[] = {
33 PORT(0x3220, 3),
34 PORT(0x3228, 3),
35 PORT(0x4220, 3),
36 PORT(0x4228, 3),
37 PORT(0x5220, 3),
38 PORT(0x5228, 3),
39 { },
40};
41
42static struct platform_device mca_device = {
43 .name = "serial8250",
44 .id = PLAT8250_DEV_MCA,
45 .dev = {
46 .platform_data = mca_data,
47 },
48};
49
50static int __init mca_init(void)
51{
52 if (!MCA_bus)
53 return -ENODEV;
54 return platform_device_register(&mca_device);
55}
56
57module_init(mca_init);
58
59MODULE_AUTHOR("Russell King");
60MODULE_DESCRIPTION("8250 serial probe module for MCA ports");
61MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/8250_pci.c b/drivers/tty/serial/8250_pci.c
new file mode 100644
index 000000000000..f41b4259ecdd
--- /dev/null
+++ b/drivers/tty/serial/8250_pci.c
@@ -0,0 +1,3956 @@
1/*
2 * Probe module for 8250/16550-type PCI serial ports.
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King, All Rights Reserved.
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.
11 */
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/pci.h>
15#include <linux/string.h>
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/delay.h>
19#include <linux/tty.h>
20#include <linux/serial_core.h>
21#include <linux/8250_pci.h>
22#include <linux/bitops.h>
23
24#include <asm/byteorder.h>
25#include <asm/io.h>
26
27#include "8250.h"
28
29#undef SERIAL_DEBUG_PCI
30
31/*
32 * init function returns:
33 * > 0 - number of ports
34 * = 0 - use board->num_ports
35 * < 0 - error
36 */
37struct pci_serial_quirk {
38 u32 vendor;
39 u32 device;
40 u32 subvendor;
41 u32 subdevice;
42 int (*init)(struct pci_dev *dev);
43 int (*setup)(struct serial_private *,
44 const struct pciserial_board *,
45 struct uart_port *, int);
46 void (*exit)(struct pci_dev *dev);
47};
48
49#define PCI_NUM_BAR_RESOURCES 6
50
51struct serial_private {
52 struct pci_dev *dev;
53 unsigned int nr;
54 void __iomem *remapped_bar[PCI_NUM_BAR_RESOURCES];
55 struct pci_serial_quirk *quirk;
56 int line[0];
57};
58
59static void moan_device(const char *str, struct pci_dev *dev)
60{
61 printk(KERN_WARNING
62 "%s: %s\n"
63 "Please send the output of lspci -vv, this\n"
64 "message (0x%04x,0x%04x,0x%04x,0x%04x), the\n"
65 "manufacturer and name of serial board or\n"
66 "modem board to rmk+serial@arm.linux.org.uk.\n",
67 pci_name(dev), str, dev->vendor, dev->device,
68 dev->subsystem_vendor, dev->subsystem_device);
69}
70
71static int
72setup_port(struct serial_private *priv, struct uart_port *port,
73 int bar, int offset, int regshift)
74{
75 struct pci_dev *dev = priv->dev;
76 unsigned long base, len;
77
78 if (bar >= PCI_NUM_BAR_RESOURCES)
79 return -EINVAL;
80
81 base = pci_resource_start(dev, bar);
82
83 if (pci_resource_flags(dev, bar) & IORESOURCE_MEM) {
84 len = pci_resource_len(dev, bar);
85
86 if (!priv->remapped_bar[bar])
87 priv->remapped_bar[bar] = ioremap_nocache(base, len);
88 if (!priv->remapped_bar[bar])
89 return -ENOMEM;
90
91 port->iotype = UPIO_MEM;
92 port->iobase = 0;
93 port->mapbase = base + offset;
94 port->membase = priv->remapped_bar[bar] + offset;
95 port->regshift = regshift;
96 } else {
97 port->iotype = UPIO_PORT;
98 port->iobase = base + offset;
99 port->mapbase = 0;
100 port->membase = NULL;
101 port->regshift = 0;
102 }
103 return 0;
104}
105
106/*
107 * ADDI-DATA GmbH communication cards <info@addi-data.com>
108 */
109static int addidata_apci7800_setup(struct serial_private *priv,
110 const struct pciserial_board *board,
111 struct uart_port *port, int idx)
112{
113 unsigned int bar = 0, offset = board->first_offset;
114 bar = FL_GET_BASE(board->flags);
115
116 if (idx < 2) {
117 offset += idx * board->uart_offset;
118 } else if ((idx >= 2) && (idx < 4)) {
119 bar += 1;
120 offset += ((idx - 2) * board->uart_offset);
121 } else if ((idx >= 4) && (idx < 6)) {
122 bar += 2;
123 offset += ((idx - 4) * board->uart_offset);
124 } else if (idx >= 6) {
125 bar += 3;
126 offset += ((idx - 6) * board->uart_offset);
127 }
128
129 return setup_port(priv, port, bar, offset, board->reg_shift);
130}
131
132/*
133 * AFAVLAB uses a different mixture of BARs and offsets
134 * Not that ugly ;) -- HW
135 */
136static int
137afavlab_setup(struct serial_private *priv, const struct pciserial_board *board,
138 struct uart_port *port, int idx)
139{
140 unsigned int bar, offset = board->first_offset;
141
142 bar = FL_GET_BASE(board->flags);
143 if (idx < 4)
144 bar += idx;
145 else {
146 bar = 4;
147 offset += (idx - 4) * board->uart_offset;
148 }
149
150 return setup_port(priv, port, bar, offset, board->reg_shift);
151}
152
153/*
154 * HP's Remote Management Console. The Diva chip came in several
155 * different versions. N-class, L2000 and A500 have two Diva chips, each
156 * with 3 UARTs (the third UART on the second chip is unused). Superdome
157 * and Keystone have one Diva chip with 3 UARTs. Some later machines have
158 * one Diva chip, but it has been expanded to 5 UARTs.
159 */
160static int pci_hp_diva_init(struct pci_dev *dev)
161{
162 int rc = 0;
163
164 switch (dev->subsystem_device) {
165 case PCI_DEVICE_ID_HP_DIVA_TOSCA1:
166 case PCI_DEVICE_ID_HP_DIVA_HALFDOME:
167 case PCI_DEVICE_ID_HP_DIVA_KEYSTONE:
168 case PCI_DEVICE_ID_HP_DIVA_EVEREST:
169 rc = 3;
170 break;
171 case PCI_DEVICE_ID_HP_DIVA_TOSCA2:
172 rc = 2;
173 break;
174 case PCI_DEVICE_ID_HP_DIVA_MAESTRO:
175 rc = 4;
176 break;
177 case PCI_DEVICE_ID_HP_DIVA_POWERBAR:
178 case PCI_DEVICE_ID_HP_DIVA_HURRICANE:
179 rc = 1;
180 break;
181 }
182
183 return rc;
184}
185
186/*
187 * HP's Diva chip puts the 4th/5th serial port further out, and
188 * some serial ports are supposed to be hidden on certain models.
189 */
190static int
191pci_hp_diva_setup(struct serial_private *priv,
192 const struct pciserial_board *board,
193 struct uart_port *port, int idx)
194{
195 unsigned int offset = board->first_offset;
196 unsigned int bar = FL_GET_BASE(board->flags);
197
198 switch (priv->dev->subsystem_device) {
199 case PCI_DEVICE_ID_HP_DIVA_MAESTRO:
200 if (idx == 3)
201 idx++;
202 break;
203 case PCI_DEVICE_ID_HP_DIVA_EVEREST:
204 if (idx > 0)
205 idx++;
206 if (idx > 2)
207 idx++;
208 break;
209 }
210 if (idx > 2)
211 offset = 0x18;
212
213 offset += idx * board->uart_offset;
214
215 return setup_port(priv, port, bar, offset, board->reg_shift);
216}
217
218/*
219 * Added for EKF Intel i960 serial boards
220 */
221static int pci_inteli960ni_init(struct pci_dev *dev)
222{
223 unsigned long oldval;
224
225 if (!(dev->subsystem_device & 0x1000))
226 return -ENODEV;
227
228 /* is firmware started? */
229 pci_read_config_dword(dev, 0x44, (void *)&oldval);
230 if (oldval == 0x00001000L) { /* RESET value */
231 printk(KERN_DEBUG "Local i960 firmware missing");
232 return -ENODEV;
233 }
234 return 0;
235}
236
237/*
238 * Some PCI serial cards using the PLX 9050 PCI interface chip require
239 * that the card interrupt be explicitly enabled or disabled. This
240 * seems to be mainly needed on card using the PLX which also use I/O
241 * mapped memory.
242 */
243static int pci_plx9050_init(struct pci_dev *dev)
244{
245 u8 irq_config;
246 void __iomem *p;
247
248 if ((pci_resource_flags(dev, 0) & IORESOURCE_MEM) == 0) {
249 moan_device("no memory in bar 0", dev);
250 return 0;
251 }
252
253 irq_config = 0x41;
254 if (dev->vendor == PCI_VENDOR_ID_PANACOM ||
255 dev->subsystem_vendor == PCI_SUBVENDOR_ID_EXSYS)
256 irq_config = 0x43;
257
258 if ((dev->vendor == PCI_VENDOR_ID_PLX) &&
259 (dev->device == PCI_DEVICE_ID_PLX_ROMULUS))
260 /*
261 * As the megawolf cards have the int pins active
262 * high, and have 2 UART chips, both ints must be
263 * enabled on the 9050. Also, the UARTS are set in
264 * 16450 mode by default, so we have to enable the
265 * 16C950 'enhanced' mode so that we can use the
266 * deep FIFOs
267 */
268 irq_config = 0x5b;
269 /*
270 * enable/disable interrupts
271 */
272 p = ioremap_nocache(pci_resource_start(dev, 0), 0x80);
273 if (p == NULL)
274 return -ENOMEM;
275 writel(irq_config, p + 0x4c);
276
277 /*
278 * Read the register back to ensure that it took effect.
279 */
280 readl(p + 0x4c);
281 iounmap(p);
282
283 return 0;
284}
285
286static void __devexit pci_plx9050_exit(struct pci_dev *dev)
287{
288 u8 __iomem *p;
289
290 if ((pci_resource_flags(dev, 0) & IORESOURCE_MEM) == 0)
291 return;
292
293 /*
294 * disable interrupts
295 */
296 p = ioremap_nocache(pci_resource_start(dev, 0), 0x80);
297 if (p != NULL) {
298 writel(0, p + 0x4c);
299
300 /*
301 * Read the register back to ensure that it took effect.
302 */
303 readl(p + 0x4c);
304 iounmap(p);
305 }
306}
307
308#define NI8420_INT_ENABLE_REG 0x38
309#define NI8420_INT_ENABLE_BIT 0x2000
310
311static void __devexit pci_ni8420_exit(struct pci_dev *dev)
312{
313 void __iomem *p;
314 unsigned long base, len;
315 unsigned int bar = 0;
316
317 if ((pci_resource_flags(dev, bar) & IORESOURCE_MEM) == 0) {
318 moan_device("no memory in bar", dev);
319 return;
320 }
321
322 base = pci_resource_start(dev, bar);
323 len = pci_resource_len(dev, bar);
324 p = ioremap_nocache(base, len);
325 if (p == NULL)
326 return;
327
328 /* Disable the CPU Interrupt */
329 writel(readl(p + NI8420_INT_ENABLE_REG) & ~(NI8420_INT_ENABLE_BIT),
330 p + NI8420_INT_ENABLE_REG);
331 iounmap(p);
332}
333
334
335/* MITE registers */
336#define MITE_IOWBSR1 0xc4
337#define MITE_IOWCR1 0xf4
338#define MITE_LCIMR1 0x08
339#define MITE_LCIMR2 0x10
340
341#define MITE_LCIMR2_CLR_CPU_IE (1 << 30)
342
343static void __devexit pci_ni8430_exit(struct pci_dev *dev)
344{
345 void __iomem *p;
346 unsigned long base, len;
347 unsigned int bar = 0;
348
349 if ((pci_resource_flags(dev, bar) & IORESOURCE_MEM) == 0) {
350 moan_device("no memory in bar", dev);
351 return;
352 }
353
354 base = pci_resource_start(dev, bar);
355 len = pci_resource_len(dev, bar);
356 p = ioremap_nocache(base, len);
357 if (p == NULL)
358 return;
359
360 /* Disable the CPU Interrupt */
361 writel(MITE_LCIMR2_CLR_CPU_IE, p + MITE_LCIMR2);
362 iounmap(p);
363}
364
365/* SBS Technologies Inc. PMC-OCTPRO and P-OCTAL cards */
366static int
367sbs_setup(struct serial_private *priv, const struct pciserial_board *board,
368 struct uart_port *port, int idx)
369{
370 unsigned int bar, offset = board->first_offset;
371
372 bar = 0;
373
374 if (idx < 4) {
375 /* first four channels map to 0, 0x100, 0x200, 0x300 */
376 offset += idx * board->uart_offset;
377 } else if (idx < 8) {
378 /* last four channels map to 0x1000, 0x1100, 0x1200, 0x1300 */
379 offset += idx * board->uart_offset + 0xC00;
380 } else /* we have only 8 ports on PMC-OCTALPRO */
381 return 1;
382
383 return setup_port(priv, port, bar, offset, board->reg_shift);
384}
385
386/*
387* This does initialization for PMC OCTALPRO cards:
388* maps the device memory, resets the UARTs (needed, bc
389* if the module is removed and inserted again, the card
390* is in the sleep mode) and enables global interrupt.
391*/
392
393/* global control register offset for SBS PMC-OctalPro */
394#define OCT_REG_CR_OFF 0x500
395
396static int sbs_init(struct pci_dev *dev)
397{
398 u8 __iomem *p;
399
400 p = pci_ioremap_bar(dev, 0);
401
402 if (p == NULL)
403 return -ENOMEM;
404 /* Set bit-4 Control Register (UART RESET) in to reset the uarts */
405 writeb(0x10, p + OCT_REG_CR_OFF);
406 udelay(50);
407 writeb(0x0, p + OCT_REG_CR_OFF);
408
409 /* Set bit-2 (INTENABLE) of Control Register */
410 writeb(0x4, p + OCT_REG_CR_OFF);
411 iounmap(p);
412
413 return 0;
414}
415
416/*
417 * Disables the global interrupt of PMC-OctalPro
418 */
419
420static void __devexit sbs_exit(struct pci_dev *dev)
421{
422 u8 __iomem *p;
423
424 p = pci_ioremap_bar(dev, 0);
425 /* FIXME: What if resource_len < OCT_REG_CR_OFF */
426 if (p != NULL)
427 writeb(0, p + OCT_REG_CR_OFF);
428 iounmap(p);
429}
430
431/*
432 * SIIG serial cards have an PCI interface chip which also controls
433 * the UART clocking frequency. Each UART can be clocked independently
434 * (except cards equipped with 4 UARTs) and initial clocking settings
435 * are stored in the EEPROM chip. It can cause problems because this
436 * version of serial driver doesn't support differently clocked UART's
437 * on single PCI card. To prevent this, initialization functions set
438 * high frequency clocking for all UART's on given card. It is safe (I
439 * hope) because it doesn't touch EEPROM settings to prevent conflicts
440 * with other OSes (like M$ DOS).
441 *
442 * SIIG support added by Andrey Panin <pazke@donpac.ru>, 10/1999
443 *
444 * There is two family of SIIG serial cards with different PCI
445 * interface chip and different configuration methods:
446 * - 10x cards have control registers in IO and/or memory space;
447 * - 20x cards have control registers in standard PCI configuration space.
448 *
449 * Note: all 10x cards have PCI device ids 0x10..
450 * all 20x cards have PCI device ids 0x20..
451 *
452 * There are also Quartet Serial cards which use Oxford Semiconductor
453 * 16954 quad UART PCI chip clocked by 18.432 MHz quartz.
454 *
455 * Note: some SIIG cards are probed by the parport_serial object.
456 */
457
458#define PCI_DEVICE_ID_SIIG_1S_10x (PCI_DEVICE_ID_SIIG_1S_10x_550 & 0xfffc)
459#define PCI_DEVICE_ID_SIIG_2S_10x (PCI_DEVICE_ID_SIIG_2S_10x_550 & 0xfff8)
460
461static int pci_siig10x_init(struct pci_dev *dev)
462{
463 u16 data;
464 void __iomem *p;
465
466 switch (dev->device & 0xfff8) {
467 case PCI_DEVICE_ID_SIIG_1S_10x: /* 1S */
468 data = 0xffdf;
469 break;
470 case PCI_DEVICE_ID_SIIG_2S_10x: /* 2S, 2S1P */
471 data = 0xf7ff;
472 break;
473 default: /* 1S1P, 4S */
474 data = 0xfffb;
475 break;
476 }
477
478 p = ioremap_nocache(pci_resource_start(dev, 0), 0x80);
479 if (p == NULL)
480 return -ENOMEM;
481
482 writew(readw(p + 0x28) & data, p + 0x28);
483 readw(p + 0x28);
484 iounmap(p);
485 return 0;
486}
487
488#define PCI_DEVICE_ID_SIIG_2S_20x (PCI_DEVICE_ID_SIIG_2S_20x_550 & 0xfffc)
489#define PCI_DEVICE_ID_SIIG_2S1P_20x (PCI_DEVICE_ID_SIIG_2S1P_20x_550 & 0xfffc)
490
491static int pci_siig20x_init(struct pci_dev *dev)
492{
493 u8 data;
494
495 /* Change clock frequency for the first UART. */
496 pci_read_config_byte(dev, 0x6f, &data);
497 pci_write_config_byte(dev, 0x6f, data & 0xef);
498
499 /* If this card has 2 UART, we have to do the same with second UART. */
500 if (((dev->device & 0xfffc) == PCI_DEVICE_ID_SIIG_2S_20x) ||
501 ((dev->device & 0xfffc) == PCI_DEVICE_ID_SIIG_2S1P_20x)) {
502 pci_read_config_byte(dev, 0x73, &data);
503 pci_write_config_byte(dev, 0x73, data & 0xef);
504 }
505 return 0;
506}
507
508static int pci_siig_init(struct pci_dev *dev)
509{
510 unsigned int type = dev->device & 0xff00;
511
512 if (type == 0x1000)
513 return pci_siig10x_init(dev);
514 else if (type == 0x2000)
515 return pci_siig20x_init(dev);
516
517 moan_device("Unknown SIIG card", dev);
518 return -ENODEV;
519}
520
521static int pci_siig_setup(struct serial_private *priv,
522 const struct pciserial_board *board,
523 struct uart_port *port, int idx)
524{
525 unsigned int bar = FL_GET_BASE(board->flags) + idx, offset = 0;
526
527 if (idx > 3) {
528 bar = 4;
529 offset = (idx - 4) * 8;
530 }
531
532 return setup_port(priv, port, bar, offset, 0);
533}
534
535/*
536 * Timedia has an explosion of boards, and to avoid the PCI table from
537 * growing *huge*, we use this function to collapse some 70 entries
538 * in the PCI table into one, for sanity's and compactness's sake.
539 */
540static const unsigned short timedia_single_port[] = {
541 0x4025, 0x4027, 0x4028, 0x5025, 0x5027, 0
542};
543
544static const unsigned short timedia_dual_port[] = {
545 0x0002, 0x4036, 0x4037, 0x4038, 0x4078, 0x4079, 0x4085,
546 0x4088, 0x4089, 0x5037, 0x5078, 0x5079, 0x5085, 0x6079,
547 0x7079, 0x8079, 0x8137, 0x8138, 0x8237, 0x8238, 0x9079,
548 0x9137, 0x9138, 0x9237, 0x9238, 0xA079, 0xB079, 0xC079,
549 0xD079, 0
550};
551
552static const unsigned short timedia_quad_port[] = {
553 0x4055, 0x4056, 0x4095, 0x4096, 0x5056, 0x8156, 0x8157,
554 0x8256, 0x8257, 0x9056, 0x9156, 0x9157, 0x9158, 0x9159,
555 0x9256, 0x9257, 0xA056, 0xA157, 0xA158, 0xA159, 0xB056,
556 0xB157, 0
557};
558
559static const unsigned short timedia_eight_port[] = {
560 0x4065, 0x4066, 0x5065, 0x5066, 0x8166, 0x9066, 0x9166,
561 0x9167, 0x9168, 0xA066, 0xA167, 0xA168, 0
562};
563
564static const struct timedia_struct {
565 int num;
566 const unsigned short *ids;
567} timedia_data[] = {
568 { 1, timedia_single_port },
569 { 2, timedia_dual_port },
570 { 4, timedia_quad_port },
571 { 8, timedia_eight_port }
572};
573
574static int pci_timedia_init(struct pci_dev *dev)
575{
576 const unsigned short *ids;
577 int i, j;
578
579 for (i = 0; i < ARRAY_SIZE(timedia_data); i++) {
580 ids = timedia_data[i].ids;
581 for (j = 0; ids[j]; j++)
582 if (dev->subsystem_device == ids[j])
583 return timedia_data[i].num;
584 }
585 return 0;
586}
587
588/*
589 * Timedia/SUNIX uses a mixture of BARs and offsets
590 * Ugh, this is ugly as all hell --- TYT
591 */
592static int
593pci_timedia_setup(struct serial_private *priv,
594 const struct pciserial_board *board,
595 struct uart_port *port, int idx)
596{
597 unsigned int bar = 0, offset = board->first_offset;
598
599 switch (idx) {
600 case 0:
601 bar = 0;
602 break;
603 case 1:
604 offset = board->uart_offset;
605 bar = 0;
606 break;
607 case 2:
608 bar = 1;
609 break;
610 case 3:
611 offset = board->uart_offset;
612 /* FALLTHROUGH */
613 case 4: /* BAR 2 */
614 case 5: /* BAR 3 */
615 case 6: /* BAR 4 */
616 case 7: /* BAR 5 */
617 bar = idx - 2;
618 }
619
620 return setup_port(priv, port, bar, offset, board->reg_shift);
621}
622
623/*
624 * Some Titan cards are also a little weird
625 */
626static int
627titan_400l_800l_setup(struct serial_private *priv,
628 const struct pciserial_board *board,
629 struct uart_port *port, int idx)
630{
631 unsigned int bar, offset = board->first_offset;
632
633 switch (idx) {
634 case 0:
635 bar = 1;
636 break;
637 case 1:
638 bar = 2;
639 break;
640 default:
641 bar = 4;
642 offset = (idx - 2) * board->uart_offset;
643 }
644
645 return setup_port(priv, port, bar, offset, board->reg_shift);
646}
647
648static int pci_xircom_init(struct pci_dev *dev)
649{
650 msleep(100);
651 return 0;
652}
653
654static int pci_ni8420_init(struct pci_dev *dev)
655{
656 void __iomem *p;
657 unsigned long base, len;
658 unsigned int bar = 0;
659
660 if ((pci_resource_flags(dev, bar) & IORESOURCE_MEM) == 0) {
661 moan_device("no memory in bar", dev);
662 return 0;
663 }
664
665 base = pci_resource_start(dev, bar);
666 len = pci_resource_len(dev, bar);
667 p = ioremap_nocache(base, len);
668 if (p == NULL)
669 return -ENOMEM;
670
671 /* Enable CPU Interrupt */
672 writel(readl(p + NI8420_INT_ENABLE_REG) | NI8420_INT_ENABLE_BIT,
673 p + NI8420_INT_ENABLE_REG);
674
675 iounmap(p);
676 return 0;
677}
678
679#define MITE_IOWBSR1_WSIZE 0xa
680#define MITE_IOWBSR1_WIN_OFFSET 0x800
681#define MITE_IOWBSR1_WENAB (1 << 7)
682#define MITE_LCIMR1_IO_IE_0 (1 << 24)
683#define MITE_LCIMR2_SET_CPU_IE (1 << 31)
684#define MITE_IOWCR1_RAMSEL_MASK 0xfffffffe
685
686static int pci_ni8430_init(struct pci_dev *dev)
687{
688 void __iomem *p;
689 unsigned long base, len;
690 u32 device_window;
691 unsigned int bar = 0;
692
693 if ((pci_resource_flags(dev, bar) & IORESOURCE_MEM) == 0) {
694 moan_device("no memory in bar", dev);
695 return 0;
696 }
697
698 base = pci_resource_start(dev, bar);
699 len = pci_resource_len(dev, bar);
700 p = ioremap_nocache(base, len);
701 if (p == NULL)
702 return -ENOMEM;
703
704 /* Set device window address and size in BAR0 */
705 device_window = ((base + MITE_IOWBSR1_WIN_OFFSET) & 0xffffff00)
706 | MITE_IOWBSR1_WENAB | MITE_IOWBSR1_WSIZE;
707 writel(device_window, p + MITE_IOWBSR1);
708
709 /* Set window access to go to RAMSEL IO address space */
710 writel((readl(p + MITE_IOWCR1) & MITE_IOWCR1_RAMSEL_MASK),
711 p + MITE_IOWCR1);
712
713 /* Enable IO Bus Interrupt 0 */
714 writel(MITE_LCIMR1_IO_IE_0, p + MITE_LCIMR1);
715
716 /* Enable CPU Interrupt */
717 writel(MITE_LCIMR2_SET_CPU_IE, p + MITE_LCIMR2);
718
719 iounmap(p);
720 return 0;
721}
722
723/* UART Port Control Register */
724#define NI8430_PORTCON 0x0f
725#define NI8430_PORTCON_TXVR_ENABLE (1 << 3)
726
727static int
728pci_ni8430_setup(struct serial_private *priv,
729 const struct pciserial_board *board,
730 struct uart_port *port, int idx)
731{
732 void __iomem *p;
733 unsigned long base, len;
734 unsigned int bar, offset = board->first_offset;
735
736 if (idx >= board->num_ports)
737 return 1;
738
739 bar = FL_GET_BASE(board->flags);
740 offset += idx * board->uart_offset;
741
742 base = pci_resource_start(priv->dev, bar);
743 len = pci_resource_len(priv->dev, bar);
744 p = ioremap_nocache(base, len);
745
746 /* enable the transciever */
747 writeb(readb(p + offset + NI8430_PORTCON) | NI8430_PORTCON_TXVR_ENABLE,
748 p + offset + NI8430_PORTCON);
749
750 iounmap(p);
751
752 return setup_port(priv, port, bar, offset, board->reg_shift);
753}
754
755
756static int pci_netmos_init(struct pci_dev *dev)
757{
758 /* subdevice 0x00PS means <P> parallel, <S> serial */
759 unsigned int num_serial = dev->subsystem_device & 0xf;
760
761 if ((dev->device == PCI_DEVICE_ID_NETMOS_9901) ||
762 (dev->device == PCI_DEVICE_ID_NETMOS_9865))
763 return 0;
764 if (dev->subsystem_vendor == PCI_VENDOR_ID_IBM &&
765 dev->subsystem_device == 0x0299)
766 return 0;
767
768 if (num_serial == 0)
769 return -ENODEV;
770 return num_serial;
771}
772
773/*
774 * These chips are available with optionally one parallel port and up to
775 * two serial ports. Unfortunately they all have the same product id.
776 *
777 * Basic configuration is done over a region of 32 I/O ports. The base
778 * ioport is called INTA or INTC, depending on docs/other drivers.
779 *
780 * The region of the 32 I/O ports is configured in POSIO0R...
781 */
782
783/* registers */
784#define ITE_887x_MISCR 0x9c
785#define ITE_887x_INTCBAR 0x78
786#define ITE_887x_UARTBAR 0x7c
787#define ITE_887x_PS0BAR 0x10
788#define ITE_887x_POSIO0 0x60
789
790/* I/O space size */
791#define ITE_887x_IOSIZE 32
792/* I/O space size (bits 26-24; 8 bytes = 011b) */
793#define ITE_887x_POSIO_IOSIZE_8 (3 << 24)
794/* I/O space size (bits 26-24; 32 bytes = 101b) */
795#define ITE_887x_POSIO_IOSIZE_32 (5 << 24)
796/* Decoding speed (1 = slow, 2 = medium, 3 = fast) */
797#define ITE_887x_POSIO_SPEED (3 << 29)
798/* enable IO_Space bit */
799#define ITE_887x_POSIO_ENABLE (1 << 31)
800
801static int pci_ite887x_init(struct pci_dev *dev)
802{
803 /* inta_addr are the configuration addresses of the ITE */
804 static const short inta_addr[] = { 0x2a0, 0x2c0, 0x220, 0x240, 0x1e0,
805 0x200, 0x280, 0 };
806 int ret, i, type;
807 struct resource *iobase = NULL;
808 u32 miscr, uartbar, ioport;
809
810 /* search for the base-ioport */
811 i = 0;
812 while (inta_addr[i] && iobase == NULL) {
813 iobase = request_region(inta_addr[i], ITE_887x_IOSIZE,
814 "ite887x");
815 if (iobase != NULL) {
816 /* write POSIO0R - speed | size | ioport */
817 pci_write_config_dword(dev, ITE_887x_POSIO0,
818 ITE_887x_POSIO_ENABLE | ITE_887x_POSIO_SPEED |
819 ITE_887x_POSIO_IOSIZE_32 | inta_addr[i]);
820 /* write INTCBAR - ioport */
821 pci_write_config_dword(dev, ITE_887x_INTCBAR,
822 inta_addr[i]);
823 ret = inb(inta_addr[i]);
824 if (ret != 0xff) {
825 /* ioport connected */
826 break;
827 }
828 release_region(iobase->start, ITE_887x_IOSIZE);
829 iobase = NULL;
830 }
831 i++;
832 }
833
834 if (!inta_addr[i]) {
835 printk(KERN_ERR "ite887x: could not find iobase\n");
836 return -ENODEV;
837 }
838
839 /* start of undocumented type checking (see parport_pc.c) */
840 type = inb(iobase->start + 0x18) & 0x0f;
841
842 switch (type) {
843 case 0x2: /* ITE8871 (1P) */
844 case 0xa: /* ITE8875 (1P) */
845 ret = 0;
846 break;
847 case 0xe: /* ITE8872 (2S1P) */
848 ret = 2;
849 break;
850 case 0x6: /* ITE8873 (1S) */
851 ret = 1;
852 break;
853 case 0x8: /* ITE8874 (2S) */
854 ret = 2;
855 break;
856 default:
857 moan_device("Unknown ITE887x", dev);
858 ret = -ENODEV;
859 }
860
861 /* configure all serial ports */
862 for (i = 0; i < ret; i++) {
863 /* read the I/O port from the device */
864 pci_read_config_dword(dev, ITE_887x_PS0BAR + (0x4 * (i + 1)),
865 &ioport);
866 ioport &= 0x0000FF00; /* the actual base address */
867 pci_write_config_dword(dev, ITE_887x_POSIO0 + (0x4 * (i + 1)),
868 ITE_887x_POSIO_ENABLE | ITE_887x_POSIO_SPEED |
869 ITE_887x_POSIO_IOSIZE_8 | ioport);
870
871 /* write the ioport to the UARTBAR */
872 pci_read_config_dword(dev, ITE_887x_UARTBAR, &uartbar);
873 uartbar &= ~(0xffff << (16 * i)); /* clear half the reg */
874 uartbar |= (ioport << (16 * i)); /* set the ioport */
875 pci_write_config_dword(dev, ITE_887x_UARTBAR, uartbar);
876
877 /* get current config */
878 pci_read_config_dword(dev, ITE_887x_MISCR, &miscr);
879 /* disable interrupts (UARTx_Routing[3:0]) */
880 miscr &= ~(0xf << (12 - 4 * i));
881 /* activate the UART (UARTx_En) */
882 miscr |= 1 << (23 - i);
883 /* write new config with activated UART */
884 pci_write_config_dword(dev, ITE_887x_MISCR, miscr);
885 }
886
887 if (ret <= 0) {
888 /* the device has no UARTs if we get here */
889 release_region(iobase->start, ITE_887x_IOSIZE);
890 }
891
892 return ret;
893}
894
895static void __devexit pci_ite887x_exit(struct pci_dev *dev)
896{
897 u32 ioport;
898 /* the ioport is bit 0-15 in POSIO0R */
899 pci_read_config_dword(dev, ITE_887x_POSIO0, &ioport);
900 ioport &= 0xffff;
901 release_region(ioport, ITE_887x_IOSIZE);
902}
903
904/*
905 * Oxford Semiconductor Inc.
906 * Check that device is part of the Tornado range of devices, then determine
907 * the number of ports available on the device.
908 */
909static int pci_oxsemi_tornado_init(struct pci_dev *dev)
910{
911 u8 __iomem *p;
912 unsigned long deviceID;
913 unsigned int number_uarts = 0;
914
915 /* OxSemi Tornado devices are all 0xCxxx */
916 if (dev->vendor == PCI_VENDOR_ID_OXSEMI &&
917 (dev->device & 0xF000) != 0xC000)
918 return 0;
919
920 p = pci_iomap(dev, 0, 5);
921 if (p == NULL)
922 return -ENOMEM;
923
924 deviceID = ioread32(p);
925 /* Tornado device */
926 if (deviceID == 0x07000200) {
927 number_uarts = ioread8(p + 4);
928 printk(KERN_DEBUG
929 "%d ports detected on Oxford PCI Express device\n",
930 number_uarts);
931 }
932 pci_iounmap(dev, p);
933 return number_uarts;
934}
935
936static int
937pci_default_setup(struct serial_private *priv,
938 const struct pciserial_board *board,
939 struct uart_port *port, int idx)
940{
941 unsigned int bar, offset = board->first_offset, maxnr;
942
943 bar = FL_GET_BASE(board->flags);
944 if (board->flags & FL_BASE_BARS)
945 bar += idx;
946 else
947 offset += idx * board->uart_offset;
948
949 maxnr = (pci_resource_len(priv->dev, bar) - board->first_offset) >>
950 (board->reg_shift + 3);
951
952 if (board->flags & FL_REGION_SZ_CAP && idx >= maxnr)
953 return 1;
954
955 return setup_port(priv, port, bar, offset, board->reg_shift);
956}
957
958static int
959ce4100_serial_setup(struct serial_private *priv,
960 const struct pciserial_board *board,
961 struct uart_port *port, int idx)
962{
963 int ret;
964
965 ret = setup_port(priv, port, 0, 0, board->reg_shift);
966 port->iotype = UPIO_MEM32;
967 port->type = PORT_XSCALE;
968 port->flags = (port->flags | UPF_FIXED_PORT | UPF_FIXED_TYPE);
969 port->regshift = 2;
970
971 return ret;
972}
973
974static int
975pci_omegapci_setup(struct serial_private *priv,
976 const struct pciserial_board *board,
977 struct uart_port *port, int idx)
978{
979 return setup_port(priv, port, 2, idx * 8, 0);
980}
981
982static int skip_tx_en_setup(struct serial_private *priv,
983 const struct pciserial_board *board,
984 struct uart_port *port, int idx)
985{
986 port->flags |= UPF_NO_TXEN_TEST;
987 printk(KERN_DEBUG "serial8250: skipping TxEn test for device "
988 "[%04x:%04x] subsystem [%04x:%04x]\n",
989 priv->dev->vendor,
990 priv->dev->device,
991 priv->dev->subsystem_vendor,
992 priv->dev->subsystem_device);
993
994 return pci_default_setup(priv, board, port, idx);
995}
996
997static int pci_eg20t_init(struct pci_dev *dev)
998{
999#if defined(CONFIG_SERIAL_PCH_UART) || defined(CONFIG_SERIAL_PCH_UART_MODULE)
1000 return -ENODEV;
1001#else
1002 return 0;
1003#endif
1004}
1005
1006/* This should be in linux/pci_ids.h */
1007#define PCI_VENDOR_ID_SBSMODULARIO 0x124B
1008#define PCI_SUBVENDOR_ID_SBSMODULARIO 0x124B
1009#define PCI_DEVICE_ID_OCTPRO 0x0001
1010#define PCI_SUBDEVICE_ID_OCTPRO232 0x0108
1011#define PCI_SUBDEVICE_ID_OCTPRO422 0x0208
1012#define PCI_SUBDEVICE_ID_POCTAL232 0x0308
1013#define PCI_SUBDEVICE_ID_POCTAL422 0x0408
1014#define PCI_VENDOR_ID_ADVANTECH 0x13fe
1015#define PCI_DEVICE_ID_INTEL_CE4100_UART 0x2e66
1016#define PCI_DEVICE_ID_ADVANTECH_PCI3620 0x3620
1017#define PCI_DEVICE_ID_TITAN_200I 0x8028
1018#define PCI_DEVICE_ID_TITAN_400I 0x8048
1019#define PCI_DEVICE_ID_TITAN_800I 0x8088
1020#define PCI_DEVICE_ID_TITAN_800EH 0xA007
1021#define PCI_DEVICE_ID_TITAN_800EHB 0xA008
1022#define PCI_DEVICE_ID_TITAN_400EH 0xA009
1023#define PCI_DEVICE_ID_TITAN_100E 0xA010
1024#define PCI_DEVICE_ID_TITAN_200E 0xA012
1025#define PCI_DEVICE_ID_TITAN_400E 0xA013
1026#define PCI_DEVICE_ID_TITAN_800E 0xA014
1027#define PCI_DEVICE_ID_TITAN_200EI 0xA016
1028#define PCI_DEVICE_ID_TITAN_200EISI 0xA017
1029#define PCI_DEVICE_ID_OXSEMI_16PCI958 0x9538
1030#define PCIE_DEVICE_ID_NEO_2_OX_IBM 0x00F6
1031#define PCI_DEVICE_ID_PLX_CRONYX_OMEGA 0xc001
1032
1033/* Unknown vendors/cards - this should not be in linux/pci_ids.h */
1034#define PCI_SUBDEVICE_ID_UNKNOWN_0x1584 0x1584
1035
1036/*
1037 * Master list of serial port init/setup/exit quirks.
1038 * This does not describe the general nature of the port.
1039 * (ie, baud base, number and location of ports, etc)
1040 *
1041 * This list is ordered alphabetically by vendor then device.
1042 * Specific entries must come before more generic entries.
1043 */
1044static struct pci_serial_quirk pci_serial_quirks[] __refdata = {
1045 /*
1046 * ADDI-DATA GmbH communication cards <info@addi-data.com>
1047 */
1048 {
1049 .vendor = PCI_VENDOR_ID_ADDIDATA_OLD,
1050 .device = PCI_DEVICE_ID_ADDIDATA_APCI7800,
1051 .subvendor = PCI_ANY_ID,
1052 .subdevice = PCI_ANY_ID,
1053 .setup = addidata_apci7800_setup,
1054 },
1055 /*
1056 * AFAVLAB cards - these may be called via parport_serial
1057 * It is not clear whether this applies to all products.
1058 */
1059 {
1060 .vendor = PCI_VENDOR_ID_AFAVLAB,
1061 .device = PCI_ANY_ID,
1062 .subvendor = PCI_ANY_ID,
1063 .subdevice = PCI_ANY_ID,
1064 .setup = afavlab_setup,
1065 },
1066 /*
1067 * HP Diva
1068 */
1069 {
1070 .vendor = PCI_VENDOR_ID_HP,
1071 .device = PCI_DEVICE_ID_HP_DIVA,
1072 .subvendor = PCI_ANY_ID,
1073 .subdevice = PCI_ANY_ID,
1074 .init = pci_hp_diva_init,
1075 .setup = pci_hp_diva_setup,
1076 },
1077 /*
1078 * Intel
1079 */
1080 {
1081 .vendor = PCI_VENDOR_ID_INTEL,
1082 .device = PCI_DEVICE_ID_INTEL_80960_RP,
1083 .subvendor = 0xe4bf,
1084 .subdevice = PCI_ANY_ID,
1085 .init = pci_inteli960ni_init,
1086 .setup = pci_default_setup,
1087 },
1088 {
1089 .vendor = PCI_VENDOR_ID_INTEL,
1090 .device = PCI_DEVICE_ID_INTEL_8257X_SOL,
1091 .subvendor = PCI_ANY_ID,
1092 .subdevice = PCI_ANY_ID,
1093 .setup = skip_tx_en_setup,
1094 },
1095 {
1096 .vendor = PCI_VENDOR_ID_INTEL,
1097 .device = PCI_DEVICE_ID_INTEL_82573L_SOL,
1098 .subvendor = PCI_ANY_ID,
1099 .subdevice = PCI_ANY_ID,
1100 .setup = skip_tx_en_setup,
1101 },
1102 {
1103 .vendor = PCI_VENDOR_ID_INTEL,
1104 .device = PCI_DEVICE_ID_INTEL_82573E_SOL,
1105 .subvendor = PCI_ANY_ID,
1106 .subdevice = PCI_ANY_ID,
1107 .setup = skip_tx_en_setup,
1108 },
1109 {
1110 .vendor = PCI_VENDOR_ID_INTEL,
1111 .device = PCI_DEVICE_ID_INTEL_CE4100_UART,
1112 .subvendor = PCI_ANY_ID,
1113 .subdevice = PCI_ANY_ID,
1114 .setup = ce4100_serial_setup,
1115 },
1116 /*
1117 * ITE
1118 */
1119 {
1120 .vendor = PCI_VENDOR_ID_ITE,
1121 .device = PCI_DEVICE_ID_ITE_8872,
1122 .subvendor = PCI_ANY_ID,
1123 .subdevice = PCI_ANY_ID,
1124 .init = pci_ite887x_init,
1125 .setup = pci_default_setup,
1126 .exit = __devexit_p(pci_ite887x_exit),
1127 },
1128 /*
1129 * National Instruments
1130 */
1131 {
1132 .vendor = PCI_VENDOR_ID_NI,
1133 .device = PCI_DEVICE_ID_NI_PCI23216,
1134 .subvendor = PCI_ANY_ID,
1135 .subdevice = PCI_ANY_ID,
1136 .init = pci_ni8420_init,
1137 .setup = pci_default_setup,
1138 .exit = __devexit_p(pci_ni8420_exit),
1139 },
1140 {
1141 .vendor = PCI_VENDOR_ID_NI,
1142 .device = PCI_DEVICE_ID_NI_PCI2328,
1143 .subvendor = PCI_ANY_ID,
1144 .subdevice = PCI_ANY_ID,
1145 .init = pci_ni8420_init,
1146 .setup = pci_default_setup,
1147 .exit = __devexit_p(pci_ni8420_exit),
1148 },
1149 {
1150 .vendor = PCI_VENDOR_ID_NI,
1151 .device = PCI_DEVICE_ID_NI_PCI2324,
1152 .subvendor = PCI_ANY_ID,
1153 .subdevice = PCI_ANY_ID,
1154 .init = pci_ni8420_init,
1155 .setup = pci_default_setup,
1156 .exit = __devexit_p(pci_ni8420_exit),
1157 },
1158 {
1159 .vendor = PCI_VENDOR_ID_NI,
1160 .device = PCI_DEVICE_ID_NI_PCI2322,
1161 .subvendor = PCI_ANY_ID,
1162 .subdevice = PCI_ANY_ID,
1163 .init = pci_ni8420_init,
1164 .setup = pci_default_setup,
1165 .exit = __devexit_p(pci_ni8420_exit),
1166 },
1167 {
1168 .vendor = PCI_VENDOR_ID_NI,
1169 .device = PCI_DEVICE_ID_NI_PCI2324I,
1170 .subvendor = PCI_ANY_ID,
1171 .subdevice = PCI_ANY_ID,
1172 .init = pci_ni8420_init,
1173 .setup = pci_default_setup,
1174 .exit = __devexit_p(pci_ni8420_exit),
1175 },
1176 {
1177 .vendor = PCI_VENDOR_ID_NI,
1178 .device = PCI_DEVICE_ID_NI_PCI2322I,
1179 .subvendor = PCI_ANY_ID,
1180 .subdevice = PCI_ANY_ID,
1181 .init = pci_ni8420_init,
1182 .setup = pci_default_setup,
1183 .exit = __devexit_p(pci_ni8420_exit),
1184 },
1185 {
1186 .vendor = PCI_VENDOR_ID_NI,
1187 .device = PCI_DEVICE_ID_NI_PXI8420_23216,
1188 .subvendor = PCI_ANY_ID,
1189 .subdevice = PCI_ANY_ID,
1190 .init = pci_ni8420_init,
1191 .setup = pci_default_setup,
1192 .exit = __devexit_p(pci_ni8420_exit),
1193 },
1194 {
1195 .vendor = PCI_VENDOR_ID_NI,
1196 .device = PCI_DEVICE_ID_NI_PXI8420_2328,
1197 .subvendor = PCI_ANY_ID,
1198 .subdevice = PCI_ANY_ID,
1199 .init = pci_ni8420_init,
1200 .setup = pci_default_setup,
1201 .exit = __devexit_p(pci_ni8420_exit),
1202 },
1203 {
1204 .vendor = PCI_VENDOR_ID_NI,
1205 .device = PCI_DEVICE_ID_NI_PXI8420_2324,
1206 .subvendor = PCI_ANY_ID,
1207 .subdevice = PCI_ANY_ID,
1208 .init = pci_ni8420_init,
1209 .setup = pci_default_setup,
1210 .exit = __devexit_p(pci_ni8420_exit),
1211 },
1212 {
1213 .vendor = PCI_VENDOR_ID_NI,
1214 .device = PCI_DEVICE_ID_NI_PXI8420_2322,
1215 .subvendor = PCI_ANY_ID,
1216 .subdevice = PCI_ANY_ID,
1217 .init = pci_ni8420_init,
1218 .setup = pci_default_setup,
1219 .exit = __devexit_p(pci_ni8420_exit),
1220 },
1221 {
1222 .vendor = PCI_VENDOR_ID_NI,
1223 .device = PCI_DEVICE_ID_NI_PXI8422_2324,
1224 .subvendor = PCI_ANY_ID,
1225 .subdevice = PCI_ANY_ID,
1226 .init = pci_ni8420_init,
1227 .setup = pci_default_setup,
1228 .exit = __devexit_p(pci_ni8420_exit),
1229 },
1230 {
1231 .vendor = PCI_VENDOR_ID_NI,
1232 .device = PCI_DEVICE_ID_NI_PXI8422_2322,
1233 .subvendor = PCI_ANY_ID,
1234 .subdevice = PCI_ANY_ID,
1235 .init = pci_ni8420_init,
1236 .setup = pci_default_setup,
1237 .exit = __devexit_p(pci_ni8420_exit),
1238 },
1239 {
1240 .vendor = PCI_VENDOR_ID_NI,
1241 .device = PCI_ANY_ID,
1242 .subvendor = PCI_ANY_ID,
1243 .subdevice = PCI_ANY_ID,
1244 .init = pci_ni8430_init,
1245 .setup = pci_ni8430_setup,
1246 .exit = __devexit_p(pci_ni8430_exit),
1247 },
1248 /*
1249 * Panacom
1250 */
1251 {
1252 .vendor = PCI_VENDOR_ID_PANACOM,
1253 .device = PCI_DEVICE_ID_PANACOM_QUADMODEM,
1254 .subvendor = PCI_ANY_ID,
1255 .subdevice = PCI_ANY_ID,
1256 .init = pci_plx9050_init,
1257 .setup = pci_default_setup,
1258 .exit = __devexit_p(pci_plx9050_exit),
1259 },
1260 {
1261 .vendor = PCI_VENDOR_ID_PANACOM,
1262 .device = PCI_DEVICE_ID_PANACOM_DUALMODEM,
1263 .subvendor = PCI_ANY_ID,
1264 .subdevice = PCI_ANY_ID,
1265 .init = pci_plx9050_init,
1266 .setup = pci_default_setup,
1267 .exit = __devexit_p(pci_plx9050_exit),
1268 },
1269 /*
1270 * PLX
1271 */
1272 {
1273 .vendor = PCI_VENDOR_ID_PLX,
1274 .device = PCI_DEVICE_ID_PLX_9030,
1275 .subvendor = PCI_SUBVENDOR_ID_PERLE,
1276 .subdevice = PCI_ANY_ID,
1277 .setup = pci_default_setup,
1278 },
1279 {
1280 .vendor = PCI_VENDOR_ID_PLX,
1281 .device = PCI_DEVICE_ID_PLX_9050,
1282 .subvendor = PCI_SUBVENDOR_ID_EXSYS,
1283 .subdevice = PCI_SUBDEVICE_ID_EXSYS_4055,
1284 .init = pci_plx9050_init,
1285 .setup = pci_default_setup,
1286 .exit = __devexit_p(pci_plx9050_exit),
1287 },
1288 {
1289 .vendor = PCI_VENDOR_ID_PLX,
1290 .device = PCI_DEVICE_ID_PLX_9050,
1291 .subvendor = PCI_SUBVENDOR_ID_KEYSPAN,
1292 .subdevice = PCI_SUBDEVICE_ID_KEYSPAN_SX2,
1293 .init = pci_plx9050_init,
1294 .setup = pci_default_setup,
1295 .exit = __devexit_p(pci_plx9050_exit),
1296 },
1297 {
1298 .vendor = PCI_VENDOR_ID_PLX,
1299 .device = PCI_DEVICE_ID_PLX_9050,
1300 .subvendor = PCI_VENDOR_ID_PLX,
1301 .subdevice = PCI_SUBDEVICE_ID_UNKNOWN_0x1584,
1302 .init = pci_plx9050_init,
1303 .setup = pci_default_setup,
1304 .exit = __devexit_p(pci_plx9050_exit),
1305 },
1306 {
1307 .vendor = PCI_VENDOR_ID_PLX,
1308 .device = PCI_DEVICE_ID_PLX_ROMULUS,
1309 .subvendor = PCI_VENDOR_ID_PLX,
1310 .subdevice = PCI_DEVICE_ID_PLX_ROMULUS,
1311 .init = pci_plx9050_init,
1312 .setup = pci_default_setup,
1313 .exit = __devexit_p(pci_plx9050_exit),
1314 },
1315 /*
1316 * SBS Technologies, Inc., PMC-OCTALPRO 232
1317 */
1318 {
1319 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
1320 .device = PCI_DEVICE_ID_OCTPRO,
1321 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
1322 .subdevice = PCI_SUBDEVICE_ID_OCTPRO232,
1323 .init = sbs_init,
1324 .setup = sbs_setup,
1325 .exit = __devexit_p(sbs_exit),
1326 },
1327 /*
1328 * SBS Technologies, Inc., PMC-OCTALPRO 422
1329 */
1330 {
1331 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
1332 .device = PCI_DEVICE_ID_OCTPRO,
1333 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
1334 .subdevice = PCI_SUBDEVICE_ID_OCTPRO422,
1335 .init = sbs_init,
1336 .setup = sbs_setup,
1337 .exit = __devexit_p(sbs_exit),
1338 },
1339 /*
1340 * SBS Technologies, Inc., P-Octal 232
1341 */
1342 {
1343 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
1344 .device = PCI_DEVICE_ID_OCTPRO,
1345 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
1346 .subdevice = PCI_SUBDEVICE_ID_POCTAL232,
1347 .init = sbs_init,
1348 .setup = sbs_setup,
1349 .exit = __devexit_p(sbs_exit),
1350 },
1351 /*
1352 * SBS Technologies, Inc., P-Octal 422
1353 */
1354 {
1355 .vendor = PCI_VENDOR_ID_SBSMODULARIO,
1356 .device = PCI_DEVICE_ID_OCTPRO,
1357 .subvendor = PCI_SUBVENDOR_ID_SBSMODULARIO,
1358 .subdevice = PCI_SUBDEVICE_ID_POCTAL422,
1359 .init = sbs_init,
1360 .setup = sbs_setup,
1361 .exit = __devexit_p(sbs_exit),
1362 },
1363 /*
1364 * SIIG cards - these may be called via parport_serial
1365 */
1366 {
1367 .vendor = PCI_VENDOR_ID_SIIG,
1368 .device = PCI_ANY_ID,
1369 .subvendor = PCI_ANY_ID,
1370 .subdevice = PCI_ANY_ID,
1371 .init = pci_siig_init,
1372 .setup = pci_siig_setup,
1373 },
1374 /*
1375 * Titan cards
1376 */
1377 {
1378 .vendor = PCI_VENDOR_ID_TITAN,
1379 .device = PCI_DEVICE_ID_TITAN_400L,
1380 .subvendor = PCI_ANY_ID,
1381 .subdevice = PCI_ANY_ID,
1382 .setup = titan_400l_800l_setup,
1383 },
1384 {
1385 .vendor = PCI_VENDOR_ID_TITAN,
1386 .device = PCI_DEVICE_ID_TITAN_800L,
1387 .subvendor = PCI_ANY_ID,
1388 .subdevice = PCI_ANY_ID,
1389 .setup = titan_400l_800l_setup,
1390 },
1391 /*
1392 * Timedia cards
1393 */
1394 {
1395 .vendor = PCI_VENDOR_ID_TIMEDIA,
1396 .device = PCI_DEVICE_ID_TIMEDIA_1889,
1397 .subvendor = PCI_VENDOR_ID_TIMEDIA,
1398 .subdevice = PCI_ANY_ID,
1399 .init = pci_timedia_init,
1400 .setup = pci_timedia_setup,
1401 },
1402 {
1403 .vendor = PCI_VENDOR_ID_TIMEDIA,
1404 .device = PCI_ANY_ID,
1405 .subvendor = PCI_ANY_ID,
1406 .subdevice = PCI_ANY_ID,
1407 .setup = pci_timedia_setup,
1408 },
1409 /*
1410 * Xircom cards
1411 */
1412 {
1413 .vendor = PCI_VENDOR_ID_XIRCOM,
1414 .device = PCI_DEVICE_ID_XIRCOM_X3201_MDM,
1415 .subvendor = PCI_ANY_ID,
1416 .subdevice = PCI_ANY_ID,
1417 .init = pci_xircom_init,
1418 .setup = pci_default_setup,
1419 },
1420 /*
1421 * Netmos cards - these may be called via parport_serial
1422 */
1423 {
1424 .vendor = PCI_VENDOR_ID_NETMOS,
1425 .device = PCI_ANY_ID,
1426 .subvendor = PCI_ANY_ID,
1427 .subdevice = PCI_ANY_ID,
1428 .init = pci_netmos_init,
1429 .setup = pci_default_setup,
1430 },
1431 /*
1432 * For Oxford Semiconductor Tornado based devices
1433 */
1434 {
1435 .vendor = PCI_VENDOR_ID_OXSEMI,
1436 .device = PCI_ANY_ID,
1437 .subvendor = PCI_ANY_ID,
1438 .subdevice = PCI_ANY_ID,
1439 .init = pci_oxsemi_tornado_init,
1440 .setup = pci_default_setup,
1441 },
1442 {
1443 .vendor = PCI_VENDOR_ID_MAINPINE,
1444 .device = PCI_ANY_ID,
1445 .subvendor = PCI_ANY_ID,
1446 .subdevice = PCI_ANY_ID,
1447 .init = pci_oxsemi_tornado_init,
1448 .setup = pci_default_setup,
1449 },
1450 {
1451 .vendor = PCI_VENDOR_ID_DIGI,
1452 .device = PCIE_DEVICE_ID_NEO_2_OX_IBM,
1453 .subvendor = PCI_SUBVENDOR_ID_IBM,
1454 .subdevice = PCI_ANY_ID,
1455 .init = pci_oxsemi_tornado_init,
1456 .setup = pci_default_setup,
1457 },
1458 {
1459 .vendor = PCI_VENDOR_ID_INTEL,
1460 .device = 0x8811,
1461 .init = pci_eg20t_init,
1462 },
1463 {
1464 .vendor = PCI_VENDOR_ID_INTEL,
1465 .device = 0x8812,
1466 .init = pci_eg20t_init,
1467 },
1468 {
1469 .vendor = PCI_VENDOR_ID_INTEL,
1470 .device = 0x8813,
1471 .init = pci_eg20t_init,
1472 },
1473 {
1474 .vendor = PCI_VENDOR_ID_INTEL,
1475 .device = 0x8814,
1476 .init = pci_eg20t_init,
1477 },
1478 {
1479 .vendor = 0x10DB,
1480 .device = 0x8027,
1481 .init = pci_eg20t_init,
1482 },
1483 {
1484 .vendor = 0x10DB,
1485 .device = 0x8028,
1486 .init = pci_eg20t_init,
1487 },
1488 {
1489 .vendor = 0x10DB,
1490 .device = 0x8029,
1491 .init = pci_eg20t_init,
1492 },
1493 {
1494 .vendor = 0x10DB,
1495 .device = 0x800C,
1496 .init = pci_eg20t_init,
1497 },
1498 {
1499 .vendor = 0x10DB,
1500 .device = 0x800D,
1501 .init = pci_eg20t_init,
1502 },
1503 {
1504 .vendor = 0x10DB,
1505 .device = 0x800D,
1506 .init = pci_eg20t_init,
1507 },
1508 /*
1509 * Cronyx Omega PCI (PLX-chip based)
1510 */
1511 {
1512 .vendor = PCI_VENDOR_ID_PLX,
1513 .device = PCI_DEVICE_ID_PLX_CRONYX_OMEGA,
1514 .subvendor = PCI_ANY_ID,
1515 .subdevice = PCI_ANY_ID,
1516 .setup = pci_omegapci_setup,
1517 },
1518 /*
1519 * Default "match everything" terminator entry
1520 */
1521 {
1522 .vendor = PCI_ANY_ID,
1523 .device = PCI_ANY_ID,
1524 .subvendor = PCI_ANY_ID,
1525 .subdevice = PCI_ANY_ID,
1526 .setup = pci_default_setup,
1527 }
1528};
1529
1530static inline int quirk_id_matches(u32 quirk_id, u32 dev_id)
1531{
1532 return quirk_id == PCI_ANY_ID || quirk_id == dev_id;
1533}
1534
1535static struct pci_serial_quirk *find_quirk(struct pci_dev *dev)
1536{
1537 struct pci_serial_quirk *quirk;
1538
1539 for (quirk = pci_serial_quirks; ; quirk++)
1540 if (quirk_id_matches(quirk->vendor, dev->vendor) &&
1541 quirk_id_matches(quirk->device, dev->device) &&
1542 quirk_id_matches(quirk->subvendor, dev->subsystem_vendor) &&
1543 quirk_id_matches(quirk->subdevice, dev->subsystem_device))
1544 break;
1545 return quirk;
1546}
1547
1548static inline int get_pci_irq(struct pci_dev *dev,
1549 const struct pciserial_board *board)
1550{
1551 if (board->flags & FL_NOIRQ)
1552 return 0;
1553 else
1554 return dev->irq;
1555}
1556
1557/*
1558 * This is the configuration table for all of the PCI serial boards
1559 * which we support. It is directly indexed by the pci_board_num_t enum
1560 * value, which is encoded in the pci_device_id PCI probe table's
1561 * driver_data member.
1562 *
1563 * The makeup of these names are:
1564 * pbn_bn{_bt}_n_baud{_offsetinhex}
1565 *
1566 * bn = PCI BAR number
1567 * bt = Index using PCI BARs
1568 * n = number of serial ports
1569 * baud = baud rate
1570 * offsetinhex = offset for each sequential port (in hex)
1571 *
1572 * This table is sorted by (in order): bn, bt, baud, offsetindex, n.
1573 *
1574 * Please note: in theory if n = 1, _bt infix should make no difference.
1575 * ie, pbn_b0_1_115200 is the same as pbn_b0_bt_1_115200
1576 */
1577enum pci_board_num_t {
1578 pbn_default = 0,
1579
1580 pbn_b0_1_115200,
1581 pbn_b0_2_115200,
1582 pbn_b0_4_115200,
1583 pbn_b0_5_115200,
1584 pbn_b0_8_115200,
1585
1586 pbn_b0_1_921600,
1587 pbn_b0_2_921600,
1588 pbn_b0_4_921600,
1589
1590 pbn_b0_2_1130000,
1591
1592 pbn_b0_4_1152000,
1593
1594 pbn_b0_2_1843200,
1595 pbn_b0_4_1843200,
1596
1597 pbn_b0_2_1843200_200,
1598 pbn_b0_4_1843200_200,
1599 pbn_b0_8_1843200_200,
1600
1601 pbn_b0_1_4000000,
1602
1603 pbn_b0_bt_1_115200,
1604 pbn_b0_bt_2_115200,
1605 pbn_b0_bt_4_115200,
1606 pbn_b0_bt_8_115200,
1607
1608 pbn_b0_bt_1_460800,
1609 pbn_b0_bt_2_460800,
1610 pbn_b0_bt_4_460800,
1611
1612 pbn_b0_bt_1_921600,
1613 pbn_b0_bt_2_921600,
1614 pbn_b0_bt_4_921600,
1615 pbn_b0_bt_8_921600,
1616
1617 pbn_b1_1_115200,
1618 pbn_b1_2_115200,
1619 pbn_b1_4_115200,
1620 pbn_b1_8_115200,
1621 pbn_b1_16_115200,
1622
1623 pbn_b1_1_921600,
1624 pbn_b1_2_921600,
1625 pbn_b1_4_921600,
1626 pbn_b1_8_921600,
1627
1628 pbn_b1_2_1250000,
1629
1630 pbn_b1_bt_1_115200,
1631 pbn_b1_bt_2_115200,
1632 pbn_b1_bt_4_115200,
1633
1634 pbn_b1_bt_2_921600,
1635
1636 pbn_b1_1_1382400,
1637 pbn_b1_2_1382400,
1638 pbn_b1_4_1382400,
1639 pbn_b1_8_1382400,
1640
1641 pbn_b2_1_115200,
1642 pbn_b2_2_115200,
1643 pbn_b2_4_115200,
1644 pbn_b2_8_115200,
1645
1646 pbn_b2_1_460800,
1647 pbn_b2_4_460800,
1648 pbn_b2_8_460800,
1649 pbn_b2_16_460800,
1650
1651 pbn_b2_1_921600,
1652 pbn_b2_4_921600,
1653 pbn_b2_8_921600,
1654
1655 pbn_b2_8_1152000,
1656
1657 pbn_b2_bt_1_115200,
1658 pbn_b2_bt_2_115200,
1659 pbn_b2_bt_4_115200,
1660
1661 pbn_b2_bt_2_921600,
1662 pbn_b2_bt_4_921600,
1663
1664 pbn_b3_2_115200,
1665 pbn_b3_4_115200,
1666 pbn_b3_8_115200,
1667
1668 pbn_b4_bt_2_921600,
1669 pbn_b4_bt_4_921600,
1670 pbn_b4_bt_8_921600,
1671
1672 /*
1673 * Board-specific versions.
1674 */
1675 pbn_panacom,
1676 pbn_panacom2,
1677 pbn_panacom4,
1678 pbn_exsys_4055,
1679 pbn_plx_romulus,
1680 pbn_oxsemi,
1681 pbn_oxsemi_1_4000000,
1682 pbn_oxsemi_2_4000000,
1683 pbn_oxsemi_4_4000000,
1684 pbn_oxsemi_8_4000000,
1685 pbn_intel_i960,
1686 pbn_sgi_ioc3,
1687 pbn_computone_4,
1688 pbn_computone_6,
1689 pbn_computone_8,
1690 pbn_sbsxrsio,
1691 pbn_exar_XR17C152,
1692 pbn_exar_XR17C154,
1693 pbn_exar_XR17C158,
1694 pbn_exar_ibm_saturn,
1695 pbn_pasemi_1682M,
1696 pbn_ni8430_2,
1697 pbn_ni8430_4,
1698 pbn_ni8430_8,
1699 pbn_ni8430_16,
1700 pbn_ADDIDATA_PCIe_1_3906250,
1701 pbn_ADDIDATA_PCIe_2_3906250,
1702 pbn_ADDIDATA_PCIe_4_3906250,
1703 pbn_ADDIDATA_PCIe_8_3906250,
1704 pbn_ce4100_1_115200,
1705 pbn_omegapci,
1706};
1707
1708/*
1709 * uart_offset - the space between channels
1710 * reg_shift - describes how the UART registers are mapped
1711 * to PCI memory by the card.
1712 * For example IER register on SBS, Inc. PMC-OctPro is located at
1713 * offset 0x10 from the UART base, while UART_IER is defined as 1
1714 * in include/linux/serial_reg.h,
1715 * see first lines of serial_in() and serial_out() in 8250.c
1716*/
1717
1718static struct pciserial_board pci_boards[] __devinitdata = {
1719 [pbn_default] = {
1720 .flags = FL_BASE0,
1721 .num_ports = 1,
1722 .base_baud = 115200,
1723 .uart_offset = 8,
1724 },
1725 [pbn_b0_1_115200] = {
1726 .flags = FL_BASE0,
1727 .num_ports = 1,
1728 .base_baud = 115200,
1729 .uart_offset = 8,
1730 },
1731 [pbn_b0_2_115200] = {
1732 .flags = FL_BASE0,
1733 .num_ports = 2,
1734 .base_baud = 115200,
1735 .uart_offset = 8,
1736 },
1737 [pbn_b0_4_115200] = {
1738 .flags = FL_BASE0,
1739 .num_ports = 4,
1740 .base_baud = 115200,
1741 .uart_offset = 8,
1742 },
1743 [pbn_b0_5_115200] = {
1744 .flags = FL_BASE0,
1745 .num_ports = 5,
1746 .base_baud = 115200,
1747 .uart_offset = 8,
1748 },
1749 [pbn_b0_8_115200] = {
1750 .flags = FL_BASE0,
1751 .num_ports = 8,
1752 .base_baud = 115200,
1753 .uart_offset = 8,
1754 },
1755 [pbn_b0_1_921600] = {
1756 .flags = FL_BASE0,
1757 .num_ports = 1,
1758 .base_baud = 921600,
1759 .uart_offset = 8,
1760 },
1761 [pbn_b0_2_921600] = {
1762 .flags = FL_BASE0,
1763 .num_ports = 2,
1764 .base_baud = 921600,
1765 .uart_offset = 8,
1766 },
1767 [pbn_b0_4_921600] = {
1768 .flags = FL_BASE0,
1769 .num_ports = 4,
1770 .base_baud = 921600,
1771 .uart_offset = 8,
1772 },
1773
1774 [pbn_b0_2_1130000] = {
1775 .flags = FL_BASE0,
1776 .num_ports = 2,
1777 .base_baud = 1130000,
1778 .uart_offset = 8,
1779 },
1780
1781 [pbn_b0_4_1152000] = {
1782 .flags = FL_BASE0,
1783 .num_ports = 4,
1784 .base_baud = 1152000,
1785 .uart_offset = 8,
1786 },
1787
1788 [pbn_b0_2_1843200] = {
1789 .flags = FL_BASE0,
1790 .num_ports = 2,
1791 .base_baud = 1843200,
1792 .uart_offset = 8,
1793 },
1794 [pbn_b0_4_1843200] = {
1795 .flags = FL_BASE0,
1796 .num_ports = 4,
1797 .base_baud = 1843200,
1798 .uart_offset = 8,
1799 },
1800
1801 [pbn_b0_2_1843200_200] = {
1802 .flags = FL_BASE0,
1803 .num_ports = 2,
1804 .base_baud = 1843200,
1805 .uart_offset = 0x200,
1806 },
1807 [pbn_b0_4_1843200_200] = {
1808 .flags = FL_BASE0,
1809 .num_ports = 4,
1810 .base_baud = 1843200,
1811 .uart_offset = 0x200,
1812 },
1813 [pbn_b0_8_1843200_200] = {
1814 .flags = FL_BASE0,
1815 .num_ports = 8,
1816 .base_baud = 1843200,
1817 .uart_offset = 0x200,
1818 },
1819 [pbn_b0_1_4000000] = {
1820 .flags = FL_BASE0,
1821 .num_ports = 1,
1822 .base_baud = 4000000,
1823 .uart_offset = 8,
1824 },
1825
1826 [pbn_b0_bt_1_115200] = {
1827 .flags = FL_BASE0|FL_BASE_BARS,
1828 .num_ports = 1,
1829 .base_baud = 115200,
1830 .uart_offset = 8,
1831 },
1832 [pbn_b0_bt_2_115200] = {
1833 .flags = FL_BASE0|FL_BASE_BARS,
1834 .num_ports = 2,
1835 .base_baud = 115200,
1836 .uart_offset = 8,
1837 },
1838 [pbn_b0_bt_4_115200] = {
1839 .flags = FL_BASE0|FL_BASE_BARS,
1840 .num_ports = 4,
1841 .base_baud = 115200,
1842 .uart_offset = 8,
1843 },
1844 [pbn_b0_bt_8_115200] = {
1845 .flags = FL_BASE0|FL_BASE_BARS,
1846 .num_ports = 8,
1847 .base_baud = 115200,
1848 .uart_offset = 8,
1849 },
1850
1851 [pbn_b0_bt_1_460800] = {
1852 .flags = FL_BASE0|FL_BASE_BARS,
1853 .num_ports = 1,
1854 .base_baud = 460800,
1855 .uart_offset = 8,
1856 },
1857 [pbn_b0_bt_2_460800] = {
1858 .flags = FL_BASE0|FL_BASE_BARS,
1859 .num_ports = 2,
1860 .base_baud = 460800,
1861 .uart_offset = 8,
1862 },
1863 [pbn_b0_bt_4_460800] = {
1864 .flags = FL_BASE0|FL_BASE_BARS,
1865 .num_ports = 4,
1866 .base_baud = 460800,
1867 .uart_offset = 8,
1868 },
1869
1870 [pbn_b0_bt_1_921600] = {
1871 .flags = FL_BASE0|FL_BASE_BARS,
1872 .num_ports = 1,
1873 .base_baud = 921600,
1874 .uart_offset = 8,
1875 },
1876 [pbn_b0_bt_2_921600] = {
1877 .flags = FL_BASE0|FL_BASE_BARS,
1878 .num_ports = 2,
1879 .base_baud = 921600,
1880 .uart_offset = 8,
1881 },
1882 [pbn_b0_bt_4_921600] = {
1883 .flags = FL_BASE0|FL_BASE_BARS,
1884 .num_ports = 4,
1885 .base_baud = 921600,
1886 .uart_offset = 8,
1887 },
1888 [pbn_b0_bt_8_921600] = {
1889 .flags = FL_BASE0|FL_BASE_BARS,
1890 .num_ports = 8,
1891 .base_baud = 921600,
1892 .uart_offset = 8,
1893 },
1894
1895 [pbn_b1_1_115200] = {
1896 .flags = FL_BASE1,
1897 .num_ports = 1,
1898 .base_baud = 115200,
1899 .uart_offset = 8,
1900 },
1901 [pbn_b1_2_115200] = {
1902 .flags = FL_BASE1,
1903 .num_ports = 2,
1904 .base_baud = 115200,
1905 .uart_offset = 8,
1906 },
1907 [pbn_b1_4_115200] = {
1908 .flags = FL_BASE1,
1909 .num_ports = 4,
1910 .base_baud = 115200,
1911 .uart_offset = 8,
1912 },
1913 [pbn_b1_8_115200] = {
1914 .flags = FL_BASE1,
1915 .num_ports = 8,
1916 .base_baud = 115200,
1917 .uart_offset = 8,
1918 },
1919 [pbn_b1_16_115200] = {
1920 .flags = FL_BASE1,
1921 .num_ports = 16,
1922 .base_baud = 115200,
1923 .uart_offset = 8,
1924 },
1925
1926 [pbn_b1_1_921600] = {
1927 .flags = FL_BASE1,
1928 .num_ports = 1,
1929 .base_baud = 921600,
1930 .uart_offset = 8,
1931 },
1932 [pbn_b1_2_921600] = {
1933 .flags = FL_BASE1,
1934 .num_ports = 2,
1935 .base_baud = 921600,
1936 .uart_offset = 8,
1937 },
1938 [pbn_b1_4_921600] = {
1939 .flags = FL_BASE1,
1940 .num_ports = 4,
1941 .base_baud = 921600,
1942 .uart_offset = 8,
1943 },
1944 [pbn_b1_8_921600] = {
1945 .flags = FL_BASE1,
1946 .num_ports = 8,
1947 .base_baud = 921600,
1948 .uart_offset = 8,
1949 },
1950 [pbn_b1_2_1250000] = {
1951 .flags = FL_BASE1,
1952 .num_ports = 2,
1953 .base_baud = 1250000,
1954 .uart_offset = 8,
1955 },
1956
1957 [pbn_b1_bt_1_115200] = {
1958 .flags = FL_BASE1|FL_BASE_BARS,
1959 .num_ports = 1,
1960 .base_baud = 115200,
1961 .uart_offset = 8,
1962 },
1963 [pbn_b1_bt_2_115200] = {
1964 .flags = FL_BASE1|FL_BASE_BARS,
1965 .num_ports = 2,
1966 .base_baud = 115200,
1967 .uart_offset = 8,
1968 },
1969 [pbn_b1_bt_4_115200] = {
1970 .flags = FL_BASE1|FL_BASE_BARS,
1971 .num_ports = 4,
1972 .base_baud = 115200,
1973 .uart_offset = 8,
1974 },
1975
1976 [pbn_b1_bt_2_921600] = {
1977 .flags = FL_BASE1|FL_BASE_BARS,
1978 .num_ports = 2,
1979 .base_baud = 921600,
1980 .uart_offset = 8,
1981 },
1982
1983 [pbn_b1_1_1382400] = {
1984 .flags = FL_BASE1,
1985 .num_ports = 1,
1986 .base_baud = 1382400,
1987 .uart_offset = 8,
1988 },
1989 [pbn_b1_2_1382400] = {
1990 .flags = FL_BASE1,
1991 .num_ports = 2,
1992 .base_baud = 1382400,
1993 .uart_offset = 8,
1994 },
1995 [pbn_b1_4_1382400] = {
1996 .flags = FL_BASE1,
1997 .num_ports = 4,
1998 .base_baud = 1382400,
1999 .uart_offset = 8,
2000 },
2001 [pbn_b1_8_1382400] = {
2002 .flags = FL_BASE1,
2003 .num_ports = 8,
2004 .base_baud = 1382400,
2005 .uart_offset = 8,
2006 },
2007
2008 [pbn_b2_1_115200] = {
2009 .flags = FL_BASE2,
2010 .num_ports = 1,
2011 .base_baud = 115200,
2012 .uart_offset = 8,
2013 },
2014 [pbn_b2_2_115200] = {
2015 .flags = FL_BASE2,
2016 .num_ports = 2,
2017 .base_baud = 115200,
2018 .uart_offset = 8,
2019 },
2020 [pbn_b2_4_115200] = {
2021 .flags = FL_BASE2,
2022 .num_ports = 4,
2023 .base_baud = 115200,
2024 .uart_offset = 8,
2025 },
2026 [pbn_b2_8_115200] = {
2027 .flags = FL_BASE2,
2028 .num_ports = 8,
2029 .base_baud = 115200,
2030 .uart_offset = 8,
2031 },
2032
2033 [pbn_b2_1_460800] = {
2034 .flags = FL_BASE2,
2035 .num_ports = 1,
2036 .base_baud = 460800,
2037 .uart_offset = 8,
2038 },
2039 [pbn_b2_4_460800] = {
2040 .flags = FL_BASE2,
2041 .num_ports = 4,
2042 .base_baud = 460800,
2043 .uart_offset = 8,
2044 },
2045 [pbn_b2_8_460800] = {
2046 .flags = FL_BASE2,
2047 .num_ports = 8,
2048 .base_baud = 460800,
2049 .uart_offset = 8,
2050 },
2051 [pbn_b2_16_460800] = {
2052 .flags = FL_BASE2,
2053 .num_ports = 16,
2054 .base_baud = 460800,
2055 .uart_offset = 8,
2056 },
2057
2058 [pbn_b2_1_921600] = {
2059 .flags = FL_BASE2,
2060 .num_ports = 1,
2061 .base_baud = 921600,
2062 .uart_offset = 8,
2063 },
2064 [pbn_b2_4_921600] = {
2065 .flags = FL_BASE2,
2066 .num_ports = 4,
2067 .base_baud = 921600,
2068 .uart_offset = 8,
2069 },
2070 [pbn_b2_8_921600] = {
2071 .flags = FL_BASE2,
2072 .num_ports = 8,
2073 .base_baud = 921600,
2074 .uart_offset = 8,
2075 },
2076
2077 [pbn_b2_8_1152000] = {
2078 .flags = FL_BASE2,
2079 .num_ports = 8,
2080 .base_baud = 1152000,
2081 .uart_offset = 8,
2082 },
2083
2084 [pbn_b2_bt_1_115200] = {
2085 .flags = FL_BASE2|FL_BASE_BARS,
2086 .num_ports = 1,
2087 .base_baud = 115200,
2088 .uart_offset = 8,
2089 },
2090 [pbn_b2_bt_2_115200] = {
2091 .flags = FL_BASE2|FL_BASE_BARS,
2092 .num_ports = 2,
2093 .base_baud = 115200,
2094 .uart_offset = 8,
2095 },
2096 [pbn_b2_bt_4_115200] = {
2097 .flags = FL_BASE2|FL_BASE_BARS,
2098 .num_ports = 4,
2099 .base_baud = 115200,
2100 .uart_offset = 8,
2101 },
2102
2103 [pbn_b2_bt_2_921600] = {
2104 .flags = FL_BASE2|FL_BASE_BARS,
2105 .num_ports = 2,
2106 .base_baud = 921600,
2107 .uart_offset = 8,
2108 },
2109 [pbn_b2_bt_4_921600] = {
2110 .flags = FL_BASE2|FL_BASE_BARS,
2111 .num_ports = 4,
2112 .base_baud = 921600,
2113 .uart_offset = 8,
2114 },
2115
2116 [pbn_b3_2_115200] = {
2117 .flags = FL_BASE3,
2118 .num_ports = 2,
2119 .base_baud = 115200,
2120 .uart_offset = 8,
2121 },
2122 [pbn_b3_4_115200] = {
2123 .flags = FL_BASE3,
2124 .num_ports = 4,
2125 .base_baud = 115200,
2126 .uart_offset = 8,
2127 },
2128 [pbn_b3_8_115200] = {
2129 .flags = FL_BASE3,
2130 .num_ports = 8,
2131 .base_baud = 115200,
2132 .uart_offset = 8,
2133 },
2134
2135 [pbn_b4_bt_2_921600] = {
2136 .flags = FL_BASE4,
2137 .num_ports = 2,
2138 .base_baud = 921600,
2139 .uart_offset = 8,
2140 },
2141 [pbn_b4_bt_4_921600] = {
2142 .flags = FL_BASE4,
2143 .num_ports = 4,
2144 .base_baud = 921600,
2145 .uart_offset = 8,
2146 },
2147 [pbn_b4_bt_8_921600] = {
2148 .flags = FL_BASE4,
2149 .num_ports = 8,
2150 .base_baud = 921600,
2151 .uart_offset = 8,
2152 },
2153
2154 /*
2155 * Entries following this are board-specific.
2156 */
2157
2158 /*
2159 * Panacom - IOMEM
2160 */
2161 [pbn_panacom] = {
2162 .flags = FL_BASE2,
2163 .num_ports = 2,
2164 .base_baud = 921600,
2165 .uart_offset = 0x400,
2166 .reg_shift = 7,
2167 },
2168 [pbn_panacom2] = {
2169 .flags = FL_BASE2|FL_BASE_BARS,
2170 .num_ports = 2,
2171 .base_baud = 921600,
2172 .uart_offset = 0x400,
2173 .reg_shift = 7,
2174 },
2175 [pbn_panacom4] = {
2176 .flags = FL_BASE2|FL_BASE_BARS,
2177 .num_ports = 4,
2178 .base_baud = 921600,
2179 .uart_offset = 0x400,
2180 .reg_shift = 7,
2181 },
2182
2183 [pbn_exsys_4055] = {
2184 .flags = FL_BASE2,
2185 .num_ports = 4,
2186 .base_baud = 115200,
2187 .uart_offset = 8,
2188 },
2189
2190 /* I think this entry is broken - the first_offset looks wrong --rmk */
2191 [pbn_plx_romulus] = {
2192 .flags = FL_BASE2,
2193 .num_ports = 4,
2194 .base_baud = 921600,
2195 .uart_offset = 8 << 2,
2196 .reg_shift = 2,
2197 .first_offset = 0x03,
2198 },
2199
2200 /*
2201 * This board uses the size of PCI Base region 0 to
2202 * signal now many ports are available
2203 */
2204 [pbn_oxsemi] = {
2205 .flags = FL_BASE0|FL_REGION_SZ_CAP,
2206 .num_ports = 32,
2207 .base_baud = 115200,
2208 .uart_offset = 8,
2209 },
2210 [pbn_oxsemi_1_4000000] = {
2211 .flags = FL_BASE0,
2212 .num_ports = 1,
2213 .base_baud = 4000000,
2214 .uart_offset = 0x200,
2215 .first_offset = 0x1000,
2216 },
2217 [pbn_oxsemi_2_4000000] = {
2218 .flags = FL_BASE0,
2219 .num_ports = 2,
2220 .base_baud = 4000000,
2221 .uart_offset = 0x200,
2222 .first_offset = 0x1000,
2223 },
2224 [pbn_oxsemi_4_4000000] = {
2225 .flags = FL_BASE0,
2226 .num_ports = 4,
2227 .base_baud = 4000000,
2228 .uart_offset = 0x200,
2229 .first_offset = 0x1000,
2230 },
2231 [pbn_oxsemi_8_4000000] = {
2232 .flags = FL_BASE0,
2233 .num_ports = 8,
2234 .base_baud = 4000000,
2235 .uart_offset = 0x200,
2236 .first_offset = 0x1000,
2237 },
2238
2239
2240 /*
2241 * EKF addition for i960 Boards form EKF with serial port.
2242 * Max 256 ports.
2243 */
2244 [pbn_intel_i960] = {
2245 .flags = FL_BASE0,
2246 .num_ports = 32,
2247 .base_baud = 921600,
2248 .uart_offset = 8 << 2,
2249 .reg_shift = 2,
2250 .first_offset = 0x10000,
2251 },
2252 [pbn_sgi_ioc3] = {
2253 .flags = FL_BASE0|FL_NOIRQ,
2254 .num_ports = 1,
2255 .base_baud = 458333,
2256 .uart_offset = 8,
2257 .reg_shift = 0,
2258 .first_offset = 0x20178,
2259 },
2260
2261 /*
2262 * Computone - uses IOMEM.
2263 */
2264 [pbn_computone_4] = {
2265 .flags = FL_BASE0,
2266 .num_ports = 4,
2267 .base_baud = 921600,
2268 .uart_offset = 0x40,
2269 .reg_shift = 2,
2270 .first_offset = 0x200,
2271 },
2272 [pbn_computone_6] = {
2273 .flags = FL_BASE0,
2274 .num_ports = 6,
2275 .base_baud = 921600,
2276 .uart_offset = 0x40,
2277 .reg_shift = 2,
2278 .first_offset = 0x200,
2279 },
2280 [pbn_computone_8] = {
2281 .flags = FL_BASE0,
2282 .num_ports = 8,
2283 .base_baud = 921600,
2284 .uart_offset = 0x40,
2285 .reg_shift = 2,
2286 .first_offset = 0x200,
2287 },
2288 [pbn_sbsxrsio] = {
2289 .flags = FL_BASE0,
2290 .num_ports = 8,
2291 .base_baud = 460800,
2292 .uart_offset = 256,
2293 .reg_shift = 4,
2294 },
2295 /*
2296 * Exar Corp. XR17C15[248] Dual/Quad/Octal UART
2297 * Only basic 16550A support.
2298 * XR17C15[24] are not tested, but they should work.
2299 */
2300 [pbn_exar_XR17C152] = {
2301 .flags = FL_BASE0,
2302 .num_ports = 2,
2303 .base_baud = 921600,
2304 .uart_offset = 0x200,
2305 },
2306 [pbn_exar_XR17C154] = {
2307 .flags = FL_BASE0,
2308 .num_ports = 4,
2309 .base_baud = 921600,
2310 .uart_offset = 0x200,
2311 },
2312 [pbn_exar_XR17C158] = {
2313 .flags = FL_BASE0,
2314 .num_ports = 8,
2315 .base_baud = 921600,
2316 .uart_offset = 0x200,
2317 },
2318 [pbn_exar_ibm_saturn] = {
2319 .flags = FL_BASE0,
2320 .num_ports = 1,
2321 .base_baud = 921600,
2322 .uart_offset = 0x200,
2323 },
2324
2325 /*
2326 * PA Semi PWRficient PA6T-1682M on-chip UART
2327 */
2328 [pbn_pasemi_1682M] = {
2329 .flags = FL_BASE0,
2330 .num_ports = 1,
2331 .base_baud = 8333333,
2332 },
2333 /*
2334 * National Instruments 843x
2335 */
2336 [pbn_ni8430_16] = {
2337 .flags = FL_BASE0,
2338 .num_ports = 16,
2339 .base_baud = 3686400,
2340 .uart_offset = 0x10,
2341 .first_offset = 0x800,
2342 },
2343 [pbn_ni8430_8] = {
2344 .flags = FL_BASE0,
2345 .num_ports = 8,
2346 .base_baud = 3686400,
2347 .uart_offset = 0x10,
2348 .first_offset = 0x800,
2349 },
2350 [pbn_ni8430_4] = {
2351 .flags = FL_BASE0,
2352 .num_ports = 4,
2353 .base_baud = 3686400,
2354 .uart_offset = 0x10,
2355 .first_offset = 0x800,
2356 },
2357 [pbn_ni8430_2] = {
2358 .flags = FL_BASE0,
2359 .num_ports = 2,
2360 .base_baud = 3686400,
2361 .uart_offset = 0x10,
2362 .first_offset = 0x800,
2363 },
2364 /*
2365 * ADDI-DATA GmbH PCI-Express communication cards <info@addi-data.com>
2366 */
2367 [pbn_ADDIDATA_PCIe_1_3906250] = {
2368 .flags = FL_BASE0,
2369 .num_ports = 1,
2370 .base_baud = 3906250,
2371 .uart_offset = 0x200,
2372 .first_offset = 0x1000,
2373 },
2374 [pbn_ADDIDATA_PCIe_2_3906250] = {
2375 .flags = FL_BASE0,
2376 .num_ports = 2,
2377 .base_baud = 3906250,
2378 .uart_offset = 0x200,
2379 .first_offset = 0x1000,
2380 },
2381 [pbn_ADDIDATA_PCIe_4_3906250] = {
2382 .flags = FL_BASE0,
2383 .num_ports = 4,
2384 .base_baud = 3906250,
2385 .uart_offset = 0x200,
2386 .first_offset = 0x1000,
2387 },
2388 [pbn_ADDIDATA_PCIe_8_3906250] = {
2389 .flags = FL_BASE0,
2390 .num_ports = 8,
2391 .base_baud = 3906250,
2392 .uart_offset = 0x200,
2393 .first_offset = 0x1000,
2394 },
2395 [pbn_ce4100_1_115200] = {
2396 .flags = FL_BASE0,
2397 .num_ports = 1,
2398 .base_baud = 921600,
2399 .reg_shift = 2,
2400 },
2401 [pbn_omegapci] = {
2402 .flags = FL_BASE0,
2403 .num_ports = 8,
2404 .base_baud = 115200,
2405 .uart_offset = 0x200,
2406 },
2407};
2408
2409static const struct pci_device_id softmodem_blacklist[] = {
2410 { PCI_VDEVICE(AL, 0x5457), }, /* ALi Corporation M5457 AC'97 Modem */
2411 { PCI_VDEVICE(MOTOROLA, 0x3052), }, /* Motorola Si3052-based modem */
2412 { PCI_DEVICE(0x1543, 0x3052), }, /* Si3052-based modem, default IDs */
2413};
2414
2415/*
2416 * Given a complete unknown PCI device, try to use some heuristics to
2417 * guess what the configuration might be, based on the pitiful PCI
2418 * serial specs. Returns 0 on success, 1 on failure.
2419 */
2420static int __devinit
2421serial_pci_guess_board(struct pci_dev *dev, struct pciserial_board *board)
2422{
2423 const struct pci_device_id *blacklist;
2424 int num_iomem, num_port, first_port = -1, i;
2425
2426 /*
2427 * If it is not a communications device or the programming
2428 * interface is greater than 6, give up.
2429 *
2430 * (Should we try to make guesses for multiport serial devices
2431 * later?)
2432 */
2433 if ((((dev->class >> 8) != PCI_CLASS_COMMUNICATION_SERIAL) &&
2434 ((dev->class >> 8) != PCI_CLASS_COMMUNICATION_MODEM)) ||
2435 (dev->class & 0xff) > 6)
2436 return -ENODEV;
2437
2438 /*
2439 * Do not access blacklisted devices that are known not to
2440 * feature serial ports.
2441 */
2442 for (blacklist = softmodem_blacklist;
2443 blacklist < softmodem_blacklist + ARRAY_SIZE(softmodem_blacklist);
2444 blacklist++) {
2445 if (dev->vendor == blacklist->vendor &&
2446 dev->device == blacklist->device)
2447 return -ENODEV;
2448 }
2449
2450 num_iomem = num_port = 0;
2451 for (i = 0; i < PCI_NUM_BAR_RESOURCES; i++) {
2452 if (pci_resource_flags(dev, i) & IORESOURCE_IO) {
2453 num_port++;
2454 if (first_port == -1)
2455 first_port = i;
2456 }
2457 if (pci_resource_flags(dev, i) & IORESOURCE_MEM)
2458 num_iomem++;
2459 }
2460
2461 /*
2462 * If there is 1 or 0 iomem regions, and exactly one port,
2463 * use it. We guess the number of ports based on the IO
2464 * region size.
2465 */
2466 if (num_iomem <= 1 && num_port == 1) {
2467 board->flags = first_port;
2468 board->num_ports = pci_resource_len(dev, first_port) / 8;
2469 return 0;
2470 }
2471
2472 /*
2473 * Now guess if we've got a board which indexes by BARs.
2474 * Each IO BAR should be 8 bytes, and they should follow
2475 * consecutively.
2476 */
2477 first_port = -1;
2478 num_port = 0;
2479 for (i = 0; i < PCI_NUM_BAR_RESOURCES; i++) {
2480 if (pci_resource_flags(dev, i) & IORESOURCE_IO &&
2481 pci_resource_len(dev, i) == 8 &&
2482 (first_port == -1 || (first_port + num_port) == i)) {
2483 num_port++;
2484 if (first_port == -1)
2485 first_port = i;
2486 }
2487 }
2488
2489 if (num_port > 1) {
2490 board->flags = first_port | FL_BASE_BARS;
2491 board->num_ports = num_port;
2492 return 0;
2493 }
2494
2495 return -ENODEV;
2496}
2497
2498static inline int
2499serial_pci_matches(const struct pciserial_board *board,
2500 const struct pciserial_board *guessed)
2501{
2502 return
2503 board->num_ports == guessed->num_ports &&
2504 board->base_baud == guessed->base_baud &&
2505 board->uart_offset == guessed->uart_offset &&
2506 board->reg_shift == guessed->reg_shift &&
2507 board->first_offset == guessed->first_offset;
2508}
2509
2510struct serial_private *
2511pciserial_init_ports(struct pci_dev *dev, const struct pciserial_board *board)
2512{
2513 struct uart_port serial_port;
2514 struct serial_private *priv;
2515 struct pci_serial_quirk *quirk;
2516 int rc, nr_ports, i;
2517
2518 nr_ports = board->num_ports;
2519
2520 /*
2521 * Find an init and setup quirks.
2522 */
2523 quirk = find_quirk(dev);
2524
2525 /*
2526 * Run the new-style initialization function.
2527 * The initialization function returns:
2528 * <0 - error
2529 * 0 - use board->num_ports
2530 * >0 - number of ports
2531 */
2532 if (quirk->init) {
2533 rc = quirk->init(dev);
2534 if (rc < 0) {
2535 priv = ERR_PTR(rc);
2536 goto err_out;
2537 }
2538 if (rc)
2539 nr_ports = rc;
2540 }
2541
2542 priv = kzalloc(sizeof(struct serial_private) +
2543 sizeof(unsigned int) * nr_ports,
2544 GFP_KERNEL);
2545 if (!priv) {
2546 priv = ERR_PTR(-ENOMEM);
2547 goto err_deinit;
2548 }
2549
2550 priv->dev = dev;
2551 priv->quirk = quirk;
2552
2553 memset(&serial_port, 0, sizeof(struct uart_port));
2554 serial_port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
2555 serial_port.uartclk = board->base_baud * 16;
2556 serial_port.irq = get_pci_irq(dev, board);
2557 serial_port.dev = &dev->dev;
2558
2559 for (i = 0; i < nr_ports; i++) {
2560 if (quirk->setup(priv, board, &serial_port, i))
2561 break;
2562
2563#ifdef SERIAL_DEBUG_PCI
2564 printk(KERN_DEBUG "Setup PCI port: port %lx, irq %d, type %d\n",
2565 serial_port.iobase, serial_port.irq, serial_port.iotype);
2566#endif
2567
2568 priv->line[i] = serial8250_register_port(&serial_port);
2569 if (priv->line[i] < 0) {
2570 printk(KERN_WARNING "Couldn't register serial port %s: %d\n", pci_name(dev), priv->line[i]);
2571 break;
2572 }
2573 }
2574 priv->nr = i;
2575 return priv;
2576
2577err_deinit:
2578 if (quirk->exit)
2579 quirk->exit(dev);
2580err_out:
2581 return priv;
2582}
2583EXPORT_SYMBOL_GPL(pciserial_init_ports);
2584
2585void pciserial_remove_ports(struct serial_private *priv)
2586{
2587 struct pci_serial_quirk *quirk;
2588 int i;
2589
2590 for (i = 0; i < priv->nr; i++)
2591 serial8250_unregister_port(priv->line[i]);
2592
2593 for (i = 0; i < PCI_NUM_BAR_RESOURCES; i++) {
2594 if (priv->remapped_bar[i])
2595 iounmap(priv->remapped_bar[i]);
2596 priv->remapped_bar[i] = NULL;
2597 }
2598
2599 /*
2600 * Find the exit quirks.
2601 */
2602 quirk = find_quirk(priv->dev);
2603 if (quirk->exit)
2604 quirk->exit(priv->dev);
2605
2606 kfree(priv);
2607}
2608EXPORT_SYMBOL_GPL(pciserial_remove_ports);
2609
2610void pciserial_suspend_ports(struct serial_private *priv)
2611{
2612 int i;
2613
2614 for (i = 0; i < priv->nr; i++)
2615 if (priv->line[i] >= 0)
2616 serial8250_suspend_port(priv->line[i]);
2617}
2618EXPORT_SYMBOL_GPL(pciserial_suspend_ports);
2619
2620void pciserial_resume_ports(struct serial_private *priv)
2621{
2622 int i;
2623
2624 /*
2625 * Ensure that the board is correctly configured.
2626 */
2627 if (priv->quirk->init)
2628 priv->quirk->init(priv->dev);
2629
2630 for (i = 0; i < priv->nr; i++)
2631 if (priv->line[i] >= 0)
2632 serial8250_resume_port(priv->line[i]);
2633}
2634EXPORT_SYMBOL_GPL(pciserial_resume_ports);
2635
2636/*
2637 * Probe one serial board. Unfortunately, there is no rhyme nor reason
2638 * to the arrangement of serial ports on a PCI card.
2639 */
2640static int __devinit
2641pciserial_init_one(struct pci_dev *dev, const struct pci_device_id *ent)
2642{
2643 struct serial_private *priv;
2644 const struct pciserial_board *board;
2645 struct pciserial_board tmp;
2646 int rc;
2647
2648 if (ent->driver_data >= ARRAY_SIZE(pci_boards)) {
2649 printk(KERN_ERR "pci_init_one: invalid driver_data: %ld\n",
2650 ent->driver_data);
2651 return -EINVAL;
2652 }
2653
2654 board = &pci_boards[ent->driver_data];
2655
2656 rc = pci_enable_device(dev);
2657 if (rc)
2658 return rc;
2659
2660 if (ent->driver_data == pbn_default) {
2661 /*
2662 * Use a copy of the pci_board entry for this;
2663 * avoid changing entries in the table.
2664 */
2665 memcpy(&tmp, board, sizeof(struct pciserial_board));
2666 board = &tmp;
2667
2668 /*
2669 * We matched one of our class entries. Try to
2670 * determine the parameters of this board.
2671 */
2672 rc = serial_pci_guess_board(dev, &tmp);
2673 if (rc)
2674 goto disable;
2675 } else {
2676 /*
2677 * We matched an explicit entry. If we are able to
2678 * detect this boards settings with our heuristic,
2679 * then we no longer need this entry.
2680 */
2681 memcpy(&tmp, &pci_boards[pbn_default],
2682 sizeof(struct pciserial_board));
2683 rc = serial_pci_guess_board(dev, &tmp);
2684 if (rc == 0 && serial_pci_matches(board, &tmp))
2685 moan_device("Redundant entry in serial pci_table.",
2686 dev);
2687 }
2688
2689 priv = pciserial_init_ports(dev, board);
2690 if (!IS_ERR(priv)) {
2691 pci_set_drvdata(dev, priv);
2692 return 0;
2693 }
2694
2695 rc = PTR_ERR(priv);
2696
2697 disable:
2698 pci_disable_device(dev);
2699 return rc;
2700}
2701
2702static void __devexit pciserial_remove_one(struct pci_dev *dev)
2703{
2704 struct serial_private *priv = pci_get_drvdata(dev);
2705
2706 pci_set_drvdata(dev, NULL);
2707
2708 pciserial_remove_ports(priv);
2709
2710 pci_disable_device(dev);
2711}
2712
2713#ifdef CONFIG_PM
2714static int pciserial_suspend_one(struct pci_dev *dev, pm_message_t state)
2715{
2716 struct serial_private *priv = pci_get_drvdata(dev);
2717
2718 if (priv)
2719 pciserial_suspend_ports(priv);
2720
2721 pci_save_state(dev);
2722 pci_set_power_state(dev, pci_choose_state(dev, state));
2723 return 0;
2724}
2725
2726static int pciserial_resume_one(struct pci_dev *dev)
2727{
2728 int err;
2729 struct serial_private *priv = pci_get_drvdata(dev);
2730
2731 pci_set_power_state(dev, PCI_D0);
2732 pci_restore_state(dev);
2733
2734 if (priv) {
2735 /*
2736 * The device may have been disabled. Re-enable it.
2737 */
2738 err = pci_enable_device(dev);
2739 /* FIXME: We cannot simply error out here */
2740 if (err)
2741 printk(KERN_ERR "pciserial: Unable to re-enable ports, trying to continue.\n");
2742 pciserial_resume_ports(priv);
2743 }
2744 return 0;
2745}
2746#endif
2747
2748static struct pci_device_id serial_pci_tbl[] = {
2749 /* Advantech use PCI_DEVICE_ID_ADVANTECH_PCI3620 (0x3620) as 'PCI_SUBVENDOR_ID' */
2750 { PCI_VENDOR_ID_ADVANTECH, PCI_DEVICE_ID_ADVANTECH_PCI3620,
2751 PCI_DEVICE_ID_ADVANTECH_PCI3620, 0x0001, 0, 0,
2752 pbn_b2_8_921600 },
2753 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
2754 PCI_SUBVENDOR_ID_CONNECT_TECH,
2755 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_232, 0, 0,
2756 pbn_b1_8_1382400 },
2757 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
2758 PCI_SUBVENDOR_ID_CONNECT_TECH,
2759 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_232, 0, 0,
2760 pbn_b1_4_1382400 },
2761 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
2762 PCI_SUBVENDOR_ID_CONNECT_TECH,
2763 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_232, 0, 0,
2764 pbn_b1_2_1382400 },
2765 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2766 PCI_SUBVENDOR_ID_CONNECT_TECH,
2767 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_232, 0, 0,
2768 pbn_b1_8_1382400 },
2769 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2770 PCI_SUBVENDOR_ID_CONNECT_TECH,
2771 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_232, 0, 0,
2772 pbn_b1_4_1382400 },
2773 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2774 PCI_SUBVENDOR_ID_CONNECT_TECH,
2775 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_232, 0, 0,
2776 pbn_b1_2_1382400 },
2777 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2778 PCI_SUBVENDOR_ID_CONNECT_TECH,
2779 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485, 0, 0,
2780 pbn_b1_8_921600 },
2781 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2782 PCI_SUBVENDOR_ID_CONNECT_TECH,
2783 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485_4_4, 0, 0,
2784 pbn_b1_8_921600 },
2785 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2786 PCI_SUBVENDOR_ID_CONNECT_TECH,
2787 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485, 0, 0,
2788 pbn_b1_4_921600 },
2789 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2790 PCI_SUBVENDOR_ID_CONNECT_TECH,
2791 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485_2_2, 0, 0,
2792 pbn_b1_4_921600 },
2793 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2794 PCI_SUBVENDOR_ID_CONNECT_TECH,
2795 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_485, 0, 0,
2796 pbn_b1_2_921600 },
2797 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2798 PCI_SUBVENDOR_ID_CONNECT_TECH,
2799 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485_2_6, 0, 0,
2800 pbn_b1_8_921600 },
2801 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2802 PCI_SUBVENDOR_ID_CONNECT_TECH,
2803 PCI_SUBDEVICE_ID_CONNECT_TECH_BH081101V1, 0, 0,
2804 pbn_b1_8_921600 },
2805 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2806 PCI_SUBVENDOR_ID_CONNECT_TECH,
2807 PCI_SUBDEVICE_ID_CONNECT_TECH_BH041101V1, 0, 0,
2808 pbn_b1_4_921600 },
2809 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V351,
2810 PCI_SUBVENDOR_ID_CONNECT_TECH,
2811 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_20MHZ, 0, 0,
2812 pbn_b1_2_1250000 },
2813 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
2814 PCI_SUBVENDOR_ID_CONNECT_TECH,
2815 PCI_SUBDEVICE_ID_CONNECT_TECH_TITAN_2, 0, 0,
2816 pbn_b0_2_1843200 },
2817 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
2818 PCI_SUBVENDOR_ID_CONNECT_TECH,
2819 PCI_SUBDEVICE_ID_CONNECT_TECH_TITAN_4, 0, 0,
2820 pbn_b0_4_1843200 },
2821 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
2822 PCI_VENDOR_ID_AFAVLAB,
2823 PCI_SUBDEVICE_ID_AFAVLAB_P061, 0, 0,
2824 pbn_b0_4_1152000 },
2825 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2826 PCI_SUBVENDOR_ID_CONNECT_TECH,
2827 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_232, 0, 0,
2828 pbn_b0_2_1843200_200 },
2829 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
2830 PCI_SUBVENDOR_ID_CONNECT_TECH,
2831 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_232, 0, 0,
2832 pbn_b0_4_1843200_200 },
2833 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
2834 PCI_SUBVENDOR_ID_CONNECT_TECH,
2835 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_232, 0, 0,
2836 pbn_b0_8_1843200_200 },
2837 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2838 PCI_SUBVENDOR_ID_CONNECT_TECH,
2839 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_1_1, 0, 0,
2840 pbn_b0_2_1843200_200 },
2841 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
2842 PCI_SUBVENDOR_ID_CONNECT_TECH,
2843 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_2, 0, 0,
2844 pbn_b0_4_1843200_200 },
2845 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
2846 PCI_SUBVENDOR_ID_CONNECT_TECH,
2847 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_4, 0, 0,
2848 pbn_b0_8_1843200_200 },
2849 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2850 PCI_SUBVENDOR_ID_CONNECT_TECH,
2851 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2, 0, 0,
2852 pbn_b0_2_1843200_200 },
2853 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
2854 PCI_SUBVENDOR_ID_CONNECT_TECH,
2855 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4, 0, 0,
2856 pbn_b0_4_1843200_200 },
2857 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
2858 PCI_SUBVENDOR_ID_CONNECT_TECH,
2859 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8, 0, 0,
2860 pbn_b0_8_1843200_200 },
2861 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2862 PCI_SUBVENDOR_ID_CONNECT_TECH,
2863 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_2_485, 0, 0,
2864 pbn_b0_2_1843200_200 },
2865 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
2866 PCI_SUBVENDOR_ID_CONNECT_TECH,
2867 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_4_485, 0, 0,
2868 pbn_b0_4_1843200_200 },
2869 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
2870 PCI_SUBVENDOR_ID_CONNECT_TECH,
2871 PCI_SUBDEVICE_ID_CONNECT_TECH_PCI_UART_8_485, 0, 0,
2872 pbn_b0_8_1843200_200 },
2873 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
2874 PCI_VENDOR_ID_IBM, PCI_SUBDEVICE_ID_IBM_SATURN_SERIAL_ONE_PORT,
2875 0, 0, pbn_exar_ibm_saturn },
2876
2877 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_U530,
2878 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2879 pbn_b2_bt_1_115200 },
2880 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM2,
2881 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2882 pbn_b2_bt_2_115200 },
2883 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM422,
2884 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2885 pbn_b2_bt_4_115200 },
2886 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM232,
2887 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2888 pbn_b2_bt_2_115200 },
2889 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_COMM4,
2890 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2891 pbn_b2_bt_4_115200 },
2892 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_COMM8,
2893 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2894 pbn_b2_8_115200 },
2895 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_7803,
2896 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2897 pbn_b2_8_460800 },
2898 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM8,
2899 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2900 pbn_b2_8_115200 },
2901
2902 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_GTEK_SERIAL2,
2903 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2904 pbn_b2_bt_2_115200 },
2905 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_SPCOM200,
2906 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2907 pbn_b2_bt_2_921600 },
2908 /*
2909 * VScom SPCOM800, from sl@s.pl
2910 */
2911 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_SPCOM800,
2912 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2913 pbn_b2_8_921600 },
2914 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_1077,
2915 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2916 pbn_b2_4_921600 },
2917 /* Unknown card - subdevice 0x1584 */
2918 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2919 PCI_VENDOR_ID_PLX,
2920 PCI_SUBDEVICE_ID_UNKNOWN_0x1584, 0, 0,
2921 pbn_b0_4_115200 },
2922 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2923 PCI_SUBVENDOR_ID_KEYSPAN,
2924 PCI_SUBDEVICE_ID_KEYSPAN_SX2, 0, 0,
2925 pbn_panacom },
2926 { PCI_VENDOR_ID_PANACOM, PCI_DEVICE_ID_PANACOM_QUADMODEM,
2927 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2928 pbn_panacom4 },
2929 { PCI_VENDOR_ID_PANACOM, PCI_DEVICE_ID_PANACOM_DUALMODEM,
2930 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2931 pbn_panacom2 },
2932 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
2933 PCI_VENDOR_ID_ESDGMBH,
2934 PCI_DEVICE_ID_ESDGMBH_CPCIASIO4, 0, 0,
2935 pbn_b2_4_115200 },
2936 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2937 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
2938 PCI_SUBDEVICE_ID_CHASE_PCIFAST4, 0, 0,
2939 pbn_b2_4_460800 },
2940 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2941 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
2942 PCI_SUBDEVICE_ID_CHASE_PCIFAST8, 0, 0,
2943 pbn_b2_8_460800 },
2944 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2945 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
2946 PCI_SUBDEVICE_ID_CHASE_PCIFAST16, 0, 0,
2947 pbn_b2_16_460800 },
2948 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2949 PCI_SUBVENDOR_ID_CHASE_PCIFAST,
2950 PCI_SUBDEVICE_ID_CHASE_PCIFAST16FMC, 0, 0,
2951 pbn_b2_16_460800 },
2952 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2953 PCI_SUBVENDOR_ID_CHASE_PCIRAS,
2954 PCI_SUBDEVICE_ID_CHASE_PCIRAS4, 0, 0,
2955 pbn_b2_4_460800 },
2956 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2957 PCI_SUBVENDOR_ID_CHASE_PCIRAS,
2958 PCI_SUBDEVICE_ID_CHASE_PCIRAS8, 0, 0,
2959 pbn_b2_8_460800 },
2960 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
2961 PCI_SUBVENDOR_ID_EXSYS,
2962 PCI_SUBDEVICE_ID_EXSYS_4055, 0, 0,
2963 pbn_exsys_4055 },
2964 /*
2965 * Megawolf Romulus PCI Serial Card, from Mike Hudson
2966 * (Exoray@isys.ca)
2967 */
2968 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_ROMULUS,
2969 0x10b5, 0x106a, 0, 0,
2970 pbn_plx_romulus },
2971 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_QSC100,
2972 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2973 pbn_b1_4_115200 },
2974 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_DSC100,
2975 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2976 pbn_b1_2_115200 },
2977 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_ESC100D,
2978 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2979 pbn_b1_8_115200 },
2980 { PCI_VENDOR_ID_QUATECH, PCI_DEVICE_ID_QUATECH_ESC100M,
2981 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2982 pbn_b1_8_115200 },
2983 { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_OXSEMI_16PCI954,
2984 PCI_VENDOR_ID_SPECIALIX, PCI_SUBDEVICE_ID_SPECIALIX_SPEED4,
2985 0, 0,
2986 pbn_b0_4_921600 },
2987 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
2988 PCI_SUBVENDOR_ID_SIIG, PCI_SUBDEVICE_ID_SIIG_QUARTET_SERIAL,
2989 0, 0,
2990 pbn_b0_4_1152000 },
2991 { PCI_VENDOR_ID_OXSEMI, 0x9505,
2992 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
2993 pbn_b0_bt_2_921600 },
2994
2995 /*
2996 * The below card is a little controversial since it is the
2997 * subject of a PCI vendor/device ID clash. (See
2998 * www.ussg.iu.edu/hypermail/linux/kernel/0303.1/0516.html).
2999 * For now just used the hex ID 0x950a.
3000 */
3001 { PCI_VENDOR_ID_OXSEMI, 0x950a,
3002 PCI_SUBVENDOR_ID_SIIG, PCI_SUBDEVICE_ID_SIIG_DUAL_SERIAL, 0, 0,
3003 pbn_b0_2_115200 },
3004 { PCI_VENDOR_ID_OXSEMI, 0x950a,
3005 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3006 pbn_b0_2_1130000 },
3007 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_C950,
3008 PCI_VENDOR_ID_OXSEMI, PCI_SUBDEVICE_ID_OXSEMI_C950, 0, 0,
3009 pbn_b0_1_921600 },
3010 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI954,
3011 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3012 pbn_b0_4_115200 },
3013 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI952,
3014 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3015 pbn_b0_bt_2_921600 },
3016 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI958,
3017 PCI_ANY_ID , PCI_ANY_ID, 0, 0,
3018 pbn_b2_8_1152000 },
3019
3020 /*
3021 * Oxford Semiconductor Inc. Tornado PCI express device range.
3022 */
3023 { PCI_VENDOR_ID_OXSEMI, 0xc101, /* OXPCIe952 1 Legacy UART */
3024 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3025 pbn_b0_1_4000000 },
3026 { PCI_VENDOR_ID_OXSEMI, 0xc105, /* OXPCIe952 1 Legacy UART */
3027 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3028 pbn_b0_1_4000000 },
3029 { PCI_VENDOR_ID_OXSEMI, 0xc11b, /* OXPCIe952 1 Native UART */
3030 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3031 pbn_oxsemi_1_4000000 },
3032 { PCI_VENDOR_ID_OXSEMI, 0xc11f, /* OXPCIe952 1 Native UART */
3033 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3034 pbn_oxsemi_1_4000000 },
3035 { PCI_VENDOR_ID_OXSEMI, 0xc120, /* OXPCIe952 1 Legacy UART */
3036 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3037 pbn_b0_1_4000000 },
3038 { PCI_VENDOR_ID_OXSEMI, 0xc124, /* OXPCIe952 1 Legacy UART */
3039 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3040 pbn_b0_1_4000000 },
3041 { PCI_VENDOR_ID_OXSEMI, 0xc138, /* OXPCIe952 1 Native UART */
3042 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3043 pbn_oxsemi_1_4000000 },
3044 { PCI_VENDOR_ID_OXSEMI, 0xc13d, /* OXPCIe952 1 Native UART */
3045 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3046 pbn_oxsemi_1_4000000 },
3047 { PCI_VENDOR_ID_OXSEMI, 0xc140, /* OXPCIe952 1 Legacy UART */
3048 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3049 pbn_b0_1_4000000 },
3050 { PCI_VENDOR_ID_OXSEMI, 0xc141, /* OXPCIe952 1 Legacy UART */
3051 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3052 pbn_b0_1_4000000 },
3053 { PCI_VENDOR_ID_OXSEMI, 0xc144, /* OXPCIe952 1 Legacy UART */
3054 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3055 pbn_b0_1_4000000 },
3056 { PCI_VENDOR_ID_OXSEMI, 0xc145, /* OXPCIe952 1 Legacy UART */
3057 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3058 pbn_b0_1_4000000 },
3059 { PCI_VENDOR_ID_OXSEMI, 0xc158, /* OXPCIe952 2 Native UART */
3060 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3061 pbn_oxsemi_2_4000000 },
3062 { PCI_VENDOR_ID_OXSEMI, 0xc15d, /* OXPCIe952 2 Native UART */
3063 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3064 pbn_oxsemi_2_4000000 },
3065 { PCI_VENDOR_ID_OXSEMI, 0xc208, /* OXPCIe954 4 Native UART */
3066 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3067 pbn_oxsemi_4_4000000 },
3068 { PCI_VENDOR_ID_OXSEMI, 0xc20d, /* OXPCIe954 4 Native UART */
3069 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3070 pbn_oxsemi_4_4000000 },
3071 { PCI_VENDOR_ID_OXSEMI, 0xc308, /* OXPCIe958 8 Native UART */
3072 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3073 pbn_oxsemi_8_4000000 },
3074 { PCI_VENDOR_ID_OXSEMI, 0xc30d, /* OXPCIe958 8 Native UART */
3075 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3076 pbn_oxsemi_8_4000000 },
3077 { PCI_VENDOR_ID_OXSEMI, 0xc40b, /* OXPCIe200 1 Native UART */
3078 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3079 pbn_oxsemi_1_4000000 },
3080 { PCI_VENDOR_ID_OXSEMI, 0xc40f, /* OXPCIe200 1 Native UART */
3081 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3082 pbn_oxsemi_1_4000000 },
3083 { PCI_VENDOR_ID_OXSEMI, 0xc41b, /* OXPCIe200 1 Native UART */
3084 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3085 pbn_oxsemi_1_4000000 },
3086 { PCI_VENDOR_ID_OXSEMI, 0xc41f, /* OXPCIe200 1 Native UART */
3087 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3088 pbn_oxsemi_1_4000000 },
3089 { PCI_VENDOR_ID_OXSEMI, 0xc42b, /* OXPCIe200 1 Native UART */
3090 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3091 pbn_oxsemi_1_4000000 },
3092 { PCI_VENDOR_ID_OXSEMI, 0xc42f, /* OXPCIe200 1 Native UART */
3093 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3094 pbn_oxsemi_1_4000000 },
3095 { PCI_VENDOR_ID_OXSEMI, 0xc43b, /* OXPCIe200 1 Native UART */
3096 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3097 pbn_oxsemi_1_4000000 },
3098 { PCI_VENDOR_ID_OXSEMI, 0xc43f, /* OXPCIe200 1 Native UART */
3099 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3100 pbn_oxsemi_1_4000000 },
3101 { PCI_VENDOR_ID_OXSEMI, 0xc44b, /* OXPCIe200 1 Native UART */
3102 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3103 pbn_oxsemi_1_4000000 },
3104 { PCI_VENDOR_ID_OXSEMI, 0xc44f, /* OXPCIe200 1 Native UART */
3105 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3106 pbn_oxsemi_1_4000000 },
3107 { PCI_VENDOR_ID_OXSEMI, 0xc45b, /* OXPCIe200 1 Native UART */
3108 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3109 pbn_oxsemi_1_4000000 },
3110 { PCI_VENDOR_ID_OXSEMI, 0xc45f, /* OXPCIe200 1 Native UART */
3111 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3112 pbn_oxsemi_1_4000000 },
3113 { PCI_VENDOR_ID_OXSEMI, 0xc46b, /* OXPCIe200 1 Native UART */
3114 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3115 pbn_oxsemi_1_4000000 },
3116 { PCI_VENDOR_ID_OXSEMI, 0xc46f, /* OXPCIe200 1 Native UART */
3117 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3118 pbn_oxsemi_1_4000000 },
3119 { PCI_VENDOR_ID_OXSEMI, 0xc47b, /* OXPCIe200 1 Native UART */
3120 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3121 pbn_oxsemi_1_4000000 },
3122 { PCI_VENDOR_ID_OXSEMI, 0xc47f, /* OXPCIe200 1 Native UART */
3123 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3124 pbn_oxsemi_1_4000000 },
3125 { PCI_VENDOR_ID_OXSEMI, 0xc48b, /* OXPCIe200 1 Native UART */
3126 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3127 pbn_oxsemi_1_4000000 },
3128 { PCI_VENDOR_ID_OXSEMI, 0xc48f, /* OXPCIe200 1 Native UART */
3129 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3130 pbn_oxsemi_1_4000000 },
3131 { PCI_VENDOR_ID_OXSEMI, 0xc49b, /* OXPCIe200 1 Native UART */
3132 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3133 pbn_oxsemi_1_4000000 },
3134 { PCI_VENDOR_ID_OXSEMI, 0xc49f, /* OXPCIe200 1 Native UART */
3135 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3136 pbn_oxsemi_1_4000000 },
3137 { PCI_VENDOR_ID_OXSEMI, 0xc4ab, /* OXPCIe200 1 Native UART */
3138 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3139 pbn_oxsemi_1_4000000 },
3140 { PCI_VENDOR_ID_OXSEMI, 0xc4af, /* OXPCIe200 1 Native UART */
3141 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3142 pbn_oxsemi_1_4000000 },
3143 { PCI_VENDOR_ID_OXSEMI, 0xc4bb, /* OXPCIe200 1 Native UART */
3144 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3145 pbn_oxsemi_1_4000000 },
3146 { PCI_VENDOR_ID_OXSEMI, 0xc4bf, /* OXPCIe200 1 Native UART */
3147 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3148 pbn_oxsemi_1_4000000 },
3149 { PCI_VENDOR_ID_OXSEMI, 0xc4cb, /* OXPCIe200 1 Native UART */
3150 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3151 pbn_oxsemi_1_4000000 },
3152 { PCI_VENDOR_ID_OXSEMI, 0xc4cf, /* OXPCIe200 1 Native UART */
3153 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3154 pbn_oxsemi_1_4000000 },
3155 /*
3156 * Mainpine Inc. IQ Express "Rev3" utilizing OxSemi Tornado
3157 */
3158 { PCI_VENDOR_ID_MAINPINE, 0x4000, /* IQ Express 1 Port V.34 Super-G3 Fax */
3159 PCI_VENDOR_ID_MAINPINE, 0x4001, 0, 0,
3160 pbn_oxsemi_1_4000000 },
3161 { PCI_VENDOR_ID_MAINPINE, 0x4000, /* IQ Express 2 Port V.34 Super-G3 Fax */
3162 PCI_VENDOR_ID_MAINPINE, 0x4002, 0, 0,
3163 pbn_oxsemi_2_4000000 },
3164 { PCI_VENDOR_ID_MAINPINE, 0x4000, /* IQ Express 4 Port V.34 Super-G3 Fax */
3165 PCI_VENDOR_ID_MAINPINE, 0x4004, 0, 0,
3166 pbn_oxsemi_4_4000000 },
3167 { PCI_VENDOR_ID_MAINPINE, 0x4000, /* IQ Express 8 Port V.34 Super-G3 Fax */
3168 PCI_VENDOR_ID_MAINPINE, 0x4008, 0, 0,
3169 pbn_oxsemi_8_4000000 },
3170
3171 /*
3172 * Digi/IBM PCIe 2-port Async EIA-232 Adapter utilizing OxSemi Tornado
3173 */
3174 { PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_2_OX_IBM,
3175 PCI_SUBVENDOR_ID_IBM, PCI_ANY_ID, 0, 0,
3176 pbn_oxsemi_2_4000000 },
3177
3178 /*
3179 * SBS Technologies, Inc. P-Octal and PMC-OCTPRO cards,
3180 * from skokodyn@yahoo.com
3181 */
3182 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
3183 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_OCTPRO232, 0, 0,
3184 pbn_sbsxrsio },
3185 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
3186 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_OCTPRO422, 0, 0,
3187 pbn_sbsxrsio },
3188 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
3189 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_POCTAL232, 0, 0,
3190 pbn_sbsxrsio },
3191 { PCI_VENDOR_ID_SBSMODULARIO, PCI_DEVICE_ID_OCTPRO,
3192 PCI_SUBVENDOR_ID_SBSMODULARIO, PCI_SUBDEVICE_ID_POCTAL422, 0, 0,
3193 pbn_sbsxrsio },
3194
3195 /*
3196 * Digitan DS560-558, from jimd@esoft.com
3197 */
3198 { PCI_VENDOR_ID_ATT, PCI_DEVICE_ID_ATT_VENUS_MODEM,
3199 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3200 pbn_b1_1_115200 },
3201
3202 /*
3203 * Titan Electronic cards
3204 * The 400L and 800L have a custom setup quirk.
3205 */
3206 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_100,
3207 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3208 pbn_b0_1_921600 },
3209 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200,
3210 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3211 pbn_b0_2_921600 },
3212 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400,
3213 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3214 pbn_b0_4_921600 },
3215 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800B,
3216 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3217 pbn_b0_4_921600 },
3218 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_100L,
3219 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3220 pbn_b1_1_921600 },
3221 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200L,
3222 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3223 pbn_b1_bt_2_921600 },
3224 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400L,
3225 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3226 pbn_b0_bt_4_921600 },
3227 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800L,
3228 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3229 pbn_b0_bt_8_921600 },
3230 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200I,
3231 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3232 pbn_b4_bt_2_921600 },
3233 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400I,
3234 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3235 pbn_b4_bt_4_921600 },
3236 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800I,
3237 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3238 pbn_b4_bt_8_921600 },
3239 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400EH,
3240 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3241 pbn_b0_4_921600 },
3242 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800EH,
3243 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3244 pbn_b0_4_921600 },
3245 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800EHB,
3246 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3247 pbn_b0_4_921600 },
3248 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_100E,
3249 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3250 pbn_oxsemi_1_4000000 },
3251 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200E,
3252 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3253 pbn_oxsemi_2_4000000 },
3254 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_400E,
3255 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3256 pbn_oxsemi_4_4000000 },
3257 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_800E,
3258 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3259 pbn_oxsemi_8_4000000 },
3260 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200EI,
3261 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3262 pbn_oxsemi_2_4000000 },
3263 { PCI_VENDOR_ID_TITAN, PCI_DEVICE_ID_TITAN_200EISI,
3264 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3265 pbn_oxsemi_2_4000000 },
3266
3267 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_10x_550,
3268 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3269 pbn_b2_1_460800 },
3270 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_10x_650,
3271 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3272 pbn_b2_1_460800 },
3273 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_10x_850,
3274 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3275 pbn_b2_1_460800 },
3276 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_10x_550,
3277 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3278 pbn_b2_bt_2_921600 },
3279 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_10x_650,
3280 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3281 pbn_b2_bt_2_921600 },
3282 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_10x_850,
3283 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3284 pbn_b2_bt_2_921600 },
3285 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_10x_550,
3286 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3287 pbn_b2_bt_4_921600 },
3288 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_10x_650,
3289 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3290 pbn_b2_bt_4_921600 },
3291 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_10x_850,
3292 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3293 pbn_b2_bt_4_921600 },
3294 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_20x_550,
3295 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3296 pbn_b0_1_921600 },
3297 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_20x_650,
3298 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3299 pbn_b0_1_921600 },
3300 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_1S_20x_850,
3301 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3302 pbn_b0_1_921600 },
3303 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_20x_550,
3304 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3305 pbn_b0_bt_2_921600 },
3306 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_20x_650,
3307 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3308 pbn_b0_bt_2_921600 },
3309 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_2S_20x_850,
3310 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3311 pbn_b0_bt_2_921600 },
3312 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_20x_550,
3313 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3314 pbn_b0_bt_4_921600 },
3315 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_20x_650,
3316 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3317 pbn_b0_bt_4_921600 },
3318 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_4S_20x_850,
3319 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3320 pbn_b0_bt_4_921600 },
3321 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_8S_20x_550,
3322 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3323 pbn_b0_bt_8_921600 },
3324 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_8S_20x_650,
3325 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3326 pbn_b0_bt_8_921600 },
3327 { PCI_VENDOR_ID_SIIG, PCI_DEVICE_ID_SIIG_8S_20x_850,
3328 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3329 pbn_b0_bt_8_921600 },
3330
3331 /*
3332 * Computone devices submitted by Doug McNash dmcnash@computone.com
3333 */
3334 { PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_PG,
3335 PCI_SUBVENDOR_ID_COMPUTONE, PCI_SUBDEVICE_ID_COMPUTONE_PG4,
3336 0, 0, pbn_computone_4 },
3337 { PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_PG,
3338 PCI_SUBVENDOR_ID_COMPUTONE, PCI_SUBDEVICE_ID_COMPUTONE_PG8,
3339 0, 0, pbn_computone_8 },
3340 { PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_PG,
3341 PCI_SUBVENDOR_ID_COMPUTONE, PCI_SUBDEVICE_ID_COMPUTONE_PG6,
3342 0, 0, pbn_computone_6 },
3343
3344 { PCI_VENDOR_ID_OXSEMI, PCI_DEVICE_ID_OXSEMI_16PCI95N,
3345 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3346 pbn_oxsemi },
3347 { PCI_VENDOR_ID_TIMEDIA, PCI_DEVICE_ID_TIMEDIA_1889,
3348 PCI_VENDOR_ID_TIMEDIA, PCI_ANY_ID, 0, 0,
3349 pbn_b0_bt_1_921600 },
3350
3351 /*
3352 * AFAVLAB serial card, from Harald Welte <laforge@gnumonks.org>
3353 */
3354 { PCI_VENDOR_ID_AFAVLAB, PCI_DEVICE_ID_AFAVLAB_P028,
3355 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3356 pbn_b0_bt_8_115200 },
3357 { PCI_VENDOR_ID_AFAVLAB, PCI_DEVICE_ID_AFAVLAB_P030,
3358 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3359 pbn_b0_bt_8_115200 },
3360
3361 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_DSERIAL,
3362 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3363 pbn_b0_bt_2_115200 },
3364 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATRO_A,
3365 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3366 pbn_b0_bt_2_115200 },
3367 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATRO_B,
3368 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3369 pbn_b0_bt_2_115200 },
3370 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATTRO_A,
3371 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3372 pbn_b0_bt_2_115200 },
3373 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUATTRO_B,
3374 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3375 pbn_b0_bt_2_115200 },
3376 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_OCTO_A,
3377 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3378 pbn_b0_bt_4_460800 },
3379 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_OCTO_B,
3380 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3381 pbn_b0_bt_4_460800 },
3382 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_PORT_PLUS,
3383 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3384 pbn_b0_bt_2_460800 },
3385 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUAD_A,
3386 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3387 pbn_b0_bt_2_460800 },
3388 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_QUAD_B,
3389 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3390 pbn_b0_bt_2_460800 },
3391 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_SSERIAL,
3392 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3393 pbn_b0_bt_1_115200 },
3394 { PCI_VENDOR_ID_LAVA, PCI_DEVICE_ID_LAVA_PORT_650,
3395 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3396 pbn_b0_bt_1_460800 },
3397
3398 /*
3399 * Korenix Jetcard F0/F1 cards (JC1204, JC1208, JC1404, JC1408).
3400 * Cards are identified by their subsystem vendor IDs, which
3401 * (in hex) match the model number.
3402 *
3403 * Note that JC140x are RS422/485 cards which require ox950
3404 * ACR = 0x10, and as such are not currently fully supported.
3405 */
3406 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF0,
3407 0x1204, 0x0004, 0, 0,
3408 pbn_b0_4_921600 },
3409 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF0,
3410 0x1208, 0x0004, 0, 0,
3411 pbn_b0_4_921600 },
3412/* { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF0,
3413 0x1402, 0x0002, 0, 0,
3414 pbn_b0_2_921600 }, */
3415/* { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF0,
3416 0x1404, 0x0004, 0, 0,
3417 pbn_b0_4_921600 }, */
3418 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF1,
3419 0x1208, 0x0004, 0, 0,
3420 pbn_b0_4_921600 },
3421
3422 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF2,
3423 0x1204, 0x0004, 0, 0,
3424 pbn_b0_4_921600 },
3425 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF2,
3426 0x1208, 0x0004, 0, 0,
3427 pbn_b0_4_921600 },
3428 { PCI_VENDOR_ID_KORENIX, PCI_DEVICE_ID_KORENIX_JETCARDF3,
3429 0x1208, 0x0004, 0, 0,
3430 pbn_b0_4_921600 },
3431 /*
3432 * Dell Remote Access Card 4 - Tim_T_Murphy@Dell.com
3433 */
3434 { PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_RAC4,
3435 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3436 pbn_b1_1_1382400 },
3437
3438 /*
3439 * Dell Remote Access Card III - Tim_T_Murphy@Dell.com
3440 */
3441 { PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_RACIII,
3442 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3443 pbn_b1_1_1382400 },
3444
3445 /*
3446 * RAStel 2 port modem, gerg@moreton.com.au
3447 */
3448 { PCI_VENDOR_ID_MORETON, PCI_DEVICE_ID_RASTEL_2PORT,
3449 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3450 pbn_b2_bt_2_115200 },
3451
3452 /*
3453 * EKF addition for i960 Boards form EKF with serial port
3454 */
3455 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_80960_RP,
3456 0xE4BF, PCI_ANY_ID, 0, 0,
3457 pbn_intel_i960 },
3458
3459 /*
3460 * Xircom Cardbus/Ethernet combos
3461 */
3462 { PCI_VENDOR_ID_XIRCOM, PCI_DEVICE_ID_XIRCOM_X3201_MDM,
3463 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3464 pbn_b0_1_115200 },
3465 /*
3466 * Xircom RBM56G cardbus modem - Dirk Arnold (temp entry)
3467 */
3468 { PCI_VENDOR_ID_XIRCOM, PCI_DEVICE_ID_XIRCOM_RBM56G,
3469 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3470 pbn_b0_1_115200 },
3471
3472 /*
3473 * Untested PCI modems, sent in from various folks...
3474 */
3475
3476 /*
3477 * Elsa Model 56K PCI Modem, from Andreas Rath <arh@01019freenet.de>
3478 */
3479 { PCI_VENDOR_ID_ROCKWELL, 0x1004,
3480 0x1048, 0x1500, 0, 0,
3481 pbn_b1_1_115200 },
3482
3483 { PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3,
3484 0xFF00, 0, 0, 0,
3485 pbn_sgi_ioc3 },
3486
3487 /*
3488 * HP Diva card
3489 */
3490 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA,
3491 PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA_RMP3, 0, 0,
3492 pbn_b1_1_115200 },
3493 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA,
3494 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3495 pbn_b0_5_115200 },
3496 { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_DIVA_AUX,
3497 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3498 pbn_b2_1_115200 },
3499
3500 { PCI_VENDOR_ID_DCI, PCI_DEVICE_ID_DCI_PCCOM2,
3501 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3502 pbn_b3_2_115200 },
3503 { PCI_VENDOR_ID_DCI, PCI_DEVICE_ID_DCI_PCCOM4,
3504 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3505 pbn_b3_4_115200 },
3506 { PCI_VENDOR_ID_DCI, PCI_DEVICE_ID_DCI_PCCOM8,
3507 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3508 pbn_b3_8_115200 },
3509
3510 /*
3511 * Exar Corp. XR17C15[248] Dual/Quad/Octal UART
3512 */
3513 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C152,
3514 PCI_ANY_ID, PCI_ANY_ID,
3515 0,
3516 0, pbn_exar_XR17C152 },
3517 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C154,
3518 PCI_ANY_ID, PCI_ANY_ID,
3519 0,
3520 0, pbn_exar_XR17C154 },
3521 { PCI_VENDOR_ID_EXAR, PCI_DEVICE_ID_EXAR_XR17C158,
3522 PCI_ANY_ID, PCI_ANY_ID,
3523 0,
3524 0, pbn_exar_XR17C158 },
3525
3526 /*
3527 * Topic TP560 Data/Fax/Voice 56k modem (reported by Evan Clarke)
3528 */
3529 { PCI_VENDOR_ID_TOPIC, PCI_DEVICE_ID_TOPIC_TP560,
3530 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3531 pbn_b0_1_115200 },
3532 /*
3533 * ITE
3534 */
3535 { PCI_VENDOR_ID_ITE, PCI_DEVICE_ID_ITE_8872,
3536 PCI_ANY_ID, PCI_ANY_ID,
3537 0, 0,
3538 pbn_b1_bt_1_115200 },
3539
3540 /*
3541 * IntaShield IS-200
3542 */
3543 { PCI_VENDOR_ID_INTASHIELD, PCI_DEVICE_ID_INTASHIELD_IS200,
3544 PCI_ANY_ID, PCI_ANY_ID, 0, 0, /* 135a.0811 */
3545 pbn_b2_2_115200 },
3546 /*
3547 * IntaShield IS-400
3548 */
3549 { PCI_VENDOR_ID_INTASHIELD, PCI_DEVICE_ID_INTASHIELD_IS400,
3550 PCI_ANY_ID, PCI_ANY_ID, 0, 0, /* 135a.0dc0 */
3551 pbn_b2_4_115200 },
3552 /*
3553 * Perle PCI-RAS cards
3554 */
3555 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
3556 PCI_SUBVENDOR_ID_PERLE, PCI_SUBDEVICE_ID_PCI_RAS4,
3557 0, 0, pbn_b2_4_921600 },
3558 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030,
3559 PCI_SUBVENDOR_ID_PERLE, PCI_SUBDEVICE_ID_PCI_RAS8,
3560 0, 0, pbn_b2_8_921600 },
3561
3562 /*
3563 * Mainpine series cards: Fairly standard layout but fools
3564 * parts of the autodetect in some cases and uses otherwise
3565 * unmatched communications subclasses in the PCI Express case
3566 */
3567
3568 { /* RockForceDUO */
3569 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3570 PCI_VENDOR_ID_MAINPINE, 0x0200,
3571 0, 0, pbn_b0_2_115200 },
3572 { /* RockForceQUATRO */
3573 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3574 PCI_VENDOR_ID_MAINPINE, 0x0300,
3575 0, 0, pbn_b0_4_115200 },
3576 { /* RockForceDUO+ */
3577 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3578 PCI_VENDOR_ID_MAINPINE, 0x0400,
3579 0, 0, pbn_b0_2_115200 },
3580 { /* RockForceQUATRO+ */
3581 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3582 PCI_VENDOR_ID_MAINPINE, 0x0500,
3583 0, 0, pbn_b0_4_115200 },
3584 { /* RockForce+ */
3585 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3586 PCI_VENDOR_ID_MAINPINE, 0x0600,
3587 0, 0, pbn_b0_2_115200 },
3588 { /* RockForce+ */
3589 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3590 PCI_VENDOR_ID_MAINPINE, 0x0700,
3591 0, 0, pbn_b0_4_115200 },
3592 { /* RockForceOCTO+ */
3593 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3594 PCI_VENDOR_ID_MAINPINE, 0x0800,
3595 0, 0, pbn_b0_8_115200 },
3596 { /* RockForceDUO+ */
3597 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3598 PCI_VENDOR_ID_MAINPINE, 0x0C00,
3599 0, 0, pbn_b0_2_115200 },
3600 { /* RockForceQUARTRO+ */
3601 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3602 PCI_VENDOR_ID_MAINPINE, 0x0D00,
3603 0, 0, pbn_b0_4_115200 },
3604 { /* RockForceOCTO+ */
3605 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3606 PCI_VENDOR_ID_MAINPINE, 0x1D00,
3607 0, 0, pbn_b0_8_115200 },
3608 { /* RockForceD1 */
3609 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3610 PCI_VENDOR_ID_MAINPINE, 0x2000,
3611 0, 0, pbn_b0_1_115200 },
3612 { /* RockForceF1 */
3613 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3614 PCI_VENDOR_ID_MAINPINE, 0x2100,
3615 0, 0, pbn_b0_1_115200 },
3616 { /* RockForceD2 */
3617 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3618 PCI_VENDOR_ID_MAINPINE, 0x2200,
3619 0, 0, pbn_b0_2_115200 },
3620 { /* RockForceF2 */
3621 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3622 PCI_VENDOR_ID_MAINPINE, 0x2300,
3623 0, 0, pbn_b0_2_115200 },
3624 { /* RockForceD4 */
3625 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3626 PCI_VENDOR_ID_MAINPINE, 0x2400,
3627 0, 0, pbn_b0_4_115200 },
3628 { /* RockForceF4 */
3629 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3630 PCI_VENDOR_ID_MAINPINE, 0x2500,
3631 0, 0, pbn_b0_4_115200 },
3632 { /* RockForceD8 */
3633 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3634 PCI_VENDOR_ID_MAINPINE, 0x2600,
3635 0, 0, pbn_b0_8_115200 },
3636 { /* RockForceF8 */
3637 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3638 PCI_VENDOR_ID_MAINPINE, 0x2700,
3639 0, 0, pbn_b0_8_115200 },
3640 { /* IQ Express D1 */
3641 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3642 PCI_VENDOR_ID_MAINPINE, 0x3000,
3643 0, 0, pbn_b0_1_115200 },
3644 { /* IQ Express F1 */
3645 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3646 PCI_VENDOR_ID_MAINPINE, 0x3100,
3647 0, 0, pbn_b0_1_115200 },
3648 { /* IQ Express D2 */
3649 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3650 PCI_VENDOR_ID_MAINPINE, 0x3200,
3651 0, 0, pbn_b0_2_115200 },
3652 { /* IQ Express F2 */
3653 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3654 PCI_VENDOR_ID_MAINPINE, 0x3300,
3655 0, 0, pbn_b0_2_115200 },
3656 { /* IQ Express D4 */
3657 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3658 PCI_VENDOR_ID_MAINPINE, 0x3400,
3659 0, 0, pbn_b0_4_115200 },
3660 { /* IQ Express F4 */
3661 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3662 PCI_VENDOR_ID_MAINPINE, 0x3500,
3663 0, 0, pbn_b0_4_115200 },
3664 { /* IQ Express D8 */
3665 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3666 PCI_VENDOR_ID_MAINPINE, 0x3C00,
3667 0, 0, pbn_b0_8_115200 },
3668 { /* IQ Express F8 */
3669 PCI_VENDOR_ID_MAINPINE, PCI_DEVICE_ID_MAINPINE_PBRIDGE,
3670 PCI_VENDOR_ID_MAINPINE, 0x3D00,
3671 0, 0, pbn_b0_8_115200 },
3672
3673
3674 /*
3675 * PA Semi PA6T-1682M on-chip UART
3676 */
3677 { PCI_VENDOR_ID_PASEMI, 0xa004,
3678 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3679 pbn_pasemi_1682M },
3680
3681 /*
3682 * National Instruments
3683 */
3684 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI23216,
3685 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3686 pbn_b1_16_115200 },
3687 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2328,
3688 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3689 pbn_b1_8_115200 },
3690 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2324,
3691 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3692 pbn_b1_bt_4_115200 },
3693 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2322,
3694 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3695 pbn_b1_bt_2_115200 },
3696 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2324I,
3697 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3698 pbn_b1_bt_4_115200 },
3699 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI2322I,
3700 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3701 pbn_b1_bt_2_115200 },
3702 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8420_23216,
3703 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3704 pbn_b1_16_115200 },
3705 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8420_2328,
3706 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3707 pbn_b1_8_115200 },
3708 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8420_2324,
3709 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3710 pbn_b1_bt_4_115200 },
3711 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8420_2322,
3712 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3713 pbn_b1_bt_2_115200 },
3714 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8422_2324,
3715 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3716 pbn_b1_bt_4_115200 },
3717 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8422_2322,
3718 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3719 pbn_b1_bt_2_115200 },
3720 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8430_2322,
3721 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3722 pbn_ni8430_2 },
3723 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8430_2322,
3724 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3725 pbn_ni8430_2 },
3726 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8430_2324,
3727 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3728 pbn_ni8430_4 },
3729 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8430_2324,
3730 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3731 pbn_ni8430_4 },
3732 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8430_2328,
3733 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3734 pbn_ni8430_8 },
3735 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8430_2328,
3736 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3737 pbn_ni8430_8 },
3738 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8430_23216,
3739 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3740 pbn_ni8430_16 },
3741 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8430_23216,
3742 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3743 pbn_ni8430_16 },
3744 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8432_2322,
3745 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3746 pbn_ni8430_2 },
3747 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8432_2322,
3748 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3749 pbn_ni8430_2 },
3750 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PXI8432_2324,
3751 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3752 pbn_ni8430_4 },
3753 { PCI_VENDOR_ID_NI, PCI_DEVICE_ID_NI_PCI8432_2324,
3754 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3755 pbn_ni8430_4 },
3756
3757 /*
3758 * ADDI-DATA GmbH communication cards <info@addi-data.com>
3759 */
3760 { PCI_VENDOR_ID_ADDIDATA,
3761 PCI_DEVICE_ID_ADDIDATA_APCI7500,
3762 PCI_ANY_ID,
3763 PCI_ANY_ID,
3764 0,
3765 0,
3766 pbn_b0_4_115200 },
3767
3768 { PCI_VENDOR_ID_ADDIDATA,
3769 PCI_DEVICE_ID_ADDIDATA_APCI7420,
3770 PCI_ANY_ID,
3771 PCI_ANY_ID,
3772 0,
3773 0,
3774 pbn_b0_2_115200 },
3775
3776 { PCI_VENDOR_ID_ADDIDATA,
3777 PCI_DEVICE_ID_ADDIDATA_APCI7300,
3778 PCI_ANY_ID,
3779 PCI_ANY_ID,
3780 0,
3781 0,
3782 pbn_b0_1_115200 },
3783
3784 { PCI_VENDOR_ID_ADDIDATA_OLD,
3785 PCI_DEVICE_ID_ADDIDATA_APCI7800,
3786 PCI_ANY_ID,
3787 PCI_ANY_ID,
3788 0,
3789 0,
3790 pbn_b1_8_115200 },
3791
3792 { PCI_VENDOR_ID_ADDIDATA,
3793 PCI_DEVICE_ID_ADDIDATA_APCI7500_2,
3794 PCI_ANY_ID,
3795 PCI_ANY_ID,
3796 0,
3797 0,
3798 pbn_b0_4_115200 },
3799
3800 { PCI_VENDOR_ID_ADDIDATA,
3801 PCI_DEVICE_ID_ADDIDATA_APCI7420_2,
3802 PCI_ANY_ID,
3803 PCI_ANY_ID,
3804 0,
3805 0,
3806 pbn_b0_2_115200 },
3807
3808 { PCI_VENDOR_ID_ADDIDATA,
3809 PCI_DEVICE_ID_ADDIDATA_APCI7300_2,
3810 PCI_ANY_ID,
3811 PCI_ANY_ID,
3812 0,
3813 0,
3814 pbn_b0_1_115200 },
3815
3816 { PCI_VENDOR_ID_ADDIDATA,
3817 PCI_DEVICE_ID_ADDIDATA_APCI7500_3,
3818 PCI_ANY_ID,
3819 PCI_ANY_ID,
3820 0,
3821 0,
3822 pbn_b0_4_115200 },
3823
3824 { PCI_VENDOR_ID_ADDIDATA,
3825 PCI_DEVICE_ID_ADDIDATA_APCI7420_3,
3826 PCI_ANY_ID,
3827 PCI_ANY_ID,
3828 0,
3829 0,
3830 pbn_b0_2_115200 },
3831
3832 { PCI_VENDOR_ID_ADDIDATA,
3833 PCI_DEVICE_ID_ADDIDATA_APCI7300_3,
3834 PCI_ANY_ID,
3835 PCI_ANY_ID,
3836 0,
3837 0,
3838 pbn_b0_1_115200 },
3839
3840 { PCI_VENDOR_ID_ADDIDATA,
3841 PCI_DEVICE_ID_ADDIDATA_APCI7800_3,
3842 PCI_ANY_ID,
3843 PCI_ANY_ID,
3844 0,
3845 0,
3846 pbn_b0_8_115200 },
3847
3848 { PCI_VENDOR_ID_ADDIDATA,
3849 PCI_DEVICE_ID_ADDIDATA_APCIe7500,
3850 PCI_ANY_ID,
3851 PCI_ANY_ID,
3852 0,
3853 0,
3854 pbn_ADDIDATA_PCIe_4_3906250 },
3855
3856 { PCI_VENDOR_ID_ADDIDATA,
3857 PCI_DEVICE_ID_ADDIDATA_APCIe7420,
3858 PCI_ANY_ID,
3859 PCI_ANY_ID,
3860 0,
3861 0,
3862 pbn_ADDIDATA_PCIe_2_3906250 },
3863
3864 { PCI_VENDOR_ID_ADDIDATA,
3865 PCI_DEVICE_ID_ADDIDATA_APCIe7300,
3866 PCI_ANY_ID,
3867 PCI_ANY_ID,
3868 0,
3869 0,
3870 pbn_ADDIDATA_PCIe_1_3906250 },
3871
3872 { PCI_VENDOR_ID_ADDIDATA,
3873 PCI_DEVICE_ID_ADDIDATA_APCIe7800,
3874 PCI_ANY_ID,
3875 PCI_ANY_ID,
3876 0,
3877 0,
3878 pbn_ADDIDATA_PCIe_8_3906250 },
3879
3880 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9835,
3881 PCI_VENDOR_ID_IBM, 0x0299,
3882 0, 0, pbn_b0_bt_2_115200 },
3883
3884 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9901,
3885 0xA000, 0x1000,
3886 0, 0, pbn_b0_1_115200 },
3887
3888 /*
3889 * Best Connectivity PCI Multi I/O cards
3890 */
3891
3892 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9865,
3893 0xA000, 0x1000,
3894 0, 0, pbn_b0_1_115200 },
3895
3896 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9865,
3897 0xA000, 0x3004,
3898 0, 0, pbn_b0_bt_4_115200 },
3899 /* Intel CE4100 */
3900 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CE4100_UART,
3901 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3902 pbn_ce4100_1_115200 },
3903
3904 /*
3905 * Cronyx Omega PCI
3906 */
3907 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_CRONYX_OMEGA,
3908 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
3909 pbn_omegapci },
3910
3911 /*
3912 * These entries match devices with class COMMUNICATION_SERIAL,
3913 * COMMUNICATION_MODEM or COMMUNICATION_MULTISERIAL
3914 */
3915 { PCI_ANY_ID, PCI_ANY_ID,
3916 PCI_ANY_ID, PCI_ANY_ID,
3917 PCI_CLASS_COMMUNICATION_SERIAL << 8,
3918 0xffff00, pbn_default },
3919 { PCI_ANY_ID, PCI_ANY_ID,
3920 PCI_ANY_ID, PCI_ANY_ID,
3921 PCI_CLASS_COMMUNICATION_MODEM << 8,
3922 0xffff00, pbn_default },
3923 { PCI_ANY_ID, PCI_ANY_ID,
3924 PCI_ANY_ID, PCI_ANY_ID,
3925 PCI_CLASS_COMMUNICATION_MULTISERIAL << 8,
3926 0xffff00, pbn_default },
3927 { 0, }
3928};
3929
3930static struct pci_driver serial_pci_driver = {
3931 .name = "serial",
3932 .probe = pciserial_init_one,
3933 .remove = __devexit_p(pciserial_remove_one),
3934#ifdef CONFIG_PM
3935 .suspend = pciserial_suspend_one,
3936 .resume = pciserial_resume_one,
3937#endif
3938 .id_table = serial_pci_tbl,
3939};
3940
3941static int __init serial8250_pci_init(void)
3942{
3943 return pci_register_driver(&serial_pci_driver);
3944}
3945
3946static void __exit serial8250_pci_exit(void)
3947{
3948 pci_unregister_driver(&serial_pci_driver);
3949}
3950
3951module_init(serial8250_pci_init);
3952module_exit(serial8250_pci_exit);
3953
3954MODULE_LICENSE("GPL");
3955MODULE_DESCRIPTION("Generic 8250/16x50 PCI serial probe module");
3956MODULE_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..fc301f6722e1
--- /dev/null
+++ b/drivers/tty/serial/8250_pnp.c
@@ -0,0 +1,521 @@
1/*
2 * Probe module for 8250/16550-type ISAPNP serial ports.
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King, All Rights Reserved.
7 *
8 * Ported to the Linux PnP Layer - (C) Adam Belay.
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/pnp.h>
18#include <linux/string.h>
19#include <linux/kernel.h>
20#include <linux/serial_core.h>
21#include <linux/bitops.h>
22
23#include <asm/byteorder.h>
24
25#include "8250.h"
26
27#define UNKNOWN_DEV 0x3000
28
29
30static const struct pnp_device_id pnp_dev_table[] = {
31 /* Archtek America Corp. */
32 /* Archtek SmartLink Modem 3334BT Plug & Play */
33 { "AAC000F", 0 },
34 /* Anchor Datacomm BV */
35 /* SXPro 144 External Data Fax Modem Plug & Play */
36 { "ADC0001", 0 },
37 /* SXPro 288 External Data Fax Modem Plug & Play */
38 { "ADC0002", 0 },
39 /* PROLiNK 1456VH ISA PnP K56flex Fax Modem */
40 { "AEI0250", 0 },
41 /* Actiontec ISA PNP 56K X2 Fax Modem */
42 { "AEI1240", 0 },
43 /* Rockwell 56K ACF II Fax+Data+Voice Modem */
44 { "AKY1021", 0 /*SPCI_FL_NO_SHIRQ*/ },
45 /* AZT3005 PnP SOUND DEVICE */
46 { "AZT4001", 0 },
47 /* Best Data Products Inc. Smart One 336F PnP Modem */
48 { "BDP3336", 0 },
49 /* Boca Research */
50 /* Boca Complete Ofc Communicator 14.4 Data-FAX */
51 { "BRI0A49", 0 },
52 /* Boca Research 33,600 ACF Modem */
53 { "BRI1400", 0 },
54 /* Boca 33.6 Kbps Internal FD34FSVD */
55 { "BRI3400", 0 },
56 /* Boca 33.6 Kbps Internal FD34FSVD */
57 { "BRI0A49", 0 },
58 /* Best Data Products Inc. Smart One 336F PnP Modem */
59 { "BDP3336", 0 },
60 /* Computer Peripherals Inc */
61 /* EuroViVa CommCenter-33.6 SP PnP */
62 { "CPI4050", 0 },
63 /* Creative Labs */
64 /* Creative Labs Phone Blaster 28.8 DSVD PnP Voice */
65 { "CTL3001", 0 },
66 /* Creative Labs Modem Blaster 28.8 DSVD PnP Voice */
67 { "CTL3011", 0 },
68 /* Davicom ISA 33.6K Modem */
69 { "DAV0336", 0 },
70 /* Creative */
71 /* Creative Modem Blaster Flash56 DI5601-1 */
72 { "DMB1032", 0 },
73 /* Creative Modem Blaster V.90 DI5660 */
74 { "DMB2001", 0 },
75 /* E-Tech */
76 /* E-Tech CyberBULLET PC56RVP */
77 { "ETT0002", 0 },
78 /* FUJITSU */
79 /* Fujitsu 33600 PnP-I2 R Plug & Play */
80 { "FUJ0202", 0 },
81 /* Fujitsu FMV-FX431 Plug & Play */
82 { "FUJ0205", 0 },
83 /* Fujitsu 33600 PnP-I4 R Plug & Play */
84 { "FUJ0206", 0 },
85 /* Fujitsu Fax Voice 33600 PNP-I5 R Plug & Play */
86 { "FUJ0209", 0 },
87 /* Archtek America Corp. */
88 /* Archtek SmartLink Modem 3334BT Plug & Play */
89 { "GVC000F", 0 },
90 /* Archtek SmartLink Modem 3334BRV 33.6K Data Fax Voice */
91 { "GVC0303", 0 },
92 /* Hayes */
93 /* Hayes Optima 288 V.34-V.FC + FAX + Voice Plug & Play */
94 { "HAY0001", 0 },
95 /* Hayes Optima 336 V.34 + FAX + Voice PnP */
96 { "HAY000C", 0 },
97 /* Hayes Optima 336B V.34 + FAX + Voice PnP */
98 { "HAY000D", 0 },
99 /* Hayes Accura 56K Ext Fax Modem PnP */
100 { "HAY5670", 0 },
101 /* Hayes Accura 56K Ext Fax Modem PnP */
102 { "HAY5674", 0 },
103 /* Hayes Accura 56K Fax Modem PnP */
104 { "HAY5675", 0 },
105 /* Hayes 288, V.34 + FAX */
106 { "HAYF000", 0 },
107 /* Hayes Optima 288 V.34 + FAX + Voice, Plug & Play */
108 { "HAYF001", 0 },
109 /* IBM */
110 /* IBM Thinkpad 701 Internal Modem Voice */
111 { "IBM0033", 0 },
112 /* Intertex */
113 /* Intertex 28k8 33k6 Voice EXT PnP */
114 { "IXDC801", 0 },
115 /* Intertex 33k6 56k Voice EXT PnP */
116 { "IXDC901", 0 },
117 /* Intertex 28k8 33k6 Voice SP EXT PnP */
118 { "IXDD801", 0 },
119 /* Intertex 33k6 56k Voice SP EXT PnP */
120 { "IXDD901", 0 },
121 /* Intertex 28k8 33k6 Voice SP INT PnP */
122 { "IXDF401", 0 },
123 /* Intertex 28k8 33k6 Voice SP EXT PnP */
124 { "IXDF801", 0 },
125 /* Intertex 33k6 56k Voice SP EXT PnP */
126 { "IXDF901", 0 },
127 /* Kortex International */
128 /* KORTEX 28800 Externe PnP */
129 { "KOR4522", 0 },
130 /* KXPro 33.6 Vocal ASVD PnP */
131 { "KORF661", 0 },
132 /* Lasat */
133 /* LASAT Internet 33600 PnP */
134 { "LAS4040", 0 },
135 /* Lasat Safire 560 PnP */
136 { "LAS4540", 0 },
137 /* Lasat Safire 336 PnP */
138 { "LAS5440", 0 },
139 /* Microcom, Inc. */
140 /* Microcom TravelPorte FAST V.34 Plug & Play */
141 { "MNP0281", 0 },
142 /* Microcom DeskPorte V.34 FAST or FAST+ Plug & Play */
143 { "MNP0336", 0 },
144 /* Microcom DeskPorte FAST EP 28.8 Plug & Play */
145 { "MNP0339", 0 },
146 /* Microcom DeskPorte 28.8P Plug & Play */
147 { "MNP0342", 0 },
148 /* Microcom DeskPorte FAST ES 28.8 Plug & Play */
149 { "MNP0500", 0 },
150 /* Microcom DeskPorte FAST ES 28.8 Plug & Play */
151 { "MNP0501", 0 },
152 /* Microcom DeskPorte 28.8S Internal Plug & Play */
153 { "MNP0502", 0 },
154 /* Motorola */
155 /* Motorola BitSURFR Plug & Play */
156 { "MOT1105", 0 },
157 /* Motorola TA210 Plug & Play */
158 { "MOT1111", 0 },
159 /* Motorola HMTA 200 (ISDN) Plug & Play */
160 { "MOT1114", 0 },
161 /* Motorola BitSURFR Plug & Play */
162 { "MOT1115", 0 },
163 /* Motorola Lifestyle 28.8 Internal */
164 { "MOT1190", 0 },
165 /* Motorola V.3400 Plug & Play */
166 { "MOT1501", 0 },
167 /* Motorola Lifestyle 28.8 V.34 Plug & Play */
168 { "MOT1502", 0 },
169 /* Motorola Power 28.8 V.34 Plug & Play */
170 { "MOT1505", 0 },
171 /* Motorola ModemSURFR External 28.8 Plug & Play */
172 { "MOT1509", 0 },
173 /* Motorola Premier 33.6 Desktop Plug & Play */
174 { "MOT150A", 0 },
175 /* Motorola VoiceSURFR 56K External PnP */
176 { "MOT150F", 0 },
177 /* Motorola ModemSURFR 56K External PnP */
178 { "MOT1510", 0 },
179 /* Motorola ModemSURFR 56K Internal PnP */
180 { "MOT1550", 0 },
181 /* Motorola ModemSURFR Internal 28.8 Plug & Play */
182 { "MOT1560", 0 },
183 /* Motorola Premier 33.6 Internal Plug & Play */
184 { "MOT1580", 0 },
185 /* Motorola OnlineSURFR 28.8 Internal Plug & Play */
186 { "MOT15B0", 0 },
187 /* Motorola VoiceSURFR 56K Internal PnP */
188 { "MOT15F0", 0 },
189 /* Com 1 */
190 /* Deskline K56 Phone System PnP */
191 { "MVX00A1", 0 },
192 /* PC Rider K56 Phone System PnP */
193 { "MVX00F2", 0 },
194 /* NEC 98NOTE SPEAKER PHONE FAX MODEM(33600bps) */
195 { "nEC8241", 0 },
196 /* Pace 56 Voice Internal Plug & Play Modem */
197 { "PMC2430", 0 },
198 /* Generic */
199 /* Generic standard PC COM port */
200 { "PNP0500", 0 },
201 /* Generic 16550A-compatible COM port */
202 { "PNP0501", 0 },
203 /* Compaq 14400 Modem */
204 { "PNPC000", 0 },
205 /* Compaq 2400/9600 Modem */
206 { "PNPC001", 0 },
207 /* Dial-Up Networking Serial Cable between 2 PCs */
208 { "PNPC031", 0 },
209 /* Dial-Up Networking Parallel Cable between 2 PCs */
210 { "PNPC032", 0 },
211 /* Standard 9600 bps Modem */
212 { "PNPC100", 0 },
213 /* Standard 14400 bps Modem */
214 { "PNPC101", 0 },
215 /* Standard 28800 bps Modem*/
216 { "PNPC102", 0 },
217 /* Standard Modem*/
218 { "PNPC103", 0 },
219 /* Standard 9600 bps Modem*/
220 { "PNPC104", 0 },
221 /* Standard 14400 bps Modem*/
222 { "PNPC105", 0 },
223 /* Standard 28800 bps Modem*/
224 { "PNPC106", 0 },
225 /* Standard Modem */
226 { "PNPC107", 0 },
227 /* Standard 9600 bps Modem */
228 { "PNPC108", 0 },
229 /* Standard 14400 bps Modem */
230 { "PNPC109", 0 },
231 /* Standard 28800 bps Modem */
232 { "PNPC10A", 0 },
233 /* Standard Modem */
234 { "PNPC10B", 0 },
235 /* Standard 9600 bps Modem */
236 { "PNPC10C", 0 },
237 /* Standard 14400 bps Modem */
238 { "PNPC10D", 0 },
239 /* Standard 28800 bps Modem */
240 { "PNPC10E", 0 },
241 /* Standard Modem */
242 { "PNPC10F", 0 },
243 /* Standard PCMCIA Card Modem */
244 { "PNP2000", 0 },
245 /* Rockwell */
246 /* Modular Technology */
247 /* Rockwell 33.6 DPF Internal PnP */
248 /* Modular Technology 33.6 Internal PnP */
249 { "ROK0030", 0 },
250 /* Kortex International */
251 /* KORTEX 14400 Externe PnP */
252 { "ROK0100", 0 },
253 /* Rockwell 28.8 */
254 { "ROK4120", 0 },
255 /* Viking Components, Inc */
256 /* Viking 28.8 INTERNAL Fax+Data+Voice PnP */
257 { "ROK4920", 0 },
258 /* Rockwell */
259 /* British Telecom */
260 /* Modular Technology */
261 /* Rockwell 33.6 DPF External PnP */
262 /* BT Prologue 33.6 External PnP */
263 /* Modular Technology 33.6 External PnP */
264 { "RSS00A0", 0 },
265 /* Viking 56K FAX INT */
266 { "RSS0262", 0 },
267 /* K56 par,VV,Voice,Speakphone,AudioSpan,PnP */
268 { "RSS0250", 0 },
269 /* SupraExpress 28.8 Data/Fax PnP modem */
270 { "SUP1310", 0 },
271 /* SupraExpress 336i PnP Voice Modem */
272 { "SUP1381", 0 },
273 /* SupraExpress 33.6 Data/Fax PnP modem */
274 { "SUP1421", 0 },
275 /* SupraExpress 33.6 Data/Fax PnP modem */
276 { "SUP1590", 0 },
277 /* SupraExpress 336i Sp ASVD */
278 { "SUP1620", 0 },
279 /* SupraExpress 33.6 Data/Fax PnP modem */
280 { "SUP1760", 0 },
281 /* SupraExpress 56i Sp Intl */
282 { "SUP2171", 0 },
283 /* Phoebe Micro */
284 /* Phoebe Micro 33.6 Data Fax 1433VQH Plug & Play */
285 { "TEX0011", 0 },
286 /* Archtek America Corp. */
287 /* Archtek SmartLink Modem 3334BT Plug & Play */
288 { "UAC000F", 0 },
289 /* 3Com Corp. */
290 /* Gateway Telepath IIvi 33.6 */
291 { "USR0000", 0 },
292 /* U.S. Robotics Sporster 33.6K Fax INT PnP */
293 { "USR0002", 0 },
294 /* Sportster Vi 14.4 PnP FAX Voicemail */
295 { "USR0004", 0 },
296 /* U.S. Robotics 33.6K Voice INT PnP */
297 { "USR0006", 0 },
298 /* U.S. Robotics 33.6K Voice EXT PnP */
299 { "USR0007", 0 },
300 /* U.S. Robotics Courier V.Everything INT PnP */
301 { "USR0009", 0 },
302 /* U.S. Robotics 33.6K Voice INT PnP */
303 { "USR2002", 0 },
304 /* U.S. Robotics 56K Voice INT PnP */
305 { "USR2070", 0 },
306 /* U.S. Robotics 56K Voice EXT PnP */
307 { "USR2080", 0 },
308 /* U.S. Robotics 56K FAX INT */
309 { "USR3031", 0 },
310 /* U.S. Robotics 56K FAX INT */
311 { "USR3050", 0 },
312 /* U.S. Robotics 56K Voice INT PnP */
313 { "USR3070", 0 },
314 /* U.S. Robotics 56K Voice EXT PnP */
315 { "USR3080", 0 },
316 /* U.S. Robotics 56K Voice INT PnP */
317 { "USR3090", 0 },
318 /* U.S. Robotics 56K Message */
319 { "USR9100", 0 },
320 /* U.S. Robotics 56K FAX EXT PnP*/
321 { "USR9160", 0 },
322 /* U.S. Robotics 56K FAX INT PnP*/
323 { "USR9170", 0 },
324 /* U.S. Robotics 56K Voice EXT PnP*/
325 { "USR9180", 0 },
326 /* U.S. Robotics 56K Voice INT PnP*/
327 { "USR9190", 0 },
328 /* Wacom tablets */
329 { "WACFXXX", 0 },
330 /* Compaq touchscreen */
331 { "FPI2002", 0 },
332 /* Fujitsu Stylistic touchscreens */
333 { "FUJ02B2", 0 },
334 { "FUJ02B3", 0 },
335 /* Fujitsu Stylistic LT touchscreens */
336 { "FUJ02B4", 0 },
337 /* Passive Fujitsu Stylistic touchscreens */
338 { "FUJ02B6", 0 },
339 { "FUJ02B7", 0 },
340 { "FUJ02B8", 0 },
341 { "FUJ02B9", 0 },
342 { "FUJ02BC", 0 },
343 /* Fujitsu Wacom Tablet PC device */
344 { "FUJ02E5", 0 },
345 /* Fujitsu P-series tablet PC device */
346 { "FUJ02E6", 0 },
347 /* Fujitsu Wacom 2FGT Tablet PC device */
348 { "FUJ02E7", 0 },
349 /* Fujitsu Wacom 1FGT Tablet PC device */
350 { "FUJ02E9", 0 },
351 /*
352 * LG C1 EXPRESS DUAL (C1-PB11A3) touch screen (actually a FUJ02E6 in
353 * disguise)
354 */
355 { "LTS0001", 0 },
356 /* Rockwell's (PORALiNK) 33600 INT PNP */
357 { "WCI0003", 0 },
358 /* Unknown PnP modems */
359 { "PNPCXXX", UNKNOWN_DEV },
360 /* More unknown PnP modems */
361 { "PNPDXXX", UNKNOWN_DEV },
362 { "", 0 }
363};
364
365MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
366
367static char *modem_names[] __devinitdata = {
368 "MODEM", "Modem", "modem", "FAX", "Fax", "fax",
369 "56K", "56k", "K56", "33.6", "28.8", "14.4",
370 "33,600", "28,800", "14,400", "33.600", "28.800", "14.400",
371 "33600", "28800", "14400", "V.90", "V.34", "V.32", NULL
372};
373
374static int __devinit check_name(char *name)
375{
376 char **tmp;
377
378 for (tmp = modem_names; *tmp; tmp++)
379 if (strstr(name, *tmp))
380 return 1;
381
382 return 0;
383}
384
385static int __devinit check_resources(struct pnp_dev *dev)
386{
387 resource_size_t base[] = {0x2f8, 0x3f8, 0x2e8, 0x3e8};
388 int i;
389
390 for (i = 0; i < ARRAY_SIZE(base); i++) {
391 if (pnp_possible_config(dev, IORESOURCE_IO, base[i], 8))
392 return 1;
393 }
394
395 return 0;
396}
397
398/*
399 * Given a complete unknown PnP device, try to use some heuristics to
400 * detect modems. Currently use such heuristic set:
401 * - dev->name or dev->bus->name must contain "modem" substring;
402 * - device must have only one IO region (8 byte long) with base address
403 * 0x2e8, 0x3e8, 0x2f8 or 0x3f8.
404 *
405 * Such detection looks very ugly, but can detect at least some of numerous
406 * PnP modems, alternatively we must hardcode all modems in pnp_devices[]
407 * table.
408 */
409static int __devinit serial_pnp_guess_board(struct pnp_dev *dev, int *flags)
410{
411 if (!(check_name(pnp_dev_name(dev)) ||
412 (dev->card && check_name(dev->card->name))))
413 return -ENODEV;
414
415 if (check_resources(dev))
416 return 0;
417
418 return -ENODEV;
419}
420
421static int __devinit
422serial_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
423{
424 struct uart_port port;
425 int ret, line, flags = dev_id->driver_data;
426
427 if (flags & UNKNOWN_DEV) {
428 ret = serial_pnp_guess_board(dev, &flags);
429 if (ret < 0)
430 return ret;
431 }
432
433 memset(&port, 0, sizeof(struct uart_port));
434 if (pnp_irq_valid(dev, 0))
435 port.irq = pnp_irq(dev, 0);
436 if (pnp_port_valid(dev, 0)) {
437 port.iobase = pnp_port_start(dev, 0);
438 port.iotype = UPIO_PORT;
439 } else if (pnp_mem_valid(dev, 0)) {
440 port.mapbase = pnp_mem_start(dev, 0);
441 port.iotype = UPIO_MEM;
442 port.flags = UPF_IOREMAP;
443 } else
444 return -ENODEV;
445
446#ifdef SERIAL_DEBUG_PNP
447 printk(KERN_DEBUG
448 "Setup PNP port: port %x, mem 0x%lx, irq %d, type %d\n",
449 port.iobase, port.mapbase, port.irq, port.iotype);
450#endif
451
452 port.flags |= UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
453 if (pnp_irq_flags(dev, 0) & IORESOURCE_IRQ_SHAREABLE)
454 port.flags |= UPF_SHARE_IRQ;
455 port.uartclk = 1843200;
456 port.dev = &dev->dev;
457
458 line = serial8250_register_port(&port);
459 if (line < 0)
460 return -ENODEV;
461
462 pnp_set_drvdata(dev, (void *)((long)line + 1));
463 return 0;
464}
465
466static void __devexit serial_pnp_remove(struct pnp_dev *dev)
467{
468 long line = (long)pnp_get_drvdata(dev);
469 if (line)
470 serial8250_unregister_port(line - 1);
471}
472
473#ifdef CONFIG_PM
474static int serial_pnp_suspend(struct pnp_dev *dev, pm_message_t state)
475{
476 long line = (long)pnp_get_drvdata(dev);
477
478 if (!line)
479 return -ENODEV;
480 serial8250_suspend_port(line - 1);
481 return 0;
482}
483
484static int serial_pnp_resume(struct pnp_dev *dev)
485{
486 long line = (long)pnp_get_drvdata(dev);
487
488 if (!line)
489 return -ENODEV;
490 serial8250_resume_port(line - 1);
491 return 0;
492}
493#else
494#define serial_pnp_suspend NULL
495#define serial_pnp_resume NULL
496#endif /* CONFIG_PM */
497
498static struct pnp_driver serial_pnp_driver = {
499 .name = "serial",
500 .probe = serial_pnp_probe,
501 .remove = __devexit_p(serial_pnp_remove),
502 .suspend = serial_pnp_suspend,
503 .resume = serial_pnp_resume,
504 .id_table = pnp_dev_table,
505};
506
507static int __init serial8250_pnp_init(void)
508{
509 return pnp_register_driver(&serial_pnp_driver);
510}
511
512static void __exit serial8250_pnp_exit(void)
513{
514 pnp_unregister_driver(&serial_pnp_driver);
515}
516
517module_init(serial8250_pnp_init);
518module_exit(serial8250_pnp_exit);
519
520MODULE_LICENSE("GPL");
521MODULE_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..636144cea932
--- /dev/null
+++ b/drivers/tty/serial/Kconfig
@@ -0,0 +1,1638 @@
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 EXPERT
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 EXPERT
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_EXYNOS4210)
541 select SERIAL_SAMSUNG_UARTS_4 if (CPU_S5PV210 || CPU_EXYNOS4210)
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_CPM
1114 tristate "CPM SCC/SMC serial port support"
1115 depends on CPM2 || 8xx
1116 select SERIAL_CORE
1117 help
1118 This driver supports the SCC and SMC serial ports on Motorola
1119 embedded PowerPC that contain a CPM1 (8xx) or CPM2 (8xxx)
1120
1121config SERIAL_CPM_CONSOLE
1122 bool "Support for console on CPM SCC/SMC serial port"
1123 depends on SERIAL_CPM=y
1124 select SERIAL_CORE_CONSOLE
1125 help
1126 Say Y here if you wish to use a SCC or SMC CPM UART as the system
1127 console (the system console is the device which receives all kernel
1128 messages and warnings and which allows logins in single user mode).
1129
1130 Even if you say Y here, the currently visible framebuffer console
1131 (/dev/tty0) will still be used as the system console by default, but
1132 you can alter that using a kernel command line option such as
1133 "console=ttyCPM0". (Try "man bootparam" or see the documentation of
1134 your boot loader (lilo or loadlin) about how to pass options to the
1135 kernel at boot time.)
1136
1137config SERIAL_SGI_L1_CONSOLE
1138 bool "SGI Altix L1 serial console support"
1139 depends on IA64_GENERIC || IA64_SGI_SN2
1140 select SERIAL_CORE
1141 select SERIAL_CORE_CONSOLE
1142 help
1143 If you have an SGI Altix and you would like to use the system
1144 controller serial port as your console (you want this!),
1145 say Y. Otherwise, say N.
1146
1147config SERIAL_MPC52xx
1148 tristate "Freescale MPC52xx/MPC512x family PSC serial support"
1149 depends on PPC_MPC52xx || PPC_MPC512x
1150 select SERIAL_CORE
1151 help
1152 This driver supports MPC52xx and MPC512x PSC serial ports. If you would
1153 like to use them, you must answer Y or M to this option. Note that
1154 for use as console, it must be included in kernel and not as a
1155 module.
1156
1157config SERIAL_MPC52xx_CONSOLE
1158 bool "Console on a Freescale MPC52xx/MPC512x family PSC serial port"
1159 depends on SERIAL_MPC52xx=y
1160 select SERIAL_CORE_CONSOLE
1161 help
1162 Select this options if you'd like to use one of the PSC serial port
1163 of the Freescale MPC52xx family as a console.
1164
1165config SERIAL_MPC52xx_CONSOLE_BAUD
1166 int "Freescale MPC52xx/MPC512x family PSC serial port baud"
1167 depends on SERIAL_MPC52xx_CONSOLE=y
1168 default "9600"
1169 help
1170 Select the MPC52xx console baud rate.
1171 This value is only used if the bootloader doesn't pass in the
1172 console baudrate
1173
1174config SERIAL_ICOM
1175 tristate "IBM Multiport Serial Adapter"
1176 depends on PCI && (PPC_ISERIES || PPC_PSERIES)
1177 select SERIAL_CORE
1178 select FW_LOADER
1179 help
1180 This driver is for a family of multiport serial adapters
1181 including 2 port RVX, 2 port internal modem, 4 port internal
1182 modem and a split 1 port RVX and 1 port internal modem.
1183
1184 This driver can also be built as a module. If so, the module
1185 will be called icom.
1186
1187config SERIAL_M32R_SIO
1188 bool "M32R SIO I/F"
1189 depends on M32R
1190 default y
1191 select SERIAL_CORE
1192 help
1193 Say Y here if you want to use the M32R serial controller.
1194
1195config SERIAL_M32R_SIO_CONSOLE
1196 bool "use SIO console"
1197 depends on SERIAL_M32R_SIO=y
1198 select SERIAL_CORE_CONSOLE
1199 help
1200 Say Y here if you want to support a serial console.
1201
1202 If you use an M3T-M32700UT or an OPSPUT platform,
1203 please say also y for SERIAL_M32R_PLDSIO.
1204
1205config SERIAL_M32R_PLDSIO
1206 bool "M32R SIO I/F on a PLD"
1207 depends on SERIAL_M32R_SIO=y && (PLAT_OPSPUT || PLAT_USRV || PLAT_M32700UT)
1208 default n
1209 help
1210 Say Y here if you want to use the M32R serial controller
1211 on a PLD (Programmable Logic Device).
1212
1213 If you use an M3T-M32700UT or an OPSPUT platform,
1214 please say Y.
1215
1216config SERIAL_TXX9
1217 bool "TMPTX39XX/49XX SIO support"
1218 depends on HAS_TXX9_SERIAL
1219 select SERIAL_CORE
1220 default y
1221
1222config HAS_TXX9_SERIAL
1223 bool
1224
1225config SERIAL_TXX9_NR_UARTS
1226 int "Maximum number of TMPTX39XX/49XX SIO ports"
1227 depends on SERIAL_TXX9
1228 default "6"
1229
1230config SERIAL_TXX9_CONSOLE
1231 bool "TMPTX39XX/49XX SIO Console support"
1232 depends on SERIAL_TXX9=y
1233 select SERIAL_CORE_CONSOLE
1234
1235config SERIAL_TXX9_STDSERIAL
1236 bool "TX39XX/49XX SIO act as standard serial"
1237 depends on !SERIAL_8250 && SERIAL_TXX9
1238
1239config SERIAL_VR41XX
1240 tristate "NEC VR4100 series Serial Interface Unit support"
1241 depends on CPU_VR41XX
1242 select SERIAL_CORE
1243 help
1244 If you have a NEC VR4100 series processor and you want to use
1245 Serial Interface Unit(SIU) or Debug Serial Interface Unit(DSIU)
1246 (not include VR4111/VR4121 DSIU), say Y. Otherwise, say N.
1247
1248config SERIAL_VR41XX_CONSOLE
1249 bool "Enable NEC VR4100 series Serial Interface Unit console"
1250 depends on SERIAL_VR41XX=y
1251 select SERIAL_CORE_CONSOLE
1252 help
1253 If you have a NEC VR4100 series processor and you want to use
1254 a console on a serial port, say Y. Otherwise, say N.
1255
1256config SERIAL_JSM
1257 tristate "Digi International NEO PCI Support"
1258 depends on PCI
1259 select SERIAL_CORE
1260 help
1261 This is a driver for Digi International's Neo series
1262 of cards which provide multiple serial ports. You would need
1263 something like this to connect more than two modems to your Linux
1264 box, for instance in order to become a dial-in server. This driver
1265 supports PCI boards only.
1266
1267 If you have a card like this, say Y here, otherwise say N.
1268
1269 To compile this driver as a module, choose M here: the
1270 module will be called jsm.
1271
1272config SERIAL_SGI_IOC4
1273 tristate "SGI IOC4 controller serial support"
1274 depends on (IA64_GENERIC || IA64_SGI_SN2) && SGI_IOC4
1275 select SERIAL_CORE
1276 help
1277 If you have an SGI Altix with an IOC4 based Base IO card
1278 and wish to use the serial ports on this card, say Y.
1279 Otherwise, say N.
1280
1281config SERIAL_SGI_IOC3
1282 tristate "SGI Altix IOC3 serial support"
1283 depends on (IA64_GENERIC || IA64_SGI_SN2) && SGI_IOC3
1284 select SERIAL_CORE
1285 help
1286 If you have an SGI Altix with an IOC3 serial card,
1287 say Y or M. Otherwise, say N.
1288
1289config SERIAL_MSM
1290 bool "MSM on-chip serial port support"
1291 depends on ARM && ARCH_MSM
1292 select SERIAL_CORE
1293
1294config SERIAL_MSM_CONSOLE
1295 bool "MSM serial console support"
1296 depends on SERIAL_MSM=y
1297 select SERIAL_CORE_CONSOLE
1298
1299config SERIAL_MSM_HS
1300 tristate "MSM UART High Speed: Serial Driver"
1301 depends on ARCH_MSM
1302 select SERIAL_CORE
1303 help
1304 If you have a machine based on MSM family of SoCs, you
1305 can enable its onboard high speed serial port by enabling
1306 this option.
1307
1308 Choose M here to compile it as a module. The module will be
1309 called msm_serial_hs.
1310
1311config SERIAL_VT8500
1312 bool "VIA VT8500 on-chip serial port support"
1313 depends on ARM && ARCH_VT8500
1314 select SERIAL_CORE
1315
1316config SERIAL_VT8500_CONSOLE
1317 bool "VIA VT8500 serial console support"
1318 depends on SERIAL_VT8500=y
1319 select SERIAL_CORE_CONSOLE
1320
1321config SERIAL_NETX
1322 tristate "NetX serial port support"
1323 depends on ARM && ARCH_NETX
1324 select SERIAL_CORE
1325 help
1326 If you have a machine based on a Hilscher NetX SoC you
1327 can enable its onboard serial port by enabling this option.
1328
1329 To compile this driver as a module, choose M here: the
1330 module will be called netx-serial.
1331
1332config SERIAL_NETX_CONSOLE
1333 bool "Console on NetX serial port"
1334 depends on SERIAL_NETX=y
1335 select SERIAL_CORE_CONSOLE
1336 help
1337 If you have enabled the serial port on the Hilscher NetX SoC
1338 you can make it the console by answering Y to this option.
1339
1340config SERIAL_OF_PLATFORM
1341 tristate "Serial port on Open Firmware platform bus"
1342 depends on OF
1343 depends on SERIAL_8250 || SERIAL_OF_PLATFORM_NWPSERIAL
1344 help
1345 If you have a PowerPC based system that has serial ports
1346 on a platform specific bus, you should enable this option.
1347 Currently, only 8250 compatible ports are supported, but
1348 others can easily be added.
1349
1350config SERIAL_OMAP
1351 tristate "OMAP serial port support"
1352 depends on ARCH_OMAP2 || ARCH_OMAP3 || ARCH_OMAP4
1353 select SERIAL_CORE
1354 help
1355 If you have a machine based on an Texas Instruments OMAP CPU you
1356 can enable its onboard serial ports by enabling this option.
1357
1358 By enabling this option you take advantage of dma feature available
1359 with the omap-serial driver. DMA support can be enabled from platform
1360 data.
1361
1362config SERIAL_OMAP_CONSOLE
1363 bool "Console on OMAP serial port"
1364 depends on SERIAL_OMAP
1365 select SERIAL_CORE_CONSOLE
1366 help
1367 Select this option if you would like to use omap serial port as
1368 console.
1369
1370 Even if you say Y here, the currently visible virtual console
1371 (/dev/tty0) will still be used as the system console by default, but
1372 you can alter that using a kernel command line option such as
1373 "console=ttyOx". (Try "man bootparam" or see the documentation of
1374 your boot loader about how to pass options to the kernel at
1375 boot time.)
1376
1377config SERIAL_OF_PLATFORM_NWPSERIAL
1378 tristate "NWP serial port driver"
1379 depends on PPC_OF && PPC_DCR
1380 select SERIAL_OF_PLATFORM
1381 select SERIAL_CORE_CONSOLE
1382 select SERIAL_CORE
1383 help
1384 This driver supports the cell network processor nwp serial
1385 device.
1386
1387config SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE
1388 bool "Console on NWP serial port"
1389 depends on SERIAL_OF_PLATFORM_NWPSERIAL=y
1390 select SERIAL_CORE_CONSOLE
1391 help
1392 Support for Console on the NWP serial ports.
1393
1394config SERIAL_LANTIQ
1395 bool "Lantiq serial driver"
1396 depends on LANTIQ
1397 select SERIAL_CORE
1398 select SERIAL_CORE_CONSOLE
1399 help
1400 Support for console and UART on Lantiq SoCs.
1401
1402config SERIAL_QE
1403 tristate "Freescale QUICC Engine serial port support"
1404 depends on QUICC_ENGINE
1405 select SERIAL_CORE
1406 select FW_LOADER
1407 default n
1408 help
1409 This driver supports the QE serial ports on Freescale embedded
1410 PowerPC that contain a QUICC Engine.
1411
1412config SERIAL_SC26XX
1413 tristate "SC2681/SC2692 serial port support"
1414 depends on SNI_RM
1415 select SERIAL_CORE
1416 help
1417 This is a driver for the onboard serial ports of
1418 older RM400 machines.
1419
1420config SERIAL_SC26XX_CONSOLE
1421 bool "Console on SC2681/SC2692 serial port"
1422 depends on SERIAL_SC26XX
1423 select SERIAL_CORE_CONSOLE
1424 help
1425 Support for Console on SC2681/SC2692 serial ports.
1426
1427config SERIAL_BFIN_SPORT
1428 tristate "Blackfin SPORT emulate UART"
1429 depends on BLACKFIN
1430 select SERIAL_CORE
1431 help
1432 Enable SPORT emulate UART on Blackfin series.
1433
1434 To compile this driver as a module, choose M here: the
1435 module will be called bfin_sport_uart.
1436
1437config SERIAL_BFIN_SPORT_CONSOLE
1438 bool "Console on Blackfin sport emulated uart"
1439 depends on SERIAL_BFIN_SPORT=y
1440 select SERIAL_CORE_CONSOLE
1441
1442config SERIAL_BFIN_SPORT0_UART
1443 bool "Enable UART over SPORT0"
1444 depends on SERIAL_BFIN_SPORT && !(BF542 || BF544)
1445 help
1446 Enable UART over SPORT0
1447
1448config SERIAL_BFIN_SPORT0_UART_CTSRTS
1449 bool "Enable UART over SPORT0 hardware flow control"
1450 depends on SERIAL_BFIN_SPORT0_UART
1451 help
1452 Enable hardware flow control in the driver.
1453
1454config SERIAL_BFIN_SPORT1_UART
1455 bool "Enable UART over SPORT1"
1456 depends on SERIAL_BFIN_SPORT
1457 help
1458 Enable UART over SPORT1
1459
1460config SERIAL_BFIN_SPORT1_UART_CTSRTS
1461 bool "Enable UART over SPORT1 hardware flow control"
1462 depends on SERIAL_BFIN_SPORT1_UART
1463 help
1464 Enable hardware flow control in the driver.
1465
1466config SERIAL_BFIN_SPORT2_UART
1467 bool "Enable UART over SPORT2"
1468 depends on SERIAL_BFIN_SPORT && (BF54x || BF538 || BF539)
1469 help
1470 Enable UART over SPORT2
1471
1472config SERIAL_BFIN_SPORT2_UART_CTSRTS
1473 bool "Enable UART over SPORT2 hardware flow control"
1474 depends on SERIAL_BFIN_SPORT2_UART
1475 help
1476 Enable hardware flow control in the driver.
1477
1478config SERIAL_BFIN_SPORT3_UART
1479 bool "Enable UART over SPORT3"
1480 depends on SERIAL_BFIN_SPORT && (BF54x || BF538 || BF539)
1481 help
1482 Enable UART over SPORT3
1483
1484config SERIAL_BFIN_SPORT3_UART_CTSRTS
1485 bool "Enable UART over SPORT3 hardware flow control"
1486 depends on SERIAL_BFIN_SPORT3_UART
1487 help
1488 Enable hardware flow control in the driver.
1489
1490config SERIAL_TIMBERDALE
1491 tristate "Support for timberdale UART"
1492 select SERIAL_CORE
1493 ---help---
1494 Add support for UART controller on timberdale.
1495
1496config SERIAL_BCM63XX
1497 tristate "bcm63xx serial port support"
1498 select SERIAL_CORE
1499 depends on BCM63XX
1500 help
1501 If you have a bcm63xx CPU, you can enable its onboard
1502 serial port by enabling this options.
1503
1504 To compile this driver as a module, choose M here: the
1505 module will be called bcm963xx_uart.
1506
1507config SERIAL_BCM63XX_CONSOLE
1508 bool "Console on bcm63xx serial port"
1509 depends on SERIAL_BCM63XX=y
1510 select SERIAL_CORE_CONSOLE
1511 help
1512 If you have enabled the serial port on the bcm63xx CPU
1513 you can make it the console by answering Y to this option.
1514
1515config SERIAL_GRLIB_GAISLER_APBUART
1516 tristate "GRLIB APBUART serial support"
1517 depends on OF && SPARC
1518 select SERIAL_CORE
1519 ---help---
1520 Add support for the GRLIB APBUART serial port.
1521
1522config SERIAL_GRLIB_GAISLER_APBUART_CONSOLE
1523 bool "Console on GRLIB APBUART serial port"
1524 depends on SERIAL_GRLIB_GAISLER_APBUART=y
1525 select SERIAL_CORE_CONSOLE
1526 help
1527 Support for running a console on the GRLIB APBUART
1528
1529config SERIAL_ALTERA_JTAGUART
1530 tristate "Altera JTAG UART support"
1531 select SERIAL_CORE
1532 help
1533 This driver supports the Altera JTAG UART port.
1534
1535config SERIAL_ALTERA_JTAGUART_CONSOLE
1536 bool "Altera JTAG UART console support"
1537 depends on SERIAL_ALTERA_JTAGUART=y
1538 select SERIAL_CORE_CONSOLE
1539 help
1540 Enable a Altera JTAG UART port to be the system console.
1541
1542config SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS
1543 bool "Bypass output when no connection"
1544 depends on SERIAL_ALTERA_JTAGUART_CONSOLE
1545 select SERIAL_CORE_CONSOLE
1546 help
1547 Bypass console output and keep going even if there is no
1548 JTAG terminal connection with the host.
1549
1550config SERIAL_ALTERA_UART
1551 tristate "Altera UART support"
1552 select SERIAL_CORE
1553 help
1554 This driver supports the Altera softcore UART port.
1555
1556config SERIAL_ALTERA_UART_MAXPORTS
1557 int "Maximum number of Altera UART ports"
1558 depends on SERIAL_ALTERA_UART
1559 default 4
1560 help
1561 This setting lets you define the maximum number of the Altera
1562 UART ports. The usual default varies from board to board, and
1563 this setting is a way of catering for that.
1564
1565config SERIAL_ALTERA_UART_BAUDRATE
1566 int "Default baudrate for Altera UART ports"
1567 depends on SERIAL_ALTERA_UART
1568 default 115200
1569 help
1570 This setting lets you define what the default baudrate is for the
1571 Altera UART ports. The usual default varies from board to board,
1572 and this setting is a way of catering for that.
1573
1574config SERIAL_ALTERA_UART_CONSOLE
1575 bool "Altera UART console support"
1576 depends on SERIAL_ALTERA_UART=y
1577 select SERIAL_CORE_CONSOLE
1578 help
1579 Enable a Altera UART port to be the system console.
1580
1581config SERIAL_IFX6X60
1582 tristate "SPI protocol driver for Infineon 6x60 modem (EXPERIMENTAL)"
1583 depends on GPIOLIB && SPI && EXPERIMENTAL
1584 help
1585 Support for the IFX6x60 modem devices on Intel MID platforms.
1586
1587config SERIAL_PCH_UART
1588 tristate "Intel EG20T PCH / OKI SEMICONDUCTOR IOH(ML7213/ML7223) UART"
1589 depends on PCI
1590 select SERIAL_CORE
1591 help
1592 This driver is for PCH(Platform controller Hub) UART of Intel EG20T
1593 which is an IOH(Input/Output Hub) for x86 embedded processor.
1594 Enabling PCH_DMA, this PCH UART works as DMA mode.
1595
1596 This driver also can be used for OKI SEMICONDUCTOR IOH(Input/
1597 Output Hub), ML7213 and ML7223.
1598 ML7213 IOH is for IVI(In-Vehicle Infotainment) use and ML7223 IOH is
1599 for MP(Media Phone) use.
1600 ML7213/ML7223 is companion chip for Intel Atom E6xx series.
1601 ML7213/ML7223 is completely compatible for Intel EG20T PCH.
1602
1603config SERIAL_MSM_SMD
1604 bool "Enable tty device interface for some SMD ports"
1605 default n
1606 depends on MSM_SMD
1607 help
1608 Enables userspace clients to read and write to some streaming SMD
1609 ports via tty device interface for MSM chipset.
1610
1611config SERIAL_MXS_AUART
1612 depends on ARCH_MXS
1613 tristate "MXS AUART support"
1614 select SERIAL_CORE
1615 help
1616 This driver supports the MXS Application UART (AUART) port.
1617
1618config SERIAL_MXS_AUART_CONSOLE
1619 bool "MXS AUART console support"
1620 depends on SERIAL_MXS_AUART=y
1621 select SERIAL_CORE_CONSOLE
1622 help
1623 Enable a MXS AUART port to be the system console.
1624
1625config SERIAL_XILINX_PS_UART
1626 tristate "Xilinx PS UART support"
1627 select SERIAL_CORE
1628 help
1629 This driver supports the Xilinx PS UART port.
1630
1631config SERIAL_XILINX_PS_UART_CONSOLE
1632 bool "Xilinx PS UART console support"
1633 depends on SERIAL_XILINX_PS_UART=y
1634 select SERIAL_CORE_CONSOLE
1635 help
1636 Enable a Xilinx PS UART port to be the system console.
1637
1638endmenu
diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile
new file mode 100644
index 000000000000..cb2628fee4c7
--- /dev/null
+++ b/drivers/tty/serial/Makefile
@@ -0,0 +1,98 @@
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_DZ) += dz.o
58obj-$(CONFIG_SERIAL_ZS) += zs.o
59obj-$(CONFIG_SERIAL_SH_SCI) += sh-sci.o
60obj-$(CONFIG_SERIAL_SGI_L1_CONSOLE) += sn_console.o
61obj-$(CONFIG_SERIAL_CPM) += cpm_uart/
62obj-$(CONFIG_SERIAL_IMX) += imx.o
63obj-$(CONFIG_SERIAL_MPC52xx) += mpc52xx_uart.o
64obj-$(CONFIG_SERIAL_ICOM) += icom.o
65obj-$(CONFIG_SERIAL_M32R_SIO) += m32r_sio.o
66obj-$(CONFIG_SERIAL_MPSC) += mpsc.o
67obj-$(CONFIG_SERIAL_SB1250_DUART) += sb1250-duart.o
68obj-$(CONFIG_ETRAX_SERIAL) += crisv10.o
69obj-$(CONFIG_SERIAL_SC26XX) += sc26xx.o
70obj-$(CONFIG_SERIAL_JSM) += jsm/
71obj-$(CONFIG_SERIAL_TXX9) += serial_txx9.o
72obj-$(CONFIG_SERIAL_VR41XX) += vr41xx_siu.o
73obj-$(CONFIG_SERIAL_SGI_IOC4) += ioc4_serial.o
74obj-$(CONFIG_SERIAL_SGI_IOC3) += ioc3_serial.o
75obj-$(CONFIG_SERIAL_ATMEL) += atmel_serial.o
76obj-$(CONFIG_SERIAL_UARTLITE) += uartlite.o
77obj-$(CONFIG_SERIAL_MSM) += msm_serial.o
78obj-$(CONFIG_SERIAL_MSM_HS) += msm_serial_hs.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
95obj-$(CONFIG_SERIAL_MSM_SMD) += msm_smd_tty.o
96obj-$(CONFIG_SERIAL_MXS_AUART) += mxs-auart.o
97obj-$(CONFIG_SERIAL_LANTIQ) += lantiq.o
98obj-$(CONFIG_SERIAL_XILINX_PS_UART) += xilinx_uartps.o
diff --git a/drivers/tty/serial/altera_jtaguart.c b/drivers/tty/serial/altera_jtaguart.c
new file mode 100644
index 000000000000..60e049b041a7
--- /dev/null
+++ b/drivers/tty/serial/altera_jtaguart.c
@@ -0,0 +1,516 @@
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
308#if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
309static void altera_jtaguart_console_putc(struct console *co, const char c)
310{
311 struct uart_port *port = &(altera_jtaguart_ports + co->index)->port;
312 unsigned long status;
313 unsigned long flags;
314
315 spin_lock_irqsave(&port->lock, flags);
316 while (((status = readl(port->membase + ALTERA_JTAGUART_CONTROL_REG)) &
317 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
318 if ((status & ALTERA_JTAGUART_CONTROL_AC_MSK) == 0) {
319 spin_unlock_irqrestore(&port->lock, flags);
320 return; /* no connection activity */
321 }
322 spin_unlock_irqrestore(&port->lock, flags);
323 cpu_relax();
324 spin_lock_irqsave(&port->lock, flags);
325 }
326 writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
327 spin_unlock_irqrestore(&port->lock, flags);
328}
329#else
330static void altera_jtaguart_console_putc(struct console *co, const char c)
331{
332 struct uart_port *port = &(altera_jtaguart_ports + co->index)->port;
333 unsigned long flags;
334
335 spin_lock_irqsave(&port->lock, flags);
336 while ((readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
337 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
338 spin_unlock_irqrestore(&port->lock, flags);
339 cpu_relax();
340 spin_lock_irqsave(&port->lock, flags);
341 }
342 writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
343 spin_unlock_irqrestore(&port->lock, flags);
344}
345#endif
346
347static void altera_jtaguart_console_write(struct console *co, const char *s,
348 unsigned int count)
349{
350 for (; count; count--, s++) {
351 altera_jtaguart_console_putc(co, *s);
352 if (*s == '\n')
353 altera_jtaguart_console_putc(co, '\r');
354 }
355}
356
357static int __init altera_jtaguart_console_setup(struct console *co,
358 char *options)
359{
360 struct uart_port *port;
361
362 if (co->index < 0 || co->index >= ALTERA_JTAGUART_MAXPORTS)
363 return -EINVAL;
364 port = &altera_jtaguart_ports[co->index].port;
365 if (port->membase == NULL)
366 return -ENODEV;
367 return 0;
368}
369
370static struct uart_driver altera_jtaguart_driver;
371
372static struct console altera_jtaguart_console = {
373 .name = "ttyJ",
374 .write = altera_jtaguart_console_write,
375 .device = uart_console_device,
376 .setup = altera_jtaguart_console_setup,
377 .flags = CON_PRINTBUFFER,
378 .index = -1,
379 .data = &altera_jtaguart_driver,
380};
381
382static int __init altera_jtaguart_console_init(void)
383{
384 register_console(&altera_jtaguart_console);
385 return 0;
386}
387
388console_initcall(altera_jtaguart_console_init);
389
390#define ALTERA_JTAGUART_CONSOLE (&altera_jtaguart_console)
391
392#else
393
394#define ALTERA_JTAGUART_CONSOLE NULL
395
396#endif /* CONFIG_ALTERA_JTAGUART_CONSOLE */
397
398static struct uart_driver altera_jtaguart_driver = {
399 .owner = THIS_MODULE,
400 .driver_name = "altera_jtaguart",
401 .dev_name = "ttyJ",
402 .major = ALTERA_JTAGUART_MAJOR,
403 .minor = ALTERA_JTAGUART_MINOR,
404 .nr = ALTERA_JTAGUART_MAXPORTS,
405 .cons = ALTERA_JTAGUART_CONSOLE,
406};
407
408static int __devinit altera_jtaguart_probe(struct platform_device *pdev)
409{
410 struct altera_jtaguart_platform_uart *platp = pdev->dev.platform_data;
411 struct uart_port *port;
412 struct resource *res_irq, *res_mem;
413 int i = pdev->id;
414
415 /* -1 emphasizes that the platform must have one port, no .N suffix */
416 if (i == -1)
417 i = 0;
418
419 if (i >= ALTERA_JTAGUART_MAXPORTS)
420 return -EINVAL;
421
422 port = &altera_jtaguart_ports[i].port;
423
424 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
425 if (res_mem)
426 port->mapbase = res_mem->start;
427 else if (platp)
428 port->mapbase = platp->mapbase;
429 else
430 return -ENODEV;
431
432 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
433 if (res_irq)
434 port->irq = res_irq->start;
435 else if (platp)
436 port->irq = platp->irq;
437 else
438 return -ENODEV;
439
440 port->membase = ioremap(port->mapbase, ALTERA_JTAGUART_SIZE);
441 if (!port->membase)
442 return -ENOMEM;
443
444 port->line = i;
445 port->type = PORT_ALTERA_JTAGUART;
446 port->iotype = SERIAL_IO_MEM;
447 port->ops = &altera_jtaguart_ops;
448 port->flags = UPF_BOOT_AUTOCONF;
449
450 uart_add_one_port(&altera_jtaguart_driver, port);
451
452 return 0;
453}
454
455static int __devexit altera_jtaguart_remove(struct platform_device *pdev)
456{
457 struct uart_port *port;
458 int i = pdev->id;
459
460 if (i == -1)
461 i = 0;
462
463 port = &altera_jtaguart_ports[i].port;
464 uart_remove_one_port(&altera_jtaguart_driver, port);
465
466 return 0;
467}
468
469#ifdef CONFIG_OF
470static struct of_device_id altera_jtaguart_match[] = {
471 { .compatible = "ALTR,juart-1.0", },
472 {},
473};
474MODULE_DEVICE_TABLE(of, altera_jtaguart_match);
475#else
476#define altera_jtaguart_match NULL
477#endif /* CONFIG_OF */
478
479static struct platform_driver altera_jtaguart_platform_driver = {
480 .probe = altera_jtaguart_probe,
481 .remove = __devexit_p(altera_jtaguart_remove),
482 .driver = {
483 .name = DRV_NAME,
484 .owner = THIS_MODULE,
485 .of_match_table = altera_jtaguart_match,
486 },
487};
488
489static int __init altera_jtaguart_init(void)
490{
491 int rc;
492
493 rc = uart_register_driver(&altera_jtaguart_driver);
494 if (rc)
495 return rc;
496 rc = platform_driver_register(&altera_jtaguart_platform_driver);
497 if (rc) {
498 uart_unregister_driver(&altera_jtaguart_driver);
499 return rc;
500 }
501 return 0;
502}
503
504static void __exit altera_jtaguart_exit(void)
505{
506 platform_driver_unregister(&altera_jtaguart_platform_driver);
507 uart_unregister_driver(&altera_jtaguart_driver);
508}
509
510module_init(altera_jtaguart_init);
511module_exit(altera_jtaguart_exit);
512
513MODULE_DESCRIPTION("Altera JTAG UART driver");
514MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
515MODULE_LICENSE("GPL");
516MODULE_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..50bc5a5ac653
--- /dev/null
+++ b/drivers/tty/serial/altera_uart.c
@@ -0,0 +1,661 @@
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/of.h>
28#include <linux/io.h>
29#include <linux/altera_uart.h>
30
31#define DRV_NAME "altera_uart"
32#define SERIAL_ALTERA_MAJOR 204
33#define SERIAL_ALTERA_MINOR 213
34
35/*
36 * Altera UART register definitions according to the Nios UART datasheet:
37 * http://www.altera.com/literature/ds/ds_nios_uart.pdf
38 */
39
40#define ALTERA_UART_SIZE 32
41
42#define ALTERA_UART_RXDATA_REG 0
43#define ALTERA_UART_TXDATA_REG 4
44#define ALTERA_UART_STATUS_REG 8
45#define ALTERA_UART_CONTROL_REG 12
46#define ALTERA_UART_DIVISOR_REG 16
47#define ALTERA_UART_EOP_REG 20
48
49#define ALTERA_UART_STATUS_PE_MSK 0x0001 /* parity error */
50#define ALTERA_UART_STATUS_FE_MSK 0x0002 /* framing error */
51#define ALTERA_UART_STATUS_BRK_MSK 0x0004 /* break */
52#define ALTERA_UART_STATUS_ROE_MSK 0x0008 /* RX overrun error */
53#define ALTERA_UART_STATUS_TOE_MSK 0x0010 /* TX overrun error */
54#define ALTERA_UART_STATUS_TMT_MSK 0x0020 /* TX shift register state */
55#define ALTERA_UART_STATUS_TRDY_MSK 0x0040 /* TX ready */
56#define ALTERA_UART_STATUS_RRDY_MSK 0x0080 /* RX ready */
57#define ALTERA_UART_STATUS_E_MSK 0x0100 /* exception condition */
58#define ALTERA_UART_STATUS_DCTS_MSK 0x0400 /* CTS logic-level change */
59#define ALTERA_UART_STATUS_CTS_MSK 0x0800 /* CTS logic state */
60#define ALTERA_UART_STATUS_EOP_MSK 0x1000 /* EOP written/read */
61
62 /* Enable interrupt on... */
63#define ALTERA_UART_CONTROL_PE_MSK 0x0001 /* ...parity error */
64#define ALTERA_UART_CONTROL_FE_MSK 0x0002 /* ...framing error */
65#define ALTERA_UART_CONTROL_BRK_MSK 0x0004 /* ...break */
66#define ALTERA_UART_CONTROL_ROE_MSK 0x0008 /* ...RX overrun */
67#define ALTERA_UART_CONTROL_TOE_MSK 0x0010 /* ...TX overrun */
68#define ALTERA_UART_CONTROL_TMT_MSK 0x0020 /* ...TX shift register empty */
69#define ALTERA_UART_CONTROL_TRDY_MSK 0x0040 /* ...TX ready */
70#define ALTERA_UART_CONTROL_RRDY_MSK 0x0080 /* ...RX ready */
71#define ALTERA_UART_CONTROL_E_MSK 0x0100 /* ...exception*/
72
73#define ALTERA_UART_CONTROL_TRBK_MSK 0x0200 /* TX break */
74#define ALTERA_UART_CONTROL_DCTS_MSK 0x0400 /* Interrupt on CTS change */
75#define ALTERA_UART_CONTROL_RTS_MSK 0x0800 /* RTS signal */
76#define ALTERA_UART_CONTROL_EOP_MSK 0x1000 /* Interrupt on EOP */
77
78/*
79 * Local per-uart structure.
80 */
81struct altera_uart {
82 struct uart_port port;
83 struct timer_list tmr;
84 unsigned int sigs; /* Local copy of line sigs */
85 unsigned short imr; /* Local IMR mirror */
86};
87
88static u32 altera_uart_readl(struct uart_port *port, int reg)
89{
90 return readl(port->membase + (reg << port->regshift));
91}
92
93static void altera_uart_writel(struct uart_port *port, u32 dat, int reg)
94{
95 writel(dat, port->membase + (reg << port->regshift));
96}
97
98static unsigned int altera_uart_tx_empty(struct uart_port *port)
99{
100 return (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
101 ALTERA_UART_STATUS_TMT_MSK) ? TIOCSER_TEMT : 0;
102}
103
104static unsigned int altera_uart_get_mctrl(struct uart_port *port)
105{
106 struct altera_uart *pp = container_of(port, struct altera_uart, port);
107 unsigned int sigs;
108
109 sigs = (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
110 ALTERA_UART_STATUS_CTS_MSK) ? TIOCM_CTS : 0;
111 sigs |= (pp->sigs & TIOCM_RTS);
112
113 return sigs;
114}
115
116static void altera_uart_set_mctrl(struct uart_port *port, unsigned int sigs)
117{
118 struct altera_uart *pp = container_of(port, struct altera_uart, port);
119
120 pp->sigs = sigs;
121 if (sigs & TIOCM_RTS)
122 pp->imr |= ALTERA_UART_CONTROL_RTS_MSK;
123 else
124 pp->imr &= ~ALTERA_UART_CONTROL_RTS_MSK;
125 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
126}
127
128static void altera_uart_start_tx(struct uart_port *port)
129{
130 struct altera_uart *pp = container_of(port, struct altera_uart, port);
131
132 pp->imr |= ALTERA_UART_CONTROL_TRDY_MSK;
133 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
134}
135
136static void altera_uart_stop_tx(struct uart_port *port)
137{
138 struct altera_uart *pp = container_of(port, struct altera_uart, port);
139
140 pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
141 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
142}
143
144static void altera_uart_stop_rx(struct uart_port *port)
145{
146 struct altera_uart *pp = container_of(port, struct altera_uart, port);
147
148 pp->imr &= ~ALTERA_UART_CONTROL_RRDY_MSK;
149 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
150}
151
152static void altera_uart_break_ctl(struct uart_port *port, int break_state)
153{
154 struct altera_uart *pp = container_of(port, struct altera_uart, port);
155 unsigned long flags;
156
157 spin_lock_irqsave(&port->lock, flags);
158 if (break_state == -1)
159 pp->imr |= ALTERA_UART_CONTROL_TRBK_MSK;
160 else
161 pp->imr &= ~ALTERA_UART_CONTROL_TRBK_MSK;
162 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
163 spin_unlock_irqrestore(&port->lock, flags);
164}
165
166static void altera_uart_enable_ms(struct uart_port *port)
167{
168}
169
170static void altera_uart_set_termios(struct uart_port *port,
171 struct ktermios *termios,
172 struct ktermios *old)
173{
174 unsigned long flags;
175 unsigned int baud, baudclk;
176
177 baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
178 baudclk = port->uartclk / baud;
179
180 if (old)
181 tty_termios_copy_hw(termios, old);
182 tty_termios_encode_baud_rate(termios, baud, baud);
183
184 spin_lock_irqsave(&port->lock, flags);
185 uart_update_timeout(port, termios->c_cflag, baud);
186 altera_uart_writel(port, baudclk, ALTERA_UART_DIVISOR_REG);
187 spin_unlock_irqrestore(&port->lock, flags);
188}
189
190static void altera_uart_rx_chars(struct altera_uart *pp)
191{
192 struct uart_port *port = &pp->port;
193 unsigned char ch, flag;
194 unsigned short status;
195
196 while ((status = altera_uart_readl(port, ALTERA_UART_STATUS_REG)) &
197 ALTERA_UART_STATUS_RRDY_MSK) {
198 ch = altera_uart_readl(port, ALTERA_UART_RXDATA_REG);
199 flag = TTY_NORMAL;
200 port->icount.rx++;
201
202 if (status & ALTERA_UART_STATUS_E_MSK) {
203 altera_uart_writel(port, status,
204 ALTERA_UART_STATUS_REG);
205
206 if (status & ALTERA_UART_STATUS_BRK_MSK) {
207 port->icount.brk++;
208 if (uart_handle_break(port))
209 continue;
210 } else if (status & ALTERA_UART_STATUS_PE_MSK) {
211 port->icount.parity++;
212 } else if (status & ALTERA_UART_STATUS_ROE_MSK) {
213 port->icount.overrun++;
214 } else if (status & ALTERA_UART_STATUS_FE_MSK) {
215 port->icount.frame++;
216 }
217
218 status &= port->read_status_mask;
219
220 if (status & ALTERA_UART_STATUS_BRK_MSK)
221 flag = TTY_BREAK;
222 else if (status & ALTERA_UART_STATUS_PE_MSK)
223 flag = TTY_PARITY;
224 else if (status & ALTERA_UART_STATUS_FE_MSK)
225 flag = TTY_FRAME;
226 }
227
228 if (uart_handle_sysrq_char(port, ch))
229 continue;
230 uart_insert_char(port, status, ALTERA_UART_STATUS_ROE_MSK, ch,
231 flag);
232 }
233
234 tty_flip_buffer_push(port->state->port.tty);
235}
236
237static void altera_uart_tx_chars(struct altera_uart *pp)
238{
239 struct uart_port *port = &pp->port;
240 struct circ_buf *xmit = &port->state->xmit;
241
242 if (port->x_char) {
243 /* Send special char - probably flow control */
244 altera_uart_writel(port, port->x_char, ALTERA_UART_TXDATA_REG);
245 port->x_char = 0;
246 port->icount.tx++;
247 return;
248 }
249
250 while (altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
251 ALTERA_UART_STATUS_TRDY_MSK) {
252 if (xmit->head == xmit->tail)
253 break;
254 altera_uart_writel(port, xmit->buf[xmit->tail],
255 ALTERA_UART_TXDATA_REG);
256 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
257 port->icount.tx++;
258 }
259
260 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
261 uart_write_wakeup(port);
262
263 if (xmit->head == xmit->tail) {
264 pp->imr &= ~ALTERA_UART_CONTROL_TRDY_MSK;
265 altera_uart_writel(port, pp->imr, ALTERA_UART_CONTROL_REG);
266 }
267}
268
269static irqreturn_t altera_uart_interrupt(int irq, void *data)
270{
271 struct uart_port *port = data;
272 struct altera_uart *pp = container_of(port, struct altera_uart, port);
273 unsigned int isr;
274
275 isr = altera_uart_readl(port, ALTERA_UART_STATUS_REG) & pp->imr;
276
277 spin_lock(&port->lock);
278 if (isr & ALTERA_UART_STATUS_RRDY_MSK)
279 altera_uart_rx_chars(pp);
280 if (isr & ALTERA_UART_STATUS_TRDY_MSK)
281 altera_uart_tx_chars(pp);
282 spin_unlock(&port->lock);
283
284 return IRQ_RETVAL(isr);
285}
286
287static void altera_uart_timer(unsigned long data)
288{
289 struct uart_port *port = (void *)data;
290 struct altera_uart *pp = container_of(port, struct altera_uart, port);
291
292 altera_uart_interrupt(0, port);
293 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
294}
295
296static void altera_uart_config_port(struct uart_port *port, int flags)
297{
298 port->type = PORT_ALTERA_UART;
299
300 /* Clear mask, so no surprise interrupts. */
301 altera_uart_writel(port, 0, ALTERA_UART_CONTROL_REG);
302 /* Clear status register */
303 altera_uart_writel(port, 0, ALTERA_UART_STATUS_REG);
304}
305
306static int altera_uart_startup(struct uart_port *port)
307{
308 struct altera_uart *pp = container_of(port, struct altera_uart, port);
309 unsigned long flags;
310 int ret;
311
312 if (!port->irq) {
313 setup_timer(&pp->tmr, altera_uart_timer, (unsigned long)port);
314 mod_timer(&pp->tmr, jiffies + uart_poll_timeout(port));
315 return 0;
316 }
317
318 ret = request_irq(port->irq, altera_uart_interrupt, IRQF_DISABLED,
319 DRV_NAME, port);
320 if (ret) {
321 pr_err(DRV_NAME ": unable to attach Altera UART %d "
322 "interrupt vector=%d\n", port->line, port->irq);
323 return ret;
324 }
325
326 spin_lock_irqsave(&port->lock, flags);
327
328 /* Enable RX interrupts now */
329 pp->imr = ALTERA_UART_CONTROL_RRDY_MSK;
330 writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
331
332 spin_unlock_irqrestore(&port->lock, flags);
333
334 return 0;
335}
336
337static void altera_uart_shutdown(struct uart_port *port)
338{
339 struct altera_uart *pp = container_of(port, struct altera_uart, port);
340 unsigned long flags;
341
342 spin_lock_irqsave(&port->lock, flags);
343
344 /* Disable all interrupts now */
345 pp->imr = 0;
346 writel(pp->imr, port->membase + ALTERA_UART_CONTROL_REG);
347
348 spin_unlock_irqrestore(&port->lock, flags);
349
350 if (port->irq)
351 free_irq(port->irq, port);
352 else
353 del_timer_sync(&pp->tmr);
354}
355
356static const char *altera_uart_type(struct uart_port *port)
357{
358 return (port->type == PORT_ALTERA_UART) ? "Altera UART" : NULL;
359}
360
361static int altera_uart_request_port(struct uart_port *port)
362{
363 /* UARTs always present */
364 return 0;
365}
366
367static void altera_uart_release_port(struct uart_port *port)
368{
369 /* Nothing to release... */
370}
371
372static int altera_uart_verify_port(struct uart_port *port,
373 struct serial_struct *ser)
374{
375 if ((ser->type != PORT_UNKNOWN) && (ser->type != PORT_ALTERA_UART))
376 return -EINVAL;
377 return 0;
378}
379
380/*
381 * Define the basic serial functions we support.
382 */
383static struct uart_ops altera_uart_ops = {
384 .tx_empty = altera_uart_tx_empty,
385 .get_mctrl = altera_uart_get_mctrl,
386 .set_mctrl = altera_uart_set_mctrl,
387 .start_tx = altera_uart_start_tx,
388 .stop_tx = altera_uart_stop_tx,
389 .stop_rx = altera_uart_stop_rx,
390 .enable_ms = altera_uart_enable_ms,
391 .break_ctl = altera_uart_break_ctl,
392 .startup = altera_uart_startup,
393 .shutdown = altera_uart_shutdown,
394 .set_termios = altera_uart_set_termios,
395 .type = altera_uart_type,
396 .request_port = altera_uart_request_port,
397 .release_port = altera_uart_release_port,
398 .config_port = altera_uart_config_port,
399 .verify_port = altera_uart_verify_port,
400};
401
402static struct altera_uart altera_uart_ports[CONFIG_SERIAL_ALTERA_UART_MAXPORTS];
403
404#if defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
405
406int __init early_altera_uart_setup(struct altera_uart_platform_uart *platp)
407{
408 struct uart_port *port;
409 int i;
410
411 for (i = 0; i < CONFIG_SERIAL_ALTERA_UART_MAXPORTS && platp[i].mapbase; i++) {
412 port = &altera_uart_ports[i].port;
413
414 port->line = i;
415 port->type = PORT_ALTERA_UART;
416 port->mapbase = platp[i].mapbase;
417 port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
418 port->iotype = SERIAL_IO_MEM;
419 port->irq = platp[i].irq;
420 port->uartclk = platp[i].uartclk;
421 port->flags = UPF_BOOT_AUTOCONF;
422 port->ops = &altera_uart_ops;
423 port->private_data = platp;
424 }
425
426 return 0;
427}
428
429static void altera_uart_console_putc(struct uart_port *port, const char c)
430{
431 while (!(altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
432 ALTERA_UART_STATUS_TRDY_MSK))
433 cpu_relax();
434
435 writel(c, port->membase + ALTERA_UART_TXDATA_REG);
436}
437
438static void altera_uart_console_write(struct console *co, const char *s,
439 unsigned int count)
440{
441 struct uart_port *port = &(altera_uart_ports + co->index)->port;
442
443 for (; count; count--, s++) {
444 altera_uart_console_putc(port, *s);
445 if (*s == '\n')
446 altera_uart_console_putc(port, '\r');
447 }
448}
449
450static int __init altera_uart_console_setup(struct console *co, char *options)
451{
452 struct uart_port *port;
453 int baud = CONFIG_SERIAL_ALTERA_UART_BAUDRATE;
454 int bits = 8;
455 int parity = 'n';
456 int flow = 'n';
457
458 if (co->index < 0 || co->index >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
459 return -EINVAL;
460 port = &altera_uart_ports[co->index].port;
461 if (!port->membase)
462 return -ENODEV;
463
464 if (options)
465 uart_parse_options(options, &baud, &parity, &bits, &flow);
466
467 return uart_set_options(port, co, baud, parity, bits, flow);
468}
469
470static struct uart_driver altera_uart_driver;
471
472static struct console altera_uart_console = {
473 .name = "ttyAL",
474 .write = altera_uart_console_write,
475 .device = uart_console_device,
476 .setup = altera_uart_console_setup,
477 .flags = CON_PRINTBUFFER,
478 .index = -1,
479 .data = &altera_uart_driver,
480};
481
482static int __init altera_uart_console_init(void)
483{
484 register_console(&altera_uart_console);
485 return 0;
486}
487
488console_initcall(altera_uart_console_init);
489
490#define ALTERA_UART_CONSOLE (&altera_uart_console)
491
492#else
493
494#define ALTERA_UART_CONSOLE NULL
495
496#endif /* CONFIG_ALTERA_UART_CONSOLE */
497
498/*
499 * Define the altera_uart UART driver structure.
500 */
501static struct uart_driver altera_uart_driver = {
502 .owner = THIS_MODULE,
503 .driver_name = DRV_NAME,
504 .dev_name = "ttyAL",
505 .major = SERIAL_ALTERA_MAJOR,
506 .minor = SERIAL_ALTERA_MINOR,
507 .nr = CONFIG_SERIAL_ALTERA_UART_MAXPORTS,
508 .cons = ALTERA_UART_CONSOLE,
509};
510
511#ifdef CONFIG_OF
512static int altera_uart_get_of_uartclk(struct platform_device *pdev,
513 struct uart_port *port)
514{
515 int len;
516 const __be32 *clk;
517
518 clk = of_get_property(pdev->dev.of_node, "clock-frequency", &len);
519 if (!clk || len < sizeof(__be32))
520 return -ENODEV;
521
522 port->uartclk = be32_to_cpup(clk);
523
524 return 0;
525}
526#else
527static int altera_uart_get_of_uartclk(struct platform_device *pdev,
528 struct uart_port *port)
529{
530 return -ENODEV;
531}
532#endif /* CONFIG_OF */
533
534static int __devinit altera_uart_probe(struct platform_device *pdev)
535{
536 struct altera_uart_platform_uart *platp = pdev->dev.platform_data;
537 struct uart_port *port;
538 struct resource *res_mem;
539 struct resource *res_irq;
540 int i = pdev->id;
541 int ret;
542
543 /* if id is -1 scan for a free id and use that one */
544 if (i == -1) {
545 for (i = 0; i < CONFIG_SERIAL_ALTERA_UART_MAXPORTS; i++)
546 if (altera_uart_ports[i].port.mapbase == 0)
547 break;
548 }
549
550 if (i < 0 || i >= CONFIG_SERIAL_ALTERA_UART_MAXPORTS)
551 return -EINVAL;
552
553 port = &altera_uart_ports[i].port;
554
555 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
556 if (res_mem)
557 port->mapbase = res_mem->start;
558 else if (platp->mapbase)
559 port->mapbase = platp->mapbase;
560 else
561 return -EINVAL;
562
563 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
564 if (res_irq)
565 port->irq = res_irq->start;
566 else if (platp->irq)
567 port->irq = platp->irq;
568
569 /* Check platform data first so we can override device node data */
570 if (platp)
571 port->uartclk = platp->uartclk;
572 else {
573 ret = altera_uart_get_of_uartclk(pdev, port);
574 if (ret)
575 return ret;
576 }
577
578 port->membase = ioremap(port->mapbase, ALTERA_UART_SIZE);
579 if (!port->membase)
580 return -ENOMEM;
581
582 if (platp)
583 port->regshift = platp->bus_shift;
584 else
585 port->regshift = 0;
586
587 port->line = i;
588 port->type = PORT_ALTERA_UART;
589 port->iotype = SERIAL_IO_MEM;
590 port->ops = &altera_uart_ops;
591 port->flags = UPF_BOOT_AUTOCONF;
592
593 dev_set_drvdata(&pdev->dev, port);
594
595 uart_add_one_port(&altera_uart_driver, port);
596
597 return 0;
598}
599
600static int __devexit altera_uart_remove(struct platform_device *pdev)
601{
602 struct uart_port *port = dev_get_drvdata(&pdev->dev);
603
604 if (port) {
605 uart_remove_one_port(&altera_uart_driver, port);
606 dev_set_drvdata(&pdev->dev, NULL);
607 port->mapbase = 0;
608 }
609
610 return 0;
611}
612
613#ifdef CONFIG_OF
614static struct of_device_id altera_uart_match[] = {
615 { .compatible = "ALTR,uart-1.0", },
616 {},
617};
618MODULE_DEVICE_TABLE(of, altera_uart_match);
619#else
620#define altera_uart_match NULL
621#endif /* CONFIG_OF */
622
623static struct platform_driver altera_uart_platform_driver = {
624 .probe = altera_uart_probe,
625 .remove = __devexit_p(altera_uart_remove),
626 .driver = {
627 .name = DRV_NAME,
628 .owner = THIS_MODULE,
629 .of_match_table = altera_uart_match,
630 },
631};
632
633static int __init altera_uart_init(void)
634{
635 int rc;
636
637 rc = uart_register_driver(&altera_uart_driver);
638 if (rc)
639 return rc;
640 rc = platform_driver_register(&altera_uart_platform_driver);
641 if (rc) {
642 uart_unregister_driver(&altera_uart_driver);
643 return rc;
644 }
645 return 0;
646}
647
648static void __exit altera_uart_exit(void)
649{
650 platform_driver_unregister(&altera_uart_platform_driver);
651 uart_unregister_driver(&altera_uart_driver);
652}
653
654module_init(altera_uart_init);
655module_exit(altera_uart_exit);
656
657MODULE_DESCRIPTION("Altera UART driver");
658MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
659MODULE_LICENSE("GPL");
660MODULE_ALIAS("platform:" DRV_NAME);
661MODULE_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..c0d10c4ddb73
--- /dev/null
+++ b/drivers/tty/serial/amba-pl010.c
@@ -0,0 +1,823 @@
1/*
2 * Driver for AMBA serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
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 as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
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 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * This is a generic driver for ARM AMBA-type serial ports. They
24 * have a lot of 16550-like features, but are not register compatible.
25 * Note that although they do have CTS, DCD and DSR inputs, they do
26 * not have an RI input, nor do they have DTR or RTS outputs. If
27 * required, these have to be supplied via some other means (eg, GPIO)
28 * and hooked into this driver.
29 */
30
31#if defined(CONFIG_SERIAL_AMBA_PL010_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/device.h>
41#include <linux/tty.h>
42#include <linux/tty_flip.h>
43#include <linux/serial_core.h>
44#include <linux/serial.h>
45#include <linux/amba/bus.h>
46#include <linux/amba/serial.h>
47#include <linux/clk.h>
48#include <linux/slab.h>
49
50#include <asm/io.h>
51
52#define UART_NR 8
53
54#define SERIAL_AMBA_MAJOR 204
55#define SERIAL_AMBA_MINOR 16
56#define SERIAL_AMBA_NR UART_NR
57
58#define AMBA_ISR_PASS_LIMIT 256
59
60#define UART_RX_DATA(s) (((s) & UART01x_FR_RXFE) == 0)
61#define UART_TX_READY(s) (((s) & UART01x_FR_TXFF) == 0)
62
63#define UART_DUMMY_RSR_RX 256
64#define UART_PORT_SIZE 64
65
66/*
67 * We wrap our port structure around the generic uart_port.
68 */
69struct uart_amba_port {
70 struct uart_port port;
71 struct clk *clk;
72 struct amba_device *dev;
73 struct amba_pl010_data *data;
74 unsigned int old_status;
75};
76
77static void pl010_stop_tx(struct uart_port *port)
78{
79 struct uart_amba_port *uap = (struct uart_amba_port *)port;
80 unsigned int cr;
81
82 cr = readb(uap->port.membase + UART010_CR);
83 cr &= ~UART010_CR_TIE;
84 writel(cr, uap->port.membase + UART010_CR);
85}
86
87static void pl010_start_tx(struct uart_port *port)
88{
89 struct uart_amba_port *uap = (struct uart_amba_port *)port;
90 unsigned int cr;
91
92 cr = readb(uap->port.membase + UART010_CR);
93 cr |= UART010_CR_TIE;
94 writel(cr, uap->port.membase + UART010_CR);
95}
96
97static void pl010_stop_rx(struct uart_port *port)
98{
99 struct uart_amba_port *uap = (struct uart_amba_port *)port;
100 unsigned int cr;
101
102 cr = readb(uap->port.membase + UART010_CR);
103 cr &= ~(UART010_CR_RIE | UART010_CR_RTIE);
104 writel(cr, uap->port.membase + UART010_CR);
105}
106
107static void pl010_enable_ms(struct uart_port *port)
108{
109 struct uart_amba_port *uap = (struct uart_amba_port *)port;
110 unsigned int cr;
111
112 cr = readb(uap->port.membase + UART010_CR);
113 cr |= UART010_CR_MSIE;
114 writel(cr, uap->port.membase + UART010_CR);
115}
116
117static void pl010_rx_chars(struct uart_amba_port *uap)
118{
119 struct tty_struct *tty = uap->port.state->port.tty;
120 unsigned int status, ch, flag, rsr, max_count = 256;
121
122 status = readb(uap->port.membase + UART01x_FR);
123 while (UART_RX_DATA(status) && max_count--) {
124 ch = readb(uap->port.membase + UART01x_DR);
125 flag = TTY_NORMAL;
126
127 uap->port.icount.rx++;
128
129 /*
130 * Note that the error handling code is
131 * out of the main execution path
132 */
133 rsr = readb(uap->port.membase + UART01x_RSR) | UART_DUMMY_RSR_RX;
134 if (unlikely(rsr & UART01x_RSR_ANY)) {
135 writel(0, uap->port.membase + UART01x_ECR);
136
137 if (rsr & UART01x_RSR_BE) {
138 rsr &= ~(UART01x_RSR_FE | UART01x_RSR_PE);
139 uap->port.icount.brk++;
140 if (uart_handle_break(&uap->port))
141 goto ignore_char;
142 } else if (rsr & UART01x_RSR_PE)
143 uap->port.icount.parity++;
144 else if (rsr & UART01x_RSR_FE)
145 uap->port.icount.frame++;
146 if (rsr & UART01x_RSR_OE)
147 uap->port.icount.overrun++;
148
149 rsr &= uap->port.read_status_mask;
150
151 if (rsr & UART01x_RSR_BE)
152 flag = TTY_BREAK;
153 else if (rsr & UART01x_RSR_PE)
154 flag = TTY_PARITY;
155 else if (rsr & UART01x_RSR_FE)
156 flag = TTY_FRAME;
157 }
158
159 if (uart_handle_sysrq_char(&uap->port, ch))
160 goto ignore_char;
161
162 uart_insert_char(&uap->port, rsr, UART01x_RSR_OE, ch, flag);
163
164 ignore_char:
165 status = readb(uap->port.membase + UART01x_FR);
166 }
167 spin_unlock(&uap->port.lock);
168 tty_flip_buffer_push(tty);
169 spin_lock(&uap->port.lock);
170}
171
172static void pl010_tx_chars(struct uart_amba_port *uap)
173{
174 struct circ_buf *xmit = &uap->port.state->xmit;
175 int count;
176
177 if (uap->port.x_char) {
178 writel(uap->port.x_char, uap->port.membase + UART01x_DR);
179 uap->port.icount.tx++;
180 uap->port.x_char = 0;
181 return;
182 }
183 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
184 pl010_stop_tx(&uap->port);
185 return;
186 }
187
188 count = uap->port.fifosize >> 1;
189 do {
190 writel(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
191 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
192 uap->port.icount.tx++;
193 if (uart_circ_empty(xmit))
194 break;
195 } while (--count > 0);
196
197 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
198 uart_write_wakeup(&uap->port);
199
200 if (uart_circ_empty(xmit))
201 pl010_stop_tx(&uap->port);
202}
203
204static void pl010_modem_status(struct uart_amba_port *uap)
205{
206 unsigned int status, delta;
207
208 writel(0, uap->port.membase + UART010_ICR);
209
210 status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
211
212 delta = status ^ uap->old_status;
213 uap->old_status = status;
214
215 if (!delta)
216 return;
217
218 if (delta & UART01x_FR_DCD)
219 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
220
221 if (delta & UART01x_FR_DSR)
222 uap->port.icount.dsr++;
223
224 if (delta & UART01x_FR_CTS)
225 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
226
227 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
228}
229
230static irqreturn_t pl010_int(int irq, void *dev_id)
231{
232 struct uart_amba_port *uap = dev_id;
233 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
234 int handled = 0;
235
236 spin_lock(&uap->port.lock);
237
238 status = readb(uap->port.membase + UART010_IIR);
239 if (status) {
240 do {
241 if (status & (UART010_IIR_RTIS | UART010_IIR_RIS))
242 pl010_rx_chars(uap);
243 if (status & UART010_IIR_MIS)
244 pl010_modem_status(uap);
245 if (status & UART010_IIR_TIS)
246 pl010_tx_chars(uap);
247
248 if (pass_counter-- == 0)
249 break;
250
251 status = readb(uap->port.membase + UART010_IIR);
252 } while (status & (UART010_IIR_RTIS | UART010_IIR_RIS |
253 UART010_IIR_TIS));
254 handled = 1;
255 }
256
257 spin_unlock(&uap->port.lock);
258
259 return IRQ_RETVAL(handled);
260}
261
262static unsigned int pl010_tx_empty(struct uart_port *port)
263{
264 struct uart_amba_port *uap = (struct uart_amba_port *)port;
265 unsigned int status = readb(uap->port.membase + UART01x_FR);
266 return status & UART01x_FR_BUSY ? 0 : TIOCSER_TEMT;
267}
268
269static unsigned int pl010_get_mctrl(struct uart_port *port)
270{
271 struct uart_amba_port *uap = (struct uart_amba_port *)port;
272 unsigned int result = 0;
273 unsigned int status;
274
275 status = readb(uap->port.membase + UART01x_FR);
276 if (status & UART01x_FR_DCD)
277 result |= TIOCM_CAR;
278 if (status & UART01x_FR_DSR)
279 result |= TIOCM_DSR;
280 if (status & UART01x_FR_CTS)
281 result |= TIOCM_CTS;
282
283 return result;
284}
285
286static void pl010_set_mctrl(struct uart_port *port, unsigned int mctrl)
287{
288 struct uart_amba_port *uap = (struct uart_amba_port *)port;
289
290 if (uap->data)
291 uap->data->set_mctrl(uap->dev, uap->port.membase, mctrl);
292}
293
294static void pl010_break_ctl(struct uart_port *port, int break_state)
295{
296 struct uart_amba_port *uap = (struct uart_amba_port *)port;
297 unsigned long flags;
298 unsigned int lcr_h;
299
300 spin_lock_irqsave(&uap->port.lock, flags);
301 lcr_h = readb(uap->port.membase + UART010_LCRH);
302 if (break_state == -1)
303 lcr_h |= UART01x_LCRH_BRK;
304 else
305 lcr_h &= ~UART01x_LCRH_BRK;
306 writel(lcr_h, uap->port.membase + UART010_LCRH);
307 spin_unlock_irqrestore(&uap->port.lock, flags);
308}
309
310static int pl010_startup(struct uart_port *port)
311{
312 struct uart_amba_port *uap = (struct uart_amba_port *)port;
313 int retval;
314
315 /*
316 * Try to enable the clock producer.
317 */
318 retval = clk_enable(uap->clk);
319 if (retval)
320 goto out;
321
322 uap->port.uartclk = clk_get_rate(uap->clk);
323
324 /*
325 * Allocate the IRQ
326 */
327 retval = request_irq(uap->port.irq, pl010_int, 0, "uart-pl010", uap);
328 if (retval)
329 goto clk_dis;
330
331 /*
332 * initialise the old status of the modem signals
333 */
334 uap->old_status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
335
336 /*
337 * Finally, enable interrupts
338 */
339 writel(UART01x_CR_UARTEN | UART010_CR_RIE | UART010_CR_RTIE,
340 uap->port.membase + UART010_CR);
341
342 return 0;
343
344 clk_dis:
345 clk_disable(uap->clk);
346 out:
347 return retval;
348}
349
350static void pl010_shutdown(struct uart_port *port)
351{
352 struct uart_amba_port *uap = (struct uart_amba_port *)port;
353
354 /*
355 * Free the interrupt
356 */
357 free_irq(uap->port.irq, uap);
358
359 /*
360 * disable all interrupts, disable the port
361 */
362 writel(0, uap->port.membase + UART010_CR);
363
364 /* disable break condition and fifos */
365 writel(readb(uap->port.membase + UART010_LCRH) &
366 ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN),
367 uap->port.membase + UART010_LCRH);
368
369 /*
370 * Shut down the clock producer
371 */
372 clk_disable(uap->clk);
373}
374
375static void
376pl010_set_termios(struct uart_port *port, struct ktermios *termios,
377 struct ktermios *old)
378{
379 struct uart_amba_port *uap = (struct uart_amba_port *)port;
380 unsigned int lcr_h, old_cr;
381 unsigned long flags;
382 unsigned int baud, quot;
383
384 /*
385 * Ask the core to calculate the divisor for us.
386 */
387 baud = uart_get_baud_rate(port, termios, old, 0, uap->port.uartclk/16);
388 quot = uart_get_divisor(port, baud);
389
390 switch (termios->c_cflag & CSIZE) {
391 case CS5:
392 lcr_h = UART01x_LCRH_WLEN_5;
393 break;
394 case CS6:
395 lcr_h = UART01x_LCRH_WLEN_6;
396 break;
397 case CS7:
398 lcr_h = UART01x_LCRH_WLEN_7;
399 break;
400 default: // CS8
401 lcr_h = UART01x_LCRH_WLEN_8;
402 break;
403 }
404 if (termios->c_cflag & CSTOPB)
405 lcr_h |= UART01x_LCRH_STP2;
406 if (termios->c_cflag & PARENB) {
407 lcr_h |= UART01x_LCRH_PEN;
408 if (!(termios->c_cflag & PARODD))
409 lcr_h |= UART01x_LCRH_EPS;
410 }
411 if (uap->port.fifosize > 1)
412 lcr_h |= UART01x_LCRH_FEN;
413
414 spin_lock_irqsave(&uap->port.lock, flags);
415
416 /*
417 * Update the per-port timeout.
418 */
419 uart_update_timeout(port, termios->c_cflag, baud);
420
421 uap->port.read_status_mask = UART01x_RSR_OE;
422 if (termios->c_iflag & INPCK)
423 uap->port.read_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
424 if (termios->c_iflag & (BRKINT | PARMRK))
425 uap->port.read_status_mask |= UART01x_RSR_BE;
426
427 /*
428 * Characters to ignore
429 */
430 uap->port.ignore_status_mask = 0;
431 if (termios->c_iflag & IGNPAR)
432 uap->port.ignore_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
433 if (termios->c_iflag & IGNBRK) {
434 uap->port.ignore_status_mask |= UART01x_RSR_BE;
435 /*
436 * If we're ignoring parity and break indicators,
437 * ignore overruns too (for real raw support).
438 */
439 if (termios->c_iflag & IGNPAR)
440 uap->port.ignore_status_mask |= UART01x_RSR_OE;
441 }
442
443 /*
444 * Ignore all characters if CREAD is not set.
445 */
446 if ((termios->c_cflag & CREAD) == 0)
447 uap->port.ignore_status_mask |= UART_DUMMY_RSR_RX;
448
449 /* first, disable everything */
450 old_cr = readb(uap->port.membase + UART010_CR) & ~UART010_CR_MSIE;
451
452 if (UART_ENABLE_MS(port, termios->c_cflag))
453 old_cr |= UART010_CR_MSIE;
454
455 writel(0, uap->port.membase + UART010_CR);
456
457 /* Set baud rate */
458 quot -= 1;
459 writel((quot & 0xf00) >> 8, uap->port.membase + UART010_LCRM);
460 writel(quot & 0xff, uap->port.membase + UART010_LCRL);
461
462 /*
463 * ----------v----------v----------v----------v-----
464 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
465 * ----------^----------^----------^----------^-----
466 */
467 writel(lcr_h, uap->port.membase + UART010_LCRH);
468 writel(old_cr, uap->port.membase + UART010_CR);
469
470 spin_unlock_irqrestore(&uap->port.lock, flags);
471}
472
473static void pl010_set_ldisc(struct uart_port *port, int new)
474{
475 if (new == N_PPS) {
476 port->flags |= UPF_HARDPPS_CD;
477 pl010_enable_ms(port);
478 } else
479 port->flags &= ~UPF_HARDPPS_CD;
480}
481
482static const char *pl010_type(struct uart_port *port)
483{
484 return port->type == PORT_AMBA ? "AMBA" : NULL;
485}
486
487/*
488 * Release the memory region(s) being used by 'port'
489 */
490static void pl010_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 pl010_request_port(struct uart_port *port)
499{
500 return request_mem_region(port->mapbase, UART_PORT_SIZE, "uart-pl010")
501 != NULL ? 0 : -EBUSY;
502}
503
504/*
505 * Configure/autoconfigure the port.
506 */
507static void pl010_config_port(struct uart_port *port, int flags)
508{
509 if (flags & UART_CONFIG_TYPE) {
510 port->type = PORT_AMBA;
511 pl010_request_port(port);
512 }
513}
514
515/*
516 * verify the new serial_struct (for TIOCSSERIAL).
517 */
518static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
519{
520 int ret = 0;
521 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
522 ret = -EINVAL;
523 if (ser->irq < 0 || ser->irq >= nr_irqs)
524 ret = -EINVAL;
525 if (ser->baud_base < 9600)
526 ret = -EINVAL;
527 return ret;
528}
529
530static struct uart_ops amba_pl010_pops = {
531 .tx_empty = pl010_tx_empty,
532 .set_mctrl = pl010_set_mctrl,
533 .get_mctrl = pl010_get_mctrl,
534 .stop_tx = pl010_stop_tx,
535 .start_tx = pl010_start_tx,
536 .stop_rx = pl010_stop_rx,
537 .enable_ms = pl010_enable_ms,
538 .break_ctl = pl010_break_ctl,
539 .startup = pl010_startup,
540 .shutdown = pl010_shutdown,
541 .set_termios = pl010_set_termios,
542 .set_ldisc = pl010_set_ldisc,
543 .type = pl010_type,
544 .release_port = pl010_release_port,
545 .request_port = pl010_request_port,
546 .config_port = pl010_config_port,
547 .verify_port = pl010_verify_port,
548};
549
550static struct uart_amba_port *amba_ports[UART_NR];
551
552#ifdef CONFIG_SERIAL_AMBA_PL010_CONSOLE
553
554static void pl010_console_putchar(struct uart_port *port, int ch)
555{
556 struct uart_amba_port *uap = (struct uart_amba_port *)port;
557 unsigned int status;
558
559 do {
560 status = readb(uap->port.membase + UART01x_FR);
561 barrier();
562 } while (!UART_TX_READY(status));
563 writel(ch, uap->port.membase + UART01x_DR);
564}
565
566static void
567pl010_console_write(struct console *co, const char *s, unsigned int count)
568{
569 struct uart_amba_port *uap = amba_ports[co->index];
570 unsigned int status, old_cr;
571
572 clk_enable(uap->clk);
573
574 /*
575 * First save the CR then disable the interrupts
576 */
577 old_cr = readb(uap->port.membase + UART010_CR);
578 writel(UART01x_CR_UARTEN, uap->port.membase + UART010_CR);
579
580 uart_console_write(&uap->port, s, count, pl010_console_putchar);
581
582 /*
583 * Finally, wait for transmitter to become empty
584 * and restore the TCR
585 */
586 do {
587 status = readb(uap->port.membase + UART01x_FR);
588 barrier();
589 } while (status & UART01x_FR_BUSY);
590 writel(old_cr, uap->port.membase + UART010_CR);
591
592 clk_disable(uap->clk);
593}
594
595static void __init
596pl010_console_get_options(struct uart_amba_port *uap, int *baud,
597 int *parity, int *bits)
598{
599 if (readb(uap->port.membase + UART010_CR) & UART01x_CR_UARTEN) {
600 unsigned int lcr_h, quot;
601 lcr_h = readb(uap->port.membase + UART010_LCRH);
602
603 *parity = 'n';
604 if (lcr_h & UART01x_LCRH_PEN) {
605 if (lcr_h & UART01x_LCRH_EPS)
606 *parity = 'e';
607 else
608 *parity = 'o';
609 }
610
611 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
612 *bits = 7;
613 else
614 *bits = 8;
615
616 quot = readb(uap->port.membase + UART010_LCRL) |
617 readb(uap->port.membase + UART010_LCRM) << 8;
618 *baud = uap->port.uartclk / (16 * (quot + 1));
619 }
620}
621
622static int __init pl010_console_setup(struct console *co, char *options)
623{
624 struct uart_amba_port *uap;
625 int baud = 38400;
626 int bits = 8;
627 int parity = 'n';
628 int flow = 'n';
629
630 /*
631 * Check whether an invalid uart number has been specified, and
632 * if so, search for the first available port that does have
633 * console support.
634 */
635 if (co->index >= UART_NR)
636 co->index = 0;
637 uap = amba_ports[co->index];
638 if (!uap)
639 return -ENODEV;
640
641 uap->port.uartclk = clk_get_rate(uap->clk);
642
643 if (options)
644 uart_parse_options(options, &baud, &parity, &bits, &flow);
645 else
646 pl010_console_get_options(uap, &baud, &parity, &bits);
647
648 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
649}
650
651static struct uart_driver amba_reg;
652static struct console amba_console = {
653 .name = "ttyAM",
654 .write = pl010_console_write,
655 .device = uart_console_device,
656 .setup = pl010_console_setup,
657 .flags = CON_PRINTBUFFER,
658 .index = -1,
659 .data = &amba_reg,
660};
661
662#define AMBA_CONSOLE &amba_console
663#else
664#define AMBA_CONSOLE NULL
665#endif
666
667static struct uart_driver amba_reg = {
668 .owner = THIS_MODULE,
669 .driver_name = "ttyAM",
670 .dev_name = "ttyAM",
671 .major = SERIAL_AMBA_MAJOR,
672 .minor = SERIAL_AMBA_MINOR,
673 .nr = UART_NR,
674 .cons = AMBA_CONSOLE,
675};
676
677static int pl010_probe(struct amba_device *dev, const struct amba_id *id)
678{
679 struct uart_amba_port *uap;
680 void __iomem *base;
681 int i, ret;
682
683 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
684 if (amba_ports[i] == NULL)
685 break;
686
687 if (i == ARRAY_SIZE(amba_ports)) {
688 ret = -EBUSY;
689 goto out;
690 }
691
692 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
693 if (!uap) {
694 ret = -ENOMEM;
695 goto out;
696 }
697
698 base = ioremap(dev->res.start, resource_size(&dev->res));
699 if (!base) {
700 ret = -ENOMEM;
701 goto free;
702 }
703
704 uap->clk = clk_get(&dev->dev, NULL);
705 if (IS_ERR(uap->clk)) {
706 ret = PTR_ERR(uap->clk);
707 goto unmap;
708 }
709
710 uap->port.dev = &dev->dev;
711 uap->port.mapbase = dev->res.start;
712 uap->port.membase = base;
713 uap->port.iotype = UPIO_MEM;
714 uap->port.irq = dev->irq[0];
715 uap->port.fifosize = 16;
716 uap->port.ops = &amba_pl010_pops;
717 uap->port.flags = UPF_BOOT_AUTOCONF;
718 uap->port.line = i;
719 uap->dev = dev;
720 uap->data = dev->dev.platform_data;
721
722 amba_ports[i] = uap;
723
724 amba_set_drvdata(dev, uap);
725 ret = uart_add_one_port(&amba_reg, &uap->port);
726 if (ret) {
727 amba_set_drvdata(dev, NULL);
728 amba_ports[i] = NULL;
729 clk_put(uap->clk);
730 unmap:
731 iounmap(base);
732 free:
733 kfree(uap);
734 }
735 out:
736 return ret;
737}
738
739static int pl010_remove(struct amba_device *dev)
740{
741 struct uart_amba_port *uap = amba_get_drvdata(dev);
742 int i;
743
744 amba_set_drvdata(dev, NULL);
745
746 uart_remove_one_port(&amba_reg, &uap->port);
747
748 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
749 if (amba_ports[i] == uap)
750 amba_ports[i] = NULL;
751
752 iounmap(uap->port.membase);
753 clk_put(uap->clk);
754 kfree(uap);
755 return 0;
756}
757
758static int pl010_suspend(struct amba_device *dev, pm_message_t state)
759{
760 struct uart_amba_port *uap = amba_get_drvdata(dev);
761
762 if (uap)
763 uart_suspend_port(&amba_reg, &uap->port);
764
765 return 0;
766}
767
768static int pl010_resume(struct amba_device *dev)
769{
770 struct uart_amba_port *uap = amba_get_drvdata(dev);
771
772 if (uap)
773 uart_resume_port(&amba_reg, &uap->port);
774
775 return 0;
776}
777
778static struct amba_id pl010_ids[] = {
779 {
780 .id = 0x00041010,
781 .mask = 0x000fffff,
782 },
783 { 0, 0 },
784};
785
786static struct amba_driver pl010_driver = {
787 .drv = {
788 .name = "uart-pl010",
789 },
790 .id_table = pl010_ids,
791 .probe = pl010_probe,
792 .remove = pl010_remove,
793 .suspend = pl010_suspend,
794 .resume = pl010_resume,
795};
796
797static int __init pl010_init(void)
798{
799 int ret;
800
801 printk(KERN_INFO "Serial: AMBA driver\n");
802
803 ret = uart_register_driver(&amba_reg);
804 if (ret == 0) {
805 ret = amba_driver_register(&pl010_driver);
806 if (ret)
807 uart_unregister_driver(&amba_reg);
808 }
809 return ret;
810}
811
812static void __exit pl010_exit(void)
813{
814 amba_driver_unregister(&pl010_driver);
815 uart_unregister_driver(&amba_reg);
816}
817
818module_init(pl010_init);
819module_exit(pl010_exit);
820
821MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
822MODULE_DESCRIPTION("ARM AMBA serial port driver");
823MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
new file mode 100644
index 000000000000..f5f6831b0a64
--- /dev/null
+++ b/drivers/tty/serial/amba-pl011.c
@@ -0,0 +1,2027 @@
1/*
2 * Driver for AMBA serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
8 * Copyright (C) 2010 ST-Ericsson SA
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 * This is a generic driver for ARM AMBA-type serial ports. They
25 * have a lot of 16550-like features, but are not register compatible.
26 * Note that although they do have CTS, DCD and DSR inputs, they do
27 * not have an RI input, nor do they have DTR or RTS outputs. If
28 * required, these have to be supplied via some other means (eg, GPIO)
29 * and hooked into this driver.
30 */
31
32#if defined(CONFIG_SERIAL_AMBA_PL011_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/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/amba/bus.h>
47#include <linux/amba/serial.h>
48#include <linux/clk.h>
49#include <linux/slab.h>
50#include <linux/dmaengine.h>
51#include <linux/dma-mapping.h>
52#include <linux/scatterlist.h>
53#include <linux/delay.h>
54
55#include <asm/io.h>
56#include <asm/sizes.h>
57
58#define UART_NR 14
59
60#define SERIAL_AMBA_MAJOR 204
61#define SERIAL_AMBA_MINOR 64
62#define SERIAL_AMBA_NR UART_NR
63
64#define AMBA_ISR_PASS_LIMIT 256
65
66#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
67#define UART_DUMMY_DR_RX (1 << 16)
68
69
70#define UART_WA_SAVE_NR 14
71
72static void pl011_lockup_wa(unsigned long data);
73static const u32 uart_wa_reg[UART_WA_SAVE_NR] = {
74 ST_UART011_DMAWM,
75 ST_UART011_TIMEOUT,
76 ST_UART011_LCRH_RX,
77 UART011_IBRD,
78 UART011_FBRD,
79 ST_UART011_LCRH_TX,
80 UART011_IFLS,
81 ST_UART011_XFCR,
82 ST_UART011_XON1,
83 ST_UART011_XON2,
84 ST_UART011_XOFF1,
85 ST_UART011_XOFF2,
86 UART011_CR,
87 UART011_IMSC
88};
89
90static u32 uart_wa_regdata[UART_WA_SAVE_NR];
91static DECLARE_TASKLET(pl011_lockup_tlet, pl011_lockup_wa, 0);
92
93/* There is by now at least one vendor with differing details, so handle it */
94struct vendor_data {
95 unsigned int ifls;
96 unsigned int fifosize;
97 unsigned int lcrh_tx;
98 unsigned int lcrh_rx;
99 bool oversampling;
100 bool interrupt_may_hang; /* vendor-specific */
101 bool dma_threshold;
102};
103
104static struct vendor_data vendor_arm = {
105 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
106 .fifosize = 16,
107 .lcrh_tx = UART011_LCRH,
108 .lcrh_rx = UART011_LCRH,
109 .oversampling = false,
110 .dma_threshold = false,
111};
112
113static struct vendor_data vendor_st = {
114 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
115 .fifosize = 64,
116 .lcrh_tx = ST_UART011_LCRH_TX,
117 .lcrh_rx = ST_UART011_LCRH_RX,
118 .oversampling = true,
119 .interrupt_may_hang = true,
120 .dma_threshold = true,
121};
122
123static struct uart_amba_port *amba_ports[UART_NR];
124
125/* Deals with DMA transactions */
126
127struct pl011_sgbuf {
128 struct scatterlist sg;
129 char *buf;
130};
131
132struct pl011_dmarx_data {
133 struct dma_chan *chan;
134 struct completion complete;
135 bool use_buf_b;
136 struct pl011_sgbuf sgbuf_a;
137 struct pl011_sgbuf sgbuf_b;
138 dma_cookie_t cookie;
139 bool running;
140};
141
142struct pl011_dmatx_data {
143 struct dma_chan *chan;
144 struct scatterlist sg;
145 char *buf;
146 bool queued;
147};
148
149/*
150 * We wrap our port structure around the generic uart_port.
151 */
152struct uart_amba_port {
153 struct uart_port port;
154 struct clk *clk;
155 const struct vendor_data *vendor;
156 unsigned int dmacr; /* dma control reg */
157 unsigned int im; /* interrupt mask */
158 unsigned int old_status;
159 unsigned int fifosize; /* vendor-specific */
160 unsigned int lcrh_tx; /* vendor-specific */
161 unsigned int lcrh_rx; /* vendor-specific */
162 bool autorts;
163 char type[12];
164 bool interrupt_may_hang; /* vendor-specific */
165#ifdef CONFIG_DMA_ENGINE
166 /* DMA stuff */
167 bool using_tx_dma;
168 bool using_rx_dma;
169 struct pl011_dmarx_data dmarx;
170 struct pl011_dmatx_data dmatx;
171#endif
172};
173
174/*
175 * Reads up to 256 characters from the FIFO or until it's empty and
176 * inserts them into the TTY layer. Returns the number of characters
177 * read from the FIFO.
178 */
179static int pl011_fifo_to_tty(struct uart_amba_port *uap)
180{
181 u16 status, ch;
182 unsigned int flag, max_count = 256;
183 int fifotaken = 0;
184
185 while (max_count--) {
186 status = readw(uap->port.membase + UART01x_FR);
187 if (status & UART01x_FR_RXFE)
188 break;
189
190 /* Take chars from the FIFO and update status */
191 ch = readw(uap->port.membase + UART01x_DR) |
192 UART_DUMMY_DR_RX;
193 flag = TTY_NORMAL;
194 uap->port.icount.rx++;
195 fifotaken++;
196
197 if (unlikely(ch & UART_DR_ERROR)) {
198 if (ch & UART011_DR_BE) {
199 ch &= ~(UART011_DR_FE | UART011_DR_PE);
200 uap->port.icount.brk++;
201 if (uart_handle_break(&uap->port))
202 continue;
203 } else if (ch & UART011_DR_PE)
204 uap->port.icount.parity++;
205 else if (ch & UART011_DR_FE)
206 uap->port.icount.frame++;
207 if (ch & UART011_DR_OE)
208 uap->port.icount.overrun++;
209
210 ch &= uap->port.read_status_mask;
211
212 if (ch & UART011_DR_BE)
213 flag = TTY_BREAK;
214 else if (ch & UART011_DR_PE)
215 flag = TTY_PARITY;
216 else if (ch & UART011_DR_FE)
217 flag = TTY_FRAME;
218 }
219
220 if (uart_handle_sysrq_char(&uap->port, ch & 255))
221 continue;
222
223 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
224 }
225
226 return fifotaken;
227}
228
229
230/*
231 * All the DMA operation mode stuff goes inside this ifdef.
232 * This assumes that you have a generic DMA device interface,
233 * no custom DMA interfaces are supported.
234 */
235#ifdef CONFIG_DMA_ENGINE
236
237#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
238
239static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
240 enum dma_data_direction dir)
241{
242 sg->buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
243 if (!sg->buf)
244 return -ENOMEM;
245
246 sg_init_one(&sg->sg, sg->buf, PL011_DMA_BUFFER_SIZE);
247
248 if (dma_map_sg(chan->device->dev, &sg->sg, 1, dir) != 1) {
249 kfree(sg->buf);
250 return -EINVAL;
251 }
252 return 0;
253}
254
255static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
256 enum dma_data_direction dir)
257{
258 if (sg->buf) {
259 dma_unmap_sg(chan->device->dev, &sg->sg, 1, dir);
260 kfree(sg->buf);
261 }
262}
263
264static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
265{
266 /* DMA is the sole user of the platform data right now */
267 struct amba_pl011_data *plat = uap->port.dev->platform_data;
268 struct dma_slave_config tx_conf = {
269 .dst_addr = uap->port.mapbase + UART01x_DR,
270 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
271 .direction = DMA_TO_DEVICE,
272 .dst_maxburst = uap->fifosize >> 1,
273 };
274 struct dma_chan *chan;
275 dma_cap_mask_t mask;
276
277 /* We need platform data */
278 if (!plat || !plat->dma_filter) {
279 dev_info(uap->port.dev, "no DMA platform data\n");
280 return;
281 }
282
283 /* Try to acquire a generic DMA engine slave TX channel */
284 dma_cap_zero(mask);
285 dma_cap_set(DMA_SLAVE, mask);
286
287 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_tx_param);
288 if (!chan) {
289 dev_err(uap->port.dev, "no TX DMA channel!\n");
290 return;
291 }
292
293 dmaengine_slave_config(chan, &tx_conf);
294 uap->dmatx.chan = chan;
295
296 dev_info(uap->port.dev, "DMA channel TX %s\n",
297 dma_chan_name(uap->dmatx.chan));
298
299 /* Optionally make use of an RX channel as well */
300 if (plat->dma_rx_param) {
301 struct dma_slave_config rx_conf = {
302 .src_addr = uap->port.mapbase + UART01x_DR,
303 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
304 .direction = DMA_FROM_DEVICE,
305 .src_maxburst = uap->fifosize >> 1,
306 };
307
308 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
309 if (!chan) {
310 dev_err(uap->port.dev, "no RX DMA channel!\n");
311 return;
312 }
313
314 dmaengine_slave_config(chan, &rx_conf);
315 uap->dmarx.chan = chan;
316
317 dev_info(uap->port.dev, "DMA channel RX %s\n",
318 dma_chan_name(uap->dmarx.chan));
319 }
320}
321
322#ifndef MODULE
323/*
324 * Stack up the UARTs and let the above initcall be done at device
325 * initcall time, because the serial driver is called as an arch
326 * initcall, and at this time the DMA subsystem is not yet registered.
327 * At this point the driver will switch over to using DMA where desired.
328 */
329struct dma_uap {
330 struct list_head node;
331 struct uart_amba_port *uap;
332};
333
334static LIST_HEAD(pl011_dma_uarts);
335
336static int __init pl011_dma_initcall(void)
337{
338 struct list_head *node, *tmp;
339
340 list_for_each_safe(node, tmp, &pl011_dma_uarts) {
341 struct dma_uap *dmau = list_entry(node, struct dma_uap, node);
342 pl011_dma_probe_initcall(dmau->uap);
343 list_del(node);
344 kfree(dmau);
345 }
346 return 0;
347}
348
349device_initcall(pl011_dma_initcall);
350
351static void pl011_dma_probe(struct uart_amba_port *uap)
352{
353 struct dma_uap *dmau = kzalloc(sizeof(struct dma_uap), GFP_KERNEL);
354 if (dmau) {
355 dmau->uap = uap;
356 list_add_tail(&dmau->node, &pl011_dma_uarts);
357 }
358}
359#else
360static void pl011_dma_probe(struct uart_amba_port *uap)
361{
362 pl011_dma_probe_initcall(uap);
363}
364#endif
365
366static void pl011_dma_remove(struct uart_amba_port *uap)
367{
368 /* TODO: remove the initcall if it has not yet executed */
369 if (uap->dmatx.chan)
370 dma_release_channel(uap->dmatx.chan);
371 if (uap->dmarx.chan)
372 dma_release_channel(uap->dmarx.chan);
373}
374
375/* Forward declare this for the refill routine */
376static int pl011_dma_tx_refill(struct uart_amba_port *uap);
377
378/*
379 * The current DMA TX buffer has been sent.
380 * Try to queue up another DMA buffer.
381 */
382static void pl011_dma_tx_callback(void *data)
383{
384 struct uart_amba_port *uap = data;
385 struct pl011_dmatx_data *dmatx = &uap->dmatx;
386 unsigned long flags;
387 u16 dmacr;
388
389 spin_lock_irqsave(&uap->port.lock, flags);
390 if (uap->dmatx.queued)
391 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
392 DMA_TO_DEVICE);
393
394 dmacr = uap->dmacr;
395 uap->dmacr = dmacr & ~UART011_TXDMAE;
396 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
397
398 /*
399 * If TX DMA was disabled, it means that we've stopped the DMA for
400 * some reason (eg, XOFF received, or we want to send an X-char.)
401 *
402 * Note: we need to be careful here of a potential race between DMA
403 * and the rest of the driver - if the driver disables TX DMA while
404 * a TX buffer completing, we must update the tx queued status to
405 * get further refills (hence we check dmacr).
406 */
407 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
408 uart_circ_empty(&uap->port.state->xmit)) {
409 uap->dmatx.queued = false;
410 spin_unlock_irqrestore(&uap->port.lock, flags);
411 return;
412 }
413
414 if (pl011_dma_tx_refill(uap) <= 0) {
415 /*
416 * We didn't queue a DMA buffer for some reason, but we
417 * have data pending to be sent. Re-enable the TX IRQ.
418 */
419 uap->im |= UART011_TXIM;
420 writew(uap->im, uap->port.membase + UART011_IMSC);
421 }
422 spin_unlock_irqrestore(&uap->port.lock, flags);
423}
424
425/*
426 * Try to refill the TX DMA buffer.
427 * Locking: called with port lock held and IRQs disabled.
428 * Returns:
429 * 1 if we queued up a TX DMA buffer.
430 * 0 if we didn't want to handle this by DMA
431 * <0 on error
432 */
433static int pl011_dma_tx_refill(struct uart_amba_port *uap)
434{
435 struct pl011_dmatx_data *dmatx = &uap->dmatx;
436 struct dma_chan *chan = dmatx->chan;
437 struct dma_device *dma_dev = chan->device;
438 struct dma_async_tx_descriptor *desc;
439 struct circ_buf *xmit = &uap->port.state->xmit;
440 unsigned int count;
441
442 /*
443 * Try to avoid the overhead involved in using DMA if the
444 * transaction fits in the first half of the FIFO, by using
445 * the standard interrupt handling. This ensures that we
446 * issue a uart_write_wakeup() at the appropriate time.
447 */
448 count = uart_circ_chars_pending(xmit);
449 if (count < (uap->fifosize >> 1)) {
450 uap->dmatx.queued = false;
451 return 0;
452 }
453
454 /*
455 * Bodge: don't send the last character by DMA, as this
456 * will prevent XON from notifying us to restart DMA.
457 */
458 count -= 1;
459
460 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
461 if (count > PL011_DMA_BUFFER_SIZE)
462 count = PL011_DMA_BUFFER_SIZE;
463
464 if (xmit->tail < xmit->head)
465 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
466 else {
467 size_t first = UART_XMIT_SIZE - xmit->tail;
468 size_t second = xmit->head;
469
470 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
471 if (second)
472 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
473 }
474
475 dmatx->sg.length = count;
476
477 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
478 uap->dmatx.queued = false;
479 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
480 return -EBUSY;
481 }
482
483 desc = dma_dev->device_prep_slave_sg(chan, &dmatx->sg, 1, DMA_TO_DEVICE,
484 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
485 if (!desc) {
486 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
487 uap->dmatx.queued = false;
488 /*
489 * If DMA cannot be used right now, we complete this
490 * transaction via IRQ and let the TTY layer retry.
491 */
492 dev_dbg(uap->port.dev, "TX DMA busy\n");
493 return -EBUSY;
494 }
495
496 /* Some data to go along to the callback */
497 desc->callback = pl011_dma_tx_callback;
498 desc->callback_param = uap;
499
500 /* All errors should happen at prepare time */
501 dmaengine_submit(desc);
502
503 /* Fire the DMA transaction */
504 dma_dev->device_issue_pending(chan);
505
506 uap->dmacr |= UART011_TXDMAE;
507 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
508 uap->dmatx.queued = true;
509
510 /*
511 * Now we know that DMA will fire, so advance the ring buffer
512 * with the stuff we just dispatched.
513 */
514 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
515 uap->port.icount.tx += count;
516
517 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
518 uart_write_wakeup(&uap->port);
519
520 return 1;
521}
522
523/*
524 * We received a transmit interrupt without a pending X-char but with
525 * pending characters.
526 * Locking: called with port lock held and IRQs disabled.
527 * Returns:
528 * false if we want to use PIO to transmit
529 * true if we queued a DMA buffer
530 */
531static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
532{
533 if (!uap->using_tx_dma)
534 return false;
535
536 /*
537 * If we already have a TX buffer queued, but received a
538 * TX interrupt, it will be because we've just sent an X-char.
539 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
540 */
541 if (uap->dmatx.queued) {
542 uap->dmacr |= UART011_TXDMAE;
543 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
544 uap->im &= ~UART011_TXIM;
545 writew(uap->im, uap->port.membase + UART011_IMSC);
546 return true;
547 }
548
549 /*
550 * We don't have a TX buffer queued, so try to queue one.
551 * If we successfully queued a buffer, mask the TX IRQ.
552 */
553 if (pl011_dma_tx_refill(uap) > 0) {
554 uap->im &= ~UART011_TXIM;
555 writew(uap->im, uap->port.membase + UART011_IMSC);
556 return true;
557 }
558 return false;
559}
560
561/*
562 * Stop the DMA transmit (eg, due to received XOFF).
563 * Locking: called with port lock held and IRQs disabled.
564 */
565static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
566{
567 if (uap->dmatx.queued) {
568 uap->dmacr &= ~UART011_TXDMAE;
569 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
570 }
571}
572
573/*
574 * Try to start a DMA transmit, or in the case of an XON/OFF
575 * character queued for send, try to get that character out ASAP.
576 * Locking: called with port lock held and IRQs disabled.
577 * Returns:
578 * false if we want the TX IRQ to be enabled
579 * true if we have a buffer queued
580 */
581static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
582{
583 u16 dmacr;
584
585 if (!uap->using_tx_dma)
586 return false;
587
588 if (!uap->port.x_char) {
589 /* no X-char, try to push chars out in DMA mode */
590 bool ret = true;
591
592 if (!uap->dmatx.queued) {
593 if (pl011_dma_tx_refill(uap) > 0) {
594 uap->im &= ~UART011_TXIM;
595 ret = true;
596 } else {
597 uap->im |= UART011_TXIM;
598 ret = false;
599 }
600 writew(uap->im, uap->port.membase + UART011_IMSC);
601 } else if (!(uap->dmacr & UART011_TXDMAE)) {
602 uap->dmacr |= UART011_TXDMAE;
603 writew(uap->dmacr,
604 uap->port.membase + UART011_DMACR);
605 }
606 return ret;
607 }
608
609 /*
610 * We have an X-char to send. Disable DMA to prevent it loading
611 * the TX fifo, and then see if we can stuff it into the FIFO.
612 */
613 dmacr = uap->dmacr;
614 uap->dmacr &= ~UART011_TXDMAE;
615 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
616
617 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
618 /*
619 * No space in the FIFO, so enable the transmit interrupt
620 * so we know when there is space. Note that once we've
621 * loaded the character, we should just re-enable DMA.
622 */
623 return false;
624 }
625
626 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
627 uap->port.icount.tx++;
628 uap->port.x_char = 0;
629
630 /* Success - restore the DMA state */
631 uap->dmacr = dmacr;
632 writew(dmacr, uap->port.membase + UART011_DMACR);
633
634 return true;
635}
636
637/*
638 * Flush the transmit buffer.
639 * Locking: called with port lock held and IRQs disabled.
640 */
641static void pl011_dma_flush_buffer(struct uart_port *port)
642{
643 struct uart_amba_port *uap = (struct uart_amba_port *)port;
644
645 if (!uap->using_tx_dma)
646 return;
647
648 /* Avoid deadlock with the DMA engine callback */
649 spin_unlock(&uap->port.lock);
650 dmaengine_terminate_all(uap->dmatx.chan);
651 spin_lock(&uap->port.lock);
652 if (uap->dmatx.queued) {
653 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
654 DMA_TO_DEVICE);
655 uap->dmatx.queued = false;
656 uap->dmacr &= ~UART011_TXDMAE;
657 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
658 }
659}
660
661static void pl011_dma_rx_callback(void *data);
662
663static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
664{
665 struct dma_chan *rxchan = uap->dmarx.chan;
666 struct dma_device *dma_dev;
667 struct pl011_dmarx_data *dmarx = &uap->dmarx;
668 struct dma_async_tx_descriptor *desc;
669 struct pl011_sgbuf *sgbuf;
670
671 if (!rxchan)
672 return -EIO;
673
674 /* Start the RX DMA job */
675 sgbuf = uap->dmarx.use_buf_b ?
676 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
677 dma_dev = rxchan->device;
678 desc = rxchan->device->device_prep_slave_sg(rxchan, &sgbuf->sg, 1,
679 DMA_FROM_DEVICE,
680 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
681 /*
682 * If the DMA engine is busy and cannot prepare a
683 * channel, no big deal, the driver will fall back
684 * to interrupt mode as a result of this error code.
685 */
686 if (!desc) {
687 uap->dmarx.running = false;
688 dmaengine_terminate_all(rxchan);
689 return -EBUSY;
690 }
691
692 /* Some data to go along to the callback */
693 desc->callback = pl011_dma_rx_callback;
694 desc->callback_param = uap;
695 dmarx->cookie = dmaengine_submit(desc);
696 dma_async_issue_pending(rxchan);
697
698 uap->dmacr |= UART011_RXDMAE;
699 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
700 uap->dmarx.running = true;
701
702 uap->im &= ~UART011_RXIM;
703 writew(uap->im, uap->port.membase + UART011_IMSC);
704
705 return 0;
706}
707
708/*
709 * This is called when either the DMA job is complete, or
710 * the FIFO timeout interrupt occurred. This must be called
711 * with the port spinlock uap->port.lock held.
712 */
713static void pl011_dma_rx_chars(struct uart_amba_port *uap,
714 u32 pending, bool use_buf_b,
715 bool readfifo)
716{
717 struct tty_struct *tty = uap->port.state->port.tty;
718 struct pl011_sgbuf *sgbuf = use_buf_b ?
719 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
720 struct device *dev = uap->dmarx.chan->device->dev;
721 int dma_count = 0;
722 u32 fifotaken = 0; /* only used for vdbg() */
723
724 /* Pick everything from the DMA first */
725 if (pending) {
726 /* Sync in buffer */
727 dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
728
729 /*
730 * First take all chars in the DMA pipe, then look in the FIFO.
731 * Note that tty_insert_flip_buf() tries to take as many chars
732 * as it can.
733 */
734 dma_count = tty_insert_flip_string(uap->port.state->port.tty,
735 sgbuf->buf, pending);
736
737 /* Return buffer to device */
738 dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
739
740 uap->port.icount.rx += dma_count;
741 if (dma_count < pending)
742 dev_warn(uap->port.dev,
743 "couldn't insert all characters (TTY is full?)\n");
744 }
745
746 /*
747 * Only continue with trying to read the FIFO if all DMA chars have
748 * been taken first.
749 */
750 if (dma_count == pending && readfifo) {
751 /* Clear any error flags */
752 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
753 uap->port.membase + UART011_ICR);
754
755 /*
756 * If we read all the DMA'd characters, and we had an
757 * incomplete buffer, that could be due to an rx error, or
758 * maybe we just timed out. Read any pending chars and check
759 * the error status.
760 *
761 * Error conditions will only occur in the FIFO, these will
762 * trigger an immediate interrupt and stop the DMA job, so we
763 * will always find the error in the FIFO, never in the DMA
764 * buffer.
765 */
766 fifotaken = pl011_fifo_to_tty(uap);
767 }
768
769 spin_unlock(&uap->port.lock);
770 dev_vdbg(uap->port.dev,
771 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
772 dma_count, fifotaken);
773 tty_flip_buffer_push(tty);
774 spin_lock(&uap->port.lock);
775}
776
777static void pl011_dma_rx_irq(struct uart_amba_port *uap)
778{
779 struct pl011_dmarx_data *dmarx = &uap->dmarx;
780 struct dma_chan *rxchan = dmarx->chan;
781 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
782 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
783 size_t pending;
784 struct dma_tx_state state;
785 enum dma_status dmastat;
786
787 /*
788 * Pause the transfer so we can trust the current counter,
789 * do this before we pause the PL011 block, else we may
790 * overflow the FIFO.
791 */
792 if (dmaengine_pause(rxchan))
793 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
794 dmastat = rxchan->device->device_tx_status(rxchan,
795 dmarx->cookie, &state);
796 if (dmastat != DMA_PAUSED)
797 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
798
799 /* Disable RX DMA - incoming data will wait in the FIFO */
800 uap->dmacr &= ~UART011_RXDMAE;
801 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
802 uap->dmarx.running = false;
803
804 pending = sgbuf->sg.length - state.residue;
805 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
806 /* Then we terminate the transfer - we now know our residue */
807 dmaengine_terminate_all(rxchan);
808
809 /*
810 * This will take the chars we have so far and insert
811 * into the framework.
812 */
813 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
814
815 /* Switch buffer & re-trigger DMA job */
816 dmarx->use_buf_b = !dmarx->use_buf_b;
817 if (pl011_dma_rx_trigger_dma(uap)) {
818 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
819 "fall back to interrupt mode\n");
820 uap->im |= UART011_RXIM;
821 writew(uap->im, uap->port.membase + UART011_IMSC);
822 }
823}
824
825static void pl011_dma_rx_callback(void *data)
826{
827 struct uart_amba_port *uap = data;
828 struct pl011_dmarx_data *dmarx = &uap->dmarx;
829 bool lastbuf = dmarx->use_buf_b;
830 int ret;
831
832 /*
833 * This completion interrupt occurs typically when the
834 * RX buffer is totally stuffed but no timeout has yet
835 * occurred. When that happens, we just want the RX
836 * routine to flush out the secondary DMA buffer while
837 * we immediately trigger the next DMA job.
838 */
839 spin_lock_irq(&uap->port.lock);
840 uap->dmarx.running = false;
841 dmarx->use_buf_b = !lastbuf;
842 ret = pl011_dma_rx_trigger_dma(uap);
843
844 pl011_dma_rx_chars(uap, PL011_DMA_BUFFER_SIZE, lastbuf, false);
845 spin_unlock_irq(&uap->port.lock);
846 /*
847 * Do this check after we picked the DMA chars so we don't
848 * get some IRQ immediately from RX.
849 */
850 if (ret) {
851 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
852 "fall back to interrupt mode\n");
853 uap->im |= UART011_RXIM;
854 writew(uap->im, uap->port.membase + UART011_IMSC);
855 }
856}
857
858/*
859 * Stop accepting received characters, when we're shutting down or
860 * suspending this port.
861 * Locking: called with port lock held and IRQs disabled.
862 */
863static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
864{
865 /* FIXME. Just disable the DMA enable */
866 uap->dmacr &= ~UART011_RXDMAE;
867 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
868}
869
870static void pl011_dma_startup(struct uart_amba_port *uap)
871{
872 int ret;
873
874 if (!uap->dmatx.chan)
875 return;
876
877 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
878 if (!uap->dmatx.buf) {
879 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
880 uap->port.fifosize = uap->fifosize;
881 return;
882 }
883
884 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
885
886 /* The DMA buffer is now the FIFO the TTY subsystem can use */
887 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
888 uap->using_tx_dma = true;
889
890 if (!uap->dmarx.chan)
891 goto skip_rx;
892
893 /* Allocate and map DMA RX buffers */
894 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
895 DMA_FROM_DEVICE);
896 if (ret) {
897 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
898 "RX buffer A", ret);
899 goto skip_rx;
900 }
901
902 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
903 DMA_FROM_DEVICE);
904 if (ret) {
905 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
906 "RX buffer B", ret);
907 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
908 DMA_FROM_DEVICE);
909 goto skip_rx;
910 }
911
912 uap->using_rx_dma = true;
913
914skip_rx:
915 /* Turn on DMA error (RX/TX will be enabled on demand) */
916 uap->dmacr |= UART011_DMAONERR;
917 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
918
919 /*
920 * ST Micro variants has some specific dma burst threshold
921 * compensation. Set this to 16 bytes, so burst will only
922 * be issued above/below 16 bytes.
923 */
924 if (uap->vendor->dma_threshold)
925 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
926 uap->port.membase + ST_UART011_DMAWM);
927
928 if (uap->using_rx_dma) {
929 if (pl011_dma_rx_trigger_dma(uap))
930 dev_dbg(uap->port.dev, "could not trigger initial "
931 "RX DMA job, fall back to interrupt mode\n");
932 }
933}
934
935static void pl011_dma_shutdown(struct uart_amba_port *uap)
936{
937 if (!(uap->using_tx_dma || uap->using_rx_dma))
938 return;
939
940 /* Disable RX and TX DMA */
941 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
942 barrier();
943
944 spin_lock_irq(&uap->port.lock);
945 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
946 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
947 spin_unlock_irq(&uap->port.lock);
948
949 if (uap->using_tx_dma) {
950 /* In theory, this should already be done by pl011_dma_flush_buffer */
951 dmaengine_terminate_all(uap->dmatx.chan);
952 if (uap->dmatx.queued) {
953 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
954 DMA_TO_DEVICE);
955 uap->dmatx.queued = false;
956 }
957
958 kfree(uap->dmatx.buf);
959 uap->using_tx_dma = false;
960 }
961
962 if (uap->using_rx_dma) {
963 dmaengine_terminate_all(uap->dmarx.chan);
964 /* Clean up the RX DMA */
965 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
966 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
967 uap->using_rx_dma = false;
968 }
969}
970
971static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
972{
973 return uap->using_rx_dma;
974}
975
976static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
977{
978 return uap->using_rx_dma && uap->dmarx.running;
979}
980
981
982#else
983/* Blank functions if the DMA engine is not available */
984static inline void pl011_dma_probe(struct uart_amba_port *uap)
985{
986}
987
988static inline void pl011_dma_remove(struct uart_amba_port *uap)
989{
990}
991
992static inline void pl011_dma_startup(struct uart_amba_port *uap)
993{
994}
995
996static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
997{
998}
999
1000static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1001{
1002 return false;
1003}
1004
1005static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1006{
1007}
1008
1009static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1010{
1011 return false;
1012}
1013
1014static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1015{
1016}
1017
1018static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1019{
1020}
1021
1022static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1023{
1024 return -EIO;
1025}
1026
1027static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1028{
1029 return false;
1030}
1031
1032static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1033{
1034 return false;
1035}
1036
1037#define pl011_dma_flush_buffer NULL
1038#endif
1039
1040
1041/*
1042 * pl011_lockup_wa
1043 * This workaround aims to break the deadlock situation
1044 * when after long transfer over uart in hardware flow
1045 * control, uart interrupt registers cannot be cleared.
1046 * Hence uart transfer gets blocked.
1047 *
1048 * It is seen that during such deadlock condition ICR
1049 * don't get cleared even on multiple write. This leads
1050 * pass_counter to decrease and finally reach zero. This
1051 * can be taken as trigger point to run this UART_BT_WA.
1052 *
1053 */
1054static void pl011_lockup_wa(unsigned long data)
1055{
1056 struct uart_amba_port *uap = amba_ports[0];
1057 void __iomem *base = uap->port.membase;
1058 struct circ_buf *xmit = &uap->port.state->xmit;
1059 struct tty_struct *tty = uap->port.state->port.tty;
1060 int buf_empty_retries = 200;
1061 int loop;
1062
1063 /* Stop HCI layer from submitting data for tx */
1064 tty->hw_stopped = 1;
1065 while (!uart_circ_empty(xmit)) {
1066 if (buf_empty_retries-- == 0)
1067 break;
1068 udelay(100);
1069 }
1070
1071 /* Backup registers */
1072 for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1073 uart_wa_regdata[loop] = readl(base + uart_wa_reg[loop]);
1074
1075 /* Disable UART so that FIFO data is flushed out */
1076 writew(0x00, uap->port.membase + UART011_CR);
1077
1078 /* Soft reset UART module */
1079 if (uap->port.dev->platform_data) {
1080 struct amba_pl011_data *plat;
1081
1082 plat = uap->port.dev->platform_data;
1083 if (plat->reset)
1084 plat->reset();
1085 }
1086
1087 /* Restore registers */
1088 for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1089 writew(uart_wa_regdata[loop] ,
1090 uap->port.membase + uart_wa_reg[loop]);
1091
1092 /* Initialise the old status of the modem signals */
1093 uap->old_status = readw(uap->port.membase + UART01x_FR) &
1094 UART01x_FR_MODEM_ANY;
1095
1096 if (readl(base + UART011_MIS) & 0x2)
1097 printk(KERN_EMERG "UART_BT_WA: ***FAILED***\n");
1098
1099 /* Start Tx/Rx */
1100 tty->hw_stopped = 0;
1101}
1102
1103static void pl011_stop_tx(struct uart_port *port)
1104{
1105 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1106
1107 uap->im &= ~UART011_TXIM;
1108 writew(uap->im, uap->port.membase + UART011_IMSC);
1109 pl011_dma_tx_stop(uap);
1110}
1111
1112static void pl011_start_tx(struct uart_port *port)
1113{
1114 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1115
1116 if (!pl011_dma_tx_start(uap)) {
1117 uap->im |= UART011_TXIM;
1118 writew(uap->im, uap->port.membase + UART011_IMSC);
1119 }
1120}
1121
1122static void pl011_stop_rx(struct uart_port *port)
1123{
1124 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1125
1126 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1127 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1128 writew(uap->im, uap->port.membase + UART011_IMSC);
1129
1130 pl011_dma_rx_stop(uap);
1131}
1132
1133static void pl011_enable_ms(struct uart_port *port)
1134{
1135 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1136
1137 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1138 writew(uap->im, uap->port.membase + UART011_IMSC);
1139}
1140
1141static void pl011_rx_chars(struct uart_amba_port *uap)
1142{
1143 struct tty_struct *tty = uap->port.state->port.tty;
1144
1145 pl011_fifo_to_tty(uap);
1146
1147 spin_unlock(&uap->port.lock);
1148 tty_flip_buffer_push(tty);
1149 /*
1150 * If we were temporarily out of DMA mode for a while,
1151 * attempt to switch back to DMA mode again.
1152 */
1153 if (pl011_dma_rx_available(uap)) {
1154 if (pl011_dma_rx_trigger_dma(uap)) {
1155 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1156 "fall back to interrupt mode again\n");
1157 uap->im |= UART011_RXIM;
1158 } else
1159 uap->im &= ~UART011_RXIM;
1160 writew(uap->im, uap->port.membase + UART011_IMSC);
1161 }
1162 spin_lock(&uap->port.lock);
1163}
1164
1165static void pl011_tx_chars(struct uart_amba_port *uap)
1166{
1167 struct circ_buf *xmit = &uap->port.state->xmit;
1168 int count;
1169
1170 if (uap->port.x_char) {
1171 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
1172 uap->port.icount.tx++;
1173 uap->port.x_char = 0;
1174 return;
1175 }
1176 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
1177 pl011_stop_tx(&uap->port);
1178 return;
1179 }
1180
1181 /* If we are using DMA mode, try to send some characters. */
1182 if (pl011_dma_tx_irq(uap))
1183 return;
1184
1185 count = uap->fifosize >> 1;
1186 do {
1187 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
1188 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1189 uap->port.icount.tx++;
1190 if (uart_circ_empty(xmit))
1191 break;
1192 } while (--count > 0);
1193
1194 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1195 uart_write_wakeup(&uap->port);
1196
1197 if (uart_circ_empty(xmit))
1198 pl011_stop_tx(&uap->port);
1199}
1200
1201static void pl011_modem_status(struct uart_amba_port *uap)
1202{
1203 unsigned int status, delta;
1204
1205 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1206
1207 delta = status ^ uap->old_status;
1208 uap->old_status = status;
1209
1210 if (!delta)
1211 return;
1212
1213 if (delta & UART01x_FR_DCD)
1214 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1215
1216 if (delta & UART01x_FR_DSR)
1217 uap->port.icount.dsr++;
1218
1219 if (delta & UART01x_FR_CTS)
1220 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1221
1222 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
1223}
1224
1225static irqreturn_t pl011_int(int irq, void *dev_id)
1226{
1227 struct uart_amba_port *uap = dev_id;
1228 unsigned long flags;
1229 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
1230 int handled = 0;
1231
1232 spin_lock_irqsave(&uap->port.lock, flags);
1233
1234 status = readw(uap->port.membase + UART011_MIS);
1235 if (status) {
1236 do {
1237 writew(status & ~(UART011_TXIS|UART011_RTIS|
1238 UART011_RXIS),
1239 uap->port.membase + UART011_ICR);
1240
1241 if (status & (UART011_RTIS|UART011_RXIS)) {
1242 if (pl011_dma_rx_running(uap))
1243 pl011_dma_rx_irq(uap);
1244 else
1245 pl011_rx_chars(uap);
1246 }
1247 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1248 UART011_CTSMIS|UART011_RIMIS))
1249 pl011_modem_status(uap);
1250 if (status & UART011_TXIS)
1251 pl011_tx_chars(uap);
1252
1253 if (pass_counter-- == 0) {
1254 if (uap->interrupt_may_hang)
1255 tasklet_schedule(&pl011_lockup_tlet);
1256 break;
1257 }
1258
1259 status = readw(uap->port.membase + UART011_MIS);
1260 } while (status != 0);
1261 handled = 1;
1262 }
1263
1264 spin_unlock_irqrestore(&uap->port.lock, flags);
1265
1266 return IRQ_RETVAL(handled);
1267}
1268
1269static unsigned int pl01x_tx_empty(struct uart_port *port)
1270{
1271 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1272 unsigned int status = readw(uap->port.membase + UART01x_FR);
1273 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1274}
1275
1276static unsigned int pl01x_get_mctrl(struct uart_port *port)
1277{
1278 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1279 unsigned int result = 0;
1280 unsigned int status = readw(uap->port.membase + UART01x_FR);
1281
1282#define TIOCMBIT(uartbit, tiocmbit) \
1283 if (status & uartbit) \
1284 result |= tiocmbit
1285
1286 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1287 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1288 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1289 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1290#undef TIOCMBIT
1291 return result;
1292}
1293
1294static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1295{
1296 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1297 unsigned int cr;
1298
1299 cr = readw(uap->port.membase + UART011_CR);
1300
1301#define TIOCMBIT(tiocmbit, uartbit) \
1302 if (mctrl & tiocmbit) \
1303 cr |= uartbit; \
1304 else \
1305 cr &= ~uartbit
1306
1307 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1308 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1309 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1310 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1311 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
1312
1313 if (uap->autorts) {
1314 /* We need to disable auto-RTS if we want to turn RTS off */
1315 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1316 }
1317#undef TIOCMBIT
1318
1319 writew(cr, uap->port.membase + UART011_CR);
1320}
1321
1322static void pl011_break_ctl(struct uart_port *port, int break_state)
1323{
1324 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1325 unsigned long flags;
1326 unsigned int lcr_h;
1327
1328 spin_lock_irqsave(&uap->port.lock, flags);
1329 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
1330 if (break_state == -1)
1331 lcr_h |= UART01x_LCRH_BRK;
1332 else
1333 lcr_h &= ~UART01x_LCRH_BRK;
1334 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
1335 spin_unlock_irqrestore(&uap->port.lock, flags);
1336}
1337
1338#ifdef CONFIG_CONSOLE_POLL
1339static int pl010_get_poll_char(struct uart_port *port)
1340{
1341 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1342 unsigned int status;
1343
1344 status = readw(uap->port.membase + UART01x_FR);
1345 if (status & UART01x_FR_RXFE)
1346 return NO_POLL_CHAR;
1347
1348 return readw(uap->port.membase + UART01x_DR);
1349}
1350
1351static void pl010_put_poll_char(struct uart_port *port,
1352 unsigned char ch)
1353{
1354 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1355
1356 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1357 barrier();
1358
1359 writew(ch, uap->port.membase + UART01x_DR);
1360}
1361
1362#endif /* CONFIG_CONSOLE_POLL */
1363
1364static int pl011_startup(struct uart_port *port)
1365{
1366 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1367 unsigned int cr;
1368 int retval;
1369
1370 /*
1371 * Try to enable the clock producer.
1372 */
1373 retval = clk_enable(uap->clk);
1374 if (retval)
1375 goto out;
1376
1377 uap->port.uartclk = clk_get_rate(uap->clk);
1378
1379 /*
1380 * Allocate the IRQ
1381 */
1382 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1383 if (retval)
1384 goto clk_dis;
1385
1386 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
1387
1388 /*
1389 * Provoke TX FIFO interrupt into asserting.
1390 */
1391 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
1392 writew(cr, uap->port.membase + UART011_CR);
1393 writew(0, uap->port.membase + UART011_FBRD);
1394 writew(1, uap->port.membase + UART011_IBRD);
1395 writew(0, uap->port.membase + uap->lcrh_rx);
1396 if (uap->lcrh_tx != uap->lcrh_rx) {
1397 int i;
1398 /*
1399 * Wait 10 PCLKs before writing LCRH_TX register,
1400 * to get this delay write read only register 10 times
1401 */
1402 for (i = 0; i < 10; ++i)
1403 writew(0xff, uap->port.membase + UART011_MIS);
1404 writew(0, uap->port.membase + uap->lcrh_tx);
1405 }
1406 writew(0, uap->port.membase + UART01x_DR);
1407 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1408 barrier();
1409
1410 cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
1411 writew(cr, uap->port.membase + UART011_CR);
1412
1413 /* Clear pending error interrupts */
1414 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
1415 uap->port.membase + UART011_ICR);
1416
1417 /*
1418 * initialise the old status of the modem signals
1419 */
1420 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1421
1422 /* Startup DMA */
1423 pl011_dma_startup(uap);
1424
1425 /*
1426 * Finally, enable interrupts, only timeouts when using DMA
1427 * if initial RX DMA job failed, start in interrupt mode
1428 * as well.
1429 */
1430 spin_lock_irq(&uap->port.lock);
1431 uap->im = UART011_RTIM;
1432 if (!pl011_dma_rx_running(uap))
1433 uap->im |= UART011_RXIM;
1434 writew(uap->im, uap->port.membase + UART011_IMSC);
1435 spin_unlock_irq(&uap->port.lock);
1436
1437 if (uap->port.dev->platform_data) {
1438 struct amba_pl011_data *plat;
1439
1440 plat = uap->port.dev->platform_data;
1441 if (plat->init)
1442 plat->init();
1443 }
1444
1445 return 0;
1446
1447 clk_dis:
1448 clk_disable(uap->clk);
1449 out:
1450 return retval;
1451}
1452
1453static void pl011_shutdown_channel(struct uart_amba_port *uap,
1454 unsigned int lcrh)
1455{
1456 unsigned long val;
1457
1458 val = readw(uap->port.membase + lcrh);
1459 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1460 writew(val, uap->port.membase + lcrh);
1461}
1462
1463static void pl011_shutdown(struct uart_port *port)
1464{
1465 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1466
1467 /*
1468 * disable all interrupts
1469 */
1470 spin_lock_irq(&uap->port.lock);
1471 uap->im = 0;
1472 writew(uap->im, uap->port.membase + UART011_IMSC);
1473 writew(0xffff, uap->port.membase + UART011_ICR);
1474 spin_unlock_irq(&uap->port.lock);
1475
1476 pl011_dma_shutdown(uap);
1477
1478 /*
1479 * Free the interrupt
1480 */
1481 free_irq(uap->port.irq, uap);
1482
1483 /*
1484 * disable the port
1485 */
1486 uap->autorts = false;
1487 writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
1488
1489 /*
1490 * disable break condition and fifos
1491 */
1492 pl011_shutdown_channel(uap, uap->lcrh_rx);
1493 if (uap->lcrh_rx != uap->lcrh_tx)
1494 pl011_shutdown_channel(uap, uap->lcrh_tx);
1495
1496 /*
1497 * Shut down the clock producer
1498 */
1499 clk_disable(uap->clk);
1500
1501 if (uap->port.dev->platform_data) {
1502 struct amba_pl011_data *plat;
1503
1504 plat = uap->port.dev->platform_data;
1505 if (plat->exit)
1506 plat->exit();
1507 }
1508
1509}
1510
1511static void
1512pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1513 struct ktermios *old)
1514{
1515 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1516 unsigned int lcr_h, old_cr;
1517 unsigned long flags;
1518 unsigned int baud, quot, clkdiv;
1519
1520 if (uap->vendor->oversampling)
1521 clkdiv = 8;
1522 else
1523 clkdiv = 16;
1524
1525 /*
1526 * Ask the core to calculate the divisor for us.
1527 */
1528 baud = uart_get_baud_rate(port, termios, old, 0,
1529 port->uartclk / clkdiv);
1530
1531 if (baud > port->uartclk/16)
1532 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1533 else
1534 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
1535
1536 switch (termios->c_cflag & CSIZE) {
1537 case CS5:
1538 lcr_h = UART01x_LCRH_WLEN_5;
1539 break;
1540 case CS6:
1541 lcr_h = UART01x_LCRH_WLEN_6;
1542 break;
1543 case CS7:
1544 lcr_h = UART01x_LCRH_WLEN_7;
1545 break;
1546 default: // CS8
1547 lcr_h = UART01x_LCRH_WLEN_8;
1548 break;
1549 }
1550 if (termios->c_cflag & CSTOPB)
1551 lcr_h |= UART01x_LCRH_STP2;
1552 if (termios->c_cflag & PARENB) {
1553 lcr_h |= UART01x_LCRH_PEN;
1554 if (!(termios->c_cflag & PARODD))
1555 lcr_h |= UART01x_LCRH_EPS;
1556 }
1557 if (uap->fifosize > 1)
1558 lcr_h |= UART01x_LCRH_FEN;
1559
1560 spin_lock_irqsave(&port->lock, flags);
1561
1562 /*
1563 * Update the per-port timeout.
1564 */
1565 uart_update_timeout(port, termios->c_cflag, baud);
1566
1567 port->read_status_mask = UART011_DR_OE | 255;
1568 if (termios->c_iflag & INPCK)
1569 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1570 if (termios->c_iflag & (BRKINT | PARMRK))
1571 port->read_status_mask |= UART011_DR_BE;
1572
1573 /*
1574 * Characters to ignore
1575 */
1576 port->ignore_status_mask = 0;
1577 if (termios->c_iflag & IGNPAR)
1578 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1579 if (termios->c_iflag & IGNBRK) {
1580 port->ignore_status_mask |= UART011_DR_BE;
1581 /*
1582 * If we're ignoring parity and break indicators,
1583 * ignore overruns too (for real raw support).
1584 */
1585 if (termios->c_iflag & IGNPAR)
1586 port->ignore_status_mask |= UART011_DR_OE;
1587 }
1588
1589 /*
1590 * Ignore all characters if CREAD is not set.
1591 */
1592 if ((termios->c_cflag & CREAD) == 0)
1593 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1594
1595 if (UART_ENABLE_MS(port, termios->c_cflag))
1596 pl011_enable_ms(port);
1597
1598 /* first, disable everything */
1599 old_cr = readw(port->membase + UART011_CR);
1600 writew(0, port->membase + UART011_CR);
1601
1602 if (termios->c_cflag & CRTSCTS) {
1603 if (old_cr & UART011_CR_RTS)
1604 old_cr |= UART011_CR_RTSEN;
1605
1606 old_cr |= UART011_CR_CTSEN;
1607 uap->autorts = true;
1608 } else {
1609 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1610 uap->autorts = false;
1611 }
1612
1613 if (uap->vendor->oversampling) {
1614 if (baud > port->uartclk / 16)
1615 old_cr |= ST_UART011_CR_OVSFACT;
1616 else
1617 old_cr &= ~ST_UART011_CR_OVSFACT;
1618 }
1619
1620 /* Set baud rate */
1621 writew(quot & 0x3f, port->membase + UART011_FBRD);
1622 writew(quot >> 6, port->membase + UART011_IBRD);
1623
1624 /*
1625 * ----------v----------v----------v----------v-----
1626 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
1627 * ----------^----------^----------^----------^-----
1628 */
1629 writew(lcr_h, port->membase + uap->lcrh_rx);
1630 if (uap->lcrh_rx != uap->lcrh_tx) {
1631 int i;
1632 /*
1633 * Wait 10 PCLKs before writing LCRH_TX register,
1634 * to get this delay write read only register 10 times
1635 */
1636 for (i = 0; i < 10; ++i)
1637 writew(0xff, uap->port.membase + UART011_MIS);
1638 writew(lcr_h, port->membase + uap->lcrh_tx);
1639 }
1640 writew(old_cr, port->membase + UART011_CR);
1641
1642 spin_unlock_irqrestore(&port->lock, flags);
1643}
1644
1645static const char *pl011_type(struct uart_port *port)
1646{
1647 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1648 return uap->port.type == PORT_AMBA ? uap->type : NULL;
1649}
1650
1651/*
1652 * Release the memory region(s) being used by 'port'
1653 */
1654static void pl010_release_port(struct uart_port *port)
1655{
1656 release_mem_region(port->mapbase, SZ_4K);
1657}
1658
1659/*
1660 * Request the memory region(s) being used by 'port'
1661 */
1662static int pl010_request_port(struct uart_port *port)
1663{
1664 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1665 != NULL ? 0 : -EBUSY;
1666}
1667
1668/*
1669 * Configure/autoconfigure the port.
1670 */
1671static void pl010_config_port(struct uart_port *port, int flags)
1672{
1673 if (flags & UART_CONFIG_TYPE) {
1674 port->type = PORT_AMBA;
1675 pl010_request_port(port);
1676 }
1677}
1678
1679/*
1680 * verify the new serial_struct (for TIOCSSERIAL).
1681 */
1682static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
1683{
1684 int ret = 0;
1685 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1686 ret = -EINVAL;
1687 if (ser->irq < 0 || ser->irq >= nr_irqs)
1688 ret = -EINVAL;
1689 if (ser->baud_base < 9600)
1690 ret = -EINVAL;
1691 return ret;
1692}
1693
1694static struct uart_ops amba_pl011_pops = {
1695 .tx_empty = pl01x_tx_empty,
1696 .set_mctrl = pl011_set_mctrl,
1697 .get_mctrl = pl01x_get_mctrl,
1698 .stop_tx = pl011_stop_tx,
1699 .start_tx = pl011_start_tx,
1700 .stop_rx = pl011_stop_rx,
1701 .enable_ms = pl011_enable_ms,
1702 .break_ctl = pl011_break_ctl,
1703 .startup = pl011_startup,
1704 .shutdown = pl011_shutdown,
1705 .flush_buffer = pl011_dma_flush_buffer,
1706 .set_termios = pl011_set_termios,
1707 .type = pl011_type,
1708 .release_port = pl010_release_port,
1709 .request_port = pl010_request_port,
1710 .config_port = pl010_config_port,
1711 .verify_port = pl010_verify_port,
1712#ifdef CONFIG_CONSOLE_POLL
1713 .poll_get_char = pl010_get_poll_char,
1714 .poll_put_char = pl010_put_poll_char,
1715#endif
1716};
1717
1718static struct uart_amba_port *amba_ports[UART_NR];
1719
1720#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1721
1722static void pl011_console_putchar(struct uart_port *port, int ch)
1723{
1724 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1725
1726 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1727 barrier();
1728 writew(ch, uap->port.membase + UART01x_DR);
1729}
1730
1731static void
1732pl011_console_write(struct console *co, const char *s, unsigned int count)
1733{
1734 struct uart_amba_port *uap = amba_ports[co->index];
1735 unsigned int status, old_cr, new_cr;
1736
1737 clk_enable(uap->clk);
1738
1739 /*
1740 * First save the CR then disable the interrupts
1741 */
1742 old_cr = readw(uap->port.membase + UART011_CR);
1743 new_cr = old_cr & ~UART011_CR_CTSEN;
1744 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1745 writew(new_cr, uap->port.membase + UART011_CR);
1746
1747 uart_console_write(&uap->port, s, count, pl011_console_putchar);
1748
1749 /*
1750 * Finally, wait for transmitter to become empty
1751 * and restore the TCR
1752 */
1753 do {
1754 status = readw(uap->port.membase + UART01x_FR);
1755 } while (status & UART01x_FR_BUSY);
1756 writew(old_cr, uap->port.membase + UART011_CR);
1757
1758 clk_disable(uap->clk);
1759}
1760
1761static void __init
1762pl011_console_get_options(struct uart_amba_port *uap, int *baud,
1763 int *parity, int *bits)
1764{
1765 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
1766 unsigned int lcr_h, ibrd, fbrd;
1767
1768 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
1769
1770 *parity = 'n';
1771 if (lcr_h & UART01x_LCRH_PEN) {
1772 if (lcr_h & UART01x_LCRH_EPS)
1773 *parity = 'e';
1774 else
1775 *parity = 'o';
1776 }
1777
1778 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
1779 *bits = 7;
1780 else
1781 *bits = 8;
1782
1783 ibrd = readw(uap->port.membase + UART011_IBRD);
1784 fbrd = readw(uap->port.membase + UART011_FBRD);
1785
1786 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
1787
1788 if (uap->vendor->oversampling) {
1789 if (readw(uap->port.membase + UART011_CR)
1790 & ST_UART011_CR_OVSFACT)
1791 *baud *= 2;
1792 }
1793 }
1794}
1795
1796static int __init pl011_console_setup(struct console *co, char *options)
1797{
1798 struct uart_amba_port *uap;
1799 int baud = 38400;
1800 int bits = 8;
1801 int parity = 'n';
1802 int flow = 'n';
1803
1804 /*
1805 * Check whether an invalid uart number has been specified, and
1806 * if so, search for the first available port that does have
1807 * console support.
1808 */
1809 if (co->index >= UART_NR)
1810 co->index = 0;
1811 uap = amba_ports[co->index];
1812 if (!uap)
1813 return -ENODEV;
1814
1815 if (uap->port.dev->platform_data) {
1816 struct amba_pl011_data *plat;
1817
1818 plat = uap->port.dev->platform_data;
1819 if (plat->init)
1820 plat->init();
1821 }
1822
1823 uap->port.uartclk = clk_get_rate(uap->clk);
1824
1825 if (options)
1826 uart_parse_options(options, &baud, &parity, &bits, &flow);
1827 else
1828 pl011_console_get_options(uap, &baud, &parity, &bits);
1829
1830 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
1831}
1832
1833static struct uart_driver amba_reg;
1834static struct console amba_console = {
1835 .name = "ttyAMA",
1836 .write = pl011_console_write,
1837 .device = uart_console_device,
1838 .setup = pl011_console_setup,
1839 .flags = CON_PRINTBUFFER,
1840 .index = -1,
1841 .data = &amba_reg,
1842};
1843
1844#define AMBA_CONSOLE (&amba_console)
1845#else
1846#define AMBA_CONSOLE NULL
1847#endif
1848
1849static struct uart_driver amba_reg = {
1850 .owner = THIS_MODULE,
1851 .driver_name = "ttyAMA",
1852 .dev_name = "ttyAMA",
1853 .major = SERIAL_AMBA_MAJOR,
1854 .minor = SERIAL_AMBA_MINOR,
1855 .nr = UART_NR,
1856 .cons = AMBA_CONSOLE,
1857};
1858
1859static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
1860{
1861 struct uart_amba_port *uap;
1862 struct vendor_data *vendor = id->data;
1863 void __iomem *base;
1864 int i, ret;
1865
1866 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1867 if (amba_ports[i] == NULL)
1868 break;
1869
1870 if (i == ARRAY_SIZE(amba_ports)) {
1871 ret = -EBUSY;
1872 goto out;
1873 }
1874
1875 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
1876 if (uap == NULL) {
1877 ret = -ENOMEM;
1878 goto out;
1879 }
1880
1881 base = ioremap(dev->res.start, resource_size(&dev->res));
1882 if (!base) {
1883 ret = -ENOMEM;
1884 goto free;
1885 }
1886
1887 uap->clk = clk_get(&dev->dev, NULL);
1888 if (IS_ERR(uap->clk)) {
1889 ret = PTR_ERR(uap->clk);
1890 goto unmap;
1891 }
1892
1893 uap->vendor = vendor;
1894 uap->lcrh_rx = vendor->lcrh_rx;
1895 uap->lcrh_tx = vendor->lcrh_tx;
1896 uap->fifosize = vendor->fifosize;
1897 uap->interrupt_may_hang = vendor->interrupt_may_hang;
1898 uap->port.dev = &dev->dev;
1899 uap->port.mapbase = dev->res.start;
1900 uap->port.membase = base;
1901 uap->port.iotype = UPIO_MEM;
1902 uap->port.irq = dev->irq[0];
1903 uap->port.fifosize = uap->fifosize;
1904 uap->port.ops = &amba_pl011_pops;
1905 uap->port.flags = UPF_BOOT_AUTOCONF;
1906 uap->port.line = i;
1907 pl011_dma_probe(uap);
1908
1909 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
1910
1911 amba_ports[i] = uap;
1912
1913 amba_set_drvdata(dev, uap);
1914 ret = uart_add_one_port(&amba_reg, &uap->port);
1915 if (ret) {
1916 amba_set_drvdata(dev, NULL);
1917 amba_ports[i] = NULL;
1918 pl011_dma_remove(uap);
1919 clk_put(uap->clk);
1920 unmap:
1921 iounmap(base);
1922 free:
1923 kfree(uap);
1924 }
1925 out:
1926 return ret;
1927}
1928
1929static int pl011_remove(struct amba_device *dev)
1930{
1931 struct uart_amba_port *uap = amba_get_drvdata(dev);
1932 int i;
1933
1934 amba_set_drvdata(dev, NULL);
1935
1936 uart_remove_one_port(&amba_reg, &uap->port);
1937
1938 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1939 if (amba_ports[i] == uap)
1940 amba_ports[i] = NULL;
1941
1942 pl011_dma_remove(uap);
1943 iounmap(uap->port.membase);
1944 clk_put(uap->clk);
1945 kfree(uap);
1946 return 0;
1947}
1948
1949#ifdef CONFIG_PM
1950static int pl011_suspend(struct amba_device *dev, pm_message_t state)
1951{
1952 struct uart_amba_port *uap = amba_get_drvdata(dev);
1953
1954 if (!uap)
1955 return -EINVAL;
1956
1957 return uart_suspend_port(&amba_reg, &uap->port);
1958}
1959
1960static int pl011_resume(struct amba_device *dev)
1961{
1962 struct uart_amba_port *uap = amba_get_drvdata(dev);
1963
1964 if (!uap)
1965 return -EINVAL;
1966
1967 return uart_resume_port(&amba_reg, &uap->port);
1968}
1969#endif
1970
1971static struct amba_id pl011_ids[] = {
1972 {
1973 .id = 0x00041011,
1974 .mask = 0x000fffff,
1975 .data = &vendor_arm,
1976 },
1977 {
1978 .id = 0x00380802,
1979 .mask = 0x00ffffff,
1980 .data = &vendor_st,
1981 },
1982 { 0, 0 },
1983};
1984
1985static struct amba_driver pl011_driver = {
1986 .drv = {
1987 .name = "uart-pl011",
1988 },
1989 .id_table = pl011_ids,
1990 .probe = pl011_probe,
1991 .remove = pl011_remove,
1992#ifdef CONFIG_PM
1993 .suspend = pl011_suspend,
1994 .resume = pl011_resume,
1995#endif
1996};
1997
1998static int __init pl011_init(void)
1999{
2000 int ret;
2001 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2002
2003 ret = uart_register_driver(&amba_reg);
2004 if (ret == 0) {
2005 ret = amba_driver_register(&pl011_driver);
2006 if (ret)
2007 uart_unregister_driver(&amba_reg);
2008 }
2009 return ret;
2010}
2011
2012static void __exit pl011_exit(void)
2013{
2014 amba_driver_unregister(&pl011_driver);
2015 uart_unregister_driver(&amba_reg);
2016}
2017
2018/*
2019 * While this can be a module, if builtin it's most likely the console
2020 * So let's leave module_exit but move module_init to an earlier place
2021 */
2022arch_initcall(pl011_init);
2023module_exit(pl011_exit);
2024
2025MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2026MODULE_DESCRIPTION("ARM AMBA serial port driver");
2027MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/apbuart.c b/drivers/tty/serial/apbuart.c
new file mode 100644
index 000000000000..19a943693e4c
--- /dev/null
+++ b/drivers/tty/serial/apbuart.c
@@ -0,0 +1,696 @@
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{
558 int i;
559 struct uart_port *port = NULL;
560
561 for (i = 0; i < grlib_apbuart_port_nr; i++) {
562 if (op->dev.of_node == grlib_apbuart_nodes[i])
563 break;
564 }
565
566 port = &grlib_apbuart_ports[i];
567 port->dev = &op->dev;
568 port->irq = op->archdata.irqs[0];
569
570 uart_add_one_port(&grlib_apbuart_driver, (struct uart_port *) port);
571
572 apbuart_flush_fifo((struct uart_port *) port);
573
574 printk(KERN_INFO "grlib-apbuart at 0x%llx, irq %d\n",
575 (unsigned long long) port->mapbase, port->irq);
576 return 0;
577}
578
579static struct of_device_id __initdata apbuart_match[] = {
580 {
581 .name = "GAISLER_APBUART",
582 },
583 {
584 .name = "01_00c",
585 },
586 {},
587};
588
589static struct platform_driver grlib_apbuart_of_driver = {
590 .probe = apbuart_probe,
591 .driver = {
592 .owner = THIS_MODULE,
593 .name = "grlib-apbuart",
594 .of_match_table = apbuart_match,
595 },
596};
597
598
599static int grlib_apbuart_configure(void)
600{
601 struct device_node *np;
602 int line = 0;
603
604 for_each_matching_node(np, apbuart_match) {
605 const int *ampopts;
606 const u32 *freq_hz;
607 const struct amba_prom_registers *regs;
608 struct uart_port *port;
609 unsigned long addr;
610
611 ampopts = of_get_property(np, "ampopts", NULL);
612 if (ampopts && (*ampopts == 0))
613 continue; /* Ignore if used by another OS instance */
614 regs = of_get_property(np, "reg", NULL);
615 /* Frequency of APB Bus is frequency of UART */
616 freq_hz = of_get_property(np, "freq", NULL);
617
618 if (!regs || !freq_hz || (*freq_hz == 0))
619 continue;
620
621 grlib_apbuart_nodes[line] = np;
622
623 addr = regs->phys_addr;
624
625 port = &grlib_apbuart_ports[line];
626
627 port->mapbase = addr;
628 port->membase = ioremap(addr, sizeof(struct grlib_apbuart_regs_map));
629 port->irq = 0;
630 port->iotype = UPIO_MEM;
631 port->ops = &grlib_apbuart_ops;
632 port->flags = UPF_BOOT_AUTOCONF;
633 port->line = line;
634 port->uartclk = *freq_hz;
635 port->fifosize = apbuart_scan_fifo_size((struct uart_port *) port, line);
636 line++;
637
638 /* We support maximum UART_NR uarts ... */
639 if (line == UART_NR)
640 break;
641 }
642
643 grlib_apbuart_driver.nr = grlib_apbuart_port_nr = line;
644 return line ? 0 : -ENODEV;
645}
646
647static int __init grlib_apbuart_init(void)
648{
649 int ret;
650
651 /* Find all APBUARTS in device the tree and initialize their ports */
652 ret = grlib_apbuart_configure();
653 if (ret)
654 return ret;
655
656 printk(KERN_INFO "Serial: GRLIB APBUART driver\n");
657
658 ret = uart_register_driver(&grlib_apbuart_driver);
659
660 if (ret) {
661 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
662 __FILE__, ret);
663 return ret;
664 }
665
666 ret = platform_driver_register(&grlib_apbuart_of_driver);
667 if (ret) {
668 printk(KERN_ERR
669 "%s: platform_driver_register failed (%i)\n",
670 __FILE__, ret);
671 uart_unregister_driver(&grlib_apbuart_driver);
672 return ret;
673 }
674
675 return ret;
676}
677
678static void __exit grlib_apbuart_exit(void)
679{
680 int i;
681
682 for (i = 0; i < grlib_apbuart_port_nr; i++)
683 uart_remove_one_port(&grlib_apbuart_driver,
684 &grlib_apbuart_ports[i]);
685
686 uart_unregister_driver(&grlib_apbuart_driver);
687 platform_driver_unregister(&grlib_apbuart_of_driver);
688}
689
690module_init(grlib_apbuart_init);
691module_exit(grlib_apbuart_exit);
692
693MODULE_AUTHOR("Aeroflex Gaisler AB");
694MODULE_DESCRIPTION("GRLIB APBUART serial driver");
695MODULE_VERSION("2.1");
696MODULE_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..af9b7814965a
--- /dev/null
+++ b/drivers/tty/serial/atmel_serial.c
@@ -0,0 +1,1828 @@
1/*
2 * Driver for Atmel AT91 / AT32 Serial ports
3 * Copyright (C) 2003 Rick Bronson
4 *
5 * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * DMA support added by Chip Coldwell.
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#include <linux/module.h>
26#include <linux/tty.h>
27#include <linux/ioport.h>
28#include <linux/slab.h>
29#include <linux/init.h>
30#include <linux/serial.h>
31#include <linux/clk.h>
32#include <linux/console.h>
33#include <linux/sysrq.h>
34#include <linux/tty_flip.h>
35#include <linux/platform_device.h>
36#include <linux/dma-mapping.h>
37#include <linux/atmel_pdc.h>
38#include <linux/atmel_serial.h>
39#include <linux/uaccess.h>
40
41#include <asm/io.h>
42#include <asm/ioctls.h>
43
44#include <asm/mach/serial_at91.h>
45#include <mach/board.h>
46
47#ifdef CONFIG_ARM
48#include <mach/cpu.h>
49#include <mach/gpio.h>
50#endif
51
52#define PDC_BUFFER_SIZE 512
53/* Revisit: We should calculate this based on the actual port settings */
54#define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
55
56#if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
57#define SUPPORT_SYSRQ
58#endif
59
60#include <linux/serial_core.h>
61
62static void atmel_start_rx(struct uart_port *port);
63static void atmel_stop_rx(struct uart_port *port);
64
65#ifdef CONFIG_SERIAL_ATMEL_TTYAT
66
67/* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
68 * should coexist with the 8250 driver, such as if we have an external 16C550
69 * UART. */
70#define SERIAL_ATMEL_MAJOR 204
71#define MINOR_START 154
72#define ATMEL_DEVICENAME "ttyAT"
73
74#else
75
76/* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
77 * name, but it is legally reserved for the 8250 driver. */
78#define SERIAL_ATMEL_MAJOR TTY_MAJOR
79#define MINOR_START 64
80#define ATMEL_DEVICENAME "ttyS"
81
82#endif
83
84#define ATMEL_ISR_PASS_LIMIT 256
85
86/* UART registers. CR is write-only, hence no GET macro */
87#define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
88#define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
89#define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
90#define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
91#define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
92#define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
93#define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
94#define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
95#define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
96#define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
97#define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
98#define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
99#define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
100
101 /* PDC registers */
102#define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
103#define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
104
105#define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
106#define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
107#define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
108#define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
109#define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
110
111#define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
112#define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
113#define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
114
115static int (*atmel_open_hook)(struct uart_port *);
116static void (*atmel_close_hook)(struct uart_port *);
117
118struct atmel_dma_buffer {
119 unsigned char *buf;
120 dma_addr_t dma_addr;
121 unsigned int dma_size;
122 unsigned int ofs;
123};
124
125struct atmel_uart_char {
126 u16 status;
127 u16 ch;
128};
129
130#define ATMEL_SERIAL_RINGSIZE 1024
131
132/*
133 * We wrap our port structure around the generic uart_port.
134 */
135struct atmel_uart_port {
136 struct uart_port uart; /* uart */
137 struct clk *clk; /* uart clock */
138 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
139 u32 backup_imr; /* IMR saved during suspend */
140 int break_active; /* break being received */
141
142 short use_dma_rx; /* enable PDC receiver */
143 short pdc_rx_idx; /* current PDC RX buffer */
144 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
145
146 short use_dma_tx; /* enable PDC transmitter */
147 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
148
149 struct tasklet_struct tasklet;
150 unsigned int irq_status;
151 unsigned int irq_status_prev;
152
153 struct circ_buf rx_ring;
154
155 struct serial_rs485 rs485; /* rs485 settings */
156 unsigned int tx_done_mask;
157};
158
159static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
160
161#ifdef SUPPORT_SYSRQ
162static struct console atmel_console;
163#endif
164
165static inline struct atmel_uart_port *
166to_atmel_uart_port(struct uart_port *uart)
167{
168 return container_of(uart, struct atmel_uart_port, uart);
169}
170
171#ifdef CONFIG_SERIAL_ATMEL_PDC
172static bool atmel_use_dma_rx(struct uart_port *port)
173{
174 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
175
176 return atmel_port->use_dma_rx;
177}
178
179static bool atmel_use_dma_tx(struct uart_port *port)
180{
181 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
182
183 return atmel_port->use_dma_tx;
184}
185#else
186static bool atmel_use_dma_rx(struct uart_port *port)
187{
188 return false;
189}
190
191static bool atmel_use_dma_tx(struct uart_port *port)
192{
193 return false;
194}
195#endif
196
197/* Enable or disable the rs485 support */
198void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
199{
200 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
201 unsigned int mode;
202
203 spin_lock(&port->lock);
204
205 /* Disable interrupts */
206 UART_PUT_IDR(port, atmel_port->tx_done_mask);
207
208 mode = UART_GET_MR(port);
209
210 /* Resetting serial mode to RS232 (0x0) */
211 mode &= ~ATMEL_US_USMODE;
212
213 atmel_port->rs485 = *rs485conf;
214
215 if (rs485conf->flags & SER_RS485_ENABLED) {
216 dev_dbg(port->dev, "Setting UART to RS485\n");
217 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
218 if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
219 UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
220 mode |= ATMEL_US_USMODE_RS485;
221 } else {
222 dev_dbg(port->dev, "Setting UART to RS232\n");
223 if (atmel_use_dma_tx(port))
224 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
225 ATMEL_US_TXBUFE;
226 else
227 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
228 }
229 UART_PUT_MR(port, mode);
230
231 /* Enable interrupts */
232 UART_PUT_IER(port, atmel_port->tx_done_mask);
233
234 spin_unlock(&port->lock);
235
236}
237
238/*
239 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
240 */
241static u_int atmel_tx_empty(struct uart_port *port)
242{
243 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
244}
245
246/*
247 * Set state of the modem control output lines
248 */
249static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
250{
251 unsigned int control = 0;
252 unsigned int mode;
253 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
254
255#ifdef CONFIG_ARCH_AT91RM9200
256 if (cpu_is_at91rm9200()) {
257 /*
258 * AT91RM9200 Errata #39: RTS0 is not internally connected
259 * to PA21. We need to drive the pin manually.
260 */
261 if (port->mapbase == AT91RM9200_BASE_US0) {
262 if (mctrl & TIOCM_RTS)
263 at91_set_gpio_value(AT91_PIN_PA21, 0);
264 else
265 at91_set_gpio_value(AT91_PIN_PA21, 1);
266 }
267 }
268#endif
269
270 if (mctrl & TIOCM_RTS)
271 control |= ATMEL_US_RTSEN;
272 else
273 control |= ATMEL_US_RTSDIS;
274
275 if (mctrl & TIOCM_DTR)
276 control |= ATMEL_US_DTREN;
277 else
278 control |= ATMEL_US_DTRDIS;
279
280 UART_PUT_CR(port, control);
281
282 /* Local loopback mode? */
283 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
284 if (mctrl & TIOCM_LOOP)
285 mode |= ATMEL_US_CHMODE_LOC_LOOP;
286 else
287 mode |= ATMEL_US_CHMODE_NORMAL;
288
289 /* Resetting serial mode to RS232 (0x0) */
290 mode &= ~ATMEL_US_USMODE;
291
292 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
293 dev_dbg(port->dev, "Setting UART to RS485\n");
294 if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
295 UART_PUT_TTGR(port,
296 atmel_port->rs485.delay_rts_after_send);
297 mode |= ATMEL_US_USMODE_RS485;
298 } else {
299 dev_dbg(port->dev, "Setting UART to RS232\n");
300 }
301 UART_PUT_MR(port, mode);
302}
303
304/*
305 * Get state of the modem control input lines
306 */
307static u_int atmel_get_mctrl(struct uart_port *port)
308{
309 unsigned int status, ret = 0;
310
311 status = UART_GET_CSR(port);
312
313 /*
314 * The control signals are active low.
315 */
316 if (!(status & ATMEL_US_DCD))
317 ret |= TIOCM_CD;
318 if (!(status & ATMEL_US_CTS))
319 ret |= TIOCM_CTS;
320 if (!(status & ATMEL_US_DSR))
321 ret |= TIOCM_DSR;
322 if (!(status & ATMEL_US_RI))
323 ret |= TIOCM_RI;
324
325 return ret;
326}
327
328/*
329 * Stop transmitting.
330 */
331static void atmel_stop_tx(struct uart_port *port)
332{
333 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
334
335 if (atmel_use_dma_tx(port)) {
336 /* disable PDC transmit */
337 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
338 }
339 /* Disable interrupts */
340 UART_PUT_IDR(port, atmel_port->tx_done_mask);
341
342 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
343 atmel_start_rx(port);
344}
345
346/*
347 * Start transmitting.
348 */
349static void atmel_start_tx(struct uart_port *port)
350{
351 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
352
353 if (atmel_use_dma_tx(port)) {
354 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
355 /* The transmitter is already running. Yes, we
356 really need this.*/
357 return;
358
359 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
360 atmel_stop_rx(port);
361
362 /* re-enable PDC transmit */
363 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
364 }
365 /* Enable interrupts */
366 UART_PUT_IER(port, atmel_port->tx_done_mask);
367}
368
369/*
370 * start receiving - port is in process of being opened.
371 */
372static void atmel_start_rx(struct uart_port *port)
373{
374 UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */
375
376 if (atmel_use_dma_rx(port)) {
377 /* enable PDC controller */
378 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
379 port->read_status_mask);
380 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
381 } else {
382 UART_PUT_IER(port, ATMEL_US_RXRDY);
383 }
384}
385
386/*
387 * Stop receiving - port is in process of being closed.
388 */
389static void atmel_stop_rx(struct uart_port *port)
390{
391 if (atmel_use_dma_rx(port)) {
392 /* disable PDC receive */
393 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
394 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
395 port->read_status_mask);
396 } else {
397 UART_PUT_IDR(port, ATMEL_US_RXRDY);
398 }
399}
400
401/*
402 * Enable modem status interrupts
403 */
404static void atmel_enable_ms(struct uart_port *port)
405{
406 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
407 | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
408}
409
410/*
411 * Control the transmission of a break signal
412 */
413static void atmel_break_ctl(struct uart_port *port, int break_state)
414{
415 if (break_state != 0)
416 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
417 else
418 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
419}
420
421/*
422 * Stores the incoming character in the ring buffer
423 */
424static void
425atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
426 unsigned int ch)
427{
428 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
429 struct circ_buf *ring = &atmel_port->rx_ring;
430 struct atmel_uart_char *c;
431
432 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
433 /* Buffer overflow, ignore char */
434 return;
435
436 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
437 c->status = status;
438 c->ch = ch;
439
440 /* Make sure the character is stored before we update head. */
441 smp_wmb();
442
443 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
444}
445
446/*
447 * Deal with parity, framing and overrun errors.
448 */
449static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
450{
451 /* clear error */
452 UART_PUT_CR(port, ATMEL_US_RSTSTA);
453
454 if (status & ATMEL_US_RXBRK) {
455 /* ignore side-effect */
456 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
457 port->icount.brk++;
458 }
459 if (status & ATMEL_US_PARE)
460 port->icount.parity++;
461 if (status & ATMEL_US_FRAME)
462 port->icount.frame++;
463 if (status & ATMEL_US_OVRE)
464 port->icount.overrun++;
465}
466
467/*
468 * Characters received (called from interrupt handler)
469 */
470static void atmel_rx_chars(struct uart_port *port)
471{
472 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
473 unsigned int status, ch;
474
475 status = UART_GET_CSR(port);
476 while (status & ATMEL_US_RXRDY) {
477 ch = UART_GET_CHAR(port);
478
479 /*
480 * note that the error handling code is
481 * out of the main execution path
482 */
483 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
484 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
485 || atmel_port->break_active)) {
486
487 /* clear error */
488 UART_PUT_CR(port, ATMEL_US_RSTSTA);
489
490 if (status & ATMEL_US_RXBRK
491 && !atmel_port->break_active) {
492 atmel_port->break_active = 1;
493 UART_PUT_IER(port, ATMEL_US_RXBRK);
494 } else {
495 /*
496 * This is either the end-of-break
497 * condition or we've received at
498 * least one character without RXBRK
499 * being set. In both cases, the next
500 * RXBRK will indicate start-of-break.
501 */
502 UART_PUT_IDR(port, ATMEL_US_RXBRK);
503 status &= ~ATMEL_US_RXBRK;
504 atmel_port->break_active = 0;
505 }
506 }
507
508 atmel_buffer_rx_char(port, status, ch);
509 status = UART_GET_CSR(port);
510 }
511
512 tasklet_schedule(&atmel_port->tasklet);
513}
514
515/*
516 * Transmit characters (called from tasklet with TXRDY interrupt
517 * disabled)
518 */
519static void atmel_tx_chars(struct uart_port *port)
520{
521 struct circ_buf *xmit = &port->state->xmit;
522 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
523
524 if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
525 UART_PUT_CHAR(port, port->x_char);
526 port->icount.tx++;
527 port->x_char = 0;
528 }
529 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
530 return;
531
532 while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
533 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
534 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
535 port->icount.tx++;
536 if (uart_circ_empty(xmit))
537 break;
538 }
539
540 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
541 uart_write_wakeup(port);
542
543 if (!uart_circ_empty(xmit))
544 /* Enable interrupts */
545 UART_PUT_IER(port, atmel_port->tx_done_mask);
546}
547
548/*
549 * receive interrupt handler.
550 */
551static void
552atmel_handle_receive(struct uart_port *port, unsigned int pending)
553{
554 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
555
556 if (atmel_use_dma_rx(port)) {
557 /*
558 * PDC receive. Just schedule the tasklet and let it
559 * figure out the details.
560 *
561 * TODO: We're not handling error flags correctly at
562 * the moment.
563 */
564 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
565 UART_PUT_IDR(port, (ATMEL_US_ENDRX
566 | ATMEL_US_TIMEOUT));
567 tasklet_schedule(&atmel_port->tasklet);
568 }
569
570 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
571 ATMEL_US_FRAME | ATMEL_US_PARE))
572 atmel_pdc_rxerr(port, pending);
573 }
574
575 /* Interrupt receive */
576 if (pending & ATMEL_US_RXRDY)
577 atmel_rx_chars(port);
578 else if (pending & ATMEL_US_RXBRK) {
579 /*
580 * End of break detected. If it came along with a
581 * character, atmel_rx_chars will handle it.
582 */
583 UART_PUT_CR(port, ATMEL_US_RSTSTA);
584 UART_PUT_IDR(port, ATMEL_US_RXBRK);
585 atmel_port->break_active = 0;
586 }
587}
588
589/*
590 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
591 */
592static void
593atmel_handle_transmit(struct uart_port *port, unsigned int pending)
594{
595 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
596
597 if (pending & atmel_port->tx_done_mask) {
598 /* Either PDC or interrupt transmission */
599 UART_PUT_IDR(port, atmel_port->tx_done_mask);
600 tasklet_schedule(&atmel_port->tasklet);
601 }
602}
603
604/*
605 * status flags interrupt handler.
606 */
607static void
608atmel_handle_status(struct uart_port *port, unsigned int pending,
609 unsigned int status)
610{
611 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
612
613 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
614 | ATMEL_US_CTSIC)) {
615 atmel_port->irq_status = status;
616 tasklet_schedule(&atmel_port->tasklet);
617 }
618}
619
620/*
621 * Interrupt handler
622 */
623static irqreturn_t atmel_interrupt(int irq, void *dev_id)
624{
625 struct uart_port *port = dev_id;
626 unsigned int status, pending, pass_counter = 0;
627
628 do {
629 status = UART_GET_CSR(port);
630 pending = status & UART_GET_IMR(port);
631 if (!pending)
632 break;
633
634 atmel_handle_receive(port, pending);
635 atmel_handle_status(port, pending, status);
636 atmel_handle_transmit(port, pending);
637 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
638
639 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
640}
641
642/*
643 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
644 */
645static void atmel_tx_dma(struct uart_port *port)
646{
647 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
648 struct circ_buf *xmit = &port->state->xmit;
649 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
650 int count;
651
652 /* nothing left to transmit? */
653 if (UART_GET_TCR(port))
654 return;
655
656 xmit->tail += pdc->ofs;
657 xmit->tail &= UART_XMIT_SIZE - 1;
658
659 port->icount.tx += pdc->ofs;
660 pdc->ofs = 0;
661
662 /* more to transmit - setup next transfer */
663
664 /* disable PDC transmit */
665 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
666
667 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
668 dma_sync_single_for_device(port->dev,
669 pdc->dma_addr,
670 pdc->dma_size,
671 DMA_TO_DEVICE);
672
673 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
674 pdc->ofs = count;
675
676 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
677 UART_PUT_TCR(port, count);
678 /* re-enable PDC transmit */
679 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
680 /* Enable interrupts */
681 UART_PUT_IER(port, atmel_port->tx_done_mask);
682 } else {
683 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
684 /* DMA done, stop TX, start RX for RS485 */
685 atmel_start_rx(port);
686 }
687 }
688
689 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
690 uart_write_wakeup(port);
691}
692
693static void atmel_rx_from_ring(struct uart_port *port)
694{
695 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
696 struct circ_buf *ring = &atmel_port->rx_ring;
697 unsigned int flg;
698 unsigned int status;
699
700 while (ring->head != ring->tail) {
701 struct atmel_uart_char c;
702
703 /* Make sure c is loaded after head. */
704 smp_rmb();
705
706 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
707
708 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
709
710 port->icount.rx++;
711 status = c.status;
712 flg = TTY_NORMAL;
713
714 /*
715 * note that the error handling code is
716 * out of the main execution path
717 */
718 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
719 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
720 if (status & ATMEL_US_RXBRK) {
721 /* ignore side-effect */
722 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
723
724 port->icount.brk++;
725 if (uart_handle_break(port))
726 continue;
727 }
728 if (status & ATMEL_US_PARE)
729 port->icount.parity++;
730 if (status & ATMEL_US_FRAME)
731 port->icount.frame++;
732 if (status & ATMEL_US_OVRE)
733 port->icount.overrun++;
734
735 status &= port->read_status_mask;
736
737 if (status & ATMEL_US_RXBRK)
738 flg = TTY_BREAK;
739 else if (status & ATMEL_US_PARE)
740 flg = TTY_PARITY;
741 else if (status & ATMEL_US_FRAME)
742 flg = TTY_FRAME;
743 }
744
745
746 if (uart_handle_sysrq_char(port, c.ch))
747 continue;
748
749 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
750 }
751
752 /*
753 * Drop the lock here since it might end up calling
754 * uart_start(), which takes the lock.
755 */
756 spin_unlock(&port->lock);
757 tty_flip_buffer_push(port->state->port.tty);
758 spin_lock(&port->lock);
759}
760
761static void atmel_rx_from_dma(struct uart_port *port)
762{
763 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
764 struct tty_struct *tty = port->state->port.tty;
765 struct atmel_dma_buffer *pdc;
766 int rx_idx = atmel_port->pdc_rx_idx;
767 unsigned int head;
768 unsigned int tail;
769 unsigned int count;
770
771 do {
772 /* Reset the UART timeout early so that we don't miss one */
773 UART_PUT_CR(port, ATMEL_US_STTTO);
774
775 pdc = &atmel_port->pdc_rx[rx_idx];
776 head = UART_GET_RPR(port) - pdc->dma_addr;
777 tail = pdc->ofs;
778
779 /* If the PDC has switched buffers, RPR won't contain
780 * any address within the current buffer. Since head
781 * is unsigned, we just need a one-way comparison to
782 * find out.
783 *
784 * In this case, we just need to consume the entire
785 * buffer and resubmit it for DMA. This will clear the
786 * ENDRX bit as well, so that we can safely re-enable
787 * all interrupts below.
788 */
789 head = min(head, pdc->dma_size);
790
791 if (likely(head != tail)) {
792 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
793 pdc->dma_size, DMA_FROM_DEVICE);
794
795 /*
796 * head will only wrap around when we recycle
797 * the DMA buffer, and when that happens, we
798 * explicitly set tail to 0. So head will
799 * always be greater than tail.
800 */
801 count = head - tail;
802
803 tty_insert_flip_string(tty, pdc->buf + pdc->ofs, count);
804
805 dma_sync_single_for_device(port->dev, pdc->dma_addr,
806 pdc->dma_size, DMA_FROM_DEVICE);
807
808 port->icount.rx += count;
809 pdc->ofs = head;
810 }
811
812 /*
813 * If the current buffer is full, we need to check if
814 * the next one contains any additional data.
815 */
816 if (head >= pdc->dma_size) {
817 pdc->ofs = 0;
818 UART_PUT_RNPR(port, pdc->dma_addr);
819 UART_PUT_RNCR(port, pdc->dma_size);
820
821 rx_idx = !rx_idx;
822 atmel_port->pdc_rx_idx = rx_idx;
823 }
824 } while (head >= pdc->dma_size);
825
826 /*
827 * Drop the lock here since it might end up calling
828 * uart_start(), which takes the lock.
829 */
830 spin_unlock(&port->lock);
831 tty_flip_buffer_push(tty);
832 spin_lock(&port->lock);
833
834 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
835}
836
837/*
838 * tasklet handling tty stuff outside the interrupt handler.
839 */
840static void atmel_tasklet_func(unsigned long data)
841{
842 struct uart_port *port = (struct uart_port *)data;
843 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
844 unsigned int status;
845 unsigned int status_change;
846
847 /* The interrupt handler does not take the lock */
848 spin_lock(&port->lock);
849
850 if (atmel_use_dma_tx(port))
851 atmel_tx_dma(port);
852 else
853 atmel_tx_chars(port);
854
855 status = atmel_port->irq_status;
856 status_change = status ^ atmel_port->irq_status_prev;
857
858 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
859 | ATMEL_US_DCD | ATMEL_US_CTS)) {
860 /* TODO: All reads to CSR will clear these interrupts! */
861 if (status_change & ATMEL_US_RI)
862 port->icount.rng++;
863 if (status_change & ATMEL_US_DSR)
864 port->icount.dsr++;
865 if (status_change & ATMEL_US_DCD)
866 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
867 if (status_change & ATMEL_US_CTS)
868 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
869
870 wake_up_interruptible(&port->state->port.delta_msr_wait);
871
872 atmel_port->irq_status_prev = status;
873 }
874
875 if (atmel_use_dma_rx(port))
876 atmel_rx_from_dma(port);
877 else
878 atmel_rx_from_ring(port);
879
880 spin_unlock(&port->lock);
881}
882
883/*
884 * Perform initialization and enable port for reception
885 */
886static int atmel_startup(struct uart_port *port)
887{
888 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
889 struct tty_struct *tty = port->state->port.tty;
890 int retval;
891
892 /*
893 * Ensure that no interrupts are enabled otherwise when
894 * request_irq() is called we could get stuck trying to
895 * handle an unexpected interrupt
896 */
897 UART_PUT_IDR(port, -1);
898
899 /*
900 * Allocate the IRQ
901 */
902 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
903 tty ? tty->name : "atmel_serial", port);
904 if (retval) {
905 printk("atmel_serial: atmel_startup - Can't get irq\n");
906 return retval;
907 }
908
909 /*
910 * Initialize DMA (if necessary)
911 */
912 if (atmel_use_dma_rx(port)) {
913 int i;
914
915 for (i = 0; i < 2; i++) {
916 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
917
918 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
919 if (pdc->buf == NULL) {
920 if (i != 0) {
921 dma_unmap_single(port->dev,
922 atmel_port->pdc_rx[0].dma_addr,
923 PDC_BUFFER_SIZE,
924 DMA_FROM_DEVICE);
925 kfree(atmel_port->pdc_rx[0].buf);
926 }
927 free_irq(port->irq, port);
928 return -ENOMEM;
929 }
930 pdc->dma_addr = dma_map_single(port->dev,
931 pdc->buf,
932 PDC_BUFFER_SIZE,
933 DMA_FROM_DEVICE);
934 pdc->dma_size = PDC_BUFFER_SIZE;
935 pdc->ofs = 0;
936 }
937
938 atmel_port->pdc_rx_idx = 0;
939
940 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
941 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
942
943 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
944 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
945 }
946 if (atmel_use_dma_tx(port)) {
947 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
948 struct circ_buf *xmit = &port->state->xmit;
949
950 pdc->buf = xmit->buf;
951 pdc->dma_addr = dma_map_single(port->dev,
952 pdc->buf,
953 UART_XMIT_SIZE,
954 DMA_TO_DEVICE);
955 pdc->dma_size = UART_XMIT_SIZE;
956 pdc->ofs = 0;
957 }
958
959 /*
960 * If there is a specific "open" function (to register
961 * control line interrupts)
962 */
963 if (atmel_open_hook) {
964 retval = atmel_open_hook(port);
965 if (retval) {
966 free_irq(port->irq, port);
967 return retval;
968 }
969 }
970
971 /* Save current CSR for comparison in atmel_tasklet_func() */
972 atmel_port->irq_status_prev = UART_GET_CSR(port);
973 atmel_port->irq_status = atmel_port->irq_status_prev;
974
975 /*
976 * Finally, enable the serial port
977 */
978 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
979 /* enable xmit & rcvr */
980 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
981
982 if (atmel_use_dma_rx(port)) {
983 /* set UART timeout */
984 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
985 UART_PUT_CR(port, ATMEL_US_STTTO);
986
987 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
988 /* enable PDC controller */
989 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
990 } else {
991 /* enable receive only */
992 UART_PUT_IER(port, ATMEL_US_RXRDY);
993 }
994
995 return 0;
996}
997
998/*
999 * Disable the port
1000 */
1001static void atmel_shutdown(struct uart_port *port)
1002{
1003 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1004 /*
1005 * Ensure everything is stopped.
1006 */
1007 atmel_stop_rx(port);
1008 atmel_stop_tx(port);
1009
1010 /*
1011 * Shut-down the DMA.
1012 */
1013 if (atmel_use_dma_rx(port)) {
1014 int i;
1015
1016 for (i = 0; i < 2; i++) {
1017 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1018
1019 dma_unmap_single(port->dev,
1020 pdc->dma_addr,
1021 pdc->dma_size,
1022 DMA_FROM_DEVICE);
1023 kfree(pdc->buf);
1024 }
1025 }
1026 if (atmel_use_dma_tx(port)) {
1027 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1028
1029 dma_unmap_single(port->dev,
1030 pdc->dma_addr,
1031 pdc->dma_size,
1032 DMA_TO_DEVICE);
1033 }
1034
1035 /*
1036 * Disable all interrupts, port and break condition.
1037 */
1038 UART_PUT_CR(port, ATMEL_US_RSTSTA);
1039 UART_PUT_IDR(port, -1);
1040
1041 /*
1042 * Free the interrupt
1043 */
1044 free_irq(port->irq, port);
1045
1046 /*
1047 * If there is a specific "close" function (to unregister
1048 * control line interrupts)
1049 */
1050 if (atmel_close_hook)
1051 atmel_close_hook(port);
1052}
1053
1054/*
1055 * Flush any TX data submitted for DMA. Called when the TX circular
1056 * buffer is reset.
1057 */
1058static void atmel_flush_buffer(struct uart_port *port)
1059{
1060 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1061
1062 if (atmel_use_dma_tx(port)) {
1063 UART_PUT_TCR(port, 0);
1064 atmel_port->pdc_tx.ofs = 0;
1065 }
1066}
1067
1068/*
1069 * Power / Clock management.
1070 */
1071static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1072 unsigned int oldstate)
1073{
1074 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1075
1076 switch (state) {
1077 case 0:
1078 /*
1079 * Enable the peripheral clock for this serial port.
1080 * This is called on uart_open() or a resume event.
1081 */
1082 clk_enable(atmel_port->clk);
1083
1084 /* re-enable interrupts if we disabled some on suspend */
1085 UART_PUT_IER(port, atmel_port->backup_imr);
1086 break;
1087 case 3:
1088 /* Back up the interrupt mask and disable all interrupts */
1089 atmel_port->backup_imr = UART_GET_IMR(port);
1090 UART_PUT_IDR(port, -1);
1091
1092 /*
1093 * Disable the peripheral clock for this serial port.
1094 * This is called on uart_close() or a suspend event.
1095 */
1096 clk_disable(atmel_port->clk);
1097 break;
1098 default:
1099 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
1100 }
1101}
1102
1103/*
1104 * Change the port parameters
1105 */
1106static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1107 struct ktermios *old)
1108{
1109 unsigned long flags;
1110 unsigned int mode, imr, quot, baud;
1111 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1112
1113 /* Get current mode register */
1114 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1115 | ATMEL_US_NBSTOP | ATMEL_US_PAR
1116 | ATMEL_US_USMODE);
1117
1118 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1119 quot = uart_get_divisor(port, baud);
1120
1121 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
1122 quot /= 8;
1123 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1124 }
1125
1126 /* byte size */
1127 switch (termios->c_cflag & CSIZE) {
1128 case CS5:
1129 mode |= ATMEL_US_CHRL_5;
1130 break;
1131 case CS6:
1132 mode |= ATMEL_US_CHRL_6;
1133 break;
1134 case CS7:
1135 mode |= ATMEL_US_CHRL_7;
1136 break;
1137 default:
1138 mode |= ATMEL_US_CHRL_8;
1139 break;
1140 }
1141
1142 /* stop bits */
1143 if (termios->c_cflag & CSTOPB)
1144 mode |= ATMEL_US_NBSTOP_2;
1145
1146 /* parity */
1147 if (termios->c_cflag & PARENB) {
1148 /* Mark or Space parity */
1149 if (termios->c_cflag & CMSPAR) {
1150 if (termios->c_cflag & PARODD)
1151 mode |= ATMEL_US_PAR_MARK;
1152 else
1153 mode |= ATMEL_US_PAR_SPACE;
1154 } else if (termios->c_cflag & PARODD)
1155 mode |= ATMEL_US_PAR_ODD;
1156 else
1157 mode |= ATMEL_US_PAR_EVEN;
1158 } else
1159 mode |= ATMEL_US_PAR_NONE;
1160
1161 /* hardware handshake (RTS/CTS) */
1162 if (termios->c_cflag & CRTSCTS)
1163 mode |= ATMEL_US_USMODE_HWHS;
1164 else
1165 mode |= ATMEL_US_USMODE_NORMAL;
1166
1167 spin_lock_irqsave(&port->lock, flags);
1168
1169 port->read_status_mask = ATMEL_US_OVRE;
1170 if (termios->c_iflag & INPCK)
1171 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1172 if (termios->c_iflag & (BRKINT | PARMRK))
1173 port->read_status_mask |= ATMEL_US_RXBRK;
1174
1175 if (atmel_use_dma_rx(port))
1176 /* need to enable error interrupts */
1177 UART_PUT_IER(port, port->read_status_mask);
1178
1179 /*
1180 * Characters to ignore
1181 */
1182 port->ignore_status_mask = 0;
1183 if (termios->c_iflag & IGNPAR)
1184 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1185 if (termios->c_iflag & IGNBRK) {
1186 port->ignore_status_mask |= ATMEL_US_RXBRK;
1187 /*
1188 * If we're ignoring parity and break indicators,
1189 * ignore overruns too (for real raw support).
1190 */
1191 if (termios->c_iflag & IGNPAR)
1192 port->ignore_status_mask |= ATMEL_US_OVRE;
1193 }
1194 /* TODO: Ignore all characters if CREAD is set.*/
1195
1196 /* update the per-port timeout */
1197 uart_update_timeout(port, termios->c_cflag, baud);
1198
1199 /*
1200 * save/disable interrupts. The tty layer will ensure that the
1201 * transmitter is empty if requested by the caller, so there's
1202 * no need to wait for it here.
1203 */
1204 imr = UART_GET_IMR(port);
1205 UART_PUT_IDR(port, -1);
1206
1207 /* disable receiver and transmitter */
1208 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
1209
1210 /* Resetting serial mode to RS232 (0x0) */
1211 mode &= ~ATMEL_US_USMODE;
1212
1213 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
1214 dev_dbg(port->dev, "Setting UART to RS485\n");
1215 if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
1216 UART_PUT_TTGR(port,
1217 atmel_port->rs485.delay_rts_after_send);
1218 mode |= ATMEL_US_USMODE_RS485;
1219 } else {
1220 dev_dbg(port->dev, "Setting UART to RS232\n");
1221 }
1222
1223 /* set the parity, stop bits and data size */
1224 UART_PUT_MR(port, mode);
1225
1226 /* set the baud rate */
1227 UART_PUT_BRGR(port, quot);
1228 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1229 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1230
1231 /* restore interrupts */
1232 UART_PUT_IER(port, imr);
1233
1234 /* CTS flow-control and modem-status interrupts */
1235 if (UART_ENABLE_MS(port, termios->c_cflag))
1236 port->ops->enable_ms(port);
1237
1238 spin_unlock_irqrestore(&port->lock, flags);
1239}
1240
1241static void atmel_set_ldisc(struct uart_port *port, int new)
1242{
1243 int line = port->line;
1244
1245 if (line >= port->state->port.tty->driver->num)
1246 return;
1247
1248 if (port->state->port.tty->ldisc->ops->num == N_PPS) {
1249 port->flags |= UPF_HARDPPS_CD;
1250 atmel_enable_ms(port);
1251 } else {
1252 port->flags &= ~UPF_HARDPPS_CD;
1253 }
1254}
1255
1256/*
1257 * Return string describing the specified port
1258 */
1259static const char *atmel_type(struct uart_port *port)
1260{
1261 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
1262}
1263
1264/*
1265 * Release the memory region(s) being used by 'port'.
1266 */
1267static void atmel_release_port(struct uart_port *port)
1268{
1269 struct platform_device *pdev = to_platform_device(port->dev);
1270 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1271
1272 release_mem_region(port->mapbase, size);
1273
1274 if (port->flags & UPF_IOREMAP) {
1275 iounmap(port->membase);
1276 port->membase = NULL;
1277 }
1278}
1279
1280/*
1281 * Request the memory region(s) being used by 'port'.
1282 */
1283static int atmel_request_port(struct uart_port *port)
1284{
1285 struct platform_device *pdev = to_platform_device(port->dev);
1286 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1287
1288 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
1289 return -EBUSY;
1290
1291 if (port->flags & UPF_IOREMAP) {
1292 port->membase = ioremap(port->mapbase, size);
1293 if (port->membase == NULL) {
1294 release_mem_region(port->mapbase, size);
1295 return -ENOMEM;
1296 }
1297 }
1298
1299 return 0;
1300}
1301
1302/*
1303 * Configure/autoconfigure the port.
1304 */
1305static void atmel_config_port(struct uart_port *port, int flags)
1306{
1307 if (flags & UART_CONFIG_TYPE) {
1308 port->type = PORT_ATMEL;
1309 atmel_request_port(port);
1310 }
1311}
1312
1313/*
1314 * Verify the new serial_struct (for TIOCSSERIAL).
1315 */
1316static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
1317{
1318 int ret = 0;
1319 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
1320 ret = -EINVAL;
1321 if (port->irq != ser->irq)
1322 ret = -EINVAL;
1323 if (ser->io_type != SERIAL_IO_MEM)
1324 ret = -EINVAL;
1325 if (port->uartclk / 16 != ser->baud_base)
1326 ret = -EINVAL;
1327 if ((void *)port->mapbase != ser->iomem_base)
1328 ret = -EINVAL;
1329 if (port->iobase != ser->port)
1330 ret = -EINVAL;
1331 if (ser->hub6 != 0)
1332 ret = -EINVAL;
1333 return ret;
1334}
1335
1336#ifdef CONFIG_CONSOLE_POLL
1337static int atmel_poll_get_char(struct uart_port *port)
1338{
1339 while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
1340 cpu_relax();
1341
1342 return UART_GET_CHAR(port);
1343}
1344
1345static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
1346{
1347 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1348 cpu_relax();
1349
1350 UART_PUT_CHAR(port, ch);
1351}
1352#endif
1353
1354static int
1355atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
1356{
1357 struct serial_rs485 rs485conf;
1358
1359 switch (cmd) {
1360 case TIOCSRS485:
1361 if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
1362 sizeof(rs485conf)))
1363 return -EFAULT;
1364
1365 atmel_config_rs485(port, &rs485conf);
1366 break;
1367
1368 case TIOCGRS485:
1369 if (copy_to_user((struct serial_rs485 *) arg,
1370 &(to_atmel_uart_port(port)->rs485),
1371 sizeof(rs485conf)))
1372 return -EFAULT;
1373 break;
1374
1375 default:
1376 return -ENOIOCTLCMD;
1377 }
1378 return 0;
1379}
1380
1381
1382
1383static struct uart_ops atmel_pops = {
1384 .tx_empty = atmel_tx_empty,
1385 .set_mctrl = atmel_set_mctrl,
1386 .get_mctrl = atmel_get_mctrl,
1387 .stop_tx = atmel_stop_tx,
1388 .start_tx = atmel_start_tx,
1389 .stop_rx = atmel_stop_rx,
1390 .enable_ms = atmel_enable_ms,
1391 .break_ctl = atmel_break_ctl,
1392 .startup = atmel_startup,
1393 .shutdown = atmel_shutdown,
1394 .flush_buffer = atmel_flush_buffer,
1395 .set_termios = atmel_set_termios,
1396 .set_ldisc = atmel_set_ldisc,
1397 .type = atmel_type,
1398 .release_port = atmel_release_port,
1399 .request_port = atmel_request_port,
1400 .config_port = atmel_config_port,
1401 .verify_port = atmel_verify_port,
1402 .pm = atmel_serial_pm,
1403 .ioctl = atmel_ioctl,
1404#ifdef CONFIG_CONSOLE_POLL
1405 .poll_get_char = atmel_poll_get_char,
1406 .poll_put_char = atmel_poll_put_char,
1407#endif
1408};
1409
1410/*
1411 * Configure the port from the platform device resource info.
1412 */
1413static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
1414 struct platform_device *pdev)
1415{
1416 struct uart_port *port = &atmel_port->uart;
1417 struct atmel_uart_data *data = pdev->dev.platform_data;
1418
1419 port->iotype = UPIO_MEM;
1420 port->flags = UPF_BOOT_AUTOCONF;
1421 port->ops = &atmel_pops;
1422 port->fifosize = 1;
1423 port->line = data->num;
1424 port->dev = &pdev->dev;
1425 port->mapbase = pdev->resource[0].start;
1426 port->irq = pdev->resource[1].start;
1427
1428 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
1429 (unsigned long)port);
1430
1431 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
1432
1433 if (data->regs)
1434 /* Already mapped by setup code */
1435 port->membase = data->regs;
1436 else {
1437 port->flags |= UPF_IOREMAP;
1438 port->membase = NULL;
1439 }
1440
1441 /* for console, the clock could already be configured */
1442 if (!atmel_port->clk) {
1443 atmel_port->clk = clk_get(&pdev->dev, "usart");
1444 clk_enable(atmel_port->clk);
1445 port->uartclk = clk_get_rate(atmel_port->clk);
1446 clk_disable(atmel_port->clk);
1447 /* only enable clock when USART is in use */
1448 }
1449
1450 atmel_port->use_dma_rx = data->use_dma_rx;
1451 atmel_port->use_dma_tx = data->use_dma_tx;
1452 atmel_port->rs485 = data->rs485;
1453 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
1454 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
1455 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
1456 else if (atmel_use_dma_tx(port)) {
1457 port->fifosize = PDC_BUFFER_SIZE;
1458 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
1459 } else {
1460 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
1461 }
1462}
1463
1464/*
1465 * Register board-specific modem-control line handlers.
1466 */
1467void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
1468{
1469 if (fns->enable_ms)
1470 atmel_pops.enable_ms = fns->enable_ms;
1471 if (fns->get_mctrl)
1472 atmel_pops.get_mctrl = fns->get_mctrl;
1473 if (fns->set_mctrl)
1474 atmel_pops.set_mctrl = fns->set_mctrl;
1475 atmel_open_hook = fns->open;
1476 atmel_close_hook = fns->close;
1477 atmel_pops.pm = fns->pm;
1478 atmel_pops.set_wake = fns->set_wake;
1479}
1480
1481#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1482static void atmel_console_putchar(struct uart_port *port, int ch)
1483{
1484 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1485 cpu_relax();
1486 UART_PUT_CHAR(port, ch);
1487}
1488
1489/*
1490 * Interrupts are disabled on entering
1491 */
1492static void atmel_console_write(struct console *co, const char *s, u_int count)
1493{
1494 struct uart_port *port = &atmel_ports[co->index].uart;
1495 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1496 unsigned int status, imr;
1497 unsigned int pdc_tx;
1498
1499 /*
1500 * First, save IMR and then disable interrupts
1501 */
1502 imr = UART_GET_IMR(port);
1503 UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
1504
1505 /* Store PDC transmit status and disable it */
1506 pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
1507 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1508
1509 uart_console_write(port, s, count, atmel_console_putchar);
1510
1511 /*
1512 * Finally, wait for transmitter to become empty
1513 * and restore IMR
1514 */
1515 do {
1516 status = UART_GET_CSR(port);
1517 } while (!(status & ATMEL_US_TXRDY));
1518
1519 /* Restore PDC transmit status */
1520 if (pdc_tx)
1521 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1522
1523 /* set interrupts back the way they were */
1524 UART_PUT_IER(port, imr);
1525}
1526
1527/*
1528 * If the port was already initialised (eg, by a boot loader),
1529 * try to determine the current setup.
1530 */
1531static void __init atmel_console_get_options(struct uart_port *port, int *baud,
1532 int *parity, int *bits)
1533{
1534 unsigned int mr, quot;
1535
1536 /*
1537 * If the baud rate generator isn't running, the port wasn't
1538 * initialized by the boot loader.
1539 */
1540 quot = UART_GET_BRGR(port) & ATMEL_US_CD;
1541 if (!quot)
1542 return;
1543
1544 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
1545 if (mr == ATMEL_US_CHRL_8)
1546 *bits = 8;
1547 else
1548 *bits = 7;
1549
1550 mr = UART_GET_MR(port) & ATMEL_US_PAR;
1551 if (mr == ATMEL_US_PAR_EVEN)
1552 *parity = 'e';
1553 else if (mr == ATMEL_US_PAR_ODD)
1554 *parity = 'o';
1555
1556 /*
1557 * The serial core only rounds down when matching this to a
1558 * supported baud rate. Make sure we don't end up slightly
1559 * lower than one of those, as it would make us fall through
1560 * to a much lower baud rate than we really want.
1561 */
1562 *baud = port->uartclk / (16 * (quot - 1));
1563}
1564
1565static int __init atmel_console_setup(struct console *co, char *options)
1566{
1567 struct uart_port *port = &atmel_ports[co->index].uart;
1568 int baud = 115200;
1569 int bits = 8;
1570 int parity = 'n';
1571 int flow = 'n';
1572
1573 if (port->membase == NULL) {
1574 /* Port not initialized yet - delay setup */
1575 return -ENODEV;
1576 }
1577
1578 clk_enable(atmel_ports[co->index].clk);
1579
1580 UART_PUT_IDR(port, -1);
1581 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1582 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1583
1584 if (options)
1585 uart_parse_options(options, &baud, &parity, &bits, &flow);
1586 else
1587 atmel_console_get_options(port, &baud, &parity, &bits);
1588
1589 return uart_set_options(port, co, baud, parity, bits, flow);
1590}
1591
1592static struct uart_driver atmel_uart;
1593
1594static struct console atmel_console = {
1595 .name = ATMEL_DEVICENAME,
1596 .write = atmel_console_write,
1597 .device = uart_console_device,
1598 .setup = atmel_console_setup,
1599 .flags = CON_PRINTBUFFER,
1600 .index = -1,
1601 .data = &atmel_uart,
1602};
1603
1604#define ATMEL_CONSOLE_DEVICE (&atmel_console)
1605
1606/*
1607 * Early console initialization (before VM subsystem initialized).
1608 */
1609static int __init atmel_console_init(void)
1610{
1611 if (atmel_default_console_device) {
1612 add_preferred_console(ATMEL_DEVICENAME,
1613 atmel_default_console_device->id, NULL);
1614 atmel_init_port(&atmel_ports[atmel_default_console_device->id],
1615 atmel_default_console_device);
1616 register_console(&atmel_console);
1617 }
1618
1619 return 0;
1620}
1621
1622console_initcall(atmel_console_init);
1623
1624/*
1625 * Late console initialization.
1626 */
1627static int __init atmel_late_console_init(void)
1628{
1629 if (atmel_default_console_device
1630 && !(atmel_console.flags & CON_ENABLED))
1631 register_console(&atmel_console);
1632
1633 return 0;
1634}
1635
1636core_initcall(atmel_late_console_init);
1637
1638static inline bool atmel_is_console_port(struct uart_port *port)
1639{
1640 return port->cons && port->cons->index == port->line;
1641}
1642
1643#else
1644#define ATMEL_CONSOLE_DEVICE NULL
1645
1646static inline bool atmel_is_console_port(struct uart_port *port)
1647{
1648 return false;
1649}
1650#endif
1651
1652static struct uart_driver atmel_uart = {
1653 .owner = THIS_MODULE,
1654 .driver_name = "atmel_serial",
1655 .dev_name = ATMEL_DEVICENAME,
1656 .major = SERIAL_ATMEL_MAJOR,
1657 .minor = MINOR_START,
1658 .nr = ATMEL_MAX_UART,
1659 .cons = ATMEL_CONSOLE_DEVICE,
1660};
1661
1662#ifdef CONFIG_PM
1663static bool atmel_serial_clk_will_stop(void)
1664{
1665#ifdef CONFIG_ARCH_AT91
1666 return at91_suspend_entering_slow_clock();
1667#else
1668 return false;
1669#endif
1670}
1671
1672static int atmel_serial_suspend(struct platform_device *pdev,
1673 pm_message_t state)
1674{
1675 struct uart_port *port = platform_get_drvdata(pdev);
1676 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1677
1678 if (atmel_is_console_port(port) && console_suspend_enabled) {
1679 /* Drain the TX shifter */
1680 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
1681 cpu_relax();
1682 }
1683
1684 /* we can not wake up if we're running on slow clock */
1685 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
1686 if (atmel_serial_clk_will_stop())
1687 device_set_wakeup_enable(&pdev->dev, 0);
1688
1689 uart_suspend_port(&atmel_uart, port);
1690
1691 return 0;
1692}
1693
1694static int atmel_serial_resume(struct platform_device *pdev)
1695{
1696 struct uart_port *port = platform_get_drvdata(pdev);
1697 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1698
1699 uart_resume_port(&atmel_uart, port);
1700 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
1701
1702 return 0;
1703}
1704#else
1705#define atmel_serial_suspend NULL
1706#define atmel_serial_resume NULL
1707#endif
1708
1709static int __devinit atmel_serial_probe(struct platform_device *pdev)
1710{
1711 struct atmel_uart_port *port;
1712 struct atmel_uart_data *pdata = pdev->dev.platform_data;
1713 void *data;
1714 int ret;
1715
1716 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
1717
1718 port = &atmel_ports[pdata->num];
1719 port->backup_imr = 0;
1720
1721 atmel_init_port(port, pdev);
1722
1723 if (!atmel_use_dma_rx(&port->uart)) {
1724 ret = -ENOMEM;
1725 data = kmalloc(sizeof(struct atmel_uart_char)
1726 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
1727 if (!data)
1728 goto err_alloc_ring;
1729 port->rx_ring.buf = data;
1730 }
1731
1732 ret = uart_add_one_port(&atmel_uart, &port->uart);
1733 if (ret)
1734 goto err_add_port;
1735
1736#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1737 if (atmel_is_console_port(&port->uart)
1738 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
1739 /*
1740 * The serial core enabled the clock for us, so undo
1741 * the clk_enable() in atmel_console_setup()
1742 */
1743 clk_disable(port->clk);
1744 }
1745#endif
1746
1747 device_init_wakeup(&pdev->dev, 1);
1748 platform_set_drvdata(pdev, port);
1749
1750 if (port->rs485.flags & SER_RS485_ENABLED) {
1751 UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
1752 UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
1753 }
1754
1755 return 0;
1756
1757err_add_port:
1758 kfree(port->rx_ring.buf);
1759 port->rx_ring.buf = NULL;
1760err_alloc_ring:
1761 if (!atmel_is_console_port(&port->uart)) {
1762 clk_put(port->clk);
1763 port->clk = NULL;
1764 }
1765
1766 return ret;
1767}
1768
1769static int __devexit atmel_serial_remove(struct platform_device *pdev)
1770{
1771 struct uart_port *port = platform_get_drvdata(pdev);
1772 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1773 int ret = 0;
1774
1775 device_init_wakeup(&pdev->dev, 0);
1776 platform_set_drvdata(pdev, NULL);
1777
1778 ret = uart_remove_one_port(&atmel_uart, port);
1779
1780 tasklet_kill(&atmel_port->tasklet);
1781 kfree(atmel_port->rx_ring.buf);
1782
1783 /* "port" is allocated statically, so we shouldn't free it */
1784
1785 clk_put(atmel_port->clk);
1786
1787 return ret;
1788}
1789
1790static struct platform_driver atmel_serial_driver = {
1791 .probe = atmel_serial_probe,
1792 .remove = __devexit_p(atmel_serial_remove),
1793 .suspend = atmel_serial_suspend,
1794 .resume = atmel_serial_resume,
1795 .driver = {
1796 .name = "atmel_usart",
1797 .owner = THIS_MODULE,
1798 },
1799};
1800
1801static int __init atmel_serial_init(void)
1802{
1803 int ret;
1804
1805 ret = uart_register_driver(&atmel_uart);
1806 if (ret)
1807 return ret;
1808
1809 ret = platform_driver_register(&atmel_serial_driver);
1810 if (ret)
1811 uart_unregister_driver(&atmel_uart);
1812
1813 return ret;
1814}
1815
1816static void __exit atmel_serial_exit(void)
1817{
1818 platform_driver_unregister(&atmel_serial_driver);
1819 uart_unregister_driver(&atmel_uart);
1820}
1821
1822module_init(atmel_serial_init);
1823module_exit(atmel_serial_exit);
1824
1825MODULE_AUTHOR("Rick Bronson");
1826MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1827MODULE_LICENSE("GPL");
1828MODULE_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..c0b68b9cad91
--- /dev/null
+++ b/drivers/tty/serial/bcm63xx_uart.c
@@ -0,0 +1,901 @@
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
254 if (unlikely(iestat & UART_IR_STAT(UART_IR_RXOVER))) {
255 unsigned int val;
256
257 /* fifo reset is required to clear
258 * interrupt */
259 val = bcm_uart_readl(port, UART_CTL_REG);
260 val |= UART_CTL_RSTRXFIFO_MASK;
261 bcm_uart_writel(port, val, UART_CTL_REG);
262
263 port->icount.overrun++;
264 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
265 }
266
267 if (!(iestat & UART_IR_STAT(UART_IR_RXNOTEMPTY)))
268 break;
269
270 cstat = c = bcm_uart_readl(port, UART_FIFO_REG);
271 port->icount.rx++;
272 flag = TTY_NORMAL;
273 c &= 0xff;
274
275 if (unlikely((cstat & UART_FIFO_ANYERR_MASK))) {
276 /* do stats first */
277 if (cstat & UART_FIFO_BRKDET_MASK) {
278 port->icount.brk++;
279 if (uart_handle_break(port))
280 continue;
281 }
282
283 if (cstat & UART_FIFO_PARERR_MASK)
284 port->icount.parity++;
285 if (cstat & UART_FIFO_FRAMEERR_MASK)
286 port->icount.frame++;
287
288 /* update flag wrt read_status_mask */
289 cstat &= port->read_status_mask;
290 if (cstat & UART_FIFO_BRKDET_MASK)
291 flag = TTY_BREAK;
292 if (cstat & UART_FIFO_FRAMEERR_MASK)
293 flag = TTY_FRAME;
294 if (cstat & UART_FIFO_PARERR_MASK)
295 flag = TTY_PARITY;
296 }
297
298 if (uart_handle_sysrq_char(port, c))
299 continue;
300
301
302 if ((cstat & port->ignore_status_mask) == 0)
303 tty_insert_flip_char(tty, c, flag);
304
305 } while (--max_count);
306
307 tty_flip_buffer_push(tty);
308}
309
310/*
311 * fill tx fifo with chars to send, stop when fifo is about to be full
312 * or when all chars have been sent.
313 */
314static void bcm_uart_do_tx(struct uart_port *port)
315{
316 struct circ_buf *xmit;
317 unsigned int val, max_count;
318
319 if (port->x_char) {
320 bcm_uart_writel(port, port->x_char, UART_FIFO_REG);
321 port->icount.tx++;
322 port->x_char = 0;
323 return;
324 }
325
326 if (uart_tx_stopped(port)) {
327 bcm_uart_stop_tx(port);
328 return;
329 }
330
331 xmit = &port->state->xmit;
332 if (uart_circ_empty(xmit))
333 goto txq_empty;
334
335 val = bcm_uart_readl(port, UART_MCTL_REG);
336 val = (val & UART_MCTL_TXFIFOFILL_MASK) >> UART_MCTL_TXFIFOFILL_SHIFT;
337 max_count = port->fifosize - val;
338
339 while (max_count--) {
340 unsigned int c;
341
342 c = xmit->buf[xmit->tail];
343 bcm_uart_writel(port, c, UART_FIFO_REG);
344 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
345 port->icount.tx++;
346 if (uart_circ_empty(xmit))
347 break;
348 }
349
350 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
351 uart_write_wakeup(port);
352
353 if (uart_circ_empty(xmit))
354 goto txq_empty;
355 return;
356
357txq_empty:
358 /* nothing to send, disable transmit interrupt */
359 val = bcm_uart_readl(port, UART_IR_REG);
360 val &= ~UART_TX_INT_MASK;
361 bcm_uart_writel(port, val, UART_IR_REG);
362 return;
363}
364
365/*
366 * process uart interrupt
367 */
368static irqreturn_t bcm_uart_interrupt(int irq, void *dev_id)
369{
370 struct uart_port *port;
371 unsigned int irqstat;
372
373 port = dev_id;
374 spin_lock(&port->lock);
375
376 irqstat = bcm_uart_readl(port, UART_IR_REG);
377 if (irqstat & UART_RX_INT_STAT)
378 bcm_uart_do_rx(port);
379
380 if (irqstat & UART_TX_INT_STAT)
381 bcm_uart_do_tx(port);
382
383 if (irqstat & UART_IR_MASK(UART_IR_EXTIP)) {
384 unsigned int estat;
385
386 estat = bcm_uart_readl(port, UART_EXTINP_REG);
387 if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_CTS))
388 uart_handle_cts_change(port,
389 estat & UART_EXTINP_CTS_MASK);
390 if (estat & UART_EXTINP_IRSTAT(UART_EXTINP_IR_DCD))
391 uart_handle_dcd_change(port,
392 estat & UART_EXTINP_DCD_MASK);
393 }
394
395 spin_unlock(&port->lock);
396 return IRQ_HANDLED;
397}
398
399/*
400 * enable rx & tx operation on uart
401 */
402static void bcm_uart_enable(struct uart_port *port)
403{
404 unsigned int val;
405
406 val = bcm_uart_readl(port, UART_CTL_REG);
407 val |= (UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK | UART_CTL_RXEN_MASK);
408 bcm_uart_writel(port, val, UART_CTL_REG);
409}
410
411/*
412 * disable rx & tx operation on uart
413 */
414static void bcm_uart_disable(struct uart_port *port)
415{
416 unsigned int val;
417
418 val = bcm_uart_readl(port, UART_CTL_REG);
419 val &= ~(UART_CTL_BRGEN_MASK | UART_CTL_TXEN_MASK |
420 UART_CTL_RXEN_MASK);
421 bcm_uart_writel(port, val, UART_CTL_REG);
422}
423
424/*
425 * clear all unread data in rx fifo and unsent data in tx fifo
426 */
427static void bcm_uart_flush(struct uart_port *port)
428{
429 unsigned int val;
430
431 /* empty rx and tx fifo */
432 val = bcm_uart_readl(port, UART_CTL_REG);
433 val |= UART_CTL_RSTRXFIFO_MASK | UART_CTL_RSTTXFIFO_MASK;
434 bcm_uart_writel(port, val, UART_CTL_REG);
435
436 /* read any pending char to make sure all irq status are
437 * cleared */
438 (void)bcm_uart_readl(port, UART_FIFO_REG);
439}
440
441/*
442 * serial core request to initialize uart and start rx operation
443 */
444static int bcm_uart_startup(struct uart_port *port)
445{
446 unsigned int val;
447 int ret;
448
449 /* mask all irq and flush port */
450 bcm_uart_disable(port);
451 bcm_uart_writel(port, 0, UART_IR_REG);
452 bcm_uart_flush(port);
453
454 /* clear any pending external input interrupt */
455 (void)bcm_uart_readl(port, UART_EXTINP_REG);
456
457 /* set rx/tx fifo thresh to fifo half size */
458 val = bcm_uart_readl(port, UART_MCTL_REG);
459 val &= ~(UART_MCTL_RXFIFOTHRESH_MASK | UART_MCTL_TXFIFOTHRESH_MASK);
460 val |= (port->fifosize / 2) << UART_MCTL_RXFIFOTHRESH_SHIFT;
461 val |= (port->fifosize / 2) << UART_MCTL_TXFIFOTHRESH_SHIFT;
462 bcm_uart_writel(port, val, UART_MCTL_REG);
463
464 /* set rx fifo timeout to 1 char time */
465 val = bcm_uart_readl(port, UART_CTL_REG);
466 val &= ~UART_CTL_RXTMOUTCNT_MASK;
467 val |= 1 << UART_CTL_RXTMOUTCNT_SHIFT;
468 bcm_uart_writel(port, val, UART_CTL_REG);
469
470 /* report any edge on dcd and cts */
471 val = UART_EXTINP_INT_MASK;
472 val |= UART_EXTINP_DCD_NOSENSE_MASK;
473 val |= UART_EXTINP_CTS_NOSENSE_MASK;
474 bcm_uart_writel(port, val, UART_EXTINP_REG);
475
476 /* register irq and enable rx interrupts */
477 ret = request_irq(port->irq, bcm_uart_interrupt, 0,
478 bcm_uart_type(port), port);
479 if (ret)
480 return ret;
481 bcm_uart_writel(port, UART_RX_INT_MASK, UART_IR_REG);
482 bcm_uart_enable(port);
483 return 0;
484}
485
486/*
487 * serial core request to flush & disable uart
488 */
489static void bcm_uart_shutdown(struct uart_port *port)
490{
491 unsigned long flags;
492
493 spin_lock_irqsave(&port->lock, flags);
494 bcm_uart_writel(port, 0, UART_IR_REG);
495 spin_unlock_irqrestore(&port->lock, flags);
496
497 bcm_uart_disable(port);
498 bcm_uart_flush(port);
499 free_irq(port->irq, port);
500}
501
502/*
503 * serial core request to change current uart setting
504 */
505static void bcm_uart_set_termios(struct uart_port *port,
506 struct ktermios *new,
507 struct ktermios *old)
508{
509 unsigned int ctl, baud, quot, ier;
510 unsigned long flags;
511
512 spin_lock_irqsave(&port->lock, flags);
513
514 /* disable uart while changing speed */
515 bcm_uart_disable(port);
516 bcm_uart_flush(port);
517
518 /* update Control register */
519 ctl = bcm_uart_readl(port, UART_CTL_REG);
520 ctl &= ~UART_CTL_BITSPERSYM_MASK;
521
522 switch (new->c_cflag & CSIZE) {
523 case CS5:
524 ctl |= (0 << UART_CTL_BITSPERSYM_SHIFT);
525 break;
526 case CS6:
527 ctl |= (1 << UART_CTL_BITSPERSYM_SHIFT);
528 break;
529 case CS7:
530 ctl |= (2 << UART_CTL_BITSPERSYM_SHIFT);
531 break;
532 default:
533 ctl |= (3 << UART_CTL_BITSPERSYM_SHIFT);
534 break;
535 }
536
537 ctl &= ~UART_CTL_STOPBITS_MASK;
538 if (new->c_cflag & CSTOPB)
539 ctl |= UART_CTL_STOPBITS_2;
540 else
541 ctl |= UART_CTL_STOPBITS_1;
542
543 ctl &= ~(UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
544 if (new->c_cflag & PARENB)
545 ctl |= (UART_CTL_RXPAREN_MASK | UART_CTL_TXPAREN_MASK);
546 ctl &= ~(UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
547 if (new->c_cflag & PARODD)
548 ctl |= (UART_CTL_RXPAREVEN_MASK | UART_CTL_TXPAREVEN_MASK);
549 bcm_uart_writel(port, ctl, UART_CTL_REG);
550
551 /* update Baudword register */
552 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
553 quot = uart_get_divisor(port, baud) - 1;
554 bcm_uart_writel(port, quot, UART_BAUD_REG);
555
556 /* update Interrupt register */
557 ier = bcm_uart_readl(port, UART_IR_REG);
558
559 ier &= ~UART_IR_MASK(UART_IR_EXTIP);
560 if (UART_ENABLE_MS(port, new->c_cflag))
561 ier |= UART_IR_MASK(UART_IR_EXTIP);
562
563 bcm_uart_writel(port, ier, UART_IR_REG);
564
565 /* update read/ignore mask */
566 port->read_status_mask = UART_FIFO_VALID_MASK;
567 if (new->c_iflag & INPCK) {
568 port->read_status_mask |= UART_FIFO_FRAMEERR_MASK;
569 port->read_status_mask |= UART_FIFO_PARERR_MASK;
570 }
571 if (new->c_iflag & (BRKINT))
572 port->read_status_mask |= UART_FIFO_BRKDET_MASK;
573
574 port->ignore_status_mask = 0;
575 if (new->c_iflag & IGNPAR)
576 port->ignore_status_mask |= UART_FIFO_PARERR_MASK;
577 if (new->c_iflag & IGNBRK)
578 port->ignore_status_mask |= UART_FIFO_BRKDET_MASK;
579 if (!(new->c_cflag & CREAD))
580 port->ignore_status_mask |= UART_FIFO_VALID_MASK;
581
582 uart_update_timeout(port, new->c_cflag, baud);
583 bcm_uart_enable(port);
584 spin_unlock_irqrestore(&port->lock, flags);
585}
586
587/*
588 * serial core request to claim uart iomem
589 */
590static int bcm_uart_request_port(struct uart_port *port)
591{
592 unsigned int size;
593
594 size = RSET_UART_SIZE;
595 if (!request_mem_region(port->mapbase, size, "bcm63xx")) {
596 dev_err(port->dev, "Memory region busy\n");
597 return -EBUSY;
598 }
599
600 port->membase = ioremap(port->mapbase, size);
601 if (!port->membase) {
602 dev_err(port->dev, "Unable to map registers\n");
603 release_mem_region(port->mapbase, size);
604 return -EBUSY;
605 }
606 return 0;
607}
608
609/*
610 * serial core request to release uart iomem
611 */
612static void bcm_uart_release_port(struct uart_port *port)
613{
614 release_mem_region(port->mapbase, RSET_UART_SIZE);
615 iounmap(port->membase);
616}
617
618/*
619 * serial core request to do any port required autoconfiguration
620 */
621static void bcm_uart_config_port(struct uart_port *port, int flags)
622{
623 if (flags & UART_CONFIG_TYPE) {
624 if (bcm_uart_request_port(port))
625 return;
626 port->type = PORT_BCM63XX;
627 }
628}
629
630/*
631 * serial core request to check that port information in serinfo are
632 * suitable
633 */
634static int bcm_uart_verify_port(struct uart_port *port,
635 struct serial_struct *serinfo)
636{
637 if (port->type != PORT_BCM63XX)
638 return -EINVAL;
639 if (port->irq != serinfo->irq)
640 return -EINVAL;
641 if (port->iotype != serinfo->io_type)
642 return -EINVAL;
643 if (port->mapbase != (unsigned long)serinfo->iomem_base)
644 return -EINVAL;
645 return 0;
646}
647
648/* serial core callbacks */
649static struct uart_ops bcm_uart_ops = {
650 .tx_empty = bcm_uart_tx_empty,
651 .get_mctrl = bcm_uart_get_mctrl,
652 .set_mctrl = bcm_uart_set_mctrl,
653 .start_tx = bcm_uart_start_tx,
654 .stop_tx = bcm_uart_stop_tx,
655 .stop_rx = bcm_uart_stop_rx,
656 .enable_ms = bcm_uart_enable_ms,
657 .break_ctl = bcm_uart_break_ctl,
658 .startup = bcm_uart_startup,
659 .shutdown = bcm_uart_shutdown,
660 .set_termios = bcm_uart_set_termios,
661 .type = bcm_uart_type,
662 .release_port = bcm_uart_release_port,
663 .request_port = bcm_uart_request_port,
664 .config_port = bcm_uart_config_port,
665 .verify_port = bcm_uart_verify_port,
666};
667
668
669
670#ifdef CONFIG_SERIAL_BCM63XX_CONSOLE
671static inline void wait_for_xmitr(struct uart_port *port)
672{
673 unsigned int tmout;
674
675 /* Wait up to 10ms for the character(s) to be sent. */
676 tmout = 10000;
677 while (--tmout) {
678 unsigned int val;
679
680 val = bcm_uart_readl(port, UART_IR_REG);
681 if (val & UART_IR_STAT(UART_IR_TXEMPTY))
682 break;
683 udelay(1);
684 }
685
686 /* Wait up to 1s for flow control if necessary */
687 if (port->flags & UPF_CONS_FLOW) {
688 tmout = 1000000;
689 while (--tmout) {
690 unsigned int val;
691
692 val = bcm_uart_readl(port, UART_EXTINP_REG);
693 if (val & UART_EXTINP_CTS_MASK)
694 break;
695 udelay(1);
696 }
697 }
698}
699
700/*
701 * output given char
702 */
703static void bcm_console_putchar(struct uart_port *port, int ch)
704{
705 wait_for_xmitr(port);
706 bcm_uart_writel(port, ch, UART_FIFO_REG);
707}
708
709/*
710 * console core request to output given string
711 */
712static void bcm_console_write(struct console *co, const char *s,
713 unsigned int count)
714{
715 struct uart_port *port;
716 unsigned long flags;
717 int locked;
718
719 port = &ports[co->index];
720
721 local_irq_save(flags);
722 if (port->sysrq) {
723 /* bcm_uart_interrupt() already took the lock */
724 locked = 0;
725 } else if (oops_in_progress) {
726 locked = spin_trylock(&port->lock);
727 } else {
728 spin_lock(&port->lock);
729 locked = 1;
730 }
731
732 /* call helper to deal with \r\n */
733 uart_console_write(port, s, count, bcm_console_putchar);
734
735 /* and wait for char to be transmitted */
736 wait_for_xmitr(port);
737
738 if (locked)
739 spin_unlock(&port->lock);
740 local_irq_restore(flags);
741}
742
743/*
744 * console core request to setup given console, find matching uart
745 * port and setup it.
746 */
747static int bcm_console_setup(struct console *co, char *options)
748{
749 struct uart_port *port;
750 int baud = 9600;
751 int bits = 8;
752 int parity = 'n';
753 int flow = 'n';
754
755 if (co->index < 0 || co->index >= BCM63XX_NR_UARTS)
756 return -EINVAL;
757 port = &ports[co->index];
758 if (!port->membase)
759 return -ENODEV;
760 if (options)
761 uart_parse_options(options, &baud, &parity, &bits, &flow);
762
763 return uart_set_options(port, co, baud, parity, bits, flow);
764}
765
766static struct uart_driver bcm_uart_driver;
767
768static struct console bcm63xx_console = {
769 .name = "ttyS",
770 .write = bcm_console_write,
771 .device = uart_console_device,
772 .setup = bcm_console_setup,
773 .flags = CON_PRINTBUFFER,
774 .index = -1,
775 .data = &bcm_uart_driver,
776};
777
778static int __init bcm63xx_console_init(void)
779{
780 register_console(&bcm63xx_console);
781 return 0;
782}
783
784console_initcall(bcm63xx_console_init);
785
786#define BCM63XX_CONSOLE (&bcm63xx_console)
787#else
788#define BCM63XX_CONSOLE NULL
789#endif /* CONFIG_SERIAL_BCM63XX_CONSOLE */
790
791static struct uart_driver bcm_uart_driver = {
792 .owner = THIS_MODULE,
793 .driver_name = "bcm63xx_uart",
794 .dev_name = "ttyS",
795 .major = TTY_MAJOR,
796 .minor = 64,
797 .nr = BCM63XX_NR_UARTS,
798 .cons = BCM63XX_CONSOLE,
799};
800
801/*
802 * platform driver probe/remove callback
803 */
804static int __devinit bcm_uart_probe(struct platform_device *pdev)
805{
806 struct resource *res_mem, *res_irq;
807 struct uart_port *port;
808 struct clk *clk;
809 int ret;
810
811 if (pdev->id < 0 || pdev->id >= BCM63XX_NR_UARTS)
812 return -EINVAL;
813
814 if (ports[pdev->id].membase)
815 return -EBUSY;
816
817 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
818 if (!res_mem)
819 return -ENODEV;
820
821 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
822 if (!res_irq)
823 return -ENODEV;
824
825 clk = clk_get(&pdev->dev, "periph");
826 if (IS_ERR(clk))
827 return -ENODEV;
828
829 port = &ports[pdev->id];
830 memset(port, 0, sizeof(*port));
831 port->iotype = UPIO_MEM;
832 port->mapbase = res_mem->start;
833 port->irq = res_irq->start;
834 port->ops = &bcm_uart_ops;
835 port->flags = UPF_BOOT_AUTOCONF;
836 port->dev = &pdev->dev;
837 port->fifosize = 16;
838 port->uartclk = clk_get_rate(clk) / 2;
839 port->line = pdev->id;
840 clk_put(clk);
841
842 ret = uart_add_one_port(&bcm_uart_driver, port);
843 if (ret) {
844 ports[pdev->id].membase = 0;
845 return ret;
846 }
847 platform_set_drvdata(pdev, port);
848 return 0;
849}
850
851static int __devexit bcm_uart_remove(struct platform_device *pdev)
852{
853 struct uart_port *port;
854
855 port = platform_get_drvdata(pdev);
856 uart_remove_one_port(&bcm_uart_driver, port);
857 platform_set_drvdata(pdev, NULL);
858 /* mark port as free */
859 ports[pdev->id].membase = 0;
860 return 0;
861}
862
863/*
864 * platform driver stuff
865 */
866static struct platform_driver bcm_uart_platform_driver = {
867 .probe = bcm_uart_probe,
868 .remove = __devexit_p(bcm_uart_remove),
869 .driver = {
870 .owner = THIS_MODULE,
871 .name = "bcm63xx_uart",
872 },
873};
874
875static int __init bcm_uart_init(void)
876{
877 int ret;
878
879 ret = uart_register_driver(&bcm_uart_driver);
880 if (ret)
881 return ret;
882
883 ret = platform_driver_register(&bcm_uart_platform_driver);
884 if (ret)
885 uart_unregister_driver(&bcm_uart_driver);
886
887 return ret;
888}
889
890static void __exit bcm_uart_exit(void)
891{
892 platform_driver_unregister(&bcm_uart_platform_driver);
893 uart_unregister_driver(&bcm_uart_driver);
894}
895
896module_init(bcm_uart_init);
897module_exit(bcm_uart_exit);
898
899MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
900MODULE_DESCRIPTION("Broadcom 63<xx integrated uart driver");
901MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/bfin_5xx.c b/drivers/tty/serial/bfin_5xx.c
new file mode 100644
index 000000000000..9b1ff2b6bb37
--- /dev/null
+++ b/drivers/tty/serial/bfin_5xx.c
@@ -0,0 +1,1597 @@
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 while (UART_GET_LSR(uart) & DR)
374 bfin_serial_rx_chars(uart);
375
376 return IRQ_HANDLED;
377}
378
379static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
380{
381 struct bfin_serial_port *uart = dev_id;
382
383#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
384 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
385 uart->scts = 0;
386 uart_handle_cts_change(&uart->port, uart->scts);
387 }
388#endif
389 spin_lock(&uart->port.lock);
390 if (UART_GET_LSR(uart) & THRE)
391 bfin_serial_tx_chars(uart);
392 spin_unlock(&uart->port.lock);
393
394 return IRQ_HANDLED;
395}
396#endif
397
398#ifdef CONFIG_SERIAL_BFIN_DMA
399static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
400{
401 struct circ_buf *xmit = &uart->port.state->xmit;
402
403 uart->tx_done = 0;
404
405 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
406 uart->tx_count = 0;
407 uart->tx_done = 1;
408 return;
409 }
410
411 if (uart->port.x_char) {
412 UART_PUT_CHAR(uart, uart->port.x_char);
413 uart->port.icount.tx++;
414 uart->port.x_char = 0;
415 }
416
417 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
418 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
419 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
420 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
421 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
422 set_dma_config(uart->tx_dma_channel,
423 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
424 INTR_ON_BUF,
425 DIMENSION_LINEAR,
426 DATA_SIZE_8,
427 DMA_SYNC_RESTART));
428 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
429 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
430 set_dma_x_modify(uart->tx_dma_channel, 1);
431 SSYNC();
432 enable_dma(uart->tx_dma_channel);
433
434 UART_SET_IER(uart, ETBEI);
435}
436
437static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
438{
439 struct tty_struct *tty = uart->port.state->port.tty;
440 int i, flg, status;
441
442 status = UART_GET_LSR(uart);
443 UART_CLEAR_LSR(uart);
444
445 uart->port.icount.rx +=
446 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
447 UART_XMIT_SIZE);
448
449 if (status & BI) {
450 uart->port.icount.brk++;
451 if (uart_handle_break(&uart->port))
452 goto dma_ignore_char;
453 status &= ~(PE | FE);
454 }
455 if (status & PE)
456 uart->port.icount.parity++;
457 if (status & OE)
458 uart->port.icount.overrun++;
459 if (status & FE)
460 uart->port.icount.frame++;
461
462 status &= uart->port.read_status_mask;
463
464 if (status & BI)
465 flg = TTY_BREAK;
466 else if (status & PE)
467 flg = TTY_PARITY;
468 else if (status & FE)
469 flg = TTY_FRAME;
470 else
471 flg = TTY_NORMAL;
472
473 for (i = uart->rx_dma_buf.tail; ; i++) {
474 if (i >= UART_XMIT_SIZE)
475 i = 0;
476 if (i == uart->rx_dma_buf.head)
477 break;
478 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
479 uart_insert_char(&uart->port, status, OE,
480 uart->rx_dma_buf.buf[i], flg);
481 }
482
483 dma_ignore_char:
484 tty_flip_buffer_push(tty);
485}
486
487void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
488{
489 int x_pos, pos;
490
491 dma_disable_irq_nosync(uart->rx_dma_channel);
492 spin_lock_bh(&uart->rx_lock);
493
494 /* 2D DMA RX buffer ring is used. Because curr_y_count and
495 * curr_x_count can't be read as an atomic operation,
496 * curr_y_count should be read before curr_x_count. When
497 * curr_x_count is read, curr_y_count may already indicate
498 * next buffer line. But, the position calculated here is
499 * still indicate the old line. The wrong position data may
500 * be smaller than current buffer tail, which cause garbages
501 * are received if it is not prohibit.
502 */
503 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
504 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
505 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
506 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
507 uart->rx_dma_nrows = 0;
508 x_pos = DMA_RX_XCOUNT - x_pos;
509 if (x_pos == DMA_RX_XCOUNT)
510 x_pos = 0;
511
512 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
513 /* Ignore receiving data if new position is in the same line of
514 * current buffer tail and small.
515 */
516 if (pos > uart->rx_dma_buf.tail ||
517 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
518 uart->rx_dma_buf.head = pos;
519 bfin_serial_dma_rx_chars(uart);
520 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
521 }
522
523 spin_unlock_bh(&uart->rx_lock);
524 dma_enable_irq(uart->rx_dma_channel);
525
526 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
527}
528
529static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
530{
531 struct bfin_serial_port *uart = dev_id;
532 struct circ_buf *xmit = &uart->port.state->xmit;
533
534#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
535 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port)&TIOCM_CTS)) {
536 uart->scts = 0;
537 uart_handle_cts_change(&uart->port, uart->scts);
538 }
539#endif
540
541 spin_lock(&uart->port.lock);
542 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
543 disable_dma(uart->tx_dma_channel);
544 clear_dma_irqstat(uart->tx_dma_channel);
545 /* Anomaly notes:
546 * 05000215 - we always clear ETBEI within last UART TX
547 * interrupt to end a string. It is always set
548 * when start a new tx.
549 */
550 UART_CLEAR_IER(uart, ETBEI);
551 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
552 uart->port.icount.tx += uart->tx_count;
553
554 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
555 uart_write_wakeup(&uart->port);
556
557 bfin_serial_dma_tx_chars(uart);
558 }
559
560 spin_unlock(&uart->port.lock);
561 return IRQ_HANDLED;
562}
563
564static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
565{
566 struct bfin_serial_port *uart = dev_id;
567 unsigned short irqstat;
568 int x_pos, pos;
569
570 spin_lock(&uart->rx_lock);
571 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
572 clear_dma_irqstat(uart->rx_dma_channel);
573
574 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
575 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
576 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
577 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
578 uart->rx_dma_nrows = 0;
579
580 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT;
581 if (pos > uart->rx_dma_buf.tail ||
582 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
583 uart->rx_dma_buf.head = pos;
584 bfin_serial_dma_rx_chars(uart);
585 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
586 }
587
588 spin_unlock(&uart->rx_lock);
589
590 return IRQ_HANDLED;
591}
592#endif
593
594/*
595 * Return TIOCSER_TEMT when transmitter is not busy.
596 */
597static unsigned int bfin_serial_tx_empty(struct uart_port *port)
598{
599 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
600 unsigned short lsr;
601
602 lsr = UART_GET_LSR(uart);
603 if (lsr & TEMT)
604 return TIOCSER_TEMT;
605 else
606 return 0;
607}
608
609static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
610{
611 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
612 u16 lcr = UART_GET_LCR(uart);
613 if (break_state)
614 lcr |= SB;
615 else
616 lcr &= ~SB;
617 UART_PUT_LCR(uart, lcr);
618 SSYNC();
619}
620
621static int bfin_serial_startup(struct uart_port *port)
622{
623 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
624
625#ifdef CONFIG_SERIAL_BFIN_DMA
626 dma_addr_t dma_handle;
627
628 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
629 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
630 return -EBUSY;
631 }
632
633 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
634 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
635 free_dma(uart->rx_dma_channel);
636 return -EBUSY;
637 }
638
639 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
640 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
641
642 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
643 uart->rx_dma_buf.head = 0;
644 uart->rx_dma_buf.tail = 0;
645 uart->rx_dma_nrows = 0;
646
647 set_dma_config(uart->rx_dma_channel,
648 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
649 INTR_ON_ROW, DIMENSION_2D,
650 DATA_SIZE_8,
651 DMA_SYNC_RESTART));
652 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
653 set_dma_x_modify(uart->rx_dma_channel, 1);
654 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
655 set_dma_y_modify(uart->rx_dma_channel, 1);
656 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
657 enable_dma(uart->rx_dma_channel);
658
659 uart->rx_dma_timer.data = (unsigned long)(uart);
660 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
661 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
662 add_timer(&(uart->rx_dma_timer));
663#else
664# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
665 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
666 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
667 kgdboc_break_enabled = 0;
668 else {
669# endif
670 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
671 "BFIN_UART_RX", uart)) {
672 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
673 return -EBUSY;
674 }
675
676 if (request_irq
677 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
678 "BFIN_UART_TX", uart)) {
679 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
680 free_irq(uart->port.irq, uart);
681 return -EBUSY;
682 }
683
684# ifdef CONFIG_BF54x
685 {
686 /*
687 * UART2 and UART3 on BF548 share interrupt PINs and DMA
688 * controllers with SPORT2 and SPORT3. UART rx and tx
689 * interrupts are generated in PIO mode only when configure
690 * their peripheral mapping registers properly, which means
691 * request corresponding DMA channels in PIO mode as well.
692 */
693 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
694
695 switch (uart->port.irq) {
696 case IRQ_UART3_RX:
697 uart_dma_ch_rx = CH_UART3_RX;
698 uart_dma_ch_tx = CH_UART3_TX;
699 break;
700 case IRQ_UART2_RX:
701 uart_dma_ch_rx = CH_UART2_RX;
702 uart_dma_ch_tx = CH_UART2_TX;
703 break;
704 default:
705 uart_dma_ch_rx = uart_dma_ch_tx = 0;
706 break;
707 };
708
709 if (uart_dma_ch_rx &&
710 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
711 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
712 free_irq(uart->port.irq, uart);
713 free_irq(uart->port.irq + 1, uart);
714 return -EBUSY;
715 }
716 if (uart_dma_ch_tx &&
717 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
718 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
719 free_dma(uart_dma_ch_rx);
720 free_irq(uart->port.irq, uart);
721 free_irq(uart->port.irq + 1, uart);
722 return -EBUSY;
723 }
724 }
725# endif
726# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
727 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
728 }
729# endif
730#endif
731
732#ifdef CONFIG_SERIAL_BFIN_CTSRTS
733 if (uart->cts_pin >= 0) {
734 if (request_irq(gpio_to_irq(uart->cts_pin),
735 bfin_serial_mctrl_cts_int,
736 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
737 IRQF_DISABLED, "BFIN_UART_CTS", uart)) {
738 uart->cts_pin = -1;
739 pr_info("Unable to attach BlackFin UART CTS interrupt. So, disable it.\n");
740 }
741 }
742 if (uart->rts_pin >= 0) {
743 gpio_direction_output(uart->rts_pin, 0);
744 }
745#endif
746#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
747 if (uart->cts_pin >= 0 && request_irq(uart->status_irq,
748 bfin_serial_mctrl_cts_int,
749 IRQF_DISABLED, "BFIN_UART_MODEM_STATUS", uart)) {
750 uart->cts_pin = -1;
751 pr_info("Unable to attach BlackFin UART Modem Status interrupt.\n");
752 }
753
754 /* CTS RTS PINs are negative assertive. */
755 UART_PUT_MCR(uart, ACTS);
756 UART_SET_IER(uart, EDSSI);
757#endif
758
759 UART_SET_IER(uart, ERBFI);
760 return 0;
761}
762
763static void bfin_serial_shutdown(struct uart_port *port)
764{
765 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
766
767#ifdef CONFIG_SERIAL_BFIN_DMA
768 disable_dma(uart->tx_dma_channel);
769 free_dma(uart->tx_dma_channel);
770 disable_dma(uart->rx_dma_channel);
771 free_dma(uart->rx_dma_channel);
772 del_timer(&(uart->rx_dma_timer));
773 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
774#else
775#ifdef CONFIG_BF54x
776 switch (uart->port.irq) {
777 case IRQ_UART3_RX:
778 free_dma(CH_UART3_RX);
779 free_dma(CH_UART3_TX);
780 break;
781 case IRQ_UART2_RX:
782 free_dma(CH_UART2_RX);
783 free_dma(CH_UART2_TX);
784 break;
785 default:
786 break;
787 };
788#endif
789 free_irq(uart->port.irq, uart);
790 free_irq(uart->port.irq+1, uart);
791#endif
792
793#ifdef CONFIG_SERIAL_BFIN_CTSRTS
794 if (uart->cts_pin >= 0)
795 free_irq(gpio_to_irq(uart->cts_pin), uart);
796#endif
797#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
798 if (uart->cts_pin >= 0)
799 free_irq(uart->status_irq, uart);
800#endif
801}
802
803static void
804bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
805 struct ktermios *old)
806{
807 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
808 unsigned long flags;
809 unsigned int baud, quot;
810 unsigned short val, ier, lcr = 0;
811
812 switch (termios->c_cflag & CSIZE) {
813 case CS8:
814 lcr = WLS(8);
815 break;
816 case CS7:
817 lcr = WLS(7);
818 break;
819 case CS6:
820 lcr = WLS(6);
821 break;
822 case CS5:
823 lcr = WLS(5);
824 break;
825 default:
826 printk(KERN_ERR "%s: word lengh not supported\n",
827 __func__);
828 }
829
830 /* Anomaly notes:
831 * 05000231 - STOP bit is always set to 1 whatever the user is set.
832 */
833 if (termios->c_cflag & CSTOPB) {
834 if (ANOMALY_05000231)
835 printk(KERN_WARNING "STOP bits other than 1 is not "
836 "supported in case of anomaly 05000231.\n");
837 else
838 lcr |= STB;
839 }
840 if (termios->c_cflag & PARENB)
841 lcr |= PEN;
842 if (!(termios->c_cflag & PARODD))
843 lcr |= EPS;
844 if (termios->c_cflag & CMSPAR)
845 lcr |= STP;
846
847 spin_lock_irqsave(&uart->port.lock, flags);
848
849 port->read_status_mask = OE;
850 if (termios->c_iflag & INPCK)
851 port->read_status_mask |= (FE | PE);
852 if (termios->c_iflag & (BRKINT | PARMRK))
853 port->read_status_mask |= BI;
854
855 /*
856 * Characters to ignore
857 */
858 port->ignore_status_mask = 0;
859 if (termios->c_iflag & IGNPAR)
860 port->ignore_status_mask |= FE | PE;
861 if (termios->c_iflag & IGNBRK) {
862 port->ignore_status_mask |= BI;
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 port->ignore_status_mask |= OE;
869 }
870
871 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
872 quot = uart_get_divisor(port, baud);
873
874 /* If discipline is not IRDA, apply ANOMALY_05000230 */
875 if (termios->c_line != N_IRDA)
876 quot -= ANOMALY_05000230;
877
878 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
879
880 /* Disable UART */
881 ier = UART_GET_IER(uart);
882 UART_DISABLE_INTS(uart);
883
884 /* Set DLAB in LCR to Access DLL and DLH */
885 UART_SET_DLAB(uart);
886
887 UART_PUT_DLL(uart, quot & 0xFF);
888 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
889 SSYNC();
890
891 /* Clear DLAB in LCR to Access THR RBR IER */
892 UART_CLEAR_DLAB(uart);
893
894 UART_PUT_LCR(uart, lcr);
895
896 /* Enable UART */
897 UART_ENABLE_INTS(uart, ier);
898
899 val = UART_GET_GCTL(uart);
900 val |= UCEN;
901 UART_PUT_GCTL(uart, val);
902
903 /* Port speed changed, update the per-port timeout. */
904 uart_update_timeout(port, termios->c_cflag, baud);
905
906 spin_unlock_irqrestore(&uart->port.lock, flags);
907}
908
909static const char *bfin_serial_type(struct uart_port *port)
910{
911 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
912
913 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
914}
915
916/*
917 * Release the memory region(s) being used by 'port'.
918 */
919static void bfin_serial_release_port(struct uart_port *port)
920{
921}
922
923/*
924 * Request the memory region(s) being used by 'port'.
925 */
926static int bfin_serial_request_port(struct uart_port *port)
927{
928 return 0;
929}
930
931/*
932 * Configure/autoconfigure the port.
933 */
934static void bfin_serial_config_port(struct uart_port *port, int flags)
935{
936 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
937
938 if (flags & UART_CONFIG_TYPE &&
939 bfin_serial_request_port(&uart->port) == 0)
940 uart->port.type = PORT_BFIN;
941}
942
943/*
944 * Verify the new serial_struct (for TIOCSSERIAL).
945 * The only change we allow are to the flags and type, and
946 * even then only between PORT_BFIN and PORT_UNKNOWN
947 */
948static int
949bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
950{
951 return 0;
952}
953
954/*
955 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
956 * In other cases, disable IrDA function.
957 */
958static void bfin_serial_set_ldisc(struct uart_port *port, int ld)
959{
960 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
961 unsigned short val;
962
963 switch (ld) {
964 case N_IRDA:
965 val = UART_GET_GCTL(uart);
966 val |= (IREN | RPOLC);
967 UART_PUT_GCTL(uart, val);
968 break;
969 default:
970 val = UART_GET_GCTL(uart);
971 val &= ~(IREN | RPOLC);
972 UART_PUT_GCTL(uart, val);
973 }
974}
975
976static void bfin_serial_reset_irda(struct uart_port *port)
977{
978 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
979 unsigned short val;
980
981 val = UART_GET_GCTL(uart);
982 val &= ~(IREN | RPOLC);
983 UART_PUT_GCTL(uart, val);
984 SSYNC();
985 val |= (IREN | RPOLC);
986 UART_PUT_GCTL(uart, val);
987 SSYNC();
988}
989
990#ifdef CONFIG_CONSOLE_POLL
991/* Anomaly notes:
992 * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
993 * losing other bits of UART_LSR is not a problem here.
994 */
995static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
996{
997 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
998
999 while (!(UART_GET_LSR(uart) & THRE))
1000 cpu_relax();
1001
1002 UART_CLEAR_DLAB(uart);
1003 UART_PUT_CHAR(uart, (unsigned char)chr);
1004}
1005
1006static int bfin_serial_poll_get_char(struct uart_port *port)
1007{
1008 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1009 unsigned char chr;
1010
1011 while (!(UART_GET_LSR(uart) & DR))
1012 cpu_relax();
1013
1014 UART_CLEAR_DLAB(uart);
1015 chr = UART_GET_CHAR(uart);
1016
1017 return chr;
1018}
1019#endif
1020
1021#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1022 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1023static void bfin_kgdboc_port_shutdown(struct uart_port *port)
1024{
1025 if (kgdboc_break_enabled) {
1026 kgdboc_break_enabled = 0;
1027 bfin_serial_shutdown(port);
1028 }
1029}
1030
1031static int bfin_kgdboc_port_startup(struct uart_port *port)
1032{
1033 kgdboc_port_line = port->line;
1034 kgdboc_break_enabled = !bfin_serial_startup(port);
1035 return 0;
1036}
1037#endif
1038
1039static struct uart_ops bfin_serial_pops = {
1040 .tx_empty = bfin_serial_tx_empty,
1041 .set_mctrl = bfin_serial_set_mctrl,
1042 .get_mctrl = bfin_serial_get_mctrl,
1043 .stop_tx = bfin_serial_stop_tx,
1044 .start_tx = bfin_serial_start_tx,
1045 .stop_rx = bfin_serial_stop_rx,
1046 .enable_ms = bfin_serial_enable_ms,
1047 .break_ctl = bfin_serial_break_ctl,
1048 .startup = bfin_serial_startup,
1049 .shutdown = bfin_serial_shutdown,
1050 .set_termios = bfin_serial_set_termios,
1051 .set_ldisc = bfin_serial_set_ldisc,
1052 .type = bfin_serial_type,
1053 .release_port = bfin_serial_release_port,
1054 .request_port = bfin_serial_request_port,
1055 .config_port = bfin_serial_config_port,
1056 .verify_port = bfin_serial_verify_port,
1057#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1058 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1059 .kgdboc_port_startup = bfin_kgdboc_port_startup,
1060 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
1061#endif
1062#ifdef CONFIG_CONSOLE_POLL
1063 .poll_put_char = bfin_serial_poll_put_char,
1064 .poll_get_char = bfin_serial_poll_get_char,
1065#endif
1066};
1067
1068#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1069/*
1070 * If the port was already initialised (eg, by a boot loader),
1071 * try to determine the current setup.
1072 */
1073static void __init
1074bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1075 int *parity, int *bits)
1076{
1077 unsigned short status;
1078
1079 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1080 if (status == (ERBFI | ETBEI)) {
1081 /* ok, the port was enabled */
1082 u16 lcr, dlh, dll;
1083
1084 lcr = UART_GET_LCR(uart);
1085
1086 *parity = 'n';
1087 if (lcr & PEN) {
1088 if (lcr & EPS)
1089 *parity = 'e';
1090 else
1091 *parity = 'o';
1092 }
1093 switch (lcr & 0x03) {
1094 case 0: *bits = 5; break;
1095 case 1: *bits = 6; break;
1096 case 2: *bits = 7; break;
1097 case 3: *bits = 8; break;
1098 }
1099 /* Set DLAB in LCR to Access DLL and DLH */
1100 UART_SET_DLAB(uart);
1101
1102 dll = UART_GET_DLL(uart);
1103 dlh = UART_GET_DLH(uart);
1104
1105 /* Clear DLAB in LCR to Access THR RBR IER */
1106 UART_CLEAR_DLAB(uart);
1107
1108 *baud = get_sclk() / (16*(dll | dlh << 8));
1109 }
1110 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
1111}
1112
1113static struct uart_driver bfin_serial_reg;
1114
1115static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1116{
1117 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1118 while (!(UART_GET_LSR(uart) & THRE))
1119 barrier();
1120 UART_PUT_CHAR(uart, ch);
1121}
1122
1123#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1124 defined (CONFIG_EARLY_PRINTK) */
1125
1126#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1127#define CLASS_BFIN_CONSOLE "bfin-console"
1128/*
1129 * Interrupts are disabled on entering
1130 */
1131static void
1132bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1133{
1134 struct bfin_serial_port *uart = bfin_serial_ports[co->index];
1135 unsigned long flags;
1136
1137 spin_lock_irqsave(&uart->port.lock, flags);
1138 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1139 spin_unlock_irqrestore(&uart->port.lock, flags);
1140
1141}
1142
1143static int __init
1144bfin_serial_console_setup(struct console *co, char *options)
1145{
1146 struct bfin_serial_port *uart;
1147 int baud = 57600;
1148 int bits = 8;
1149 int parity = 'n';
1150# if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1151 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1152 int flow = 'r';
1153# else
1154 int flow = 'n';
1155# endif
1156
1157 /*
1158 * Check whether an invalid uart number has been specified, and
1159 * if so, search for the first available port that does have
1160 * console support.
1161 */
1162 if (co->index < 0 || co->index >= BFIN_UART_NR_PORTS)
1163 return -ENODEV;
1164
1165 uart = bfin_serial_ports[co->index];
1166 if (!uart)
1167 return -ENODEV;
1168
1169 if (options)
1170 uart_parse_options(options, &baud, &parity, &bits, &flow);
1171 else
1172 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1173
1174 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
1175}
1176
1177static struct console bfin_serial_console = {
1178 .name = BFIN_SERIAL_DEV_NAME,
1179 .write = bfin_serial_console_write,
1180 .device = uart_console_device,
1181 .setup = bfin_serial_console_setup,
1182 .flags = CON_PRINTBUFFER,
1183 .index = -1,
1184 .data = &bfin_serial_reg,
1185};
1186#define BFIN_SERIAL_CONSOLE &bfin_serial_console
1187#else
1188#define BFIN_SERIAL_CONSOLE NULL
1189#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1190
1191#ifdef CONFIG_EARLY_PRINTK
1192static struct bfin_serial_port bfin_earlyprintk_port;
1193#define CLASS_BFIN_EARLYPRINTK "bfin-earlyprintk"
1194
1195/*
1196 * Interrupts are disabled on entering
1197 */
1198static void
1199bfin_earlyprintk_console_write(struct console *co, const char *s, unsigned int count)
1200{
1201 unsigned long flags;
1202
1203 if (bfin_earlyprintk_port.port.line != co->index)
1204 return;
1205
1206 spin_lock_irqsave(&bfin_earlyprintk_port.port.lock, flags);
1207 uart_console_write(&bfin_earlyprintk_port.port, s, count,
1208 bfin_serial_console_putchar);
1209 spin_unlock_irqrestore(&bfin_earlyprintk_port.port.lock, flags);
1210}
1211
1212/*
1213 * This should have a .setup or .early_setup in it, but then things get called
1214 * without the command line options, and the baud rate gets messed up - so
1215 * don't let the common infrastructure play with things. (see calls to setup
1216 * & earlysetup in ./kernel/printk.c:register_console()
1217 */
1218static struct __initdata console bfin_early_serial_console = {
1219 .name = "early_BFuart",
1220 .write = bfin_earlyprintk_console_write,
1221 .device = uart_console_device,
1222 .flags = CON_PRINTBUFFER,
1223 .index = -1,
1224 .data = &bfin_serial_reg,
1225};
1226#endif
1227
1228static struct uart_driver bfin_serial_reg = {
1229 .owner = THIS_MODULE,
1230 .driver_name = DRIVER_NAME,
1231 .dev_name = BFIN_SERIAL_DEV_NAME,
1232 .major = BFIN_SERIAL_MAJOR,
1233 .minor = BFIN_SERIAL_MINOR,
1234 .nr = BFIN_UART_NR_PORTS,
1235 .cons = BFIN_SERIAL_CONSOLE,
1236};
1237
1238static int bfin_serial_suspend(struct platform_device *pdev, pm_message_t state)
1239{
1240 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1241
1242 return uart_suspend_port(&bfin_serial_reg, &uart->port);
1243}
1244
1245static int bfin_serial_resume(struct platform_device *pdev)
1246{
1247 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1248
1249 return uart_resume_port(&bfin_serial_reg, &uart->port);
1250}
1251
1252static int bfin_serial_probe(struct platform_device *pdev)
1253{
1254 struct resource *res;
1255 struct bfin_serial_port *uart = NULL;
1256 int ret = 0;
1257
1258 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1259 dev_err(&pdev->dev, "Wrong bfin uart platform device id.\n");
1260 return -ENOENT;
1261 }
1262
1263 if (bfin_serial_ports[pdev->id] == NULL) {
1264
1265 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
1266 if (!uart) {
1267 dev_err(&pdev->dev,
1268 "fail to malloc bfin_serial_port\n");
1269 return -ENOMEM;
1270 }
1271 bfin_serial_ports[pdev->id] = uart;
1272
1273#ifdef CONFIG_EARLY_PRINTK
1274 if (!(bfin_earlyprintk_port.port.membase
1275 && bfin_earlyprintk_port.port.line == pdev->id)) {
1276 /*
1277 * If the peripheral PINs of current port is allocated
1278 * in earlyprintk probe stage, don't do it again.
1279 */
1280#endif
1281 ret = peripheral_request_list(
1282 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1283 if (ret) {
1284 dev_err(&pdev->dev,
1285 "fail to request bfin serial peripherals\n");
1286 goto out_error_free_mem;
1287 }
1288#ifdef CONFIG_EARLY_PRINTK
1289 }
1290#endif
1291
1292 spin_lock_init(&uart->port.lock);
1293 uart->port.uartclk = get_sclk();
1294 uart->port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1295 uart->port.ops = &bfin_serial_pops;
1296 uart->port.line = pdev->id;
1297 uart->port.iotype = UPIO_MEM;
1298 uart->port.flags = UPF_BOOT_AUTOCONF;
1299
1300 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1301 if (res == NULL) {
1302 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1303 ret = -ENOENT;
1304 goto out_error_free_peripherals;
1305 }
1306
1307 uart->port.membase = ioremap(res->start,
1308 res->end - res->start);
1309 if (!uart->port.membase) {
1310 dev_err(&pdev->dev, "Cannot map uart IO\n");
1311 ret = -ENXIO;
1312 goto out_error_free_peripherals;
1313 }
1314 uart->port.mapbase = res->start;
1315
1316 uart->port.irq = platform_get_irq(pdev, 0);
1317 if (uart->port.irq < 0) {
1318 dev_err(&pdev->dev, "No uart RX/TX IRQ specified\n");
1319 ret = -ENOENT;
1320 goto out_error_unmap;
1321 }
1322
1323 uart->status_irq = platform_get_irq(pdev, 1);
1324 if (uart->status_irq < 0) {
1325 dev_err(&pdev->dev, "No uart status IRQ specified\n");
1326 ret = -ENOENT;
1327 goto out_error_unmap;
1328 }
1329
1330#ifdef CONFIG_SERIAL_BFIN_DMA
1331 spin_lock_init(&uart->rx_lock);
1332 uart->tx_done = 1;
1333 uart->tx_count = 0;
1334
1335 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1336 if (res == NULL) {
1337 dev_err(&pdev->dev, "No uart TX DMA channel specified\n");
1338 ret = -ENOENT;
1339 goto out_error_unmap;
1340 }
1341 uart->tx_dma_channel = res->start;
1342
1343 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1344 if (res == NULL) {
1345 dev_err(&pdev->dev, "No uart RX DMA channel specified\n");
1346 ret = -ENOENT;
1347 goto out_error_unmap;
1348 }
1349 uart->rx_dma_channel = res->start;
1350
1351 init_timer(&(uart->rx_dma_timer));
1352#endif
1353
1354#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1355 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1356 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1357 if (res == NULL)
1358 uart->cts_pin = -1;
1359 else
1360 uart->cts_pin = res->start;
1361
1362 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
1363 if (res == NULL)
1364 uart->rts_pin = -1;
1365 else
1366 uart->rts_pin = res->start;
1367# if defined(CONFIG_SERIAL_BFIN_CTSRTS)
1368 if (uart->rts_pin >= 0)
1369 gpio_request(uart->rts_pin, DRIVER_NAME);
1370# endif
1371#endif
1372 }
1373
1374#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1375 if (!is_early_platform_device(pdev)) {
1376#endif
1377 uart = bfin_serial_ports[pdev->id];
1378 uart->port.dev = &pdev->dev;
1379 dev_set_drvdata(&pdev->dev, uart);
1380 ret = uart_add_one_port(&bfin_serial_reg, &uart->port);
1381#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1382 }
1383#endif
1384
1385 if (!ret)
1386 return 0;
1387
1388 if (uart) {
1389out_error_unmap:
1390 iounmap(uart->port.membase);
1391out_error_free_peripherals:
1392 peripheral_free_list(
1393 (unsigned short *)pdev->dev.platform_data);
1394out_error_free_mem:
1395 kfree(uart);
1396 bfin_serial_ports[pdev->id] = NULL;
1397 }
1398
1399 return ret;
1400}
1401
1402static int __devexit bfin_serial_remove(struct platform_device *pdev)
1403{
1404 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1405
1406 dev_set_drvdata(&pdev->dev, NULL);
1407
1408 if (uart) {
1409 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1410#ifdef CONFIG_SERIAL_BFIN_CTSRTS
1411 if (uart->rts_pin >= 0)
1412 gpio_free(uart->rts_pin);
1413#endif
1414 iounmap(uart->port.membase);
1415 peripheral_free_list(
1416 (unsigned short *)pdev->dev.platform_data);
1417 kfree(uart);
1418 bfin_serial_ports[pdev->id] = NULL;
1419 }
1420
1421 return 0;
1422}
1423
1424static struct platform_driver bfin_serial_driver = {
1425 .probe = bfin_serial_probe,
1426 .remove = __devexit_p(bfin_serial_remove),
1427 .suspend = bfin_serial_suspend,
1428 .resume = bfin_serial_resume,
1429 .driver = {
1430 .name = DRIVER_NAME,
1431 .owner = THIS_MODULE,
1432 },
1433};
1434
1435#if defined(CONFIG_SERIAL_BFIN_CONSOLE)
1436static __initdata struct early_platform_driver early_bfin_serial_driver = {
1437 .class_str = CLASS_BFIN_CONSOLE,
1438 .pdrv = &bfin_serial_driver,
1439 .requested_id = EARLY_PLATFORM_ID_UNSET,
1440};
1441
1442static int __init bfin_serial_rs_console_init(void)
1443{
1444 early_platform_driver_register(&early_bfin_serial_driver, DRIVER_NAME);
1445
1446 early_platform_driver_probe(CLASS_BFIN_CONSOLE, BFIN_UART_NR_PORTS, 0);
1447
1448 register_console(&bfin_serial_console);
1449
1450 return 0;
1451}
1452console_initcall(bfin_serial_rs_console_init);
1453#endif
1454
1455#ifdef CONFIG_EARLY_PRINTK
1456/*
1457 * Memory can't be allocated dynamically during earlyprink init stage.
1458 * So, do individual probe for earlyprink with a static uart port variable.
1459 */
1460static int bfin_earlyprintk_probe(struct platform_device *pdev)
1461{
1462 struct resource *res;
1463 int ret;
1464
1465 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1466 dev_err(&pdev->dev, "Wrong earlyprintk platform device id.\n");
1467 return -ENOENT;
1468 }
1469
1470 ret = peripheral_request_list(
1471 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1472 if (ret) {
1473 dev_err(&pdev->dev,
1474 "fail to request bfin serial peripherals\n");
1475 return ret;
1476 }
1477
1478 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1479 if (res == NULL) {
1480 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1481 ret = -ENOENT;
1482 goto out_error_free_peripherals;
1483 }
1484
1485 bfin_earlyprintk_port.port.membase = ioremap(res->start,
1486 res->end - res->start);
1487 if (!bfin_earlyprintk_port.port.membase) {
1488 dev_err(&pdev->dev, "Cannot map uart IO\n");
1489 ret = -ENXIO;
1490 goto out_error_free_peripherals;
1491 }
1492 bfin_earlyprintk_port.port.mapbase = res->start;
1493 bfin_earlyprintk_port.port.line = pdev->id;
1494 bfin_earlyprintk_port.port.uartclk = get_sclk();
1495 bfin_earlyprintk_port.port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1496 spin_lock_init(&bfin_earlyprintk_port.port.lock);
1497
1498 return 0;
1499
1500out_error_free_peripherals:
1501 peripheral_free_list(
1502 (unsigned short *)pdev->dev.platform_data);
1503
1504 return ret;
1505}
1506
1507static struct platform_driver bfin_earlyprintk_driver = {
1508 .probe = bfin_earlyprintk_probe,
1509 .driver = {
1510 .name = DRIVER_NAME,
1511 .owner = THIS_MODULE,
1512 },
1513};
1514
1515static __initdata struct early_platform_driver early_bfin_earlyprintk_driver = {
1516 .class_str = CLASS_BFIN_EARLYPRINTK,
1517 .pdrv = &bfin_earlyprintk_driver,
1518 .requested_id = EARLY_PLATFORM_ID_UNSET,
1519};
1520
1521struct console __init *bfin_earlyserial_init(unsigned int port,
1522 unsigned int cflag)
1523{
1524 struct ktermios t;
1525 char port_name[20];
1526
1527 if (port < 0 || port >= BFIN_UART_NR_PORTS)
1528 return NULL;
1529
1530 /*
1531 * Only probe resource of the given port in earlyprintk boot arg.
1532 * The expected port id should be indicated in port name string.
1533 */
1534 snprintf(port_name, 20, DRIVER_NAME ".%d", port);
1535 early_platform_driver_register(&early_bfin_earlyprintk_driver,
1536 port_name);
1537 early_platform_driver_probe(CLASS_BFIN_EARLYPRINTK, 1, 0);
1538
1539 if (!bfin_earlyprintk_port.port.membase)
1540 return NULL;
1541
1542#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1543 /*
1544 * If we are using early serial, don't let the normal console rewind
1545 * log buffer, since that causes things to be printed multiple times
1546 */
1547 bfin_serial_console.flags &= ~CON_PRINTBUFFER;
1548#endif
1549
1550 bfin_early_serial_console.index = port;
1551 t.c_cflag = cflag;
1552 t.c_iflag = 0;
1553 t.c_oflag = 0;
1554 t.c_lflag = ICANON;
1555 t.c_line = port;
1556 bfin_serial_set_termios(&bfin_earlyprintk_port.port, &t, &t);
1557
1558 return &bfin_early_serial_console;
1559}
1560#endif /* CONFIG_EARLY_PRINTK */
1561
1562static int __init bfin_serial_init(void)
1563{
1564 int ret;
1565
1566 pr_info("Blackfin serial driver\n");
1567
1568 ret = uart_register_driver(&bfin_serial_reg);
1569 if (ret) {
1570 pr_err("failed to register %s:%d\n",
1571 bfin_serial_reg.driver_name, ret);
1572 }
1573
1574 ret = platform_driver_register(&bfin_serial_driver);
1575 if (ret) {
1576 pr_err("fail to register bfin uart\n");
1577 uart_unregister_driver(&bfin_serial_reg);
1578 }
1579
1580 return ret;
1581}
1582
1583static void __exit bfin_serial_exit(void)
1584{
1585 platform_driver_unregister(&bfin_serial_driver);
1586 uart_unregister_driver(&bfin_serial_reg);
1587}
1588
1589
1590module_init(bfin_serial_init);
1591module_exit(bfin_serial_exit);
1592
1593MODULE_AUTHOR("Sonic Zhang, Aubrey Li");
1594MODULE_DESCRIPTION("Blackfin generic serial port driver");
1595MODULE_LICENSE("GPL");
1596MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
1597MODULE_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..891d194ae754
--- /dev/null
+++ b/drivers/tty/serial/bfin_sport_uart.c
@@ -0,0 +1,934 @@
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 over SPORT CTS interrupt. So, disable it.\n");
300 }
301 }
302 if (up->rts_pin >= 0)
303 gpio_direction_output(up->rts_pin, 0);
304#endif
305
306 return 0;
307 fail2:
308 free_irq(up->port.irq+1, up);
309 fail1:
310 free_irq(up->port.irq, up);
311
312 return ret;
313}
314
315/*
316 * sport_uart_tx_chars
317 *
318 * ret 1 means need to enable sport.
319 * ret 0 means do nothing.
320 */
321static int sport_uart_tx_chars(struct sport_uart_port *up)
322{
323 struct circ_buf *xmit = &up->port.state->xmit;
324
325 if (SPORT_GET_STAT(up) & TXF)
326 return 0;
327
328 if (up->port.x_char) {
329 tx_one_byte(up, up->port.x_char);
330 up->port.icount.tx++;
331 up->port.x_char = 0;
332 return 1;
333 }
334
335 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
336 /* The waiting loop to stop SPORT TX from TX interrupt is
337 * too long. This may block SPORT RX interrupts and cause
338 * RX FIFO overflow. So, do stop sport TX only after the last
339 * char in TX FIFO is moved into the shift register.
340 */
341 if (SPORT_GET_STAT(up) & TXHRE)
342 sport_stop_tx(&up->port);
343 return 0;
344 }
345
346 while(!(SPORT_GET_STAT(up) & TXF) && !uart_circ_empty(xmit)) {
347 tx_one_byte(up, xmit->buf[xmit->tail]);
348 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE -1);
349 up->port.icount.tx++;
350 }
351
352 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
353 uart_write_wakeup(&up->port);
354
355 return 1;
356}
357
358static unsigned int sport_tx_empty(struct uart_port *port)
359{
360 struct sport_uart_port *up = (struct sport_uart_port *)port;
361 unsigned int stat;
362
363 stat = SPORT_GET_STAT(up);
364 pr_debug("%s stat:%04x\n", __func__, stat);
365 if (stat & TXHRE) {
366 return TIOCSER_TEMT;
367 } else
368 return 0;
369}
370
371static void sport_stop_tx(struct uart_port *port)
372{
373 struct sport_uart_port *up = (struct sport_uart_port *)port;
374
375 pr_debug("%s enter\n", __func__);
376
377 if (!(SPORT_GET_TCR1(up) & TSPEN))
378 return;
379
380 /* Although the hold register is empty, last byte is still in shift
381 * register and not sent out yet. So, put a dummy data into TX FIFO.
382 * Then, sport tx stops when last byte is shift out and the dummy
383 * data is moved into the shift register.
384 */
385 SPORT_PUT_TX(up, 0xffff);
386 while (!(SPORT_GET_STAT(up) & TXHRE))
387 cpu_relax();
388
389 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
390 SSYNC();
391
392 return;
393}
394
395static void sport_start_tx(struct uart_port *port)
396{
397 struct sport_uart_port *up = (struct sport_uart_port *)port;
398
399 pr_debug("%s enter\n", __func__);
400
401 /* Write data into SPORT FIFO before enable SPROT to transmit */
402 if (sport_uart_tx_chars(up)) {
403 /* Enable transmit, then an interrupt will generated */
404 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
405 SSYNC();
406 }
407
408 pr_debug("%s exit\n", __func__);
409}
410
411static void sport_stop_rx(struct uart_port *port)
412{
413 struct sport_uart_port *up = (struct sport_uart_port *)port;
414
415 pr_debug("%s enter\n", __func__);
416 /* Disable sport to stop rx */
417 SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
418 SSYNC();
419}
420
421static void sport_enable_ms(struct uart_port *port)
422{
423 pr_debug("%s enter\n", __func__);
424}
425
426static void sport_break_ctl(struct uart_port *port, int break_state)
427{
428 pr_debug("%s enter\n", __func__);
429}
430
431static void sport_shutdown(struct uart_port *port)
432{
433 struct sport_uart_port *up = (struct sport_uart_port *)port;
434
435 dev_dbg(port->dev, "%s enter\n", __func__);
436
437 /* Disable sport */
438 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
439 SPORT_PUT_RCR1(up, (SPORT_GET_RCR1(up) & ~RSPEN));
440 SSYNC();
441
442 free_irq(up->port.irq, up);
443 free_irq(up->port.irq+1, up);
444 free_irq(up->err_irq, up);
445#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
446 if (up->cts_pin >= 0)
447 free_irq(gpio_to_irq(up->cts_pin), up);
448#endif
449}
450
451static const char *sport_type(struct uart_port *port)
452{
453 struct sport_uart_port *up = (struct sport_uart_port *)port;
454
455 pr_debug("%s enter\n", __func__);
456 return up->port.type == PORT_BFIN_SPORT ? "BFIN-SPORT-UART" : NULL;
457}
458
459static void sport_release_port(struct uart_port *port)
460{
461 pr_debug("%s enter\n", __func__);
462}
463
464static int sport_request_port(struct uart_port *port)
465{
466 pr_debug("%s enter\n", __func__);
467 return 0;
468}
469
470static void sport_config_port(struct uart_port *port, int flags)
471{
472 struct sport_uart_port *up = (struct sport_uart_port *)port;
473
474 pr_debug("%s enter\n", __func__);
475 up->port.type = PORT_BFIN_SPORT;
476}
477
478static int sport_verify_port(struct uart_port *port, struct serial_struct *ser)
479{
480 pr_debug("%s enter\n", __func__);
481 return 0;
482}
483
484static void sport_set_termios(struct uart_port *port,
485 struct ktermios *termios, struct ktermios *old)
486{
487 struct sport_uart_port *up = (struct sport_uart_port *)port;
488 unsigned long flags;
489 int i;
490
491 pr_debug("%s enter, c_cflag:%08x\n", __func__, termios->c_cflag);
492
493 switch (termios->c_cflag & CSIZE) {
494 case CS8:
495 up->csize = 8;
496 break;
497 case CS7:
498 up->csize = 7;
499 break;
500 case CS6:
501 up->csize = 6;
502 break;
503 case CS5:
504 up->csize = 5;
505 break;
506 default:
507 pr_warning("requested word length not supported\n");
508 }
509
510 if (termios->c_cflag & CSTOPB) {
511 up->stopb = 1;
512 }
513 if (termios->c_cflag & PARENB) {
514 pr_warning("PAREN bits is not supported yet\n");
515 /* up->parib = 1; */
516 }
517
518 spin_lock_irqsave(&up->port.lock, flags);
519
520 port->read_status_mask = 0;
521
522 /*
523 * Characters to ignore
524 */
525 port->ignore_status_mask = 0;
526
527 /* RX extract mask */
528 up->rxmask = 0x01 | (((up->csize + up->stopb) * 2 - 1) << 0x8);
529 /* TX masks, 8 bit data and 1 bit stop for example:
530 * mask1 = b#0111111110
531 * mask2 = b#1000000000
532 */
533 for (i = 0, up->txmask1 = 0; i < up->csize; i++)
534 up->txmask1 |= (1<<i);
535 up->txmask2 = (1<<i);
536 if (up->stopb) {
537 ++i;
538 up->txmask2 |= (1<<i);
539 }
540 up->txmask1 <<= 1;
541 up->txmask2 <<= 1;
542 /* uart baud rate */
543 port->uartclk = uart_get_baud_rate(port, termios, old, 0, get_sclk()/16);
544
545 /* Disable UART */
546 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
547 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) & ~RSPEN);
548
549 sport_uart_setup(up, up->csize + up->stopb, port->uartclk);
550
551 /* driver TX line high after config, one dummy data is
552 * necessary to stop sport after shift one byte
553 */
554 SPORT_PUT_TX(up, 0xffff);
555 SPORT_PUT_TX(up, 0xffff);
556 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
557 SSYNC();
558 while (!(SPORT_GET_STAT(up) & TXHRE))
559 cpu_relax();
560 SPORT_PUT_TCR1(up, SPORT_GET_TCR1(up) & ~TSPEN);
561 SSYNC();
562
563 /* Port speed changed, update the per-port timeout. */
564 uart_update_timeout(port, termios->c_cflag, port->uartclk);
565
566 /* Enable sport rx */
567 SPORT_PUT_RCR1(up, SPORT_GET_RCR1(up) | RSPEN);
568 SSYNC();
569
570 spin_unlock_irqrestore(&up->port.lock, flags);
571}
572
573struct uart_ops sport_uart_ops = {
574 .tx_empty = sport_tx_empty,
575 .set_mctrl = sport_set_mctrl,
576 .get_mctrl = sport_get_mctrl,
577 .stop_tx = sport_stop_tx,
578 .start_tx = sport_start_tx,
579 .stop_rx = sport_stop_rx,
580 .enable_ms = sport_enable_ms,
581 .break_ctl = sport_break_ctl,
582 .startup = sport_startup,
583 .shutdown = sport_shutdown,
584 .set_termios = sport_set_termios,
585 .type = sport_type,
586 .release_port = sport_release_port,
587 .request_port = sport_request_port,
588 .config_port = sport_config_port,
589 .verify_port = sport_verify_port,
590};
591
592#define BFIN_SPORT_UART_MAX_PORTS 4
593
594static struct sport_uart_port *bfin_sport_uart_ports[BFIN_SPORT_UART_MAX_PORTS];
595
596#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
597#define CLASS_BFIN_SPORT_CONSOLE "bfin-sport-console"
598
599static int __init
600sport_uart_console_setup(struct console *co, char *options)
601{
602 struct sport_uart_port *up;
603 int baud = 57600;
604 int bits = 8;
605 int parity = 'n';
606# ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
607 int flow = 'r';
608# else
609 int flow = 'n';
610# endif
611
612 /* Check whether an invalid uart number has been specified */
613 if (co->index < 0 || co->index >= BFIN_SPORT_UART_MAX_PORTS)
614 return -ENODEV;
615
616 up = bfin_sport_uart_ports[co->index];
617 if (!up)
618 return -ENODEV;
619
620 if (options)
621 uart_parse_options(options, &baud, &parity, &bits, &flow);
622
623 return uart_set_options(&up->port, co, baud, parity, bits, flow);
624}
625
626static void sport_uart_console_putchar(struct uart_port *port, int ch)
627{
628 struct sport_uart_port *up = (struct sport_uart_port *)port;
629
630 while (SPORT_GET_STAT(up) & TXF)
631 barrier();
632
633 tx_one_byte(up, ch);
634}
635
636/*
637 * Interrupts are disabled on entering
638 */
639static void
640sport_uart_console_write(struct console *co, const char *s, unsigned int count)
641{
642 struct sport_uart_port *up = bfin_sport_uart_ports[co->index];
643 unsigned long flags;
644
645 spin_lock_irqsave(&up->port.lock, flags);
646
647 if (SPORT_GET_TCR1(up) & TSPEN)
648 uart_console_write(&up->port, s, count, sport_uart_console_putchar);
649 else {
650 /* dummy data to start sport */
651 while (SPORT_GET_STAT(up) & TXF)
652 barrier();
653 SPORT_PUT_TX(up, 0xffff);
654 /* Enable transmit, then an interrupt will generated */
655 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) | TSPEN));
656 SSYNC();
657
658 uart_console_write(&up->port, s, count, sport_uart_console_putchar);
659
660 /* Although the hold register is empty, last byte is still in shift
661 * register and not sent out yet. So, put a dummy data into TX FIFO.
662 * Then, sport tx stops when last byte is shift out and the dummy
663 * data is moved into the shift register.
664 */
665 while (SPORT_GET_STAT(up) & TXF)
666 barrier();
667 SPORT_PUT_TX(up, 0xffff);
668 while (!(SPORT_GET_STAT(up) & TXHRE))
669 barrier();
670
671 /* Stop sport tx transfer */
672 SPORT_PUT_TCR1(up, (SPORT_GET_TCR1(up) & ~TSPEN));
673 SSYNC();
674 }
675
676 spin_unlock_irqrestore(&up->port.lock, flags);
677}
678
679static struct uart_driver sport_uart_reg;
680
681static struct console sport_uart_console = {
682 .name = DEVICE_NAME,
683 .write = sport_uart_console_write,
684 .device = uart_console_device,
685 .setup = sport_uart_console_setup,
686 .flags = CON_PRINTBUFFER,
687 .index = -1,
688 .data = &sport_uart_reg,
689};
690
691#define SPORT_UART_CONSOLE (&sport_uart_console)
692#else
693#define SPORT_UART_CONSOLE NULL
694#endif /* CONFIG_SERIAL_BFIN_SPORT_CONSOLE */
695
696
697static struct uart_driver sport_uart_reg = {
698 .owner = THIS_MODULE,
699 .driver_name = DRV_NAME,
700 .dev_name = DEVICE_NAME,
701 .major = 204,
702 .minor = 84,
703 .nr = BFIN_SPORT_UART_MAX_PORTS,
704 .cons = SPORT_UART_CONSOLE,
705};
706
707#ifdef CONFIG_PM
708static int sport_uart_suspend(struct device *dev)
709{
710 struct sport_uart_port *sport = dev_get_drvdata(dev);
711
712 dev_dbg(dev, "%s enter\n", __func__);
713 if (sport)
714 uart_suspend_port(&sport_uart_reg, &sport->port);
715
716 return 0;
717}
718
719static int sport_uart_resume(struct device *dev)
720{
721 struct sport_uart_port *sport = dev_get_drvdata(dev);
722
723 dev_dbg(dev, "%s enter\n", __func__);
724 if (sport)
725 uart_resume_port(&sport_uart_reg, &sport->port);
726
727 return 0;
728}
729
730static struct dev_pm_ops bfin_sport_uart_dev_pm_ops = {
731 .suspend = sport_uart_suspend,
732 .resume = sport_uart_resume,
733};
734#endif
735
736static int __devinit sport_uart_probe(struct platform_device *pdev)
737{
738 struct resource *res;
739 struct sport_uart_port *sport;
740 int ret = 0;
741
742 dev_dbg(&pdev->dev, "%s enter\n", __func__);
743
744 if (pdev->id < 0 || pdev->id >= BFIN_SPORT_UART_MAX_PORTS) {
745 dev_err(&pdev->dev, "Wrong sport uart platform device id.\n");
746 return -ENOENT;
747 }
748
749 if (bfin_sport_uart_ports[pdev->id] == NULL) {
750 bfin_sport_uart_ports[pdev->id] =
751 kzalloc(sizeof(struct sport_uart_port), GFP_KERNEL);
752 sport = bfin_sport_uart_ports[pdev->id];
753 if (!sport) {
754 dev_err(&pdev->dev,
755 "Fail to malloc sport_uart_port\n");
756 return -ENOMEM;
757 }
758
759 ret = peripheral_request_list(
760 (unsigned short *)pdev->dev.platform_data, DRV_NAME);
761 if (ret) {
762 dev_err(&pdev->dev,
763 "Fail to request SPORT peripherals\n");
764 goto out_error_free_mem;
765 }
766
767 spin_lock_init(&sport->port.lock);
768 sport->port.fifosize = SPORT_TX_FIFO_SIZE,
769 sport->port.ops = &sport_uart_ops;
770 sport->port.line = pdev->id;
771 sport->port.iotype = UPIO_MEM;
772 sport->port.flags = UPF_BOOT_AUTOCONF;
773
774 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
775 if (res == NULL) {
776 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
777 ret = -ENOENT;
778 goto out_error_free_peripherals;
779 }
780
781 sport->port.membase = ioremap(res->start, resource_size(res));
782 if (!sport->port.membase) {
783 dev_err(&pdev->dev, "Cannot map sport IO\n");
784 ret = -ENXIO;
785 goto out_error_free_peripherals;
786 }
787 sport->port.mapbase = res->start;
788
789 sport->port.irq = platform_get_irq(pdev, 0);
790 if ((int)sport->port.irq < 0) {
791 dev_err(&pdev->dev, "No sport RX/TX IRQ specified\n");
792 ret = -ENOENT;
793 goto out_error_unmap;
794 }
795
796 sport->err_irq = platform_get_irq(pdev, 1);
797 if (sport->err_irq < 0) {
798 dev_err(&pdev->dev, "No sport status IRQ specified\n");
799 ret = -ENOENT;
800 goto out_error_unmap;
801 }
802#ifdef CONFIG_SERIAL_BFIN_SPORT_CTSRTS
803 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
804 if (res == NULL)
805 sport->cts_pin = -1;
806 else
807 sport->cts_pin = res->start;
808
809 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
810 if (res == NULL)
811 sport->rts_pin = -1;
812 else
813 sport->rts_pin = res->start;
814
815 if (sport->rts_pin >= 0)
816 gpio_request(sport->rts_pin, DRV_NAME);
817#endif
818 }
819
820#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
821 if (!is_early_platform_device(pdev)) {
822#endif
823 sport = bfin_sport_uart_ports[pdev->id];
824 sport->port.dev = &pdev->dev;
825 dev_set_drvdata(&pdev->dev, sport);
826 ret = uart_add_one_port(&sport_uart_reg, &sport->port);
827#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
828 }
829#endif
830 if (!ret)
831 return 0;
832
833 if (sport) {
834out_error_unmap:
835 iounmap(sport->port.membase);
836out_error_free_peripherals:
837 peripheral_free_list(
838 (unsigned short *)pdev->dev.platform_data);
839out_error_free_mem:
840 kfree(sport);
841 bfin_sport_uart_ports[pdev->id] = NULL;
842 }
843
844 return ret;
845}
846
847static int __devexit sport_uart_remove(struct platform_device *pdev)
848{
849 struct sport_uart_port *sport = platform_get_drvdata(pdev);
850
851 dev_dbg(&pdev->dev, "%s enter\n", __func__);
852 dev_set_drvdata(&pdev->dev, NULL);
853
854 if (sport) {
855 uart_remove_one_port(&sport_uart_reg, &sport->port);
856#ifdef CONFIG_SERIAL_BFIN_CTSRTS
857 if (sport->rts_pin >= 0)
858 gpio_free(sport->rts_pin);
859#endif
860 iounmap(sport->port.membase);
861 peripheral_free_list(
862 (unsigned short *)pdev->dev.platform_data);
863 kfree(sport);
864 bfin_sport_uart_ports[pdev->id] = NULL;
865 }
866
867 return 0;
868}
869
870static struct platform_driver sport_uart_driver = {
871 .probe = sport_uart_probe,
872 .remove = __devexit_p(sport_uart_remove),
873 .driver = {
874 .name = DRV_NAME,
875#ifdef CONFIG_PM
876 .pm = &bfin_sport_uart_dev_pm_ops,
877#endif
878 },
879};
880
881#ifdef CONFIG_SERIAL_BFIN_SPORT_CONSOLE
882static __initdata struct early_platform_driver early_sport_uart_driver = {
883 .class_str = CLASS_BFIN_SPORT_CONSOLE,
884 .pdrv = &sport_uart_driver,
885 .requested_id = EARLY_PLATFORM_ID_UNSET,
886};
887
888static int __init sport_uart_rs_console_init(void)
889{
890 early_platform_driver_register(&early_sport_uart_driver, DRV_NAME);
891
892 early_platform_driver_probe(CLASS_BFIN_SPORT_CONSOLE,
893 BFIN_SPORT_UART_MAX_PORTS, 0);
894
895 register_console(&sport_uart_console);
896
897 return 0;
898}
899console_initcall(sport_uart_rs_console_init);
900#endif
901
902static int __init sport_uart_init(void)
903{
904 int ret;
905
906 pr_info("Blackfin uart over sport driver\n");
907
908 ret = uart_register_driver(&sport_uart_reg);
909 if (ret) {
910 pr_err("failed to register %s:%d\n",
911 sport_uart_reg.driver_name, ret);
912 return ret;
913 }
914
915 ret = platform_driver_register(&sport_uart_driver);
916 if (ret) {
917 pr_err("failed to register sport uart driver:%d\n", ret);
918 uart_unregister_driver(&sport_uart_reg);
919 }
920
921 return ret;
922}
923module_init(sport_uart_init);
924
925static void __exit sport_uart_exit(void)
926{
927 platform_driver_unregister(&sport_uart_driver);
928 uart_unregister_driver(&sport_uart_reg);
929}
930module_exit(sport_uart_exit);
931
932MODULE_AUTHOR("Sonic Zhang, Roy Huang");
933MODULE_DESCRIPTION("Blackfin serial over SPORT driver");
934MODULE_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..e6c3dbd781d6
--- /dev/null
+++ b/drivers/tty/serial/clps711x.c
@@ -0,0 +1,577 @@
1/*
2 * Driver for CLPS711x serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
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 as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
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 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
25#define SUPPORT_SYSRQ
26#endif
27
28#include <linux/module.h>
29#include <linux/ioport.h>
30#include <linux/init.h>
31#include <linux/console.h>
32#include <linux/sysrq.h>
33#include <linux/spinlock.h>
34#include <linux/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 <mach/hardware.h>
42#include <asm/irq.h>
43#include <asm/hardware/clps7111.h>
44
45#define UART_NR 2
46
47#define SERIAL_CLPS711X_MAJOR 204
48#define SERIAL_CLPS711X_MINOR 40
49#define SERIAL_CLPS711X_NR UART_NR
50
51/*
52 * We use the relevant SYSCON register as a base address for these ports.
53 */
54#define UBRLCR(port) ((port)->iobase + UBRLCR1 - SYSCON1)
55#define UARTDR(port) ((port)->iobase + UARTDR1 - SYSCON1)
56#define SYSFLG(port) ((port)->iobase + SYSFLG1 - SYSCON1)
57#define SYSCON(port) ((port)->iobase + SYSCON1 - SYSCON1)
58
59#define TX_IRQ(port) ((port)->irq)
60#define RX_IRQ(port) ((port)->irq + 1)
61
62#define UART_ANY_ERR (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR)
63
64#define tx_enabled(port) ((port)->unused[0])
65
66static void clps711xuart_stop_tx(struct uart_port *port)
67{
68 if (tx_enabled(port)) {
69 disable_irq(TX_IRQ(port));
70 tx_enabled(port) = 0;
71 }
72}
73
74static void clps711xuart_start_tx(struct uart_port *port)
75{
76 if (!tx_enabled(port)) {
77 enable_irq(TX_IRQ(port));
78 tx_enabled(port) = 1;
79 }
80}
81
82static void clps711xuart_stop_rx(struct uart_port *port)
83{
84 disable_irq(RX_IRQ(port));
85}
86
87static void clps711xuart_enable_ms(struct uart_port *port)
88{
89}
90
91static irqreturn_t clps711xuart_int_rx(int irq, void *dev_id)
92{
93 struct uart_port *port = dev_id;
94 struct tty_struct *tty = port->state->port.tty;
95 unsigned int status, ch, flg;
96
97 status = clps_readl(SYSFLG(port));
98 while (!(status & SYSFLG_URXFE)) {
99 ch = clps_readl(UARTDR(port));
100
101 port->icount.rx++;
102
103 flg = TTY_NORMAL;
104
105 /*
106 * Note that the error handling code is
107 * out of the main execution path
108 */
109 if (unlikely(ch & UART_ANY_ERR)) {
110 if (ch & UARTDR_PARERR)
111 port->icount.parity++;
112 else if (ch & UARTDR_FRMERR)
113 port->icount.frame++;
114 if (ch & UARTDR_OVERR)
115 port->icount.overrun++;
116
117 ch &= port->read_status_mask;
118
119 if (ch & UARTDR_PARERR)
120 flg = TTY_PARITY;
121 else if (ch & UARTDR_FRMERR)
122 flg = TTY_FRAME;
123
124#ifdef SUPPORT_SYSRQ
125 port->sysrq = 0;
126#endif
127 }
128
129 if (uart_handle_sysrq_char(port, ch))
130 goto ignore_char;
131
132 /*
133 * CHECK: does overrun affect the current character?
134 * ASSUMPTION: it does not.
135 */
136 uart_insert_char(port, ch, UARTDR_OVERR, ch, flg);
137
138 ignore_char:
139 status = clps_readl(SYSFLG(port));
140 }
141 tty_flip_buffer_push(tty);
142 return IRQ_HANDLED;
143}
144
145static irqreturn_t clps711xuart_int_tx(int irq, void *dev_id)
146{
147 struct uart_port *port = dev_id;
148 struct circ_buf *xmit = &port->state->xmit;
149 int count;
150
151 if (port->x_char) {
152 clps_writel(port->x_char, UARTDR(port));
153 port->icount.tx++;
154 port->x_char = 0;
155 return IRQ_HANDLED;
156 }
157 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
158 clps711xuart_stop_tx(port);
159 return IRQ_HANDLED;
160 }
161
162 count = port->fifosize >> 1;
163 do {
164 clps_writel(xmit->buf[xmit->tail], UARTDR(port));
165 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
166 port->icount.tx++;
167 if (uart_circ_empty(xmit))
168 break;
169 } while (--count > 0);
170
171 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
172 uart_write_wakeup(port);
173
174 if (uart_circ_empty(xmit))
175 clps711xuart_stop_tx(port);
176
177 return IRQ_HANDLED;
178}
179
180static unsigned int clps711xuart_tx_empty(struct uart_port *port)
181{
182 unsigned int status = clps_readl(SYSFLG(port));
183 return status & SYSFLG_UBUSY ? 0 : TIOCSER_TEMT;
184}
185
186static unsigned int clps711xuart_get_mctrl(struct uart_port *port)
187{
188 unsigned int port_addr;
189 unsigned int result = 0;
190 unsigned int status;
191
192 port_addr = SYSFLG(port);
193 if (port_addr == SYSFLG1) {
194 status = clps_readl(SYSFLG1);
195 if (status & SYSFLG1_DCD)
196 result |= TIOCM_CAR;
197 if (status & SYSFLG1_DSR)
198 result |= TIOCM_DSR;
199 if (status & SYSFLG1_CTS)
200 result |= TIOCM_CTS;
201 }
202
203 return result;
204}
205
206static void
207clps711xuart_set_mctrl_null(struct uart_port *port, unsigned int mctrl)
208{
209}
210
211static void clps711xuart_break_ctl(struct uart_port *port, int break_state)
212{
213 unsigned long flags;
214 unsigned int ubrlcr;
215
216 spin_lock_irqsave(&port->lock, flags);
217 ubrlcr = clps_readl(UBRLCR(port));
218 if (break_state == -1)
219 ubrlcr |= UBRLCR_BREAK;
220 else
221 ubrlcr &= ~UBRLCR_BREAK;
222 clps_writel(ubrlcr, UBRLCR(port));
223 spin_unlock_irqrestore(&port->lock, flags);
224}
225
226static int clps711xuart_startup(struct uart_port *port)
227{
228 unsigned int syscon;
229 int retval;
230
231 tx_enabled(port) = 1;
232
233 /*
234 * Allocate the IRQs
235 */
236 retval = request_irq(TX_IRQ(port), clps711xuart_int_tx, 0,
237 "clps711xuart_tx", port);
238 if (retval)
239 return retval;
240
241 retval = request_irq(RX_IRQ(port), clps711xuart_int_rx, 0,
242 "clps711xuart_rx", port);
243 if (retval) {
244 free_irq(TX_IRQ(port), port);
245 return retval;
246 }
247
248 /*
249 * enable the port
250 */
251 syscon = clps_readl(SYSCON(port));
252 syscon |= SYSCON_UARTEN;
253 clps_writel(syscon, SYSCON(port));
254
255 return 0;
256}
257
258static void clps711xuart_shutdown(struct uart_port *port)
259{
260 unsigned int ubrlcr, syscon;
261
262 /*
263 * Free the interrupt
264 */
265 free_irq(TX_IRQ(port), port); /* TX interrupt */
266 free_irq(RX_IRQ(port), port); /* RX interrupt */
267
268 /*
269 * disable the port
270 */
271 syscon = clps_readl(SYSCON(port));
272 syscon &= ~SYSCON_UARTEN;
273 clps_writel(syscon, SYSCON(port));
274
275 /*
276 * disable break condition and fifos
277 */
278 ubrlcr = clps_readl(UBRLCR(port));
279 ubrlcr &= ~(UBRLCR_FIFOEN | UBRLCR_BREAK);
280 clps_writel(ubrlcr, UBRLCR(port));
281}
282
283static void
284clps711xuart_set_termios(struct uart_port *port, struct ktermios *termios,
285 struct ktermios *old)
286{
287 unsigned int ubrlcr, baud, quot;
288 unsigned long flags;
289
290 /*
291 * We don't implement CREAD.
292 */
293 termios->c_cflag |= CREAD;
294
295 /*
296 * Ask the core to calculate the divisor for us.
297 */
298 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
299 quot = uart_get_divisor(port, baud);
300
301 switch (termios->c_cflag & CSIZE) {
302 case CS5:
303 ubrlcr = UBRLCR_WRDLEN5;
304 break;
305 case CS6:
306 ubrlcr = UBRLCR_WRDLEN6;
307 break;
308 case CS7:
309 ubrlcr = UBRLCR_WRDLEN7;
310 break;
311 default: // CS8
312 ubrlcr = UBRLCR_WRDLEN8;
313 break;
314 }
315 if (termios->c_cflag & CSTOPB)
316 ubrlcr |= UBRLCR_XSTOP;
317 if (termios->c_cflag & PARENB) {
318 ubrlcr |= UBRLCR_PRTEN;
319 if (!(termios->c_cflag & PARODD))
320 ubrlcr |= UBRLCR_EVENPRT;
321 }
322 if (port->fifosize > 1)
323 ubrlcr |= UBRLCR_FIFOEN;
324
325 spin_lock_irqsave(&port->lock, flags);
326
327 /*
328 * Update the per-port timeout.
329 */
330 uart_update_timeout(port, termios->c_cflag, baud);
331
332 port->read_status_mask = UARTDR_OVERR;
333 if (termios->c_iflag & INPCK)
334 port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
335
336 /*
337 * Characters to ignore
338 */
339 port->ignore_status_mask = 0;
340 if (termios->c_iflag & IGNPAR)
341 port->ignore_status_mask |= UARTDR_FRMERR | UARTDR_PARERR;
342 if (termios->c_iflag & IGNBRK) {
343 /*
344 * If we're ignoring parity and break indicators,
345 * ignore overruns to (for real raw support).
346 */
347 if (termios->c_iflag & IGNPAR)
348 port->ignore_status_mask |= UARTDR_OVERR;
349 }
350
351 quot -= 1;
352
353 clps_writel(ubrlcr | quot, UBRLCR(port));
354
355 spin_unlock_irqrestore(&port->lock, flags);
356}
357
358static const char *clps711xuart_type(struct uart_port *port)
359{
360 return port->type == PORT_CLPS711X ? "CLPS711x" : NULL;
361}
362
363/*
364 * Configure/autoconfigure the port.
365 */
366static void clps711xuart_config_port(struct uart_port *port, int flags)
367{
368 if (flags & UART_CONFIG_TYPE)
369 port->type = PORT_CLPS711X;
370}
371
372static void clps711xuart_release_port(struct uart_port *port)
373{
374}
375
376static int clps711xuart_request_port(struct uart_port *port)
377{
378 return 0;
379}
380
381static struct uart_ops clps711x_pops = {
382 .tx_empty = clps711xuart_tx_empty,
383 .set_mctrl = clps711xuart_set_mctrl_null,
384 .get_mctrl = clps711xuart_get_mctrl,
385 .stop_tx = clps711xuart_stop_tx,
386 .start_tx = clps711xuart_start_tx,
387 .stop_rx = clps711xuart_stop_rx,
388 .enable_ms = clps711xuart_enable_ms,
389 .break_ctl = clps711xuart_break_ctl,
390 .startup = clps711xuart_startup,
391 .shutdown = clps711xuart_shutdown,
392 .set_termios = clps711xuart_set_termios,
393 .type = clps711xuart_type,
394 .config_port = clps711xuart_config_port,
395 .release_port = clps711xuart_release_port,
396 .request_port = clps711xuart_request_port,
397};
398
399static struct uart_port clps711x_ports[UART_NR] = {
400 {
401 .iobase = SYSCON1,
402 .irq = IRQ_UTXINT1, /* IRQ_URXINT1, IRQ_UMSINT */
403 .uartclk = 3686400,
404 .fifosize = 16,
405 .ops = &clps711x_pops,
406 .line = 0,
407 .flags = UPF_BOOT_AUTOCONF,
408 },
409 {
410 .iobase = SYSCON2,
411 .irq = IRQ_UTXINT2, /* IRQ_URXINT2 */
412 .uartclk = 3686400,
413 .fifosize = 16,
414 .ops = &clps711x_pops,
415 .line = 1,
416 .flags = UPF_BOOT_AUTOCONF,
417 }
418};
419
420#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
421static void clps711xuart_console_putchar(struct uart_port *port, int ch)
422{
423 while (clps_readl(SYSFLG(port)) & SYSFLG_UTXFF)
424 barrier();
425 clps_writel(ch, UARTDR(port));
426}
427
428/*
429 * Print a string to the serial port trying not to disturb
430 * any possible real use of the port...
431 *
432 * The console_lock must be held when we get here.
433 *
434 * Note that this is called with interrupts already disabled
435 */
436static void
437clps711xuart_console_write(struct console *co, const char *s,
438 unsigned int count)
439{
440 struct uart_port *port = clps711x_ports + co->index;
441 unsigned int status, syscon;
442
443 /*
444 * Ensure that the port is enabled.
445 */
446 syscon = clps_readl(SYSCON(port));
447 clps_writel(syscon | SYSCON_UARTEN, SYSCON(port));
448
449 uart_console_write(port, s, count, clps711xuart_console_putchar);
450
451 /*
452 * Finally, wait for transmitter to become empty
453 * and restore the uart state.
454 */
455 do {
456 status = clps_readl(SYSFLG(port));
457 } while (status & SYSFLG_UBUSY);
458
459 clps_writel(syscon, SYSCON(port));
460}
461
462static void __init
463clps711xuart_console_get_options(struct uart_port *port, int *baud,
464 int *parity, int *bits)
465{
466 if (clps_readl(SYSCON(port)) & SYSCON_UARTEN) {
467 unsigned int ubrlcr, quot;
468
469 ubrlcr = clps_readl(UBRLCR(port));
470
471 *parity = 'n';
472 if (ubrlcr & UBRLCR_PRTEN) {
473 if (ubrlcr & UBRLCR_EVENPRT)
474 *parity = 'e';
475 else
476 *parity = 'o';
477 }
478
479 if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
480 *bits = 7;
481 else
482 *bits = 8;
483
484 quot = ubrlcr & UBRLCR_BAUD_MASK;
485 *baud = port->uartclk / (16 * (quot + 1));
486 }
487}
488
489static int __init clps711xuart_console_setup(struct console *co, char *options)
490{
491 struct uart_port *port;
492 int baud = 38400;
493 int bits = 8;
494 int parity = 'n';
495 int flow = 'n';
496
497 /*
498 * Check whether an invalid uart number has been specified, and
499 * if so, search for the first available port that does have
500 * console support.
501 */
502 port = uart_get_console(clps711x_ports, UART_NR, co);
503
504 if (options)
505 uart_parse_options(options, &baud, &parity, &bits, &flow);
506 else
507 clps711xuart_console_get_options(port, &baud, &parity, &bits);
508
509 return uart_set_options(port, co, baud, parity, bits, flow);
510}
511
512static struct uart_driver clps711x_reg;
513static struct console clps711x_console = {
514 .name = "ttyCL",
515 .write = clps711xuart_console_write,
516 .device = uart_console_device,
517 .setup = clps711xuart_console_setup,
518 .flags = CON_PRINTBUFFER,
519 .index = -1,
520 .data = &clps711x_reg,
521};
522
523static int __init clps711xuart_console_init(void)
524{
525 register_console(&clps711x_console);
526 return 0;
527}
528console_initcall(clps711xuart_console_init);
529
530#define CLPS711X_CONSOLE &clps711x_console
531#else
532#define CLPS711X_CONSOLE NULL
533#endif
534
535static struct uart_driver clps711x_reg = {
536 .driver_name = "ttyCL",
537 .dev_name = "ttyCL",
538 .major = SERIAL_CLPS711X_MAJOR,
539 .minor = SERIAL_CLPS711X_MINOR,
540 .nr = UART_NR,
541
542 .cons = CLPS711X_CONSOLE,
543};
544
545static int __init clps711xuart_init(void)
546{
547 int ret, i;
548
549 printk(KERN_INFO "Serial: CLPS711x driver\n");
550
551 ret = uart_register_driver(&clps711x_reg);
552 if (ret)
553 return ret;
554
555 for (i = 0; i < UART_NR; i++)
556 uart_add_one_port(&clps711x_reg, &clps711x_ports[i]);
557
558 return 0;
559}
560
561static void __exit clps711xuart_exit(void)
562{
563 int i;
564
565 for (i = 0; i < UART_NR; i++)
566 uart_remove_one_port(&clps711x_reg, &clps711x_ports[i]);
567
568 uart_unregister_driver(&clps711x_reg);
569}
570
571module_init(clps711xuart_init);
572module_exit(clps711xuart_exit);
573
574MODULE_AUTHOR("Deep Blue Solutions Ltd");
575MODULE_DESCRIPTION("CLPS-711x generic serial driver");
576MODULE_LICENSE("GPL");
577MODULE_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..cf34d26ff6cd
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart.h
@@ -0,0 +1,145 @@
1/*
2 * Driver for CPM (SCC/SMC) serial ports
3 *
4 * Copyright (C) 2004 Freescale Semiconductor, Inc.
5 *
6 * 2006 (c) MontaVista Software, Inc.
7 * Vitaly Bordug <vbordug@ru.mvista.com>
8 *
9 * This file is licensed under the terms of the GNU General Public License
10 * version 2. This program is licensed "as is" without any warranty of any
11 * kind, whether express or implied.
12 *
13 */
14#ifndef CPM_UART_H
15#define CPM_UART_H
16
17#include <linux/platform_device.h>
18#include <linux/fs_uart_pd.h>
19
20#if defined(CONFIG_CPM2)
21#include "cpm_uart_cpm2.h"
22#elif defined(CONFIG_8xx)
23#include "cpm_uart_cpm1.h"
24#endif
25
26#define SERIAL_CPM_MAJOR 204
27#define SERIAL_CPM_MINOR 46
28
29#define IS_SMC(pinfo) (pinfo->flags & FLAG_SMC)
30#define IS_DISCARDING(pinfo) (pinfo->flags & FLAG_DISCARDING)
31#define FLAG_DISCARDING 0x00000004 /* when set, don't discard */
32#define FLAG_SMC 0x00000002
33#define FLAG_CONSOLE 0x00000001
34
35#define UART_SMC1 fsid_smc1_uart
36#define UART_SMC2 fsid_smc2_uart
37#define UART_SCC1 fsid_scc1_uart
38#define UART_SCC2 fsid_scc2_uart
39#define UART_SCC3 fsid_scc3_uart
40#define UART_SCC4 fsid_scc4_uart
41
42#define UART_NR fs_uart_nr
43
44#define RX_NUM_FIFO 4
45#define RX_BUF_SIZE 32
46#define TX_NUM_FIFO 4
47#define TX_BUF_SIZE 32
48
49#define SCC_WAIT_CLOSING 100
50
51#define GPIO_CTS 0
52#define GPIO_RTS 1
53#define GPIO_DCD 2
54#define GPIO_DSR 3
55#define GPIO_DTR 4
56#define GPIO_RI 5
57
58#define NUM_GPIOS (GPIO_RI+1)
59
60struct uart_cpm_port {
61 struct uart_port port;
62 u16 rx_nrfifos;
63 u16 rx_fifosize;
64 u16 tx_nrfifos;
65 u16 tx_fifosize;
66 smc_t __iomem *smcp;
67 smc_uart_t __iomem *smcup;
68 scc_t __iomem *sccp;
69 scc_uart_t __iomem *sccup;
70 cbd_t __iomem *rx_bd_base;
71 cbd_t __iomem *rx_cur;
72 cbd_t __iomem *tx_bd_base;
73 cbd_t __iomem *tx_cur;
74 unsigned char *tx_buf;
75 unsigned char *rx_buf;
76 u32 flags;
77 struct clk *clk;
78 u8 brg;
79 uint dp_addr;
80 void *mem_addr;
81 dma_addr_t dma_addr;
82 u32 mem_size;
83 /* wait on close if needed */
84 int wait_closing;
85 /* value to combine with opcode to form cpm command */
86 u32 command;
87 int gpios[NUM_GPIOS];
88};
89
90extern int cpm_uart_nr;
91extern struct uart_cpm_port cpm_uart_ports[UART_NR];
92
93/* these are located in their respective files */
94void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd);
95void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
96 struct device_node *np);
97void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram);
98int cpm_uart_init_portdesc(void);
99int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con);
100void cpm_uart_freebuf(struct uart_cpm_port *pinfo);
101
102void smc1_lineif(struct uart_cpm_port *pinfo);
103void smc2_lineif(struct uart_cpm_port *pinfo);
104void scc1_lineif(struct uart_cpm_port *pinfo);
105void scc2_lineif(struct uart_cpm_port *pinfo);
106void scc3_lineif(struct uart_cpm_port *pinfo);
107void scc4_lineif(struct uart_cpm_port *pinfo);
108
109/*
110 virtual to phys transtalion
111*/
112static inline unsigned long cpu2cpm_addr(void *addr,
113 struct uart_cpm_port *pinfo)
114{
115 int offset;
116 u32 val = (u32)addr;
117 u32 mem = (u32)pinfo->mem_addr;
118 /* sane check */
119 if (likely(val >= mem && val < mem + pinfo->mem_size)) {
120 offset = val - mem;
121 return pinfo->dma_addr + offset;
122 }
123 /* something nasty happened */
124 BUG();
125 return 0;
126}
127
128static inline void *cpm2cpu_addr(unsigned long addr,
129 struct uart_cpm_port *pinfo)
130{
131 int offset;
132 u32 val = addr;
133 u32 dma = (u32)pinfo->dma_addr;
134 /* sane check */
135 if (likely(val >= dma && val < dma + pinfo->mem_size)) {
136 offset = val - dma;
137 return pinfo->mem_addr + offset;
138 }
139 /* something nasty happened */
140 BUG();
141 return NULL;
142}
143
144
145#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..9488da74d4f7
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_core.c
@@ -0,0 +1,1440 @@
1/*
2 * Driver for CPM (SCC/SMC) serial ports; core driver
3 *
4 * Based on arch/ppc/cpm2_io/uart.c by Dan Malek
5 * Based on ppc8xx.c by Thomas Gleixner
6 * Based on drivers/serial/amba.c by Russell King
7 *
8 * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
9 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
10 *
11 * Copyright (C) 2004, 2007 Freescale Semiconductor, Inc.
12 * (C) 2004 Intracom, S.A.
13 * (C) 2005-2006 MontaVista Software, Inc.
14 * Vitaly Bordug <vbordug@ru.mvista.com>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 */
31
32#include <linux/module.h>
33#include <linux/tty.h>
34#include <linux/ioport.h>
35#include <linux/init.h>
36#include <linux/serial.h>
37#include <linux/console.h>
38#include <linux/sysrq.h>
39#include <linux/device.h>
40#include <linux/bootmem.h>
41#include <linux/dma-mapping.h>
42#include <linux/fs_uart_pd.h>
43#include <linux/of_platform.h>
44#include <linux/gpio.h>
45#include <linux/of_gpio.h>
46#include <linux/clk.h>
47
48#include <asm/io.h>
49#include <asm/irq.h>
50#include <asm/delay.h>
51#include <asm/fs_pd.h>
52#include <asm/udbg.h>
53
54#if defined(CONFIG_SERIAL_CPM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
55#define SUPPORT_SYSRQ
56#endif
57
58#include <linux/serial_core.h>
59#include <linux/kernel.h>
60
61#include "cpm_uart.h"
62
63
64/**************************************************************/
65
66static int cpm_uart_tx_pump(struct uart_port *port);
67static void cpm_uart_init_smc(struct uart_cpm_port *pinfo);
68static void cpm_uart_init_scc(struct uart_cpm_port *pinfo);
69static void cpm_uart_initbd(struct uart_cpm_port *pinfo);
70
71/**************************************************************/
72
73#define HW_BUF_SPD_THRESHOLD 9600
74
75/*
76 * Check, if transmit buffers are processed
77*/
78static unsigned int cpm_uart_tx_empty(struct uart_port *port)
79{
80 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
81 cbd_t __iomem *bdp = pinfo->tx_bd_base;
82 int ret = 0;
83
84 while (1) {
85 if (in_be16(&bdp->cbd_sc) & BD_SC_READY)
86 break;
87
88 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP) {
89 ret = TIOCSER_TEMT;
90 break;
91 }
92 bdp++;
93 }
94
95 pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
96
97 return ret;
98}
99
100static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
101{
102 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
103
104 if (pinfo->gpios[GPIO_RTS] >= 0)
105 gpio_set_value(pinfo->gpios[GPIO_RTS], !(mctrl & TIOCM_RTS));
106
107 if (pinfo->gpios[GPIO_DTR] >= 0)
108 gpio_set_value(pinfo->gpios[GPIO_DTR], !(mctrl & TIOCM_DTR));
109}
110
111static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
112{
113 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
114 unsigned int mctrl = TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
115
116 if (pinfo->gpios[GPIO_CTS] >= 0) {
117 if (gpio_get_value(pinfo->gpios[GPIO_CTS]))
118 mctrl &= ~TIOCM_CTS;
119 }
120
121 if (pinfo->gpios[GPIO_DSR] >= 0) {
122 if (gpio_get_value(pinfo->gpios[GPIO_DSR]))
123 mctrl &= ~TIOCM_DSR;
124 }
125
126 if (pinfo->gpios[GPIO_DCD] >= 0) {
127 if (gpio_get_value(pinfo->gpios[GPIO_DCD]))
128 mctrl &= ~TIOCM_CAR;
129 }
130
131 if (pinfo->gpios[GPIO_RI] >= 0) {
132 if (!gpio_get_value(pinfo->gpios[GPIO_RI]))
133 mctrl |= TIOCM_RNG;
134 }
135
136 return mctrl;
137}
138
139/*
140 * Stop transmitter
141 */
142static void cpm_uart_stop_tx(struct uart_port *port)
143{
144 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
145 smc_t __iomem *smcp = pinfo->smcp;
146 scc_t __iomem *sccp = pinfo->sccp;
147
148 pr_debug("CPM uart[%d]:stop tx\n", port->line);
149
150 if (IS_SMC(pinfo))
151 clrbits8(&smcp->smc_smcm, SMCM_TX);
152 else
153 clrbits16(&sccp->scc_sccm, UART_SCCM_TX);
154}
155
156/*
157 * Start transmitter
158 */
159static void cpm_uart_start_tx(struct uart_port *port)
160{
161 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
162 smc_t __iomem *smcp = pinfo->smcp;
163 scc_t __iomem *sccp = pinfo->sccp;
164
165 pr_debug("CPM uart[%d]:start tx\n", port->line);
166
167 if (IS_SMC(pinfo)) {
168 if (in_8(&smcp->smc_smcm) & SMCM_TX)
169 return;
170 } else {
171 if (in_be16(&sccp->scc_sccm) & UART_SCCM_TX)
172 return;
173 }
174
175 if (cpm_uart_tx_pump(port) != 0) {
176 if (IS_SMC(pinfo)) {
177 setbits8(&smcp->smc_smcm, SMCM_TX);
178 } else {
179 setbits16(&sccp->scc_sccm, UART_SCCM_TX);
180 }
181 }
182}
183
184/*
185 * Stop receiver
186 */
187static void cpm_uart_stop_rx(struct uart_port *port)
188{
189 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
190 smc_t __iomem *smcp = pinfo->smcp;
191 scc_t __iomem *sccp = pinfo->sccp;
192
193 pr_debug("CPM uart[%d]:stop rx\n", port->line);
194
195 if (IS_SMC(pinfo))
196 clrbits8(&smcp->smc_smcm, SMCM_RX);
197 else
198 clrbits16(&sccp->scc_sccm, UART_SCCM_RX);
199}
200
201/*
202 * Enable Modem status interrupts
203 */
204static void cpm_uart_enable_ms(struct uart_port *port)
205{
206 pr_debug("CPM uart[%d]:enable ms\n", port->line);
207}
208
209/*
210 * Generate a break.
211 */
212static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
213{
214 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
215
216 pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
217 break_state);
218
219 if (break_state)
220 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
221 else
222 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
223}
224
225/*
226 * Transmit characters, refill buffer descriptor, if possible
227 */
228static void cpm_uart_int_tx(struct uart_port *port)
229{
230 pr_debug("CPM uart[%d]:TX INT\n", port->line);
231
232 cpm_uart_tx_pump(port);
233}
234
235#ifdef CONFIG_CONSOLE_POLL
236static int serial_polled;
237#endif
238
239/*
240 * Receive characters
241 */
242static void cpm_uart_int_rx(struct uart_port *port)
243{
244 int i;
245 unsigned char ch;
246 u8 *cp;
247 struct tty_struct *tty = port->state->port.tty;
248 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
249 cbd_t __iomem *bdp;
250 u16 status;
251 unsigned int flg;
252
253 pr_debug("CPM uart[%d]:RX INT\n", port->line);
254
255 /* Just loop through the closed BDs and copy the characters into
256 * the buffer.
257 */
258 bdp = pinfo->rx_cur;
259 for (;;) {
260#ifdef CONFIG_CONSOLE_POLL
261 if (unlikely(serial_polled)) {
262 serial_polled = 0;
263 return;
264 }
265#endif
266 /* get status */
267 status = in_be16(&bdp->cbd_sc);
268 /* If this one is empty, return happy */
269 if (status & BD_SC_EMPTY)
270 break;
271
272 /* get number of characters, and check spce in flip-buffer */
273 i = in_be16(&bdp->cbd_datlen);
274
275 /* If we have not enough room in tty flip buffer, then we try
276 * later, which will be the next rx-interrupt or a timeout
277 */
278 if(tty_buffer_request_room(tty, i) < i) {
279 printk(KERN_WARNING "No room in flip buffer\n");
280 return;
281 }
282
283 /* get pointer */
284 cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
285
286 /* loop through the buffer */
287 while (i-- > 0) {
288 ch = *cp++;
289 port->icount.rx++;
290 flg = TTY_NORMAL;
291
292 if (status &
293 (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
294 goto handle_error;
295 if (uart_handle_sysrq_char(port, ch))
296 continue;
297#ifdef CONFIG_CONSOLE_POLL
298 if (unlikely(serial_polled)) {
299 serial_polled = 0;
300 return;
301 }
302#endif
303 error_return:
304 tty_insert_flip_char(tty, ch, flg);
305
306 } /* End while (i--) */
307
308 /* This BD is ready to be used again. Clear status. get next */
309 clrbits16(&bdp->cbd_sc, BD_SC_BR | BD_SC_FR | BD_SC_PR |
310 BD_SC_OV | BD_SC_ID);
311 setbits16(&bdp->cbd_sc, BD_SC_EMPTY);
312
313 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
314 bdp = pinfo->rx_bd_base;
315 else
316 bdp++;
317
318 } /* End for (;;) */
319
320 /* Write back buffer pointer */
321 pinfo->rx_cur = bdp;
322
323 /* activate BH processing */
324 tty_flip_buffer_push(tty);
325
326 return;
327
328 /* Error processing */
329
330 handle_error:
331 /* Statistics */
332 if (status & BD_SC_BR)
333 port->icount.brk++;
334 if (status & BD_SC_PR)
335 port->icount.parity++;
336 if (status & BD_SC_FR)
337 port->icount.frame++;
338 if (status & BD_SC_OV)
339 port->icount.overrun++;
340
341 /* Mask out ignored conditions */
342 status &= port->read_status_mask;
343
344 /* Handle the remaining ones */
345 if (status & BD_SC_BR)
346 flg = TTY_BREAK;
347 else if (status & BD_SC_PR)
348 flg = TTY_PARITY;
349 else if (status & BD_SC_FR)
350 flg = TTY_FRAME;
351
352 /* overrun does not affect the current character ! */
353 if (status & BD_SC_OV) {
354 ch = 0;
355 flg = TTY_OVERRUN;
356 /* We skip this buffer */
357 /* CHECK: Is really nothing senseful there */
358 /* ASSUMPTION: it contains nothing valid */
359 i = 0;
360 }
361#ifdef SUPPORT_SYSRQ
362 port->sysrq = 0;
363#endif
364 goto error_return;
365}
366
367/*
368 * Asynchron mode interrupt handler
369 */
370static irqreturn_t cpm_uart_int(int irq, void *data)
371{
372 u8 events;
373 struct uart_port *port = data;
374 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
375 smc_t __iomem *smcp = pinfo->smcp;
376 scc_t __iomem *sccp = pinfo->sccp;
377
378 pr_debug("CPM uart[%d]:IRQ\n", port->line);
379
380 if (IS_SMC(pinfo)) {
381 events = in_8(&smcp->smc_smce);
382 out_8(&smcp->smc_smce, events);
383 if (events & SMCM_BRKE)
384 uart_handle_break(port);
385 if (events & SMCM_RX)
386 cpm_uart_int_rx(port);
387 if (events & SMCM_TX)
388 cpm_uart_int_tx(port);
389 } else {
390 events = in_be16(&sccp->scc_scce);
391 out_be16(&sccp->scc_scce, events);
392 if (events & UART_SCCM_BRKE)
393 uart_handle_break(port);
394 if (events & UART_SCCM_RX)
395 cpm_uart_int_rx(port);
396 if (events & UART_SCCM_TX)
397 cpm_uart_int_tx(port);
398 }
399 return (events) ? IRQ_HANDLED : IRQ_NONE;
400}
401
402static int cpm_uart_startup(struct uart_port *port)
403{
404 int retval;
405 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
406
407 pr_debug("CPM uart[%d]:startup\n", port->line);
408
409 /* If the port is not the console, make sure rx is disabled. */
410 if (!(pinfo->flags & FLAG_CONSOLE)) {
411 /* Disable UART rx */
412 if (IS_SMC(pinfo)) {
413 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN);
414 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
415 } else {
416 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR);
417 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
418 }
419 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
420 }
421 /* Install interrupt handler. */
422 retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
423 if (retval)
424 return retval;
425
426 /* Startup rx-int */
427 if (IS_SMC(pinfo)) {
428 setbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
429 setbits16(&pinfo->smcp->smc_smcmr, (SMCMR_REN | SMCMR_TEN));
430 } else {
431 setbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
432 setbits32(&pinfo->sccp->scc_gsmrl, (SCC_GSMRL_ENR | SCC_GSMRL_ENT));
433 }
434
435 return 0;
436}
437
438inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
439{
440 set_current_state(TASK_UNINTERRUPTIBLE);
441 schedule_timeout(pinfo->wait_closing);
442}
443
444/*
445 * Shutdown the uart
446 */
447static void cpm_uart_shutdown(struct uart_port *port)
448{
449 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
450
451 pr_debug("CPM uart[%d]:shutdown\n", port->line);
452
453 /* free interrupt handler */
454 free_irq(port->irq, port);
455
456 /* If the port is not the console, disable Rx and Tx. */
457 if (!(pinfo->flags & FLAG_CONSOLE)) {
458 /* Wait for all the BDs marked sent */
459 while(!cpm_uart_tx_empty(port)) {
460 set_current_state(TASK_UNINTERRUPTIBLE);
461 schedule_timeout(2);
462 }
463
464 if (pinfo->wait_closing)
465 cpm_uart_wait_until_send(pinfo);
466
467 /* Stop uarts */
468 if (IS_SMC(pinfo)) {
469 smc_t __iomem *smcp = pinfo->smcp;
470 clrbits16(&smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
471 clrbits8(&smcp->smc_smcm, SMCM_RX | SMCM_TX);
472 } else {
473 scc_t __iomem *sccp = pinfo->sccp;
474 clrbits32(&sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
475 clrbits16(&sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
476 }
477
478 /* Shut them really down and reinit buffer descriptors */
479 if (IS_SMC(pinfo)) {
480 out_be16(&pinfo->smcup->smc_brkcr, 0);
481 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
482 } else {
483 out_be16(&pinfo->sccup->scc_brkcr, 0);
484 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
485 }
486
487 cpm_uart_initbd(pinfo);
488 }
489}
490
491static void cpm_uart_set_termios(struct uart_port *port,
492 struct ktermios *termios,
493 struct ktermios *old)
494{
495 int baud;
496 unsigned long flags;
497 u16 cval, scval, prev_mode;
498 int bits, sbits;
499 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
500 smc_t __iomem *smcp = pinfo->smcp;
501 scc_t __iomem *sccp = pinfo->sccp;
502
503 pr_debug("CPM uart[%d]:set_termios\n", port->line);
504
505 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
506 if (baud <= HW_BUF_SPD_THRESHOLD ||
507 (pinfo->port.state && pinfo->port.state->port.tty->low_latency))
508 pinfo->rx_fifosize = 1;
509 else
510 pinfo->rx_fifosize = RX_BUF_SIZE;
511
512 /* Character length programmed into the mode register is the
513 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
514 * 1 or 2 stop bits, minus 1.
515 * The value 'bits' counts this for us.
516 */
517 cval = 0;
518 scval = 0;
519
520 /* byte size */
521 switch (termios->c_cflag & CSIZE) {
522 case CS5:
523 bits = 5;
524 break;
525 case CS6:
526 bits = 6;
527 break;
528 case CS7:
529 bits = 7;
530 break;
531 case CS8:
532 bits = 8;
533 break;
534 /* Never happens, but GCC is too dumb to figure it out */
535 default:
536 bits = 8;
537 break;
538 }
539 sbits = bits - 5;
540
541 if (termios->c_cflag & CSTOPB) {
542 cval |= SMCMR_SL; /* Two stops */
543 scval |= SCU_PSMR_SL;
544 bits++;
545 }
546
547 if (termios->c_cflag & PARENB) {
548 cval |= SMCMR_PEN;
549 scval |= SCU_PSMR_PEN;
550 bits++;
551 if (!(termios->c_cflag & PARODD)) {
552 cval |= SMCMR_PM_EVEN;
553 scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
554 }
555 }
556
557 /*
558 * Update the timeout
559 */
560 uart_update_timeout(port, termios->c_cflag, baud);
561
562 /*
563 * Set up parity check flag
564 */
565#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
566
567 port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
568 if (termios->c_iflag & INPCK)
569 port->read_status_mask |= BD_SC_FR | BD_SC_PR;
570 if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
571 port->read_status_mask |= BD_SC_BR;
572
573 /*
574 * Characters to ignore
575 */
576 port->ignore_status_mask = 0;
577 if (termios->c_iflag & IGNPAR)
578 port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
579 if (termios->c_iflag & IGNBRK) {
580 port->ignore_status_mask |= BD_SC_BR;
581 /*
582 * If we're ignore parity and break indicators, ignore
583 * overruns too. (For real raw support).
584 */
585 if (termios->c_iflag & IGNPAR)
586 port->ignore_status_mask |= BD_SC_OV;
587 }
588 /*
589 * !!! ignore all characters if CREAD is not set
590 */
591 if ((termios->c_cflag & CREAD) == 0)
592 port->read_status_mask &= ~BD_SC_EMPTY;
593
594 spin_lock_irqsave(&port->lock, flags);
595
596 /* Start bit has not been added (so don't, because we would just
597 * subtract it later), and we need to add one for the number of
598 * stops bits (there is always at least one).
599 */
600 bits++;
601 if (IS_SMC(pinfo)) {
602 /*
603 * MRBLR can be changed while an SMC/SCC is operating only
604 * if it is done in a single bus cycle with one 16-bit move
605 * (not two 8-bit bus cycles back-to-back). This occurs when
606 * the cp shifts control to the next RxBD, so the change does
607 * not take effect immediately. To guarantee the exact RxBD
608 * on which the change occurs, change MRBLR only while the
609 * SMC/SCC receiver is disabled.
610 */
611 out_be16(&pinfo->smcup->smc_mrblr, pinfo->rx_fifosize);
612
613 /* Set the mode register. We want to keep a copy of the
614 * enables, because we want to put them back if they were
615 * present.
616 */
617 prev_mode = in_be16(&smcp->smc_smcmr) & (SMCMR_REN | SMCMR_TEN);
618 /* Output in *one* operation, so we don't interrupt RX/TX if they
619 * were already enabled. */
620 out_be16(&smcp->smc_smcmr, smcr_mk_clen(bits) | cval |
621 SMCMR_SM_UART | prev_mode);
622 } else {
623 out_be16(&pinfo->sccup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
624 out_be16(&sccp->scc_psmr, (sbits << 12) | scval);
625 }
626
627 if (pinfo->clk)
628 clk_set_rate(pinfo->clk, baud);
629 else
630 cpm_set_brg(pinfo->brg - 1, baud);
631 spin_unlock_irqrestore(&port->lock, flags);
632}
633
634static const char *cpm_uart_type(struct uart_port *port)
635{
636 pr_debug("CPM uart[%d]:uart_type\n", port->line);
637
638 return port->type == PORT_CPM ? "CPM UART" : NULL;
639}
640
641/*
642 * verify the new serial_struct (for TIOCSSERIAL).
643 */
644static int cpm_uart_verify_port(struct uart_port *port,
645 struct serial_struct *ser)
646{
647 int ret = 0;
648
649 pr_debug("CPM uart[%d]:verify_port\n", port->line);
650
651 if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
652 ret = -EINVAL;
653 if (ser->irq < 0 || ser->irq >= nr_irqs)
654 ret = -EINVAL;
655 if (ser->baud_base < 9600)
656 ret = -EINVAL;
657 return ret;
658}
659
660/*
661 * Transmit characters, refill buffer descriptor, if possible
662 */
663static int cpm_uart_tx_pump(struct uart_port *port)
664{
665 cbd_t __iomem *bdp;
666 u8 *p;
667 int count;
668 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
669 struct circ_buf *xmit = &port->state->xmit;
670
671 /* Handle xon/xoff */
672 if (port->x_char) {
673 /* Pick next descriptor and fill from buffer */
674 bdp = pinfo->tx_cur;
675
676 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
677
678 *p++ = port->x_char;
679
680 out_be16(&bdp->cbd_datlen, 1);
681 setbits16(&bdp->cbd_sc, BD_SC_READY);
682 /* Get next BD. */
683 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
684 bdp = pinfo->tx_bd_base;
685 else
686 bdp++;
687 pinfo->tx_cur = bdp;
688
689 port->icount.tx++;
690 port->x_char = 0;
691 return 1;
692 }
693
694 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
695 cpm_uart_stop_tx(port);
696 return 0;
697 }
698
699 /* Pick next descriptor and fill from buffer */
700 bdp = pinfo->tx_cur;
701
702 while (!(in_be16(&bdp->cbd_sc) & BD_SC_READY) &&
703 xmit->tail != xmit->head) {
704 count = 0;
705 p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
706 while (count < pinfo->tx_fifosize) {
707 *p++ = xmit->buf[xmit->tail];
708 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
709 port->icount.tx++;
710 count++;
711 if (xmit->head == xmit->tail)
712 break;
713 }
714 out_be16(&bdp->cbd_datlen, count);
715 setbits16(&bdp->cbd_sc, BD_SC_READY);
716 /* Get next BD. */
717 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
718 bdp = pinfo->tx_bd_base;
719 else
720 bdp++;
721 }
722 pinfo->tx_cur = bdp;
723
724 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
725 uart_write_wakeup(port);
726
727 if (uart_circ_empty(xmit)) {
728 cpm_uart_stop_tx(port);
729 return 0;
730 }
731
732 return 1;
733}
734
735/*
736 * init buffer descriptors
737 */
738static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
739{
740 int i;
741 u8 *mem_addr;
742 cbd_t __iomem *bdp;
743
744 pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
745
746 /* Set the physical address of the host memory
747 * buffers in the buffer descriptors, and the
748 * virtual address for us to work with.
749 */
750 mem_addr = pinfo->mem_addr;
751 bdp = pinfo->rx_cur = pinfo->rx_bd_base;
752 for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
753 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
754 out_be16(&bdp->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
755 mem_addr += pinfo->rx_fifosize;
756 }
757
758 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
759 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
760
761 /* Set the physical address of the host memory
762 * buffers in the buffer descriptors, and the
763 * virtual address for us to work with.
764 */
765 mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
766 bdp = pinfo->tx_cur = pinfo->tx_bd_base;
767 for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
768 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
769 out_be16(&bdp->cbd_sc, BD_SC_INTRPT);
770 mem_addr += pinfo->tx_fifosize;
771 }
772
773 out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
774 out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_INTRPT);
775}
776
777static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
778{
779 scc_t __iomem *scp;
780 scc_uart_t __iomem *sup;
781
782 pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
783
784 scp = pinfo->sccp;
785 sup = pinfo->sccup;
786
787 /* Store address */
788 out_be16(&pinfo->sccup->scc_genscc.scc_rbase,
789 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
790 out_be16(&pinfo->sccup->scc_genscc.scc_tbase,
791 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
792
793 /* Set up the uart parameters in the
794 * parameter ram.
795 */
796
797 cpm_set_scc_fcr(sup);
798
799 out_be16(&sup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
800 out_be16(&sup->scc_maxidl, pinfo->rx_fifosize);
801 out_be16(&sup->scc_brkcr, 1);
802 out_be16(&sup->scc_parec, 0);
803 out_be16(&sup->scc_frmec, 0);
804 out_be16(&sup->scc_nosec, 0);
805 out_be16(&sup->scc_brkec, 0);
806 out_be16(&sup->scc_uaddr1, 0);
807 out_be16(&sup->scc_uaddr2, 0);
808 out_be16(&sup->scc_toseq, 0);
809 out_be16(&sup->scc_char1, 0x8000);
810 out_be16(&sup->scc_char2, 0x8000);
811 out_be16(&sup->scc_char3, 0x8000);
812 out_be16(&sup->scc_char4, 0x8000);
813 out_be16(&sup->scc_char5, 0x8000);
814 out_be16(&sup->scc_char6, 0x8000);
815 out_be16(&sup->scc_char7, 0x8000);
816 out_be16(&sup->scc_char8, 0x8000);
817 out_be16(&sup->scc_rccm, 0xc0ff);
818
819 /* Send the CPM an initialize command.
820 */
821 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
822
823 /* Set UART mode, 8 bit, no parity, one stop.
824 * Enable receive and transmit.
825 */
826 out_be32(&scp->scc_gsmrh, 0);
827 out_be32(&scp->scc_gsmrl,
828 SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
829
830 /* Enable rx interrupts and clear all pending events. */
831 out_be16(&scp->scc_sccm, 0);
832 out_be16(&scp->scc_scce, 0xffff);
833 out_be16(&scp->scc_dsr, 0x7e7e);
834 out_be16(&scp->scc_psmr, 0x3000);
835
836 setbits32(&scp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
837}
838
839static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
840{
841 smc_t __iomem *sp;
842 smc_uart_t __iomem *up;
843
844 pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
845
846 sp = pinfo->smcp;
847 up = pinfo->smcup;
848
849 /* Store address */
850 out_be16(&pinfo->smcup->smc_rbase,
851 (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
852 out_be16(&pinfo->smcup->smc_tbase,
853 (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
854
855/*
856 * In case SMC1 is being relocated...
857 */
858#if defined (CONFIG_I2C_SPI_SMC1_UCODE_PATCH)
859 out_be16(&up->smc_rbptr, in_be16(&pinfo->smcup->smc_rbase));
860 out_be16(&up->smc_tbptr, in_be16(&pinfo->smcup->smc_tbase));
861 out_be32(&up->smc_rstate, 0);
862 out_be32(&up->smc_tstate, 0);
863 out_be16(&up->smc_brkcr, 1); /* number of break chars */
864 out_be16(&up->smc_brkec, 0);
865#endif
866
867 /* Set up the uart parameters in the
868 * parameter ram.
869 */
870 cpm_set_smc_fcr(up);
871
872 /* Using idle character time requires some additional tuning. */
873 out_be16(&up->smc_mrblr, pinfo->rx_fifosize);
874 out_be16(&up->smc_maxidl, pinfo->rx_fifosize);
875 out_be16(&up->smc_brklen, 0);
876 out_be16(&up->smc_brkec, 0);
877 out_be16(&up->smc_brkcr, 1);
878
879 cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
880
881 /* Set UART mode, 8 bit, no parity, one stop.
882 * Enable receive and transmit.
883 */
884 out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
885
886 /* Enable only rx interrupts clear all pending events. */
887 out_8(&sp->smc_smcm, 0);
888 out_8(&sp->smc_smce, 0xff);
889
890 setbits16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
891}
892
893/*
894 * Initialize port. This is called from early_console stuff
895 * so we have to be careful here !
896 */
897static int cpm_uart_request_port(struct uart_port *port)
898{
899 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
900 int ret;
901
902 pr_debug("CPM uart[%d]:request port\n", port->line);
903
904 if (pinfo->flags & FLAG_CONSOLE)
905 return 0;
906
907 if (IS_SMC(pinfo)) {
908 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
909 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
910 } else {
911 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
912 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
913 }
914
915 ret = cpm_uart_allocbuf(pinfo, 0);
916
917 if (ret)
918 return ret;
919
920 cpm_uart_initbd(pinfo);
921 if (IS_SMC(pinfo))
922 cpm_uart_init_smc(pinfo);
923 else
924 cpm_uart_init_scc(pinfo);
925
926 return 0;
927}
928
929static void cpm_uart_release_port(struct uart_port *port)
930{
931 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
932
933 if (!(pinfo->flags & FLAG_CONSOLE))
934 cpm_uart_freebuf(pinfo);
935}
936
937/*
938 * Configure/autoconfigure the port.
939 */
940static void cpm_uart_config_port(struct uart_port *port, int flags)
941{
942 pr_debug("CPM uart[%d]:config_port\n", port->line);
943
944 if (flags & UART_CONFIG_TYPE) {
945 port->type = PORT_CPM;
946 cpm_uart_request_port(port);
947 }
948}
949
950#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_CPM_CONSOLE)
951/*
952 * Write a string to the serial port
953 * Note that this is called with interrupts already disabled
954 */
955static void cpm_uart_early_write(struct uart_cpm_port *pinfo,
956 const char *string, u_int count)
957{
958 unsigned int i;
959 cbd_t __iomem *bdp, *bdbase;
960 unsigned char *cpm_outp_addr;
961
962 /* Get the address of the host memory buffer.
963 */
964 bdp = pinfo->tx_cur;
965 bdbase = pinfo->tx_bd_base;
966
967 /*
968 * Now, do each character. This is not as bad as it looks
969 * since this is a holding FIFO and not a transmitting FIFO.
970 * We could add the complexity of filling the entire transmit
971 * buffer, but we would just wait longer between accesses......
972 */
973 for (i = 0; i < count; i++, string++) {
974 /* Wait for transmitter fifo to empty.
975 * Ready indicates output is ready, and xmt is doing
976 * that, not that it is ready for us to send.
977 */
978 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
979 ;
980
981 /* Send the character out.
982 * If the buffer address is in the CPM DPRAM, don't
983 * convert it.
984 */
985 cpm_outp_addr = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr),
986 pinfo);
987 *cpm_outp_addr = *string;
988
989 out_be16(&bdp->cbd_datlen, 1);
990 setbits16(&bdp->cbd_sc, BD_SC_READY);
991
992 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
993 bdp = bdbase;
994 else
995 bdp++;
996
997 /* if a LF, also do CR... */
998 if (*string == 10) {
999 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1000 ;
1001
1002 cpm_outp_addr = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr),
1003 pinfo);
1004 *cpm_outp_addr = 13;
1005
1006 out_be16(&bdp->cbd_datlen, 1);
1007 setbits16(&bdp->cbd_sc, BD_SC_READY);
1008
1009 if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
1010 bdp = bdbase;
1011 else
1012 bdp++;
1013 }
1014 }
1015
1016 /*
1017 * Finally, Wait for transmitter & holding register to empty
1018 * and restore the IER
1019 */
1020 while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
1021 ;
1022
1023 pinfo->tx_cur = bdp;
1024}
1025#endif
1026
1027#ifdef CONFIG_CONSOLE_POLL
1028/* Serial polling routines for writing and reading from the uart while
1029 * in an interrupt or debug context.
1030 */
1031
1032#define GDB_BUF_SIZE 512 /* power of 2, please */
1033
1034static char poll_buf[GDB_BUF_SIZE];
1035static char *pollp;
1036static int poll_chars;
1037
1038static int poll_wait_key(char *obuf, struct uart_cpm_port *pinfo)
1039{
1040 u_char c, *cp;
1041 volatile cbd_t *bdp;
1042 int i;
1043
1044 /* Get the address of the host memory buffer.
1045 */
1046 bdp = pinfo->rx_cur;
1047 while (bdp->cbd_sc & BD_SC_EMPTY)
1048 ;
1049
1050 /* If the buffer address is in the CPM DPRAM, don't
1051 * convert it.
1052 */
1053 cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
1054
1055 if (obuf) {
1056 i = c = bdp->cbd_datlen;
1057 while (i-- > 0)
1058 *obuf++ = *cp++;
1059 } else
1060 c = *cp;
1061 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
1062 bdp->cbd_sc |= BD_SC_EMPTY;
1063
1064 if (bdp->cbd_sc & BD_SC_WRAP)
1065 bdp = pinfo->rx_bd_base;
1066 else
1067 bdp++;
1068 pinfo->rx_cur = (cbd_t *)bdp;
1069
1070 return (int)c;
1071}
1072
1073static int cpm_get_poll_char(struct uart_port *port)
1074{
1075 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
1076
1077 if (!serial_polled) {
1078 serial_polled = 1;
1079 poll_chars = 0;
1080 }
1081 if (poll_chars <= 0) {
1082 poll_chars = poll_wait_key(poll_buf, pinfo);
1083 pollp = poll_buf;
1084 }
1085 poll_chars--;
1086 return *pollp++;
1087}
1088
1089static void cpm_put_poll_char(struct uart_port *port,
1090 unsigned char c)
1091{
1092 struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
1093 static char ch[2];
1094
1095 ch[0] = (char)c;
1096 cpm_uart_early_write(pinfo, ch, 1);
1097}
1098#endif /* CONFIG_CONSOLE_POLL */
1099
1100static struct uart_ops cpm_uart_pops = {
1101 .tx_empty = cpm_uart_tx_empty,
1102 .set_mctrl = cpm_uart_set_mctrl,
1103 .get_mctrl = cpm_uart_get_mctrl,
1104 .stop_tx = cpm_uart_stop_tx,
1105 .start_tx = cpm_uart_start_tx,
1106 .stop_rx = cpm_uart_stop_rx,
1107 .enable_ms = cpm_uart_enable_ms,
1108 .break_ctl = cpm_uart_break_ctl,
1109 .startup = cpm_uart_startup,
1110 .shutdown = cpm_uart_shutdown,
1111 .set_termios = cpm_uart_set_termios,
1112 .type = cpm_uart_type,
1113 .release_port = cpm_uart_release_port,
1114 .request_port = cpm_uart_request_port,
1115 .config_port = cpm_uart_config_port,
1116 .verify_port = cpm_uart_verify_port,
1117#ifdef CONFIG_CONSOLE_POLL
1118 .poll_get_char = cpm_get_poll_char,
1119 .poll_put_char = cpm_put_poll_char,
1120#endif
1121};
1122
1123struct uart_cpm_port cpm_uart_ports[UART_NR];
1124
1125static int cpm_uart_init_port(struct device_node *np,
1126 struct uart_cpm_port *pinfo)
1127{
1128 const u32 *data;
1129 void __iomem *mem, *pram;
1130 int len;
1131 int ret;
1132 int i;
1133
1134 data = of_get_property(np, "clock", NULL);
1135 if (data) {
1136 struct clk *clk = clk_get(NULL, (const char*)data);
1137 if (!IS_ERR(clk))
1138 pinfo->clk = clk;
1139 }
1140 if (!pinfo->clk) {
1141 data = of_get_property(np, "fsl,cpm-brg", &len);
1142 if (!data || len != 4) {
1143 printk(KERN_ERR "CPM UART %s has no/invalid "
1144 "fsl,cpm-brg property.\n", np->name);
1145 return -EINVAL;
1146 }
1147 pinfo->brg = *data;
1148 }
1149
1150 data = of_get_property(np, "fsl,cpm-command", &len);
1151 if (!data || len != 4) {
1152 printk(KERN_ERR "CPM UART %s has no/invalid "
1153 "fsl,cpm-command property.\n", np->name);
1154 return -EINVAL;
1155 }
1156 pinfo->command = *data;
1157
1158 mem = of_iomap(np, 0);
1159 if (!mem)
1160 return -ENOMEM;
1161
1162 if (of_device_is_compatible(np, "fsl,cpm1-scc-uart") ||
1163 of_device_is_compatible(np, "fsl,cpm2-scc-uart")) {
1164 pinfo->sccp = mem;
1165 pinfo->sccup = pram = cpm_uart_map_pram(pinfo, np);
1166 } else if (of_device_is_compatible(np, "fsl,cpm1-smc-uart") ||
1167 of_device_is_compatible(np, "fsl,cpm2-smc-uart")) {
1168 pinfo->flags |= FLAG_SMC;
1169 pinfo->smcp = mem;
1170 pinfo->smcup = pram = cpm_uart_map_pram(pinfo, np);
1171 } else {
1172 ret = -ENODEV;
1173 goto out_mem;
1174 }
1175
1176 if (!pram) {
1177 ret = -ENOMEM;
1178 goto out_mem;
1179 }
1180
1181 pinfo->tx_nrfifos = TX_NUM_FIFO;
1182 pinfo->tx_fifosize = TX_BUF_SIZE;
1183 pinfo->rx_nrfifos = RX_NUM_FIFO;
1184 pinfo->rx_fifosize = RX_BUF_SIZE;
1185
1186 pinfo->port.uartclk = ppc_proc_freq;
1187 pinfo->port.mapbase = (unsigned long)mem;
1188 pinfo->port.type = PORT_CPM;
1189 pinfo->port.ops = &cpm_uart_pops,
1190 pinfo->port.iotype = UPIO_MEM;
1191 pinfo->port.fifosize = pinfo->tx_nrfifos * pinfo->tx_fifosize;
1192 spin_lock_init(&pinfo->port.lock);
1193
1194 pinfo->port.irq = of_irq_to_resource(np, 0, NULL);
1195 if (pinfo->port.irq == NO_IRQ) {
1196 ret = -EINVAL;
1197 goto out_pram;
1198 }
1199
1200 for (i = 0; i < NUM_GPIOS; i++)
1201 pinfo->gpios[i] = of_get_gpio(np, i);
1202
1203#ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1204 udbg_putc = NULL;
1205#endif
1206
1207 return cpm_uart_request_port(&pinfo->port);
1208
1209out_pram:
1210 cpm_uart_unmap_pram(pinfo, pram);
1211out_mem:
1212 iounmap(mem);
1213 return ret;
1214}
1215
1216#ifdef CONFIG_SERIAL_CPM_CONSOLE
1217/*
1218 * Print a string to the serial port trying not to disturb
1219 * any possible real use of the port...
1220 *
1221 * Note that this is called with interrupts already disabled
1222 */
1223static void cpm_uart_console_write(struct console *co, const char *s,
1224 u_int count)
1225{
1226 struct uart_cpm_port *pinfo = &cpm_uart_ports[co->index];
1227 unsigned long flags;
1228 int nolock = oops_in_progress;
1229
1230 if (unlikely(nolock)) {
1231 local_irq_save(flags);
1232 } else {
1233 spin_lock_irqsave(&pinfo->port.lock, flags);
1234 }
1235
1236 cpm_uart_early_write(pinfo, s, count);
1237
1238 if (unlikely(nolock)) {
1239 local_irq_restore(flags);
1240 } else {
1241 spin_unlock_irqrestore(&pinfo->port.lock, flags);
1242 }
1243}
1244
1245
1246static int __init cpm_uart_console_setup(struct console *co, char *options)
1247{
1248 int baud = 38400;
1249 int bits = 8;
1250 int parity = 'n';
1251 int flow = 'n';
1252 int ret;
1253 struct uart_cpm_port *pinfo;
1254 struct uart_port *port;
1255
1256 struct device_node *np = NULL;
1257 int i = 0;
1258
1259 if (co->index >= UART_NR) {
1260 printk(KERN_ERR "cpm_uart: console index %d too high\n",
1261 co->index);
1262 return -ENODEV;
1263 }
1264
1265 do {
1266 np = of_find_node_by_type(np, "serial");
1267 if (!np)
1268 return -ENODEV;
1269
1270 if (!of_device_is_compatible(np, "fsl,cpm1-smc-uart") &&
1271 !of_device_is_compatible(np, "fsl,cpm1-scc-uart") &&
1272 !of_device_is_compatible(np, "fsl,cpm2-smc-uart") &&
1273 !of_device_is_compatible(np, "fsl,cpm2-scc-uart"))
1274 i--;
1275 } while (i++ != co->index);
1276
1277 pinfo = &cpm_uart_ports[co->index];
1278
1279 pinfo->flags |= FLAG_CONSOLE;
1280 port = &pinfo->port;
1281
1282 ret = cpm_uart_init_port(np, pinfo);
1283 of_node_put(np);
1284 if (ret)
1285 return ret;
1286
1287 if (options) {
1288 uart_parse_options(options, &baud, &parity, &bits, &flow);
1289 } else {
1290 if ((baud = uart_baudrate()) == -1)
1291 baud = 9600;
1292 }
1293
1294 if (IS_SMC(pinfo)) {
1295 out_be16(&pinfo->smcup->smc_brkcr, 0);
1296 cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
1297 clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
1298 clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
1299 } else {
1300 out_be16(&pinfo->sccup->scc_brkcr, 0);
1301 cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
1302 clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
1303 clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
1304 }
1305
1306 ret = cpm_uart_allocbuf(pinfo, 1);
1307
1308 if (ret)
1309 return ret;
1310
1311 cpm_uart_initbd(pinfo);
1312
1313 if (IS_SMC(pinfo))
1314 cpm_uart_init_smc(pinfo);
1315 else
1316 cpm_uart_init_scc(pinfo);
1317
1318 uart_set_options(port, co, baud, parity, bits, flow);
1319 cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
1320
1321 return 0;
1322}
1323
1324static struct uart_driver cpm_reg;
1325static struct console cpm_scc_uart_console = {
1326 .name = "ttyCPM",
1327 .write = cpm_uart_console_write,
1328 .device = uart_console_device,
1329 .setup = cpm_uart_console_setup,
1330 .flags = CON_PRINTBUFFER,
1331 .index = -1,
1332 .data = &cpm_reg,
1333};
1334
1335static int __init cpm_uart_console_init(void)
1336{
1337 register_console(&cpm_scc_uart_console);
1338 return 0;
1339}
1340
1341console_initcall(cpm_uart_console_init);
1342
1343#define CPM_UART_CONSOLE &cpm_scc_uart_console
1344#else
1345#define CPM_UART_CONSOLE NULL
1346#endif
1347
1348static struct uart_driver cpm_reg = {
1349 .owner = THIS_MODULE,
1350 .driver_name = "ttyCPM",
1351 .dev_name = "ttyCPM",
1352 .major = SERIAL_CPM_MAJOR,
1353 .minor = SERIAL_CPM_MINOR,
1354 .cons = CPM_UART_CONSOLE,
1355 .nr = UART_NR,
1356};
1357
1358static int probe_index;
1359
1360static int __devinit cpm_uart_probe(struct platform_device *ofdev)
1361{
1362 int index = probe_index++;
1363 struct uart_cpm_port *pinfo = &cpm_uart_ports[index];
1364 int ret;
1365
1366 pinfo->port.line = index;
1367
1368 if (index >= UART_NR)
1369 return -ENODEV;
1370
1371 dev_set_drvdata(&ofdev->dev, pinfo);
1372
1373 /* initialize the device pointer for the port */
1374 pinfo->port.dev = &ofdev->dev;
1375
1376 ret = cpm_uart_init_port(ofdev->dev.of_node, pinfo);
1377 if (ret)
1378 return ret;
1379
1380 return uart_add_one_port(&cpm_reg, &pinfo->port);
1381}
1382
1383static int __devexit cpm_uart_remove(struct platform_device *ofdev)
1384{
1385 struct uart_cpm_port *pinfo = dev_get_drvdata(&ofdev->dev);
1386 return uart_remove_one_port(&cpm_reg, &pinfo->port);
1387}
1388
1389static struct of_device_id cpm_uart_match[] = {
1390 {
1391 .compatible = "fsl,cpm1-smc-uart",
1392 },
1393 {
1394 .compatible = "fsl,cpm1-scc-uart",
1395 },
1396 {
1397 .compatible = "fsl,cpm2-smc-uart",
1398 },
1399 {
1400 .compatible = "fsl,cpm2-scc-uart",
1401 },
1402 {}
1403};
1404
1405static struct platform_driver cpm_uart_driver = {
1406 .driver = {
1407 .name = "cpm_uart",
1408 .owner = THIS_MODULE,
1409 .of_match_table = cpm_uart_match,
1410 },
1411 .probe = cpm_uart_probe,
1412 .remove = cpm_uart_remove,
1413 };
1414
1415static int __init cpm_uart_init(void)
1416{
1417 int ret = uart_register_driver(&cpm_reg);
1418 if (ret)
1419 return ret;
1420
1421 ret = platform_driver_register(&cpm_uart_driver);
1422 if (ret)
1423 uart_unregister_driver(&cpm_reg);
1424
1425 return ret;
1426}
1427
1428static void __exit cpm_uart_exit(void)
1429{
1430 platform_driver_unregister(&cpm_uart_driver);
1431 uart_unregister_driver(&cpm_reg);
1432}
1433
1434module_init(cpm_uart_init);
1435module_exit(cpm_uart_exit);
1436
1437MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
1438MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
1439MODULE_LICENSE("GPL");
1440MODULE_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..18f79575894a
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.c
@@ -0,0 +1,136 @@
1/*
2 * Driver for CPM (SCC/SMC) serial ports; CPM1 definitions
3 *
4 * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
5 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
6 *
7 * Copyright (C) 2004 Freescale Semiconductor, Inc.
8 * (C) 2004 Intracom, S.A.
9 * (C) 2006 MontaVista Software, Inc.
10 * Vitaly Bordug <vbordug@ru.mvista.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#include <linux/module.h>
29#include <linux/tty.h>
30#include <linux/gfp.h>
31#include <linux/ioport.h>
32#include <linux/init.h>
33#include <linux/serial.h>
34#include <linux/console.h>
35#include <linux/sysrq.h>
36#include <linux/device.h>
37#include <linux/bootmem.h>
38#include <linux/dma-mapping.h>
39
40#include <asm/io.h>
41#include <asm/irq.h>
42#include <asm/fs_pd.h>
43
44#include <linux/serial_core.h>
45#include <linux/kernel.h>
46
47#include <linux/of.h>
48
49#include "cpm_uart.h"
50
51/**************************************************************/
52
53void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
54{
55 cpm_command(port->command, cmd);
56}
57
58void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
59 struct device_node *np)
60{
61 return of_iomap(np, 1);
62}
63
64void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram)
65{
66 iounmap(pram);
67}
68
69/*
70 * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
71 * receive buffer descriptors from dual port ram, and a character
72 * buffer area from host mem. If we are allocating for the console we need
73 * to do it from bootmem
74 */
75int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
76{
77 int dpmemsz, memsz;
78 u8 *dp_mem;
79 unsigned long dp_offset;
80 u8 *mem_addr;
81 dma_addr_t dma_addr = 0;
82
83 pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
84
85 dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
86 dp_offset = cpm_dpalloc(dpmemsz, 8);
87 if (IS_ERR_VALUE(dp_offset)) {
88 printk(KERN_ERR
89 "cpm_uart_cpm1.c: could not allocate buffer descriptors\n");
90 return -ENOMEM;
91 }
92 dp_mem = cpm_dpram_addr(dp_offset);
93
94 memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
95 L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
96 if (is_con) {
97 /* was hostalloc but changed cause it blows away the */
98 /* large tlb mapping when pinning the kernel area */
99 mem_addr = (u8 *) cpm_dpram_addr(cpm_dpalloc(memsz, 8));
100 dma_addr = (u32)cpm_dpram_phys(mem_addr);
101 } else
102 mem_addr = dma_alloc_coherent(pinfo->port.dev, memsz, &dma_addr,
103 GFP_KERNEL);
104
105 if (mem_addr == NULL) {
106 cpm_dpfree(dp_offset);
107 printk(KERN_ERR
108 "cpm_uart_cpm1.c: could not allocate coherent memory\n");
109 return -ENOMEM;
110 }
111
112 pinfo->dp_addr = dp_offset;
113 pinfo->mem_addr = mem_addr; /* virtual address*/
114 pinfo->dma_addr = dma_addr; /* physical address*/
115 pinfo->mem_size = memsz;
116
117 pinfo->rx_buf = mem_addr;
118 pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
119 * pinfo->rx_fifosize);
120
121 pinfo->rx_bd_base = (cbd_t __iomem __force *)dp_mem;
122 pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
123
124 return 0;
125}
126
127void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
128{
129 dma_free_coherent(pinfo->port.dev, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
130 pinfo->rx_fifosize) +
131 L1_CACHE_ALIGN(pinfo->tx_nrfifos *
132 pinfo->tx_fifosize), pinfo->mem_addr,
133 pinfo->dma_addr);
134
135 cpm_dpfree(pinfo->dp_addr);
136}
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..60c7e94cde1e
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_cpm1.h
@@ -0,0 +1,32 @@
1/*
2 * Driver for CPM (SCC/SMC) serial ports
3 *
4 * definitions for cpm1
5 *
6 */
7
8#ifndef CPM_UART_CPM1_H
9#define CPM_UART_CPM1_H
10
11#include <asm/cpm1.h>
12
13static inline void cpm_set_brg(int brg, int baud)
14{
15 cpm_setbrg(brg, baud);
16}
17
18static inline void cpm_set_scc_fcr(scc_uart_t __iomem * sup)
19{
20 out_8(&sup->scc_genscc.scc_rfcr, SMC_EB);
21 out_8(&sup->scc_genscc.scc_tfcr, SMC_EB);
22}
23
24static inline void cpm_set_smc_fcr(smc_uart_t __iomem * up)
25{
26 out_8(&up->smc_rfcr, SMC_EB);
27 out_8(&up->smc_tfcr, SMC_EB);
28}
29
30#define DPRAM_BASE ((u8 __iomem __force *)cpm_dpram_addr(0))
31
32#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..a4927e66e741
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.c
@@ -0,0 +1,172 @@
1/*
2 * Driver for CPM (SCC/SMC) serial ports; CPM2 definitions
3 *
4 * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
5 * Pantelis Antoniou (panto@intracom.gr) (CPM1)
6 *
7 * Copyright (C) 2004 Freescale Semiconductor, Inc.
8 * (C) 2004 Intracom, S.A.
9 * (C) 2006 MontaVista Software, Inc.
10 * Vitaly Bordug <vbordug@ru.mvista.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#include <linux/module.h>
29#include <linux/tty.h>
30#include <linux/ioport.h>
31#include <linux/slab.h>
32#include <linux/init.h>
33#include <linux/serial.h>
34#include <linux/console.h>
35#include <linux/sysrq.h>
36#include <linux/device.h>
37#include <linux/bootmem.h>
38#include <linux/dma-mapping.h>
39
40#include <asm/io.h>
41#include <asm/irq.h>
42#include <asm/fs_pd.h>
43#include <asm/prom.h>
44
45#include <linux/serial_core.h>
46#include <linux/kernel.h>
47
48#include "cpm_uart.h"
49
50/**************************************************************/
51
52void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
53{
54 cpm_command(port->command, cmd);
55}
56
57void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
58 struct device_node *np)
59{
60 void __iomem *pram;
61 unsigned long offset;
62 struct resource res;
63 resource_size_t len;
64
65 /* Don't remap parameter RAM if it has already been initialized
66 * during console setup.
67 */
68 if (IS_SMC(port) && port->smcup)
69 return port->smcup;
70 else if (!IS_SMC(port) && port->sccup)
71 return port->sccup;
72
73 if (of_address_to_resource(np, 1, &res))
74 return NULL;
75
76 len = resource_size(&res);
77 pram = ioremap(res.start, len);
78 if (!pram)
79 return NULL;
80
81 if (!IS_SMC(port))
82 return pram;
83
84 if (len != 2) {
85 printk(KERN_WARNING "cpm_uart[%d]: device tree references "
86 "SMC pram, using boot loader/wrapper pram mapping. "
87 "Please fix your device tree to reference the pram "
88 "base register instead.\n",
89 port->port.line);
90 return pram;
91 }
92
93 offset = cpm_dpalloc(PROFF_SMC_SIZE, 64);
94 out_be16(pram, offset);
95 iounmap(pram);
96 return cpm_muram_addr(offset);
97}
98
99void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram)
100{
101 if (!IS_SMC(port))
102 iounmap(pram);
103}
104
105/*
106 * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
107 * receive buffer descriptors from dual port ram, and a character
108 * buffer area from host mem. If we are allocating for the console we need
109 * to do it from bootmem
110 */
111int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
112{
113 int dpmemsz, memsz;
114 u8 __iomem *dp_mem;
115 unsigned long dp_offset;
116 u8 *mem_addr;
117 dma_addr_t dma_addr = 0;
118
119 pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
120
121 dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
122 dp_offset = cpm_dpalloc(dpmemsz, 8);
123 if (IS_ERR_VALUE(dp_offset)) {
124 printk(KERN_ERR
125 "cpm_uart_cpm.c: could not allocate buffer descriptors\n");
126 return -ENOMEM;
127 }
128
129 dp_mem = cpm_dpram_addr(dp_offset);
130
131 memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
132 L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
133 if (is_con) {
134 mem_addr = kzalloc(memsz, GFP_NOWAIT);
135 dma_addr = virt_to_bus(mem_addr);
136 }
137 else
138 mem_addr = dma_alloc_coherent(pinfo->port.dev, memsz, &dma_addr,
139 GFP_KERNEL);
140
141 if (mem_addr == NULL) {
142 cpm_dpfree(dp_offset);
143 printk(KERN_ERR
144 "cpm_uart_cpm.c: could not allocate coherent memory\n");
145 return -ENOMEM;
146 }
147
148 pinfo->dp_addr = dp_offset;
149 pinfo->mem_addr = mem_addr;
150 pinfo->dma_addr = dma_addr;
151 pinfo->mem_size = memsz;
152
153 pinfo->rx_buf = mem_addr;
154 pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
155 * pinfo->rx_fifosize);
156
157 pinfo->rx_bd_base = (cbd_t __iomem *)dp_mem;
158 pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
159
160 return 0;
161}
162
163void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
164{
165 dma_free_coherent(pinfo->port.dev, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
166 pinfo->rx_fifosize) +
167 L1_CACHE_ALIGN(pinfo->tx_nrfifos *
168 pinfo->tx_fifosize), (void __force *)pinfo->mem_addr,
169 pinfo->dma_addr);
170
171 cpm_dpfree(pinfo->dp_addr);
172}
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..51e651a69938
--- /dev/null
+++ b/drivers/tty/serial/cpm_uart/cpm_uart_cpm2.h
@@ -0,0 +1,32 @@
1/*
2 * Driver for CPM (SCC/SMC) serial ports
3 *
4 * definitions for cpm2
5 *
6 */
7
8#ifndef CPM_UART_CPM2_H
9#define CPM_UART_CPM2_H
10
11#include <asm/cpm2.h>
12
13static inline void cpm_set_brg(int brg, int baud)
14{
15 cpm_setbrg(brg, baud);
16}
17
18static inline void cpm_set_scc_fcr(scc_uart_t __iomem *sup)
19{
20 out_8(&sup->scc_genscc.scc_rfcr, CPMFCR_GBL | CPMFCR_EB);
21 out_8(&sup->scc_genscc.scc_tfcr, CPMFCR_GBL | CPMFCR_EB);
22}
23
24static inline void cpm_set_smc_fcr(smc_uart_t __iomem *up)
25{
26 out_8(&up->smc_rfcr, CPMFCR_GBL | CPMFCR_EB);
27 out_8(&up->smc_tfcr, CPMFCR_GBL | CPMFCR_EB);
28}
29
30#define DPRAM_BASE ((u8 __iomem __force *)cpm_dpram_addr(0))
31
32#endif
diff --git a/drivers/tty/serial/crisv10.c b/drivers/tty/serial/crisv10.c
new file mode 100644
index 000000000000..225123b37f19
--- /dev/null
+++ b/drivers/tty/serial/crisv10.c
@@ -0,0 +1,4572 @@
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, unsigned int set, unsigned int clear)
3585{
3586 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3587 unsigned long flags;
3588
3589 local_irq_save(flags);
3590
3591 if (clear & TIOCM_RTS)
3592 e100_rts(info, 0);
3593 if (clear & TIOCM_DTR)
3594 e100_dtr(info, 0);
3595 /* Handle FEMALE behaviour */
3596 if (clear & TIOCM_RI)
3597 e100_ri_out(info, 0);
3598 if (clear & TIOCM_CD)
3599 e100_cd_out(info, 0);
3600
3601 if (set & TIOCM_RTS)
3602 e100_rts(info, 1);
3603 if (set & TIOCM_DTR)
3604 e100_dtr(info, 1);
3605 /* Handle FEMALE behaviour */
3606 if (set & TIOCM_RI)
3607 e100_ri_out(info, 1);
3608 if (set & TIOCM_CD)
3609 e100_cd_out(info, 1);
3610
3611 local_irq_restore(flags);
3612 return 0;
3613}
3614
3615static int
3616rs_tiocmget(struct tty_struct *tty)
3617{
3618 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3619 unsigned int result;
3620 unsigned long flags;
3621
3622 local_irq_save(flags);
3623
3624 result =
3625 (!E100_RTS_GET(info) ? TIOCM_RTS : 0)
3626 | (!E100_DTR_GET(info) ? TIOCM_DTR : 0)
3627 | (!E100_RI_GET(info) ? TIOCM_RNG : 0)
3628 | (!E100_DSR_GET(info) ? TIOCM_DSR : 0)
3629 | (!E100_CD_GET(info) ? TIOCM_CAR : 0)
3630 | (!E100_CTS_GET(info) ? TIOCM_CTS : 0);
3631
3632 local_irq_restore(flags);
3633
3634#ifdef SERIAL_DEBUG_IO
3635 printk(KERN_DEBUG "ser%i: modem state: %i 0x%08X\n",
3636 info->line, result, result);
3637 {
3638 char s[100];
3639
3640 get_control_state_str(result, s);
3641 printk(KERN_DEBUG "state: %s\n", s);
3642 }
3643#endif
3644 return result;
3645
3646}
3647
3648
3649static int
3650rs_ioctl(struct tty_struct *tty,
3651 unsigned int cmd, unsigned long arg)
3652{
3653 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3654
3655 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
3656 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
3657 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
3658 if (tty->flags & (1 << TTY_IO_ERROR))
3659 return -EIO;
3660 }
3661
3662 switch (cmd) {
3663 case TIOCGSERIAL:
3664 return get_serial_info(info,
3665 (struct serial_struct *) arg);
3666 case TIOCSSERIAL:
3667 return set_serial_info(info,
3668 (struct serial_struct *) arg);
3669 case TIOCSERGETLSR: /* Get line status register */
3670 return get_lsr_info(info, (unsigned int *) arg);
3671
3672 case TIOCSERGSTRUCT:
3673 if (copy_to_user((struct e100_serial *) arg,
3674 info, sizeof(struct e100_serial)))
3675 return -EFAULT;
3676 return 0;
3677
3678#if defined(CONFIG_ETRAX_RS485)
3679 case TIOCSERSETRS485:
3680 {
3681 /* In this ioctl we still use the old structure
3682 * rs485_control for backward compatibility
3683 * (if we use serial_rs485, then old user-level code
3684 * wouldn't work anymore...).
3685 * The use of this ioctl is deprecated: use TIOCSRS485
3686 * instead.*/
3687 struct rs485_control rs485ctrl;
3688 struct serial_rs485 rs485data;
3689 printk(KERN_DEBUG "The use of this ioctl is deprecated. Use TIOCSRS485 instead\n");
3690 if (copy_from_user(&rs485ctrl, (struct rs485_control *)arg,
3691 sizeof(rs485ctrl)))
3692 return -EFAULT;
3693
3694 rs485data.delay_rts_before_send = rs485ctrl.delay_rts_before_send;
3695 rs485data.flags = 0;
3696 if (rs485data.delay_rts_before_send != 0)
3697 rs485data.flags |= SER_RS485_RTS_BEFORE_SEND;
3698 else
3699 rs485data.flags &= ~(SER_RS485_RTS_BEFORE_SEND);
3700
3701 if (rs485ctrl.enabled)
3702 rs485data.flags |= SER_RS485_ENABLED;
3703 else
3704 rs485data.flags &= ~(SER_RS485_ENABLED);
3705
3706 if (rs485ctrl.rts_on_send)
3707 rs485data.flags |= SER_RS485_RTS_ON_SEND;
3708 else
3709 rs485data.flags &= ~(SER_RS485_RTS_ON_SEND);
3710
3711 if (rs485ctrl.rts_after_sent)
3712 rs485data.flags |= SER_RS485_RTS_AFTER_SEND;
3713 else
3714 rs485data.flags &= ~(SER_RS485_RTS_AFTER_SEND);
3715
3716 return e100_enable_rs485(tty, &rs485data);
3717 }
3718
3719 case TIOCSRS485:
3720 {
3721 /* This is the new version of TIOCSRS485, with new
3722 * data structure serial_rs485 */
3723 struct serial_rs485 rs485data;
3724 if (copy_from_user(&rs485data, (struct rs485_control *)arg,
3725 sizeof(rs485data)))
3726 return -EFAULT;
3727
3728 return e100_enable_rs485(tty, &rs485data);
3729 }
3730
3731 case TIOCGRS485:
3732 {
3733 struct serial_rs485 *rs485data =
3734 &(((struct e100_serial *)tty->driver_data)->rs485);
3735 /* This is the ioctl to get RS485 data from user-space */
3736 if (copy_to_user((struct serial_rs485 *) arg,
3737 rs485data,
3738 sizeof(struct serial_rs485)))
3739 return -EFAULT;
3740 break;
3741 }
3742
3743 case TIOCSERWRRS485:
3744 {
3745 struct rs485_write rs485wr;
3746 if (copy_from_user(&rs485wr, (struct rs485_write *)arg,
3747 sizeof(rs485wr)))
3748 return -EFAULT;
3749
3750 return e100_write_rs485(tty, rs485wr.outc, rs485wr.outc_size);
3751 }
3752#endif
3753
3754 default:
3755 return -ENOIOCTLCMD;
3756 }
3757 return 0;
3758}
3759
3760static void
3761rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
3762{
3763 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3764
3765 change_speed(info);
3766
3767 /* Handle turning off CRTSCTS */
3768 if ((old_termios->c_cflag & CRTSCTS) &&
3769 !(tty->termios->c_cflag & CRTSCTS)) {
3770 tty->hw_stopped = 0;
3771 rs_start(tty);
3772 }
3773
3774}
3775
3776/*
3777 * ------------------------------------------------------------
3778 * rs_close()
3779 *
3780 * This routine is called when the serial port gets closed. First, we
3781 * wait for the last remaining data to be sent. Then, we unlink its
3782 * S structure from the interrupt chain if necessary, and we free
3783 * that IRQ if nothing is left in the chain.
3784 * ------------------------------------------------------------
3785 */
3786static void
3787rs_close(struct tty_struct *tty, struct file * filp)
3788{
3789 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3790 unsigned long flags;
3791
3792 if (!info)
3793 return;
3794
3795 /* interrupts are disabled for this entire function */
3796
3797 local_irq_save(flags);
3798
3799 if (tty_hung_up_p(filp)) {
3800 local_irq_restore(flags);
3801 return;
3802 }
3803
3804#ifdef SERIAL_DEBUG_OPEN
3805 printk("[%d] rs_close ttyS%d, count = %d\n", current->pid,
3806 info->line, info->count);
3807#endif
3808 if ((tty->count == 1) && (info->count != 1)) {
3809 /*
3810 * Uh, oh. tty->count is 1, which means that the tty
3811 * structure will be freed. Info->count should always
3812 * be one in these conditions. If it's greater than
3813 * one, we've got real problems, since it means the
3814 * serial port won't be shutdown.
3815 */
3816 printk(KERN_CRIT
3817 "rs_close: bad serial port count; tty->count is 1, "
3818 "info->count is %d\n", info->count);
3819 info->count = 1;
3820 }
3821 if (--info->count < 0) {
3822 printk(KERN_CRIT "rs_close: bad serial port count for ttyS%d: %d\n",
3823 info->line, info->count);
3824 info->count = 0;
3825 }
3826 if (info->count) {
3827 local_irq_restore(flags);
3828 return;
3829 }
3830 info->flags |= ASYNC_CLOSING;
3831 /*
3832 * Save the termios structure, since this port may have
3833 * separate termios for callout and dialin.
3834 */
3835 if (info->flags & ASYNC_NORMAL_ACTIVE)
3836 info->normal_termios = *tty->termios;
3837 /*
3838 * Now we wait for the transmit buffer to clear; and we notify
3839 * the line discipline to only process XON/XOFF characters.
3840 */
3841 tty->closing = 1;
3842 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
3843 tty_wait_until_sent(tty, info->closing_wait);
3844 /*
3845 * At this point we stop accepting input. To do this, we
3846 * disable the serial receiver and the DMA receive interrupt.
3847 */
3848#ifdef SERIAL_HANDLE_EARLY_ERRORS
3849 e100_disable_serial_data_irq(info);
3850#endif
3851
3852#ifndef CONFIG_SVINTO_SIM
3853 e100_disable_rx(info);
3854 e100_disable_rx_irq(info);
3855
3856 if (info->flags & ASYNC_INITIALIZED) {
3857 /*
3858 * Before we drop DTR, make sure the UART transmitter
3859 * has completely drained; this is especially
3860 * important as we have a transmit FIFO!
3861 */
3862 rs_wait_until_sent(tty, HZ);
3863 }
3864#endif
3865
3866 shutdown(info);
3867 rs_flush_buffer(tty);
3868 tty_ldisc_flush(tty);
3869 tty->closing = 0;
3870 info->event = 0;
3871 info->port.tty = NULL;
3872 if (info->blocked_open) {
3873 if (info->close_delay)
3874 schedule_timeout_interruptible(info->close_delay);
3875 wake_up_interruptible(&info->open_wait);
3876 }
3877 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
3878 wake_up_interruptible(&info->close_wait);
3879 local_irq_restore(flags);
3880
3881 /* port closed */
3882
3883#if defined(CONFIG_ETRAX_RS485)
3884 if (info->rs485.flags & SER_RS485_ENABLED) {
3885 info->rs485.flags &= ~(SER_RS485_ENABLED);
3886#if defined(CONFIG_ETRAX_RS485_ON_PA)
3887 *R_PORT_PA_DATA = port_pa_data_shadow &= ~(1 << rs485_pa_bit);
3888#endif
3889#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
3890 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
3891 rs485_port_g_bit, 0);
3892#endif
3893#if defined(CONFIG_ETRAX_RS485_LTC1387)
3894 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
3895 CONFIG_ETRAX_RS485_LTC1387_DXEN_PORT_G_BIT, 0);
3896 REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow,
3897 CONFIG_ETRAX_RS485_LTC1387_RXEN_PORT_G_BIT, 0);
3898#endif
3899 }
3900#endif
3901
3902 /*
3903 * Release any allocated DMA irq's.
3904 */
3905 if (info->dma_in_enabled) {
3906 free_irq(info->dma_in_irq_nbr, info);
3907 cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
3908 info->uses_dma_in = 0;
3909#ifdef SERIAL_DEBUG_OPEN
3910 printk(KERN_DEBUG "DMA irq '%s' freed\n",
3911 info->dma_in_irq_description);
3912#endif
3913 }
3914 if (info->dma_out_enabled) {
3915 free_irq(info->dma_out_irq_nbr, info);
3916 cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
3917 info->uses_dma_out = 0;
3918#ifdef SERIAL_DEBUG_OPEN
3919 printk(KERN_DEBUG "DMA irq '%s' freed\n",
3920 info->dma_out_irq_description);
3921#endif
3922 }
3923}
3924
3925/*
3926 * rs_wait_until_sent() --- wait until the transmitter is empty
3927 */
3928static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
3929{
3930 unsigned long orig_jiffies;
3931 struct e100_serial *info = (struct e100_serial *)tty->driver_data;
3932 unsigned long curr_time = jiffies;
3933 unsigned long curr_time_usec = GET_JIFFIES_USEC();
3934 long elapsed_usec =
3935 (curr_time - info->last_tx_active) * (1000000/HZ) +
3936 curr_time_usec - info->last_tx_active_usec;
3937
3938 /*
3939 * Check R_DMA_CHx_STATUS bit 0-6=number of available bytes in FIFO
3940 * R_DMA_CHx_HWSW bit 31-16=nbr of bytes left in DMA buffer (0=64k)
3941 */
3942 orig_jiffies = jiffies;
3943 while (info->xmit.head != info->xmit.tail || /* More in send queue */
3944 (*info->ostatusadr & 0x007f) || /* more in FIFO */
3945 (elapsed_usec < 2*info->char_time_usec)) {
3946 schedule_timeout_interruptible(1);
3947 if (signal_pending(current))
3948 break;
3949 if (timeout && time_after(jiffies, orig_jiffies + timeout))
3950 break;
3951 curr_time = jiffies;
3952 curr_time_usec = GET_JIFFIES_USEC();
3953 elapsed_usec =
3954 (curr_time - info->last_tx_active) * (1000000/HZ) +
3955 curr_time_usec - info->last_tx_active_usec;
3956 }
3957 set_current_state(TASK_RUNNING);
3958}
3959
3960/*
3961 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
3962 */
3963void
3964rs_hangup(struct tty_struct *tty)
3965{
3966 struct e100_serial * info = (struct e100_serial *)tty->driver_data;
3967
3968 rs_flush_buffer(tty);
3969 shutdown(info);
3970 info->event = 0;
3971 info->count = 0;
3972 info->flags &= ~ASYNC_NORMAL_ACTIVE;
3973 info->port.tty = NULL;
3974 wake_up_interruptible(&info->open_wait);
3975}
3976
3977/*
3978 * ------------------------------------------------------------
3979 * rs_open() and friends
3980 * ------------------------------------------------------------
3981 */
3982static int
3983block_til_ready(struct tty_struct *tty, struct file * filp,
3984 struct e100_serial *info)
3985{
3986 DECLARE_WAITQUEUE(wait, current);
3987 unsigned long flags;
3988 int retval;
3989 int do_clocal = 0, extra_count = 0;
3990
3991 /*
3992 * If the device is in the middle of being closed, then block
3993 * until it's done, and then try again.
3994 */
3995 if (tty_hung_up_p(filp) ||
3996 (info->flags & ASYNC_CLOSING)) {
3997 wait_event_interruptible_tty(info->close_wait,
3998 !(info->flags & ASYNC_CLOSING));
3999#ifdef SERIAL_DO_RESTART
4000 if (info->flags & ASYNC_HUP_NOTIFY)
4001 return -EAGAIN;
4002 else
4003 return -ERESTARTSYS;
4004#else
4005 return -EAGAIN;
4006#endif
4007 }
4008
4009 /*
4010 * If non-blocking mode is set, or the port is not enabled,
4011 * then make the check up front and then exit.
4012 */
4013 if ((filp->f_flags & O_NONBLOCK) ||
4014 (tty->flags & (1 << TTY_IO_ERROR))) {
4015 info->flags |= ASYNC_NORMAL_ACTIVE;
4016 return 0;
4017 }
4018
4019 if (tty->termios->c_cflag & CLOCAL) {
4020 do_clocal = 1;
4021 }
4022
4023 /*
4024 * Block waiting for the carrier detect and the line to become
4025 * free (i.e., not in use by the callout). While we are in
4026 * this loop, info->count is dropped by one, so that
4027 * rs_close() knows when to free things. We restore it upon
4028 * exit, either normal or abnormal.
4029 */
4030 retval = 0;
4031 add_wait_queue(&info->open_wait, &wait);
4032#ifdef SERIAL_DEBUG_OPEN
4033 printk("block_til_ready before block: ttyS%d, count = %d\n",
4034 info->line, info->count);
4035#endif
4036 local_irq_save(flags);
4037 if (!tty_hung_up_p(filp)) {
4038 extra_count++;
4039 info->count--;
4040 }
4041 local_irq_restore(flags);
4042 info->blocked_open++;
4043 while (1) {
4044 local_irq_save(flags);
4045 /* assert RTS and DTR */
4046 e100_rts(info, 1);
4047 e100_dtr(info, 1);
4048 local_irq_restore(flags);
4049 set_current_state(TASK_INTERRUPTIBLE);
4050 if (tty_hung_up_p(filp) ||
4051 !(info->flags & ASYNC_INITIALIZED)) {
4052#ifdef SERIAL_DO_RESTART
4053 if (info->flags & ASYNC_HUP_NOTIFY)
4054 retval = -EAGAIN;
4055 else
4056 retval = -ERESTARTSYS;
4057#else
4058 retval = -EAGAIN;
4059#endif
4060 break;
4061 }
4062 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
4063 /* && (do_clocal || DCD_IS_ASSERTED) */
4064 break;
4065 if (signal_pending(current)) {
4066 retval = -ERESTARTSYS;
4067 break;
4068 }
4069#ifdef SERIAL_DEBUG_OPEN
4070 printk("block_til_ready blocking: ttyS%d, count = %d\n",
4071 info->line, info->count);
4072#endif
4073 tty_unlock();
4074 schedule();
4075 tty_lock();
4076 }
4077 set_current_state(TASK_RUNNING);
4078 remove_wait_queue(&info->open_wait, &wait);
4079 if (extra_count)
4080 info->count++;
4081 info->blocked_open--;
4082#ifdef SERIAL_DEBUG_OPEN
4083 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
4084 info->line, info->count);
4085#endif
4086 if (retval)
4087 return retval;
4088 info->flags |= ASYNC_NORMAL_ACTIVE;
4089 return 0;
4090}
4091
4092static void
4093deinit_port(struct e100_serial *info)
4094{
4095 if (info->dma_out_enabled) {
4096 cris_free_dma(info->dma_out_nbr, info->dma_out_irq_description);
4097 free_irq(info->dma_out_irq_nbr, info);
4098 }
4099 if (info->dma_in_enabled) {
4100 cris_free_dma(info->dma_in_nbr, info->dma_in_irq_description);
4101 free_irq(info->dma_in_irq_nbr, info);
4102 }
4103}
4104
4105/*
4106 * This routine is called whenever a serial port is opened.
4107 * It performs the serial-specific initialization for the tty structure.
4108 */
4109static int
4110rs_open(struct tty_struct *tty, struct file * filp)
4111{
4112 struct e100_serial *info;
4113 int retval, line;
4114 unsigned long page;
4115 int allocated_resources = 0;
4116
4117 /* find which port we want to open */
4118 line = tty->index;
4119
4120 if (line < 0 || line >= NR_PORTS)
4121 return -ENODEV;
4122
4123 /* find the corresponding e100_serial struct in the table */
4124 info = rs_table + line;
4125
4126 /* don't allow the opening of ports that are not enabled in the HW config */
4127 if (!info->enabled)
4128 return -ENODEV;
4129
4130#ifdef SERIAL_DEBUG_OPEN
4131 printk("[%d] rs_open %s, count = %d\n", current->pid, tty->name,
4132 info->count);
4133#endif
4134
4135 info->count++;
4136 tty->driver_data = info;
4137 info->port.tty = tty;
4138
4139 info->port.tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
4140
4141 if (!tmp_buf) {
4142 page = get_zeroed_page(GFP_KERNEL);
4143 if (!page) {
4144 return -ENOMEM;
4145 }
4146 if (tmp_buf)
4147 free_page(page);
4148 else
4149 tmp_buf = (unsigned char *) page;
4150 }
4151
4152 /*
4153 * If the port is in the middle of closing, bail out now
4154 */
4155 if (tty_hung_up_p(filp) ||
4156 (info->flags & ASYNC_CLOSING)) {
4157 wait_event_interruptible_tty(info->close_wait,
4158 !(info->flags & ASYNC_CLOSING));
4159#ifdef SERIAL_DO_RESTART
4160 return ((info->flags & ASYNC_HUP_NOTIFY) ?
4161 -EAGAIN : -ERESTARTSYS);
4162#else
4163 return -EAGAIN;
4164#endif
4165 }
4166
4167 /*
4168 * If DMA is enabled try to allocate the irq's.
4169 */
4170 if (info->count == 1) {
4171 allocated_resources = 1;
4172 if (info->dma_in_enabled) {
4173 if (request_irq(info->dma_in_irq_nbr,
4174 rec_interrupt,
4175 info->dma_in_irq_flags,
4176 info->dma_in_irq_description,
4177 info)) {
4178 printk(KERN_WARNING "DMA irq '%s' busy; "
4179 "falling back to non-DMA mode\n",
4180 info->dma_in_irq_description);
4181 /* Make sure we never try to use DMA in */
4182 /* for the port again. */
4183 info->dma_in_enabled = 0;
4184 } else if (cris_request_dma(info->dma_in_nbr,
4185 info->dma_in_irq_description,
4186 DMA_VERBOSE_ON_ERROR,
4187 info->dma_owner)) {
4188 free_irq(info->dma_in_irq_nbr, info);
4189 printk(KERN_WARNING "DMA '%s' busy; "
4190 "falling back to non-DMA mode\n",
4191 info->dma_in_irq_description);
4192 /* Make sure we never try to use DMA in */
4193 /* for the port again. */
4194 info->dma_in_enabled = 0;
4195 }
4196#ifdef SERIAL_DEBUG_OPEN
4197 else
4198 printk(KERN_DEBUG "DMA irq '%s' allocated\n",
4199 info->dma_in_irq_description);
4200#endif
4201 }
4202 if (info->dma_out_enabled) {
4203 if (request_irq(info->dma_out_irq_nbr,
4204 tr_interrupt,
4205 info->dma_out_irq_flags,
4206 info->dma_out_irq_description,
4207 info)) {
4208 printk(KERN_WARNING "DMA irq '%s' busy; "
4209 "falling back to non-DMA mode\n",
4210 info->dma_out_irq_description);
4211 /* Make sure we never try to use DMA out */
4212 /* for the port again. */
4213 info->dma_out_enabled = 0;
4214 } else if (cris_request_dma(info->dma_out_nbr,
4215 info->dma_out_irq_description,
4216 DMA_VERBOSE_ON_ERROR,
4217 info->dma_owner)) {
4218 free_irq(info->dma_out_irq_nbr, info);
4219 printk(KERN_WARNING "DMA '%s' busy; "
4220 "falling back to non-DMA mode\n",
4221 info->dma_out_irq_description);
4222 /* Make sure we never try to use DMA out */
4223 /* for the port again. */
4224 info->dma_out_enabled = 0;
4225 }
4226#ifdef SERIAL_DEBUG_OPEN
4227 else
4228 printk(KERN_DEBUG "DMA irq '%s' allocated\n",
4229 info->dma_out_irq_description);
4230#endif
4231 }
4232 }
4233
4234 /*
4235 * Start up the serial port
4236 */
4237
4238 retval = startup(info);
4239 if (retval) {
4240 if (allocated_resources)
4241 deinit_port(info);
4242
4243 /* FIXME Decrease count info->count here too? */
4244 return retval;
4245 }
4246
4247
4248 retval = block_til_ready(tty, filp, info);
4249 if (retval) {
4250#ifdef SERIAL_DEBUG_OPEN
4251 printk("rs_open returning after block_til_ready with %d\n",
4252 retval);
4253#endif
4254 if (allocated_resources)
4255 deinit_port(info);
4256
4257 return retval;
4258 }
4259
4260 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
4261 *tty->termios = info->normal_termios;
4262 change_speed(info);
4263 }
4264
4265#ifdef SERIAL_DEBUG_OPEN
4266 printk("rs_open ttyS%d successful...\n", info->line);
4267#endif
4268 DLOG_INT_TRIG( log_int_pos = 0);
4269
4270 DFLIP( if (info->line == SERIAL_DEBUG_LINE) {
4271 info->icount.rx = 0;
4272 } );
4273
4274 return 0;
4275}
4276
4277#ifdef CONFIG_PROC_FS
4278/*
4279 * /proc fs routines....
4280 */
4281
4282static void seq_line_info(struct seq_file *m, struct e100_serial *info)
4283{
4284 unsigned long tmp;
4285
4286 seq_printf(m, "%d: uart:E100 port:%lX irq:%d",
4287 info->line, (unsigned long)info->ioport, info->irq);
4288
4289 if (!info->ioport || (info->type == PORT_UNKNOWN)) {
4290 seq_printf(m, "\n");
4291 return;
4292 }
4293
4294 seq_printf(m, " baud:%d", info->baud);
4295 seq_printf(m, " tx:%lu rx:%lu",
4296 (unsigned long)info->icount.tx,
4297 (unsigned long)info->icount.rx);
4298 tmp = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
4299 if (tmp)
4300 seq_printf(m, " tx_pend:%lu/%lu",
4301 (unsigned long)tmp,
4302 (unsigned long)SERIAL_XMIT_SIZE);
4303
4304 seq_printf(m, " rx_pend:%lu/%lu",
4305 (unsigned long)info->recv_cnt,
4306 (unsigned long)info->max_recv_cnt);
4307
4308#if 1
4309 if (info->port.tty) {
4310 if (info->port.tty->stopped)
4311 seq_printf(m, " stopped:%i",
4312 (int)info->port.tty->stopped);
4313 if (info->port.tty->hw_stopped)
4314 seq_printf(m, " hw_stopped:%i",
4315 (int)info->port.tty->hw_stopped);
4316 }
4317
4318 {
4319 unsigned char rstat = info->ioport[REG_STATUS];
4320 if (rstat & IO_MASK(R_SERIAL0_STATUS, xoff_detect))
4321 seq_printf(m, " xoff_detect:1");
4322 }
4323
4324#endif
4325
4326 if (info->icount.frame)
4327 seq_printf(m, " fe:%lu", (unsigned long)info->icount.frame);
4328
4329 if (info->icount.parity)
4330 seq_printf(m, " pe:%lu", (unsigned long)info->icount.parity);
4331
4332 if (info->icount.brk)
4333 seq_printf(m, " brk:%lu", (unsigned long)info->icount.brk);
4334
4335 if (info->icount.overrun)
4336 seq_printf(m, " oe:%lu", (unsigned long)info->icount.overrun);
4337
4338 /*
4339 * Last thing is the RS-232 status lines
4340 */
4341 if (!E100_RTS_GET(info))
4342 seq_puts(m, "|RTS");
4343 if (!E100_CTS_GET(info))
4344 seq_puts(m, "|CTS");
4345 if (!E100_DTR_GET(info))
4346 seq_puts(m, "|DTR");
4347 if (!E100_DSR_GET(info))
4348 seq_puts(m, "|DSR");
4349 if (!E100_CD_GET(info))
4350 seq_puts(m, "|CD");
4351 if (!E100_RI_GET(info))
4352 seq_puts(m, "|RI");
4353 seq_puts(m, "\n");
4354}
4355
4356
4357static int crisv10_proc_show(struct seq_file *m, void *v)
4358{
4359 int i;
4360
4361 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
4362
4363 for (i = 0; i < NR_PORTS; i++) {
4364 if (!rs_table[i].enabled)
4365 continue;
4366 seq_line_info(m, &rs_table[i]);
4367 }
4368#ifdef DEBUG_LOG_INCLUDED
4369 for (i = 0; i < debug_log_pos; i++) {
4370 seq_printf(m, "%-4i %lu.%lu ",
4371 i, debug_log[i].time,
4372 timer_data_to_ns(debug_log[i].timer_data));
4373 seq_printf(m, debug_log[i].string, debug_log[i].value);
4374 }
4375 seq_printf(m, "debug_log %i/%i\n", i, DEBUG_LOG_SIZE);
4376 debug_log_pos = 0;
4377#endif
4378 return 0;
4379}
4380
4381static int crisv10_proc_open(struct inode *inode, struct file *file)
4382{
4383 return single_open(file, crisv10_proc_show, NULL);
4384}
4385
4386static const struct file_operations crisv10_proc_fops = {
4387 .owner = THIS_MODULE,
4388 .open = crisv10_proc_open,
4389 .read = seq_read,
4390 .llseek = seq_lseek,
4391 .release = single_release,
4392};
4393#endif
4394
4395
4396/* Finally, routines used to initialize the serial driver. */
4397
4398static void show_serial_version(void)
4399{
4400 printk(KERN_INFO
4401 "ETRAX 100LX serial-driver %s, "
4402 "(c) 2000-2004 Axis Communications AB\r\n",
4403 &serial_version[11]); /* "$Revision: x.yy" */
4404}
4405
4406/* rs_init inits the driver at boot (using the module_init chain) */
4407
4408static const struct tty_operations rs_ops = {
4409 .open = rs_open,
4410 .close = rs_close,
4411 .write = rs_write,
4412 .flush_chars = rs_flush_chars,
4413 .write_room = rs_write_room,
4414 .chars_in_buffer = rs_chars_in_buffer,
4415 .flush_buffer = rs_flush_buffer,
4416 .ioctl = rs_ioctl,
4417 .throttle = rs_throttle,
4418 .unthrottle = rs_unthrottle,
4419 .set_termios = rs_set_termios,
4420 .stop = rs_stop,
4421 .start = rs_start,
4422 .hangup = rs_hangup,
4423 .break_ctl = rs_break,
4424 .send_xchar = rs_send_xchar,
4425 .wait_until_sent = rs_wait_until_sent,
4426 .tiocmget = rs_tiocmget,
4427 .tiocmset = rs_tiocmset,
4428#ifdef CONFIG_PROC_FS
4429 .proc_fops = &crisv10_proc_fops,
4430#endif
4431};
4432
4433static int __init rs_init(void)
4434{
4435 int i;
4436 struct e100_serial *info;
4437 struct tty_driver *driver = alloc_tty_driver(NR_PORTS);
4438
4439 if (!driver)
4440 return -ENOMEM;
4441
4442 show_serial_version();
4443
4444 /* Setup the timed flush handler system */
4445
4446#if !defined(CONFIG_ETRAX_SERIAL_FAST_TIMER)
4447 setup_timer(&flush_timer, timed_flush_handler, 0);
4448 mod_timer(&flush_timer, jiffies + 5);
4449#endif
4450
4451#if defined(CONFIG_ETRAX_RS485)
4452#if defined(CONFIG_ETRAX_RS485_ON_PA)
4453 if (cris_io_interface_allocate_pins(if_ser0, 'a', rs485_pa_bit,
4454 rs485_pa_bit)) {
4455 printk(KERN_CRIT "ETRAX100LX serial: Could not allocate "
4456 "RS485 pin\n");
4457 put_tty_driver(driver);
4458 return -EBUSY;
4459 }
4460#endif
4461#if defined(CONFIG_ETRAX_RS485_ON_PORT_G)
4462 if (cris_io_interface_allocate_pins(if_ser0, 'g', rs485_pa_bit,
4463 rs485_port_g_bit)) {
4464 printk(KERN_CRIT "ETRAX100LX serial: Could not allocate "
4465 "RS485 pin\n");
4466 put_tty_driver(driver);
4467 return -EBUSY;
4468 }
4469#endif
4470#endif
4471
4472 /* Initialize the tty_driver structure */
4473
4474 driver->driver_name = "serial";
4475 driver->name = "ttyS";
4476 driver->major = TTY_MAJOR;
4477 driver->minor_start = 64;
4478 driver->type = TTY_DRIVER_TYPE_SERIAL;
4479 driver->subtype = SERIAL_TYPE_NORMAL;
4480 driver->init_termios = tty_std_termios;
4481 driver->init_termios.c_cflag =
4482 B115200 | CS8 | CREAD | HUPCL | CLOCAL; /* is normally B9600 default... */
4483 driver->init_termios.c_ispeed = 115200;
4484 driver->init_termios.c_ospeed = 115200;
4485 driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
4486
4487 tty_set_operations(driver, &rs_ops);
4488 serial_driver = driver;
4489 if (tty_register_driver(driver))
4490 panic("Couldn't register serial driver\n");
4491 /* do some initializing for the separate ports */
4492
4493 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
4494 if (info->enabled) {
4495 if (cris_request_io_interface(info->io_if,
4496 info->io_if_description)) {
4497 printk(KERN_CRIT "ETRAX100LX async serial: "
4498 "Could not allocate IO pins for "
4499 "%s, port %d\n",
4500 info->io_if_description, i);
4501 info->enabled = 0;
4502 }
4503 }
4504 info->uses_dma_in = 0;
4505 info->uses_dma_out = 0;
4506 info->line = i;
4507 info->port.tty = NULL;
4508 info->type = PORT_ETRAX;
4509 info->tr_running = 0;
4510 info->forced_eop = 0;
4511 info->baud_base = DEF_BAUD_BASE;
4512 info->custom_divisor = 0;
4513 info->flags = 0;
4514 info->close_delay = 5*HZ/10;
4515 info->closing_wait = 30*HZ;
4516 info->x_char = 0;
4517 info->event = 0;
4518 info->count = 0;
4519 info->blocked_open = 0;
4520 info->normal_termios = driver->init_termios;
4521 init_waitqueue_head(&info->open_wait);
4522 init_waitqueue_head(&info->close_wait);
4523 info->xmit.buf = NULL;
4524 info->xmit.tail = info->xmit.head = 0;
4525 info->first_recv_buffer = info->last_recv_buffer = NULL;
4526 info->recv_cnt = info->max_recv_cnt = 0;
4527 info->last_tx_active_usec = 0;
4528 info->last_tx_active = 0;
4529
4530#if defined(CONFIG_ETRAX_RS485)
4531 /* Set sane defaults */
4532 info->rs485.flags &= ~(SER_RS485_RTS_ON_SEND);
4533 info->rs485.flags |= SER_RS485_RTS_AFTER_SEND;
4534 info->rs485.flags &= ~(SER_RS485_RTS_BEFORE_SEND);
4535 info->rs485.delay_rts_before_send = 0;
4536 info->rs485.flags &= ~(SER_RS485_ENABLED);
4537#endif
4538 INIT_WORK(&info->work, do_softint);
4539
4540 if (info->enabled) {
4541 printk(KERN_INFO "%s%d at %p is a builtin UART with DMA\n",
4542 serial_driver->name, info->line, info->ioport);
4543 }
4544 }
4545#ifdef CONFIG_ETRAX_FAST_TIMER
4546#ifdef CONFIG_ETRAX_SERIAL_FAST_TIMER
4547 memset(fast_timers, 0, sizeof(fast_timers));
4548#endif
4549#ifdef CONFIG_ETRAX_RS485
4550 memset(fast_timers_rs485, 0, sizeof(fast_timers_rs485));
4551#endif
4552 fast_timer_init();
4553#endif
4554
4555#ifndef CONFIG_SVINTO_SIM
4556#ifndef CONFIG_ETRAX_KGDB
4557 /* Not needed in simulator. May only complicate stuff. */
4558 /* hook the irq's for DMA channel 6 and 7, serial output and input, and some more... */
4559
4560 if (request_irq(SERIAL_IRQ_NBR, ser_interrupt,
4561 IRQF_SHARED | IRQF_DISABLED, "serial ", driver))
4562 panic("%s: Failed to request irq8", __func__);
4563
4564#endif
4565#endif /* CONFIG_SVINTO_SIM */
4566
4567 return 0;
4568}
4569
4570/* this makes sure that rs_init is called during kernel boot */
4571
4572module_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..8a869e58f6d7
--- /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 Receiver */
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..5315525220fb
--- /dev/null
+++ b/drivers/tty/serial/ifx6x60.c
@@ -0,0 +1,1414 @@
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 <russ.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/kfifo.h>
45#include <linux/tty_flip.h>
46#include <linux/timer.h>
47#include <linux/serial.h>
48#include <linux/interrupt.h>
49#include <linux/irq.h>
50#include <linux/rfkill.h>
51#include <linux/fs.h>
52#include <linux/ip.h>
53#include <linux/dmapool.h>
54#include <linux/gpio.h>
55#include <linux/sched.h>
56#include <linux/time.h>
57#include <linux/wait.h>
58#include <linux/pm.h>
59#include <linux/pm_runtime.h>
60#include <linux/spi/ifx_modem.h>
61#include <linux/delay.h>
62
63#include "ifx6x60.h"
64
65#define IFX_SPI_MORE_MASK 0x10
66#define IFX_SPI_MORE_BIT 12 /* bit position in u16 */
67#define IFX_SPI_CTS_BIT 13 /* bit position in u16 */
68#define IFX_SPI_MODE SPI_MODE_1
69#define IFX_SPI_TTY_ID 0
70#define IFX_SPI_TIMEOUT_SEC 2
71#define IFX_SPI_HEADER_0 (-1)
72#define IFX_SPI_HEADER_F (-2)
73
74/* forward reference */
75static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev);
76
77/* local variables */
78static int spi_bpw = 16; /* 8, 16 or 32 bit word length */
79static struct tty_driver *tty_drv;
80static struct ifx_spi_device *saved_ifx_dev;
81static struct lock_class_key ifx_spi_key;
82
83/* GPIO/GPE settings */
84
85/**
86 * mrdy_set_high - set MRDY GPIO
87 * @ifx: device we are controlling
88 *
89 */
90static inline void mrdy_set_high(struct ifx_spi_device *ifx)
91{
92 gpio_set_value(ifx->gpio.mrdy, 1);
93}
94
95/**
96 * mrdy_set_low - clear MRDY GPIO
97 * @ifx: device we are controlling
98 *
99 */
100static inline void mrdy_set_low(struct ifx_spi_device *ifx)
101{
102 gpio_set_value(ifx->gpio.mrdy, 0);
103}
104
105/**
106 * ifx_spi_power_state_set
107 * @ifx_dev: our SPI device
108 * @val: bits to set
109 *
110 * Set bit in power status and signal power system if status becomes non-0
111 */
112static void
113ifx_spi_power_state_set(struct ifx_spi_device *ifx_dev, unsigned char val)
114{
115 unsigned long flags;
116
117 spin_lock_irqsave(&ifx_dev->power_lock, flags);
118
119 /*
120 * if power status is already non-0, just update, else
121 * tell power system
122 */
123 if (!ifx_dev->power_status)
124 pm_runtime_get(&ifx_dev->spi_dev->dev);
125 ifx_dev->power_status |= val;
126
127 spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
128}
129
130/**
131 * ifx_spi_power_state_clear - clear power bit
132 * @ifx_dev: our SPI device
133 * @val: bits to clear
134 *
135 * clear bit in power status and signal power system if status becomes 0
136 */
137static void
138ifx_spi_power_state_clear(struct ifx_spi_device *ifx_dev, unsigned char val)
139{
140 unsigned long flags;
141
142 spin_lock_irqsave(&ifx_dev->power_lock, flags);
143
144 if (ifx_dev->power_status) {
145 ifx_dev->power_status &= ~val;
146 if (!ifx_dev->power_status)
147 pm_runtime_put(&ifx_dev->spi_dev->dev);
148 }
149
150 spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
151}
152
153/**
154 * swap_buf
155 * @buf: our buffer
156 * @len : number of bytes (not words) in the buffer
157 * @end: end of buffer
158 *
159 * Swap the contents of a buffer into big endian format
160 */
161static inline void swap_buf(u16 *buf, int len, void *end)
162{
163 int n;
164
165 len = ((len + 1) >> 1);
166 if ((void *)&buf[len] > end) {
167 pr_err("swap_buf: swap exceeds boundary (%p > %p)!",
168 &buf[len], end);
169 return;
170 }
171 for (n = 0; n < len; n++) {
172 *buf = cpu_to_be16(*buf);
173 buf++;
174 }
175}
176
177/**
178 * mrdy_assert - assert MRDY line
179 * @ifx_dev: our SPI device
180 *
181 * Assert mrdy and set timer to wait for SRDY interrupt, if SRDY is low
182 * now.
183 *
184 * FIXME: Can SRDY even go high as we are running this code ?
185 */
186static void mrdy_assert(struct ifx_spi_device *ifx_dev)
187{
188 int val = gpio_get_value(ifx_dev->gpio.srdy);
189 if (!val) {
190 if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING,
191 &ifx_dev->flags)) {
192 ifx_dev->spi_timer.expires =
193 jiffies + IFX_SPI_TIMEOUT_SEC*HZ;
194 add_timer(&ifx_dev->spi_timer);
195
196 }
197 }
198 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_DATA_PENDING);
199 mrdy_set_high(ifx_dev);
200}
201
202/**
203 * ifx_spi_hangup - hang up an IFX device
204 * @ifx_dev: our SPI device
205 *
206 * Hang up the tty attached to the IFX device if one is currently
207 * open. If not take no action
208 */
209static void ifx_spi_ttyhangup(struct ifx_spi_device *ifx_dev)
210{
211 struct tty_port *pport = &ifx_dev->tty_port;
212 struct tty_struct *tty = tty_port_tty_get(pport);
213 if (tty) {
214 tty_hangup(tty);
215 tty_kref_put(tty);
216 }
217}
218
219/**
220 * ifx_spi_timeout - SPI timeout
221 * @arg: our SPI device
222 *
223 * The SPI has timed out: hang up the tty. Users will then see a hangup
224 * and error events.
225 */
226static void ifx_spi_timeout(unsigned long arg)
227{
228 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *)arg;
229
230 dev_warn(&ifx_dev->spi_dev->dev, "*** SPI Timeout ***");
231 ifx_spi_ttyhangup(ifx_dev);
232 mrdy_set_low(ifx_dev);
233 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
234}
235
236/* char/tty operations */
237
238/**
239 * ifx_spi_tiocmget - get modem lines
240 * @tty: our tty device
241 * @filp: file handle issuing the request
242 *
243 * Map the signal state into Linux modem flags and report the value
244 * in Linux terms
245 */
246static int ifx_spi_tiocmget(struct tty_struct *tty)
247{
248 unsigned int value;
249 struct ifx_spi_device *ifx_dev = tty->driver_data;
250
251 value =
252 (test_bit(IFX_SPI_RTS, &ifx_dev->signal_state) ? TIOCM_RTS : 0) |
253 (test_bit(IFX_SPI_DTR, &ifx_dev->signal_state) ? TIOCM_DTR : 0) |
254 (test_bit(IFX_SPI_CTS, &ifx_dev->signal_state) ? TIOCM_CTS : 0) |
255 (test_bit(IFX_SPI_DSR, &ifx_dev->signal_state) ? TIOCM_DSR : 0) |
256 (test_bit(IFX_SPI_DCD, &ifx_dev->signal_state) ? TIOCM_CAR : 0) |
257 (test_bit(IFX_SPI_RI, &ifx_dev->signal_state) ? TIOCM_RNG : 0);
258 return value;
259}
260
261/**
262 * ifx_spi_tiocmset - set modem bits
263 * @tty: the tty structure
264 * @set: bits to set
265 * @clear: bits to clear
266 *
267 * The IFX6x60 only supports DTR and RTS. Set them accordingly
268 * and flag that an update to the modem is needed.
269 *
270 * FIXME: do we need to kick the tranfers when we do this ?
271 */
272static int ifx_spi_tiocmset(struct tty_struct *tty,
273 unsigned int set, unsigned int clear)
274{
275 struct ifx_spi_device *ifx_dev = tty->driver_data;
276
277 if (set & TIOCM_RTS)
278 set_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
279 if (set & TIOCM_DTR)
280 set_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
281 if (clear & TIOCM_RTS)
282 clear_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
283 if (clear & TIOCM_DTR)
284 clear_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
285
286 set_bit(IFX_SPI_UPDATE, &ifx_dev->signal_state);
287 return 0;
288}
289
290/**
291 * ifx_spi_open - called on tty open
292 * @tty: our tty device
293 * @filp: file handle being associated with the tty
294 *
295 * Open the tty interface. We let the tty_port layer do all the work
296 * for us.
297 *
298 * FIXME: Remove single device assumption and saved_ifx_dev
299 */
300static int ifx_spi_open(struct tty_struct *tty, struct file *filp)
301{
302 return tty_port_open(&saved_ifx_dev->tty_port, tty, filp);
303}
304
305/**
306 * ifx_spi_close - called when our tty closes
307 * @tty: the tty being closed
308 * @filp: the file handle being closed
309 *
310 * Perform the close of the tty. We use the tty_port layer to do all
311 * our hard work.
312 */
313static void ifx_spi_close(struct tty_struct *tty, struct file *filp)
314{
315 struct ifx_spi_device *ifx_dev = tty->driver_data;
316 tty_port_close(&ifx_dev->tty_port, tty, filp);
317 /* FIXME: should we do an ifx_spi_reset here ? */
318}
319
320/**
321 * ifx_decode_spi_header - decode received header
322 * @buffer: the received data
323 * @length: decoded length
324 * @more: decoded more flag
325 * @received_cts: status of cts we received
326 *
327 * Note how received_cts is handled -- if header is all F it is left
328 * the same as it was, if header is all 0 it is set to 0 otherwise it is
329 * taken from the incoming header.
330 *
331 * FIXME: endianness
332 */
333static int ifx_spi_decode_spi_header(unsigned char *buffer, int *length,
334 unsigned char *more, unsigned char *received_cts)
335{
336 u16 h1;
337 u16 h2;
338 u16 *in_buffer = (u16 *)buffer;
339
340 h1 = *in_buffer;
341 h2 = *(in_buffer+1);
342
343 if (h1 == 0 && h2 == 0) {
344 *received_cts = 0;
345 return IFX_SPI_HEADER_0;
346 } else if (h1 == 0xffff && h2 == 0xffff) {
347 /* spi_slave_cts remains as it was */
348 return IFX_SPI_HEADER_F;
349 }
350
351 *length = h1 & 0xfff; /* upper bits of byte are flags */
352 *more = (buffer[1] >> IFX_SPI_MORE_BIT) & 1;
353 *received_cts = (buffer[3] >> IFX_SPI_CTS_BIT) & 1;
354 return 0;
355}
356
357/**
358 * ifx_setup_spi_header - set header fields
359 * @txbuffer: pointer to start of SPI buffer
360 * @tx_count: bytes
361 * @more: indicate if more to follow
362 *
363 * Format up an SPI header for a transfer
364 *
365 * FIXME: endianness?
366 */
367static void ifx_spi_setup_spi_header(unsigned char *txbuffer, int tx_count,
368 unsigned char more)
369{
370 *(u16 *)(txbuffer) = tx_count;
371 *(u16 *)(txbuffer+2) = IFX_SPI_PAYLOAD_SIZE;
372 txbuffer[1] |= (more << IFX_SPI_MORE_BIT) & IFX_SPI_MORE_MASK;
373}
374
375/**
376 * ifx_spi_wakeup_serial - SPI space made
377 * @port_data: our SPI device
378 *
379 * We have emptied the FIFO enough that we want to get more data
380 * queued into it. Poke the line discipline via tty_wakeup so that
381 * it will feed us more bits
382 */
383static void ifx_spi_wakeup_serial(struct ifx_spi_device *ifx_dev)
384{
385 struct tty_struct *tty;
386
387 tty = tty_port_tty_get(&ifx_dev->tty_port);
388 if (!tty)
389 return;
390 tty_wakeup(tty);
391 tty_kref_put(tty);
392}
393
394/**
395 * ifx_spi_prepare_tx_buffer - prepare transmit frame
396 * @ifx_dev: our SPI device
397 *
398 * The transmit buffr needs a header and various other bits of
399 * information followed by as much data as we can pull from the FIFO
400 * and transfer. This function formats up a suitable buffer in the
401 * ifx_dev->tx_buffer
402 *
403 * FIXME: performance - should we wake the tty when the queue is half
404 * empty ?
405 */
406static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device *ifx_dev)
407{
408 int temp_count;
409 int queue_length;
410 int tx_count;
411 unsigned char *tx_buffer;
412
413 tx_buffer = ifx_dev->tx_buffer;
414 memset(tx_buffer, 0, IFX_SPI_TRANSFER_SIZE);
415
416 /* make room for required SPI header */
417 tx_buffer += IFX_SPI_HEADER_OVERHEAD;
418 tx_count = IFX_SPI_HEADER_OVERHEAD;
419
420 /* clear to signal no more data if this turns out to be the
421 * last buffer sent in a sequence */
422 ifx_dev->spi_more = 0;
423
424 /* if modem cts is set, just send empty buffer */
425 if (!ifx_dev->spi_slave_cts) {
426 /* see if there's tx data */
427 queue_length = kfifo_len(&ifx_dev->tx_fifo);
428 if (queue_length != 0) {
429 /* data to mux -- see if there's room for it */
430 temp_count = min(queue_length, IFX_SPI_PAYLOAD_SIZE);
431 temp_count = kfifo_out_locked(&ifx_dev->tx_fifo,
432 tx_buffer, temp_count,
433 &ifx_dev->fifo_lock);
434
435 /* update buffer pointer and data count in message */
436 tx_buffer += temp_count;
437 tx_count += temp_count;
438 if (temp_count == queue_length)
439 /* poke port to get more data */
440 ifx_spi_wakeup_serial(ifx_dev);
441 else /* more data in port, use next SPI message */
442 ifx_dev->spi_more = 1;
443 }
444 }
445 /* have data and info for header -- set up SPI header in buffer */
446 /* spi header needs payload size, not entire buffer size */
447 ifx_spi_setup_spi_header(ifx_dev->tx_buffer,
448 tx_count-IFX_SPI_HEADER_OVERHEAD,
449 ifx_dev->spi_more);
450 /* swap actual data in the buffer */
451 swap_buf((u16 *)(ifx_dev->tx_buffer), tx_count,
452 &ifx_dev->tx_buffer[IFX_SPI_TRANSFER_SIZE]);
453 return tx_count;
454}
455
456/**
457 * ifx_spi_write - line discipline write
458 * @tty: our tty device
459 * @buf: pointer to buffer to write (kernel space)
460 * @count: size of buffer
461 *
462 * Write the characters we have been given into the FIFO. If the device
463 * is not active then activate it, when the SRDY line is asserted back
464 * this will commence I/O
465 */
466static int ifx_spi_write(struct tty_struct *tty, const unsigned char *buf,
467 int count)
468{
469 struct ifx_spi_device *ifx_dev = tty->driver_data;
470 unsigned char *tmp_buf = (unsigned char *)buf;
471 int tx_count = kfifo_in_locked(&ifx_dev->tx_fifo, tmp_buf, count,
472 &ifx_dev->fifo_lock);
473 mrdy_assert(ifx_dev);
474 return tx_count;
475}
476
477/**
478 * ifx_spi_chars_in_buffer - line discipline helper
479 * @tty: our tty device
480 *
481 * Report how much data we can accept before we drop bytes. As we use
482 * a simple FIFO this is nice and easy.
483 */
484static int ifx_spi_write_room(struct tty_struct *tty)
485{
486 struct ifx_spi_device *ifx_dev = tty->driver_data;
487 return IFX_SPI_FIFO_SIZE - kfifo_len(&ifx_dev->tx_fifo);
488}
489
490/**
491 * ifx_spi_chars_in_buffer - line discipline helper
492 * @tty: our tty device
493 *
494 * Report how many characters we have buffered. In our case this is the
495 * number of bytes sitting in our transmit FIFO.
496 */
497static int ifx_spi_chars_in_buffer(struct tty_struct *tty)
498{
499 struct ifx_spi_device *ifx_dev = tty->driver_data;
500 return kfifo_len(&ifx_dev->tx_fifo);
501}
502
503/**
504 * ifx_port_hangup
505 * @port: our tty port
506 *
507 * tty port hang up. Called when tty_hangup processing is invoked either
508 * by loss of carrier, or by software (eg vhangup). Serialized against
509 * activate/shutdown by the tty layer.
510 */
511static void ifx_spi_hangup(struct tty_struct *tty)
512{
513 struct ifx_spi_device *ifx_dev = tty->driver_data;
514 tty_port_hangup(&ifx_dev->tty_port);
515}
516
517/**
518 * ifx_port_activate
519 * @port: our tty port
520 *
521 * tty port activate method - called for first open. Serialized
522 * with hangup and shutdown by the tty layer.
523 */
524static int ifx_port_activate(struct tty_port *port, struct tty_struct *tty)
525{
526 struct ifx_spi_device *ifx_dev =
527 container_of(port, struct ifx_spi_device, tty_port);
528
529 /* clear any old data; can't do this in 'close' */
530 kfifo_reset(&ifx_dev->tx_fifo);
531
532 /* put port data into this tty */
533 tty->driver_data = ifx_dev;
534
535 /* allows flip string push from int context */
536 tty->low_latency = 1;
537
538 return 0;
539}
540
541/**
542 * ifx_port_shutdown
543 * @port: our tty port
544 *
545 * tty port shutdown method - called for last port close. Serialized
546 * with hangup and activate by the tty layer.
547 */
548static void ifx_port_shutdown(struct tty_port *port)
549{
550 struct ifx_spi_device *ifx_dev =
551 container_of(port, struct ifx_spi_device, tty_port);
552
553 mrdy_set_low(ifx_dev);
554 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
555 tasklet_kill(&ifx_dev->io_work_tasklet);
556}
557
558static const struct tty_port_operations ifx_tty_port_ops = {
559 .activate = ifx_port_activate,
560 .shutdown = ifx_port_shutdown,
561};
562
563static const struct tty_operations ifx_spi_serial_ops = {
564 .open = ifx_spi_open,
565 .close = ifx_spi_close,
566 .write = ifx_spi_write,
567 .hangup = ifx_spi_hangup,
568 .write_room = ifx_spi_write_room,
569 .chars_in_buffer = ifx_spi_chars_in_buffer,
570 .tiocmget = ifx_spi_tiocmget,
571 .tiocmset = ifx_spi_tiocmset,
572};
573
574/**
575 * ifx_spi_insert_fip_string - queue received data
576 * @ifx_ser: our SPI device
577 * @chars: buffer we have received
578 * @size: number of chars reeived
579 *
580 * Queue bytes to the tty assuming the tty side is currently open. If
581 * not the discard the data.
582 */
583static void ifx_spi_insert_flip_string(struct ifx_spi_device *ifx_dev,
584 unsigned char *chars, size_t size)
585{
586 struct tty_struct *tty = tty_port_tty_get(&ifx_dev->tty_port);
587 if (!tty)
588 return;
589 tty_insert_flip_string(tty, chars, size);
590 tty_flip_buffer_push(tty);
591 tty_kref_put(tty);
592}
593
594/**
595 * ifx_spi_complete - SPI transfer completed
596 * @ctx: our SPI device
597 *
598 * An SPI transfer has completed. Process any received data and kick off
599 * any further transmits we can commence.
600 */
601static void ifx_spi_complete(void *ctx)
602{
603 struct ifx_spi_device *ifx_dev = ctx;
604 struct tty_struct *tty;
605 struct tty_ldisc *ldisc = NULL;
606 int length;
607 int actual_length;
608 unsigned char more;
609 unsigned char cts;
610 int local_write_pending = 0;
611 int queue_length;
612 int srdy;
613 int decode_result;
614
615 mrdy_set_low(ifx_dev);
616
617 if (!ifx_dev->spi_msg.status) {
618 /* check header validity, get comm flags */
619 swap_buf((u16 *)ifx_dev->rx_buffer, IFX_SPI_HEADER_OVERHEAD,
620 &ifx_dev->rx_buffer[IFX_SPI_HEADER_OVERHEAD]);
621 decode_result = ifx_spi_decode_spi_header(ifx_dev->rx_buffer,
622 &length, &more, &cts);
623 if (decode_result == IFX_SPI_HEADER_0) {
624 dev_dbg(&ifx_dev->spi_dev->dev,
625 "ignore input: invalid header 0");
626 ifx_dev->spi_slave_cts = 0;
627 goto complete_exit;
628 } else if (decode_result == IFX_SPI_HEADER_F) {
629 dev_dbg(&ifx_dev->spi_dev->dev,
630 "ignore input: invalid header F");
631 goto complete_exit;
632 }
633
634 ifx_dev->spi_slave_cts = cts;
635
636 actual_length = min((unsigned int)length,
637 ifx_dev->spi_msg.actual_length);
638 swap_buf((u16 *)(ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD),
639 actual_length,
640 &ifx_dev->rx_buffer[IFX_SPI_TRANSFER_SIZE]);
641 ifx_spi_insert_flip_string(
642 ifx_dev,
643 ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD,
644 (size_t)actual_length);
645 } else {
646 dev_dbg(&ifx_dev->spi_dev->dev, "SPI transfer error %d",
647 ifx_dev->spi_msg.status);
648 }
649
650complete_exit:
651 if (ifx_dev->write_pending) {
652 ifx_dev->write_pending = 0;
653 local_write_pending = 1;
654 }
655
656 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &(ifx_dev->flags));
657
658 queue_length = kfifo_len(&ifx_dev->tx_fifo);
659 srdy = gpio_get_value(ifx_dev->gpio.srdy);
660 if (!srdy)
661 ifx_spi_power_state_clear(ifx_dev, IFX_SPI_POWER_SRDY);
662
663 /* schedule output if there is more to do */
664 if (test_and_clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags))
665 tasklet_schedule(&ifx_dev->io_work_tasklet);
666 else {
667 if (more || ifx_dev->spi_more || queue_length > 0 ||
668 local_write_pending) {
669 if (ifx_dev->spi_slave_cts) {
670 if (more)
671 mrdy_assert(ifx_dev);
672 } else
673 mrdy_assert(ifx_dev);
674 } else {
675 /*
676 * poke line discipline driver if any for more data
677 * may or may not get more data to write
678 * for now, say not busy
679 */
680 ifx_spi_power_state_clear(ifx_dev,
681 IFX_SPI_POWER_DATA_PENDING);
682 tty = tty_port_tty_get(&ifx_dev->tty_port);
683 if (tty) {
684 ldisc = tty_ldisc_ref(tty);
685 if (ldisc) {
686 ldisc->ops->write_wakeup(tty);
687 tty_ldisc_deref(ldisc);
688 }
689 tty_kref_put(tty);
690 }
691 }
692 }
693}
694
695/**
696 * ifx_spio_io - I/O tasklet
697 * @data: our SPI device
698 *
699 * Queue data for transmission if possible and then kick off the
700 * transfer.
701 */
702static void ifx_spi_io(unsigned long data)
703{
704 int retval;
705 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *) data;
706
707 if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags)) {
708 if (ifx_dev->gpio.unack_srdy_int_nb > 0)
709 ifx_dev->gpio.unack_srdy_int_nb--;
710
711 ifx_spi_prepare_tx_buffer(ifx_dev);
712
713 spi_message_init(&ifx_dev->spi_msg);
714 INIT_LIST_HEAD(&ifx_dev->spi_msg.queue);
715
716 ifx_dev->spi_msg.context = ifx_dev;
717 ifx_dev->spi_msg.complete = ifx_spi_complete;
718
719 /* set up our spi transfer */
720 /* note len is BYTES, not transfers */
721 ifx_dev->spi_xfer.len = IFX_SPI_TRANSFER_SIZE;
722 ifx_dev->spi_xfer.cs_change = 0;
723 ifx_dev->spi_xfer.speed_hz = ifx_dev->spi_dev->max_speed_hz;
724 /* ifx_dev->spi_xfer.speed_hz = 390625; */
725 ifx_dev->spi_xfer.bits_per_word = spi_bpw;
726
727 ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer;
728 ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer;
729
730 /*
731 * setup dma pointers
732 */
733 if (ifx_dev->use_dma) {
734 ifx_dev->spi_msg.is_dma_mapped = 1;
735 ifx_dev->tx_dma = ifx_dev->tx_bus;
736 ifx_dev->rx_dma = ifx_dev->rx_bus;
737 ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma;
738 ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma;
739 } else {
740 ifx_dev->spi_msg.is_dma_mapped = 0;
741 ifx_dev->tx_dma = (dma_addr_t)0;
742 ifx_dev->rx_dma = (dma_addr_t)0;
743 ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0;
744 ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0;
745 }
746
747 spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg);
748
749 /* Assert MRDY. This may have already been done by the write
750 * routine.
751 */
752 mrdy_assert(ifx_dev);
753
754 retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg);
755 if (retval) {
756 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS,
757 &ifx_dev->flags);
758 tasklet_schedule(&ifx_dev->io_work_tasklet);
759 return;
760 }
761 } else
762 ifx_dev->write_pending = 1;
763}
764
765/**
766 * ifx_spi_free_port - free up the tty side
767 * @ifx_dev: IFX device going away
768 *
769 * Unregister and free up a port when the device goes away
770 */
771static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev)
772{
773 if (ifx_dev->tty_dev)
774 tty_unregister_device(tty_drv, ifx_dev->minor);
775 kfifo_free(&ifx_dev->tx_fifo);
776}
777
778/**
779 * ifx_spi_create_port - create a new port
780 * @ifx_dev: our spi device
781 *
782 * Allocate and initialise the tty port that goes with this interface
783 * and add it to the tty layer so that it can be opened.
784 */
785static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev)
786{
787 int ret = 0;
788 struct tty_port *pport = &ifx_dev->tty_port;
789
790 spin_lock_init(&ifx_dev->fifo_lock);
791 lockdep_set_class_and_subclass(&ifx_dev->fifo_lock,
792 &ifx_spi_key, 0);
793
794 if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) {
795 ret = -ENOMEM;
796 goto error_ret;
797 }
798
799 tty_port_init(pport);
800 pport->ops = &ifx_tty_port_ops;
801 ifx_dev->minor = IFX_SPI_TTY_ID;
802 ifx_dev->tty_dev = tty_register_device(tty_drv, ifx_dev->minor,
803 &ifx_dev->spi_dev->dev);
804 if (IS_ERR(ifx_dev->tty_dev)) {
805 dev_dbg(&ifx_dev->spi_dev->dev,
806 "%s: registering tty device failed", __func__);
807 ret = PTR_ERR(ifx_dev->tty_dev);
808 goto error_ret;
809 }
810 return 0;
811
812error_ret:
813 ifx_spi_free_port(ifx_dev);
814 return ret;
815}
816
817/**
818 * ifx_spi_handle_srdy - handle SRDY
819 * @ifx_dev: device asserting SRDY
820 *
821 * Check our device state and see what we need to kick off when SRDY
822 * is asserted. This usually means killing the timer and firing off the
823 * I/O processing.
824 */
825static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev)
826{
827 if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) {
828 del_timer_sync(&ifx_dev->spi_timer);
829 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
830 }
831
832 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY);
833
834 if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags))
835 tasklet_schedule(&ifx_dev->io_work_tasklet);
836 else
837 set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
838}
839
840/**
841 * ifx_spi_srdy_interrupt - SRDY asserted
842 * @irq: our IRQ number
843 * @dev: our ifx device
844 *
845 * The modem asserted SRDY. Handle the srdy event
846 */
847static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev)
848{
849 struct ifx_spi_device *ifx_dev = dev;
850 ifx_dev->gpio.unack_srdy_int_nb++;
851 ifx_spi_handle_srdy(ifx_dev);
852 return IRQ_HANDLED;
853}
854
855/**
856 * ifx_spi_reset_interrupt - Modem has changed reset state
857 * @irq: interrupt number
858 * @dev: our device pointer
859 *
860 * The modem has either entered or left reset state. Check the GPIO
861 * line to see which.
862 *
863 * FIXME: review locking on MR_INPROGRESS versus
864 * parallel unsolicited reset/solicited reset
865 */
866static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev)
867{
868 struct ifx_spi_device *ifx_dev = dev;
869 int val = gpio_get_value(ifx_dev->gpio.reset_out);
870 int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state);
871
872 if (val == 0) {
873 /* entered reset */
874 set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
875 if (!solreset) {
876 /* unsolicited reset */
877 ifx_spi_ttyhangup(ifx_dev);
878 }
879 } else {
880 /* exited reset */
881 clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
882 if (solreset) {
883 set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state);
884 wake_up(&ifx_dev->mdm_reset_wait);
885 }
886 }
887 return IRQ_HANDLED;
888}
889
890/**
891 * ifx_spi_free_device - free device
892 * @ifx_dev: device to free
893 *
894 * Free the IFX device
895 */
896static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev)
897{
898 ifx_spi_free_port(ifx_dev);
899 dma_free_coherent(&ifx_dev->spi_dev->dev,
900 IFX_SPI_TRANSFER_SIZE,
901 ifx_dev->tx_buffer,
902 ifx_dev->tx_bus);
903 dma_free_coherent(&ifx_dev->spi_dev->dev,
904 IFX_SPI_TRANSFER_SIZE,
905 ifx_dev->rx_buffer,
906 ifx_dev->rx_bus);
907}
908
909/**
910 * ifx_spi_reset - reset modem
911 * @ifx_dev: modem to reset
912 *
913 * Perform a reset on the modem
914 */
915static int ifx_spi_reset(struct ifx_spi_device *ifx_dev)
916{
917 int ret;
918 /*
919 * set up modem power, reset
920 *
921 * delays are required on some platforms for the modem
922 * to reset properly
923 */
924 set_bit(MR_START, &ifx_dev->mdm_reset_state);
925 gpio_set_value(ifx_dev->gpio.po, 0);
926 gpio_set_value(ifx_dev->gpio.reset, 0);
927 msleep(25);
928 gpio_set_value(ifx_dev->gpio.reset, 1);
929 msleep(1);
930 gpio_set_value(ifx_dev->gpio.po, 1);
931 msleep(1);
932 gpio_set_value(ifx_dev->gpio.po, 0);
933 ret = wait_event_timeout(ifx_dev->mdm_reset_wait,
934 test_bit(MR_COMPLETE,
935 &ifx_dev->mdm_reset_state),
936 IFX_RESET_TIMEOUT);
937 if (!ret)
938 dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)",
939 ifx_dev->mdm_reset_state);
940
941 ifx_dev->mdm_reset_state = 0;
942 return ret;
943}
944
945/**
946 * ifx_spi_spi_probe - probe callback
947 * @spi: our possible matching SPI device
948 *
949 * Probe for a 6x60 modem on SPI bus. Perform any needed device and
950 * GPIO setup.
951 *
952 * FIXME:
953 * - Support for multiple devices
954 * - Split out MID specific GPIO handling eventually
955 */
956
957static int ifx_spi_spi_probe(struct spi_device *spi)
958{
959 int ret;
960 int srdy;
961 struct ifx_modem_platform_data *pl_data;
962 struct ifx_spi_device *ifx_dev;
963
964 if (saved_ifx_dev) {
965 dev_dbg(&spi->dev, "ignoring subsequent detection");
966 return -ENODEV;
967 }
968
969 pl_data = (struct ifx_modem_platform_data *)spi->dev.platform_data;
970 if (!pl_data) {
971 dev_err(&spi->dev, "missing platform data!");
972 return -ENODEV;
973 }
974
975 /* initialize structure to hold our device variables */
976 ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL);
977 if (!ifx_dev) {
978 dev_err(&spi->dev, "spi device allocation failed");
979 return -ENOMEM;
980 }
981 saved_ifx_dev = ifx_dev;
982 ifx_dev->spi_dev = spi;
983 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
984 spin_lock_init(&ifx_dev->write_lock);
985 spin_lock_init(&ifx_dev->power_lock);
986 ifx_dev->power_status = 0;
987 init_timer(&ifx_dev->spi_timer);
988 ifx_dev->spi_timer.function = ifx_spi_timeout;
989 ifx_dev->spi_timer.data = (unsigned long)ifx_dev;
990 ifx_dev->modem = pl_data->modem_type;
991 ifx_dev->use_dma = pl_data->use_dma;
992 ifx_dev->max_hz = pl_data->max_hz;
993 /* initialize spi mode, etc */
994 spi->max_speed_hz = ifx_dev->max_hz;
995 spi->mode = IFX_SPI_MODE | (SPI_LOOP & spi->mode);
996 spi->bits_per_word = spi_bpw;
997 ret = spi_setup(spi);
998 if (ret) {
999 dev_err(&spi->dev, "SPI setup wasn't successful %d", ret);
1000 return -ENODEV;
1001 }
1002
1003 /* ensure SPI protocol flags are initialized to enable transfer */
1004 ifx_dev->spi_more = 0;
1005 ifx_dev->spi_slave_cts = 0;
1006
1007 /*initialize transfer and dma buffers */
1008 ifx_dev->tx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1009 IFX_SPI_TRANSFER_SIZE,
1010 &ifx_dev->tx_bus,
1011 GFP_KERNEL);
1012 if (!ifx_dev->tx_buffer) {
1013 dev_err(&spi->dev, "DMA-TX buffer allocation failed");
1014 ret = -ENOMEM;
1015 goto error_ret;
1016 }
1017 ifx_dev->rx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1018 IFX_SPI_TRANSFER_SIZE,
1019 &ifx_dev->rx_bus,
1020 GFP_KERNEL);
1021 if (!ifx_dev->rx_buffer) {
1022 dev_err(&spi->dev, "DMA-RX buffer allocation failed");
1023 ret = -ENOMEM;
1024 goto error_ret;
1025 }
1026
1027 /* initialize waitq for modem reset */
1028 init_waitqueue_head(&ifx_dev->mdm_reset_wait);
1029
1030 spi_set_drvdata(spi, ifx_dev);
1031 tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io,
1032 (unsigned long)ifx_dev);
1033
1034 set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags);
1035
1036 /* create our tty port */
1037 ret = ifx_spi_create_port(ifx_dev);
1038 if (ret != 0) {
1039 dev_err(&spi->dev, "create default tty port failed");
1040 goto error_ret;
1041 }
1042
1043 ifx_dev->gpio.reset = pl_data->rst_pmu;
1044 ifx_dev->gpio.po = pl_data->pwr_on;
1045 ifx_dev->gpio.mrdy = pl_data->mrdy;
1046 ifx_dev->gpio.srdy = pl_data->srdy;
1047 ifx_dev->gpio.reset_out = pl_data->rst_out;
1048
1049 dev_info(&spi->dev, "gpios %d, %d, %d, %d, %d",
1050 ifx_dev->gpio.reset, ifx_dev->gpio.po, ifx_dev->gpio.mrdy,
1051 ifx_dev->gpio.srdy, ifx_dev->gpio.reset_out);
1052
1053 /* Configure gpios */
1054 ret = gpio_request(ifx_dev->gpio.reset, "ifxModem");
1055 if (ret < 0) {
1056 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET)",
1057 ifx_dev->gpio.reset);
1058 goto error_ret;
1059 }
1060 ret += gpio_direction_output(ifx_dev->gpio.reset, 0);
1061 ret += gpio_export(ifx_dev->gpio.reset, 1);
1062 if (ret) {
1063 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET)",
1064 ifx_dev->gpio.reset);
1065 ret = -EBUSY;
1066 goto error_ret2;
1067 }
1068
1069 ret = gpio_request(ifx_dev->gpio.po, "ifxModem");
1070 ret += gpio_direction_output(ifx_dev->gpio.po, 0);
1071 ret += gpio_export(ifx_dev->gpio.po, 1);
1072 if (ret) {
1073 dev_err(&spi->dev, "Unable to configure GPIO%d (ON)",
1074 ifx_dev->gpio.po);
1075 ret = -EBUSY;
1076 goto error_ret3;
1077 }
1078
1079 ret = gpio_request(ifx_dev->gpio.mrdy, "ifxModem");
1080 if (ret < 0) {
1081 dev_err(&spi->dev, "Unable to allocate GPIO%d (MRDY)",
1082 ifx_dev->gpio.mrdy);
1083 goto error_ret3;
1084 }
1085 ret += gpio_export(ifx_dev->gpio.mrdy, 1);
1086 ret += gpio_direction_output(ifx_dev->gpio.mrdy, 0);
1087 if (ret) {
1088 dev_err(&spi->dev, "Unable to configure GPIO%d (MRDY)",
1089 ifx_dev->gpio.mrdy);
1090 ret = -EBUSY;
1091 goto error_ret4;
1092 }
1093
1094 ret = gpio_request(ifx_dev->gpio.srdy, "ifxModem");
1095 if (ret < 0) {
1096 dev_err(&spi->dev, "Unable to allocate GPIO%d (SRDY)",
1097 ifx_dev->gpio.srdy);
1098 ret = -EBUSY;
1099 goto error_ret4;
1100 }
1101 ret += gpio_export(ifx_dev->gpio.srdy, 1);
1102 ret += gpio_direction_input(ifx_dev->gpio.srdy);
1103 if (ret) {
1104 dev_err(&spi->dev, "Unable to configure GPIO%d (SRDY)",
1105 ifx_dev->gpio.srdy);
1106 ret = -EBUSY;
1107 goto error_ret5;
1108 }
1109
1110 ret = gpio_request(ifx_dev->gpio.reset_out, "ifxModem");
1111 if (ret < 0) {
1112 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET_OUT)",
1113 ifx_dev->gpio.reset_out);
1114 goto error_ret5;
1115 }
1116 ret += gpio_export(ifx_dev->gpio.reset_out, 1);
1117 ret += gpio_direction_input(ifx_dev->gpio.reset_out);
1118 if (ret) {
1119 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET_OUT)",
1120 ifx_dev->gpio.reset_out);
1121 ret = -EBUSY;
1122 goto error_ret6;
1123 }
1124
1125 ret = request_irq(gpio_to_irq(ifx_dev->gpio.reset_out),
1126 ifx_spi_reset_interrupt,
1127 IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME,
1128 (void *)ifx_dev);
1129 if (ret) {
1130 dev_err(&spi->dev, "Unable to get irq %x\n",
1131 gpio_to_irq(ifx_dev->gpio.reset_out));
1132 goto error_ret6;
1133 }
1134
1135 ret = ifx_spi_reset(ifx_dev);
1136
1137 ret = request_irq(gpio_to_irq(ifx_dev->gpio.srdy),
1138 ifx_spi_srdy_interrupt,
1139 IRQF_TRIGGER_RISING, DRVNAME,
1140 (void *)ifx_dev);
1141 if (ret) {
1142 dev_err(&spi->dev, "Unable to get irq %x",
1143 gpio_to_irq(ifx_dev->gpio.srdy));
1144 goto error_ret7;
1145 }
1146
1147 /* set pm runtime power state and register with power system */
1148 pm_runtime_set_active(&spi->dev);
1149 pm_runtime_enable(&spi->dev);
1150
1151 /* handle case that modem is already signaling SRDY */
1152 /* no outgoing tty open at this point, this just satisfies the
1153 * modem's read and should reset communication properly
1154 */
1155 srdy = gpio_get_value(ifx_dev->gpio.srdy);
1156
1157 if (srdy) {
1158 mrdy_assert(ifx_dev);
1159 ifx_spi_handle_srdy(ifx_dev);
1160 } else
1161 mrdy_set_low(ifx_dev);
1162 return 0;
1163
1164error_ret7:
1165 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1166error_ret6:
1167 gpio_free(ifx_dev->gpio.srdy);
1168error_ret5:
1169 gpio_free(ifx_dev->gpio.mrdy);
1170error_ret4:
1171 gpio_free(ifx_dev->gpio.reset);
1172error_ret3:
1173 gpio_free(ifx_dev->gpio.po);
1174error_ret2:
1175 gpio_free(ifx_dev->gpio.reset_out);
1176error_ret:
1177 ifx_spi_free_device(ifx_dev);
1178 saved_ifx_dev = NULL;
1179 return ret;
1180}
1181
1182/**
1183 * ifx_spi_spi_remove - SPI device was removed
1184 * @spi: SPI device
1185 *
1186 * FIXME: We should be shutting the device down here not in
1187 * the module unload path.
1188 */
1189
1190static int ifx_spi_spi_remove(struct spi_device *spi)
1191{
1192 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1193 /* stop activity */
1194 tasklet_kill(&ifx_dev->io_work_tasklet);
1195 /* free irq */
1196 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1197 free_irq(gpio_to_irq(ifx_dev->gpio.srdy), (void *)ifx_dev);
1198
1199 gpio_free(ifx_dev->gpio.srdy);
1200 gpio_free(ifx_dev->gpio.mrdy);
1201 gpio_free(ifx_dev->gpio.reset);
1202 gpio_free(ifx_dev->gpio.po);
1203 gpio_free(ifx_dev->gpio.reset_out);
1204
1205 /* free allocations */
1206 ifx_spi_free_device(ifx_dev);
1207
1208 saved_ifx_dev = NULL;
1209 return 0;
1210}
1211
1212/**
1213 * ifx_spi_spi_shutdown - called on SPI shutdown
1214 * @spi: SPI device
1215 *
1216 * No action needs to be taken here
1217 */
1218
1219static void ifx_spi_spi_shutdown(struct spi_device *spi)
1220{
1221}
1222
1223/*
1224 * various suspends and resumes have nothing to do
1225 * no hardware to save state for
1226 */
1227
1228/**
1229 * ifx_spi_spi_suspend - suspend SPI on system suspend
1230 * @dev: device being suspended
1231 *
1232 * Suspend the SPI side. No action needed on Intel MID platforms, may
1233 * need extending for other systems.
1234 */
1235static int ifx_spi_spi_suspend(struct spi_device *spi, pm_message_t msg)
1236{
1237 return 0;
1238}
1239
1240/**
1241 * ifx_spi_spi_resume - resume SPI side on system resume
1242 * @dev: device being suspended
1243 *
1244 * Suspend the SPI side. No action needed on Intel MID platforms, may
1245 * need extending for other systems.
1246 */
1247static int ifx_spi_spi_resume(struct spi_device *spi)
1248{
1249 return 0;
1250}
1251
1252/**
1253 * ifx_spi_pm_suspend - suspend modem on system suspend
1254 * @dev: device being suspended
1255 *
1256 * Suspend the modem. No action needed on Intel MID platforms, may
1257 * need extending for other systems.
1258 */
1259static int ifx_spi_pm_suspend(struct device *dev)
1260{
1261 return 0;
1262}
1263
1264/**
1265 * ifx_spi_pm_resume - resume modem on system resume
1266 * @dev: device being suspended
1267 *
1268 * Allow the modem to resume. No action needed.
1269 *
1270 * FIXME: do we need to reset anything here ?
1271 */
1272static int ifx_spi_pm_resume(struct device *dev)
1273{
1274 return 0;
1275}
1276
1277/**
1278 * ifx_spi_pm_runtime_resume - suspend modem
1279 * @dev: device being suspended
1280 *
1281 * Allow the modem to resume. No action needed.
1282 */
1283static int ifx_spi_pm_runtime_resume(struct device *dev)
1284{
1285 return 0;
1286}
1287
1288/**
1289 * ifx_spi_pm_runtime_suspend - suspend modem
1290 * @dev: device being suspended
1291 *
1292 * Allow the modem to suspend and thus suspend to continue up the
1293 * device tree.
1294 */
1295static int ifx_spi_pm_runtime_suspend(struct device *dev)
1296{
1297 return 0;
1298}
1299
1300/**
1301 * ifx_spi_pm_runtime_idle - check if modem idle
1302 * @dev: our device
1303 *
1304 * Check conditions and queue runtime suspend if idle.
1305 */
1306static int ifx_spi_pm_runtime_idle(struct device *dev)
1307{
1308 struct spi_device *spi = to_spi_device(dev);
1309 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1310
1311 if (!ifx_dev->power_status)
1312 pm_runtime_suspend(dev);
1313
1314 return 0;
1315}
1316
1317static const struct dev_pm_ops ifx_spi_pm = {
1318 .resume = ifx_spi_pm_resume,
1319 .suspend = ifx_spi_pm_suspend,
1320 .runtime_resume = ifx_spi_pm_runtime_resume,
1321 .runtime_suspend = ifx_spi_pm_runtime_suspend,
1322 .runtime_idle = ifx_spi_pm_runtime_idle
1323};
1324
1325static const struct spi_device_id ifx_id_table[] = {
1326 {"ifx6160", 0},
1327 {"ifx6260", 0},
1328 { }
1329};
1330MODULE_DEVICE_TABLE(spi, ifx_id_table);
1331
1332/* spi operations */
1333static const struct spi_driver ifx_spi_driver = {
1334 .driver = {
1335 .name = DRVNAME,
1336 .bus = &spi_bus_type,
1337 .pm = &ifx_spi_pm,
1338 .owner = THIS_MODULE},
1339 .probe = ifx_spi_spi_probe,
1340 .shutdown = ifx_spi_spi_shutdown,
1341 .remove = __devexit_p(ifx_spi_spi_remove),
1342 .suspend = ifx_spi_spi_suspend,
1343 .resume = ifx_spi_spi_resume,
1344 .id_table = ifx_id_table
1345};
1346
1347/**
1348 * ifx_spi_exit - module exit
1349 *
1350 * Unload the module.
1351 */
1352
1353static void __exit ifx_spi_exit(void)
1354{
1355 /* unregister */
1356 tty_unregister_driver(tty_drv);
1357 spi_unregister_driver((void *)&ifx_spi_driver);
1358}
1359
1360/**
1361 * ifx_spi_init - module entry point
1362 *
1363 * Initialise the SPI and tty interfaces for the IFX SPI driver
1364 * We need to initialize upper-edge spi driver after the tty
1365 * driver because otherwise the spi probe will race
1366 */
1367
1368static int __init ifx_spi_init(void)
1369{
1370 int result;
1371
1372 tty_drv = alloc_tty_driver(1);
1373 if (!tty_drv) {
1374 pr_err("%s: alloc_tty_driver failed", DRVNAME);
1375 return -ENOMEM;
1376 }
1377
1378 tty_drv->magic = TTY_DRIVER_MAGIC;
1379 tty_drv->owner = THIS_MODULE;
1380 tty_drv->driver_name = DRVNAME;
1381 tty_drv->name = TTYNAME;
1382 tty_drv->minor_start = IFX_SPI_TTY_ID;
1383 tty_drv->num = 1;
1384 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1385 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1386 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1387 tty_drv->init_termios = tty_std_termios;
1388
1389 tty_set_operations(tty_drv, &ifx_spi_serial_ops);
1390
1391 result = tty_register_driver(tty_drv);
1392 if (result) {
1393 pr_err("%s: tty_register_driver failed(%d)",
1394 DRVNAME, result);
1395 put_tty_driver(tty_drv);
1396 return result;
1397 }
1398
1399 result = spi_register_driver((void *)&ifx_spi_driver);
1400 if (result) {
1401 pr_err("%s: spi_register_driver failed(%d)",
1402 DRVNAME, result);
1403 tty_unregister_driver(tty_drv);
1404 }
1405 return result;
1406}
1407
1408module_init(ifx_spi_init);
1409module_exit(ifx_spi_exit);
1410
1411MODULE_AUTHOR("Intel");
1412MODULE_DESCRIPTION("IFX6x60 spi driver");
1413MODULE_LICENSE("GPL");
1414MODULE_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..e8464baf9e75
--- /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_SPI_MAX_MINORS 1
33#define IFX_SPI_TRANSFER_SIZE 2048
34#define IFX_SPI_FIFO_SIZE 4096
35
36#define IFX_SPI_HEADER_OVERHEAD 4
37#define IFX_RESET_TIMEOUT msecs_to_jiffies(50)
38
39/* device flags bitfield definitions */
40#define IFX_SPI_STATE_PRESENT 0
41#define IFX_SPI_STATE_IO_IN_PROGRESS 1
42#define IFX_SPI_STATE_IO_READY 2
43#define IFX_SPI_STATE_TIMER_PENDING 3
44
45/* flow control bitfields */
46#define IFX_SPI_DCD 0
47#define IFX_SPI_CTS 1
48#define IFX_SPI_DSR 2
49#define IFX_SPI_RI 3
50#define IFX_SPI_DTR 4
51#define IFX_SPI_RTS 5
52#define IFX_SPI_TX_FC 6
53#define IFX_SPI_RX_FC 7
54#define IFX_SPI_UPDATE 8
55
56#define IFX_SPI_PAYLOAD_SIZE (IFX_SPI_TRANSFER_SIZE - \
57 IFX_SPI_HEADER_OVERHEAD)
58
59#define IFX_SPI_IRQ_TYPE DETECT_EDGE_RISING
60#define IFX_SPI_GPIO_TARGET 0
61#define IFX_SPI_GPIO0 0x105
62
63#define IFX_SPI_STATUS_TIMEOUT (2000*HZ)
64
65/* values for bits in power status byte */
66#define IFX_SPI_POWER_DATA_PENDING 1
67#define IFX_SPI_POWER_SRDY 2
68
69struct ifx_spi_device {
70 /* Our SPI device */
71 struct spi_device *spi_dev;
72
73 /* Port specific data */
74 struct kfifo tx_fifo;
75 spinlock_t fifo_lock;
76 unsigned long signal_state;
77
78 /* TTY Layer logic */
79 struct tty_port tty_port;
80 struct device *tty_dev;
81 int minor;
82
83 /* Low level I/O work */
84 struct tasklet_struct io_work_tasklet;
85 unsigned long flags;
86 dma_addr_t rx_dma;
87 dma_addr_t tx_dma;
88
89 int modem; /* Modem type */
90 int use_dma; /* provide dma-able addrs in SPI msg */
91 long max_hz; /* max SPI frequency */
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..a54473123e0a
--- /dev/null
+++ b/drivers/tty/serial/imx.c
@@ -0,0 +1,1379 @@
1/*
2 * Driver for Motorola IMX serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Author: Sascha Hauer <sascha@saschahauer.de>
7 * Copyright (C) 2004 Pengutronix
8 *
9 * Copyright (C) 2009 emlix GmbH
10 * Author: Fabian Godehardt (added IrDA support for iMX)
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 * [29-Mar-2005] Mike Lee
27 * Added hardware handshake
28 */
29
30#if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
31#define SUPPORT_SYSRQ
32#endif
33
34#include <linux/module.h>
35#include <linux/ioport.h>
36#include <linux/init.h>
37#include <linux/console.h>
38#include <linux/sysrq.h>
39#include <linux/platform_device.h>
40#include <linux/tty.h>
41#include <linux/tty_flip.h>
42#include <linux/serial_core.h>
43#include <linux/serial.h>
44#include <linux/clk.h>
45#include <linux/delay.h>
46#include <linux/rational.h>
47#include <linux/slab.h>
48
49#include <asm/io.h>
50#include <asm/irq.h>
51#include <mach/hardware.h>
52#include <mach/imx-uart.h>
53
54/* Register definitions */
55#define URXD0 0x0 /* Receiver Register */
56#define URTX0 0x40 /* Transmitter Register */
57#define UCR1 0x80 /* Control Register 1 */
58#define UCR2 0x84 /* Control Register 2 */
59#define UCR3 0x88 /* Control Register 3 */
60#define UCR4 0x8c /* Control Register 4 */
61#define UFCR 0x90 /* FIFO Control Register */
62#define USR1 0x94 /* Status Register 1 */
63#define USR2 0x98 /* Status Register 2 */
64#define UESC 0x9c /* Escape Character Register */
65#define UTIM 0xa0 /* Escape Timer Register */
66#define UBIR 0xa4 /* BRM Incremental Register */
67#define UBMR 0xa8 /* BRM Modulator Register */
68#define UBRC 0xac /* Baud Rate Count Register */
69#define MX2_ONEMS 0xb0 /* One Millisecond register */
70#define UTS (cpu_is_mx1() ? 0xd0 : 0xb4) /* UART Test Register */
71
72/* UART Control Register Bit Fields.*/
73#define URXD_CHARRDY (1<<15)
74#define URXD_ERR (1<<14)
75#define URXD_OVRRUN (1<<13)
76#define URXD_FRMERR (1<<12)
77#define URXD_BRK (1<<11)
78#define URXD_PRERR (1<<10)
79#define UCR1_ADEN (1<<15) /* Auto detect interrupt */
80#define UCR1_ADBR (1<<14) /* Auto detect baud rate */
81#define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
82#define UCR1_IDEN (1<<12) /* Idle condition interrupt */
83#define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
84#define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
85#define UCR1_IREN (1<<7) /* Infrared interface enable */
86#define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
87#define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
88#define UCR1_SNDBRK (1<<4) /* Send break */
89#define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
90#define MX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, mx1 only */
91#define UCR1_DOZE (1<<1) /* Doze */
92#define UCR1_UARTEN (1<<0) /* UART enabled */
93#define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
94#define UCR2_IRTS (1<<14) /* Ignore RTS pin */
95#define UCR2_CTSC (1<<13) /* CTS pin control */
96#define UCR2_CTS (1<<12) /* Clear to send */
97#define UCR2_ESCEN (1<<11) /* Escape enable */
98#define UCR2_PREN (1<<8) /* Parity enable */
99#define UCR2_PROE (1<<7) /* Parity odd/even */
100#define UCR2_STPB (1<<6) /* Stop */
101#define UCR2_WS (1<<5) /* Word size */
102#define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
103#define UCR2_TXEN (1<<2) /* Transmitter enabled */
104#define UCR2_RXEN (1<<1) /* Receiver enabled */
105#define UCR2_SRST (1<<0) /* SW reset */
106#define UCR3_DTREN (1<<13) /* DTR interrupt enable */
107#define UCR3_PARERREN (1<<12) /* Parity enable */
108#define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
109#define UCR3_DSR (1<<10) /* Data set ready */
110#define UCR3_DCD (1<<9) /* Data carrier detect */
111#define UCR3_RI (1<<8) /* Ring indicator */
112#define UCR3_TIMEOUTEN (1<<7) /* Timeout interrupt enable */
113#define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
114#define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
115#define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
116#define MX1_UCR3_REF25 (1<<3) /* Ref freq 25 MHz, only on mx1 */
117#define MX1_UCR3_REF30 (1<<2) /* Ref Freq 30 MHz, only on mx1 */
118#define MX2_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select, on mx2/mx3 */
119#define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
120#define UCR3_BPEN (1<<0) /* Preset registers enable */
121#define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
122#define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
123#define UCR4_INVR (1<<9) /* Inverted infrared reception */
124#define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
125#define UCR4_WKEN (1<<7) /* Wake interrupt enable */
126#define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
127#define UCR4_IRSC (1<<5) /* IR special case */
128#define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
129#define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
130#define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
131#define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
132#define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
133#define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
134#define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
135#define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
136#define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
137#define USR1_RTSS (1<<14) /* RTS pin status */
138#define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
139#define USR1_RTSD (1<<12) /* RTS delta */
140#define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
141#define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
142#define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
143#define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
144#define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
145#define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
146#define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
147#define USR2_ADET (1<<15) /* Auto baud rate detect complete */
148#define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
149#define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
150#define USR2_IDLE (1<<12) /* Idle condition */
151#define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
152#define USR2_WAKE (1<<7) /* Wake */
153#define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
154#define USR2_TXDC (1<<3) /* Transmitter complete */
155#define USR2_BRCD (1<<2) /* Break condition */
156#define USR2_ORE (1<<1) /* Overrun error */
157#define USR2_RDR (1<<0) /* Recv data ready */
158#define UTS_FRCPERR (1<<13) /* Force parity error */
159#define UTS_LOOP (1<<12) /* Loop tx and rx */
160#define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
161#define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
162#define UTS_TXFULL (1<<4) /* TxFIFO full */
163#define UTS_RXFULL (1<<3) /* RxFIFO full */
164#define UTS_SOFTRST (1<<0) /* Software reset */
165
166/* We've been assigned a range on the "Low-density serial ports" major */
167#define SERIAL_IMX_MAJOR 207
168#define MINOR_START 16
169#define DEV_NAME "ttymxc"
170#define MAX_INTERNAL_IRQ MXC_INTERNAL_IRQS
171
172/*
173 * This determines how often we check the modem status signals
174 * for any change. They generally aren't connected to an IRQ
175 * so we have to poll them. We also check immediately before
176 * filling the TX fifo incase CTS has been dropped.
177 */
178#define MCTRL_TIMEOUT (250*HZ/1000)
179
180#define DRIVER_NAME "IMX-uart"
181
182#define UART_NR 8
183
184struct imx_port {
185 struct uart_port port;
186 struct timer_list timer;
187 unsigned int old_status;
188 int txirq,rxirq,rtsirq;
189 unsigned int have_rtscts:1;
190 unsigned int use_irda:1;
191 unsigned int irda_inv_rx:1;
192 unsigned int irda_inv_tx:1;
193 unsigned short trcv_delay; /* transceiver delay */
194 struct clk *clk;
195};
196
197#ifdef CONFIG_IRDA
198#define USE_IRDA(sport) ((sport)->use_irda)
199#else
200#define USE_IRDA(sport) (0)
201#endif
202
203/*
204 * Handle any change of modem status signal since we were last called.
205 */
206static void imx_mctrl_check(struct imx_port *sport)
207{
208 unsigned int status, changed;
209
210 status = sport->port.ops->get_mctrl(&sport->port);
211 changed = status ^ sport->old_status;
212
213 if (changed == 0)
214 return;
215
216 sport->old_status = status;
217
218 if (changed & TIOCM_RI)
219 sport->port.icount.rng++;
220 if (changed & TIOCM_DSR)
221 sport->port.icount.dsr++;
222 if (changed & TIOCM_CAR)
223 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
224 if (changed & TIOCM_CTS)
225 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
226
227 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
228}
229
230/*
231 * This is our per-port timeout handler, for checking the
232 * modem status signals.
233 */
234static void imx_timeout(unsigned long data)
235{
236 struct imx_port *sport = (struct imx_port *)data;
237 unsigned long flags;
238
239 if (sport->port.state) {
240 spin_lock_irqsave(&sport->port.lock, flags);
241 imx_mctrl_check(sport);
242 spin_unlock_irqrestore(&sport->port.lock, flags);
243
244 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
245 }
246}
247
248/*
249 * interrupts disabled on entry
250 */
251static void imx_stop_tx(struct uart_port *port)
252{
253 struct imx_port *sport = (struct imx_port *)port;
254 unsigned long temp;
255
256 if (USE_IRDA(sport)) {
257 /* half duplex - wait for end of transmission */
258 int n = 256;
259 while ((--n > 0) &&
260 !(readl(sport->port.membase + USR2) & USR2_TXDC)) {
261 udelay(5);
262 barrier();
263 }
264 /*
265 * irda transceiver - wait a bit more to avoid
266 * cutoff, hardware dependent
267 */
268 udelay(sport->trcv_delay);
269
270 /*
271 * half duplex - reactivate receive mode,
272 * flush receive pipe echo crap
273 */
274 if (readl(sport->port.membase + USR2) & USR2_TXDC) {
275 temp = readl(sport->port.membase + UCR1);
276 temp &= ~(UCR1_TXMPTYEN | UCR1_TRDYEN);
277 writel(temp, sport->port.membase + UCR1);
278
279 temp = readl(sport->port.membase + UCR4);
280 temp &= ~(UCR4_TCEN);
281 writel(temp, sport->port.membase + UCR4);
282
283 while (readl(sport->port.membase + URXD0) &
284 URXD_CHARRDY)
285 barrier();
286
287 temp = readl(sport->port.membase + UCR1);
288 temp |= UCR1_RRDYEN;
289 writel(temp, sport->port.membase + UCR1);
290
291 temp = readl(sport->port.membase + UCR4);
292 temp |= UCR4_DREN;
293 writel(temp, sport->port.membase + UCR4);
294 }
295 return;
296 }
297
298 temp = readl(sport->port.membase + UCR1);
299 writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
300}
301
302/*
303 * interrupts disabled on entry
304 */
305static void imx_stop_rx(struct uart_port *port)
306{
307 struct imx_port *sport = (struct imx_port *)port;
308 unsigned long temp;
309
310 temp = readl(sport->port.membase + UCR2);
311 writel(temp &~ UCR2_RXEN, sport->port.membase + UCR2);
312}
313
314/*
315 * Set the modem control timer to fire immediately.
316 */
317static void imx_enable_ms(struct uart_port *port)
318{
319 struct imx_port *sport = (struct imx_port *)port;
320
321 mod_timer(&sport->timer, jiffies);
322}
323
324static inline void imx_transmit_buffer(struct imx_port *sport)
325{
326 struct circ_buf *xmit = &sport->port.state->xmit;
327
328 while (!uart_circ_empty(xmit) &&
329 !(readl(sport->port.membase + UTS) & UTS_TXFULL)) {
330 /* send xmit->buf[xmit->tail]
331 * out the port here */
332 writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
333 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
334 sport->port.icount.tx++;
335 }
336
337 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
338 uart_write_wakeup(&sport->port);
339
340 if (uart_circ_empty(xmit))
341 imx_stop_tx(&sport->port);
342}
343
344/*
345 * interrupts disabled on entry
346 */
347static void imx_start_tx(struct uart_port *port)
348{
349 struct imx_port *sport = (struct imx_port *)port;
350 unsigned long temp;
351
352 if (USE_IRDA(sport)) {
353 /* half duplex in IrDA mode; have to disable receive mode */
354 temp = readl(sport->port.membase + UCR4);
355 temp &= ~(UCR4_DREN);
356 writel(temp, sport->port.membase + UCR4);
357
358 temp = readl(sport->port.membase + UCR1);
359 temp &= ~(UCR1_RRDYEN);
360 writel(temp, sport->port.membase + UCR1);
361 }
362
363 temp = readl(sport->port.membase + UCR1);
364 writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
365
366 if (USE_IRDA(sport)) {
367 temp = readl(sport->port.membase + UCR1);
368 temp |= UCR1_TRDYEN;
369 writel(temp, sport->port.membase + UCR1);
370
371 temp = readl(sport->port.membase + UCR4);
372 temp |= UCR4_TCEN;
373 writel(temp, sport->port.membase + UCR4);
374 }
375
376 if (readl(sport->port.membase + UTS) & UTS_TXEMPTY)
377 imx_transmit_buffer(sport);
378}
379
380static irqreturn_t imx_rtsint(int irq, void *dev_id)
381{
382 struct imx_port *sport = dev_id;
383 unsigned int val;
384 unsigned long flags;
385
386 spin_lock_irqsave(&sport->port.lock, flags);
387
388 writel(USR1_RTSD, sport->port.membase + USR1);
389 val = readl(sport->port.membase + USR1) & USR1_RTSS;
390 uart_handle_cts_change(&sport->port, !!val);
391 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
392
393 spin_unlock_irqrestore(&sport->port.lock, flags);
394 return IRQ_HANDLED;
395}
396
397static irqreturn_t imx_txint(int irq, void *dev_id)
398{
399 struct imx_port *sport = dev_id;
400 struct circ_buf *xmit = &sport->port.state->xmit;
401 unsigned long flags;
402
403 spin_lock_irqsave(&sport->port.lock,flags);
404 if (sport->port.x_char)
405 {
406 /* Send next char */
407 writel(sport->port.x_char, sport->port.membase + URTX0);
408 goto out;
409 }
410
411 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
412 imx_stop_tx(&sport->port);
413 goto out;
414 }
415
416 imx_transmit_buffer(sport);
417
418 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
419 uart_write_wakeup(&sport->port);
420
421out:
422 spin_unlock_irqrestore(&sport->port.lock,flags);
423 return IRQ_HANDLED;
424}
425
426static irqreturn_t imx_rxint(int irq, void *dev_id)
427{
428 struct imx_port *sport = dev_id;
429 unsigned int rx,flg,ignored = 0;
430 struct tty_struct *tty = sport->port.state->port.tty;
431 unsigned long flags, temp;
432
433 spin_lock_irqsave(&sport->port.lock,flags);
434
435 while (readl(sport->port.membase + USR2) & USR2_RDR) {
436 flg = TTY_NORMAL;
437 sport->port.icount.rx++;
438
439 rx = readl(sport->port.membase + URXD0);
440
441 temp = readl(sport->port.membase + USR2);
442 if (temp & USR2_BRCD) {
443 writel(USR2_BRCD, sport->port.membase + USR2);
444 if (uart_handle_break(&sport->port))
445 continue;
446 }
447
448 if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
449 continue;
450
451 if (rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) ) {
452 if (rx & URXD_PRERR)
453 sport->port.icount.parity++;
454 else if (rx & URXD_FRMERR)
455 sport->port.icount.frame++;
456 if (rx & URXD_OVRRUN)
457 sport->port.icount.overrun++;
458
459 if (rx & sport->port.ignore_status_mask) {
460 if (++ignored > 100)
461 goto out;
462 continue;
463 }
464
465 rx &= sport->port.read_status_mask;
466
467 if (rx & URXD_PRERR)
468 flg = TTY_PARITY;
469 else if (rx & URXD_FRMERR)
470 flg = TTY_FRAME;
471 if (rx & URXD_OVRRUN)
472 flg = TTY_OVERRUN;
473
474#ifdef SUPPORT_SYSRQ
475 sport->port.sysrq = 0;
476#endif
477 }
478
479 tty_insert_flip_char(tty, rx, flg);
480 }
481
482out:
483 spin_unlock_irqrestore(&sport->port.lock,flags);
484 tty_flip_buffer_push(tty);
485 return IRQ_HANDLED;
486}
487
488static irqreturn_t imx_int(int irq, void *dev_id)
489{
490 struct imx_port *sport = dev_id;
491 unsigned int sts;
492
493 sts = readl(sport->port.membase + USR1);
494
495 if (sts & USR1_RRDY)
496 imx_rxint(irq, dev_id);
497
498 if (sts & USR1_TRDY &&
499 readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN)
500 imx_txint(irq, dev_id);
501
502 if (sts & USR1_RTSD)
503 imx_rtsint(irq, dev_id);
504
505 return IRQ_HANDLED;
506}
507
508/*
509 * Return TIOCSER_TEMT when transmitter is not busy.
510 */
511static unsigned int imx_tx_empty(struct uart_port *port)
512{
513 struct imx_port *sport = (struct imx_port *)port;
514
515 return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
516}
517
518/*
519 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
520 */
521static unsigned int imx_get_mctrl(struct uart_port *port)
522{
523 struct imx_port *sport = (struct imx_port *)port;
524 unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
525
526 if (readl(sport->port.membase + USR1) & USR1_RTSS)
527 tmp |= TIOCM_CTS;
528
529 if (readl(sport->port.membase + UCR2) & UCR2_CTS)
530 tmp |= TIOCM_RTS;
531
532 return tmp;
533}
534
535static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
536{
537 struct imx_port *sport = (struct imx_port *)port;
538 unsigned long temp;
539
540 temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
541
542 if (mctrl & TIOCM_RTS)
543 temp |= UCR2_CTS;
544
545 writel(temp, sport->port.membase + UCR2);
546}
547
548/*
549 * Interrupts always disabled.
550 */
551static void imx_break_ctl(struct uart_port *port, int break_state)
552{
553 struct imx_port *sport = (struct imx_port *)port;
554 unsigned long flags, temp;
555
556 spin_lock_irqsave(&sport->port.lock, flags);
557
558 temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
559
560 if ( break_state != 0 )
561 temp |= UCR1_SNDBRK;
562
563 writel(temp, sport->port.membase + UCR1);
564
565 spin_unlock_irqrestore(&sport->port.lock, flags);
566}
567
568#define TXTL 2 /* reset default */
569#define RXTL 1 /* reset default */
570
571static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
572{
573 unsigned int val;
574 unsigned int ufcr_rfdiv;
575
576 /* set receiver / transmitter trigger level.
577 * RFDIV is set such way to satisfy requested uartclk value
578 */
579 val = TXTL << 10 | RXTL;
580 ufcr_rfdiv = (clk_get_rate(sport->clk) + sport->port.uartclk / 2)
581 / sport->port.uartclk;
582
583 if(!ufcr_rfdiv)
584 ufcr_rfdiv = 1;
585
586 val |= UFCR_RFDIV_REG(ufcr_rfdiv);
587
588 writel(val, sport->port.membase + UFCR);
589
590 return 0;
591}
592
593/* half the RX buffer size */
594#define CTSTL 16
595
596static int imx_startup(struct uart_port *port)
597{
598 struct imx_port *sport = (struct imx_port *)port;
599 int retval;
600 unsigned long flags, temp;
601
602 imx_setup_ufcr(sport, 0);
603
604 /* disable the DREN bit (Data Ready interrupt enable) before
605 * requesting IRQs
606 */
607 temp = readl(sport->port.membase + UCR4);
608
609 if (USE_IRDA(sport))
610 temp |= UCR4_IRSC;
611
612 /* set the trigger level for CTS */
613 temp &= ~(UCR4_CTSTL_MASK<< UCR4_CTSTL_SHF);
614 temp |= CTSTL<< UCR4_CTSTL_SHF;
615
616 writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
617
618 if (USE_IRDA(sport)) {
619 /* reset fifo's and state machines */
620 int i = 100;
621 temp = readl(sport->port.membase + UCR2);
622 temp &= ~UCR2_SRST;
623 writel(temp, sport->port.membase + UCR2);
624 while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) &&
625 (--i > 0)) {
626 udelay(1);
627 }
628 }
629
630 /*
631 * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
632 * chips only have one interrupt.
633 */
634 if (sport->txirq > 0) {
635 retval = request_irq(sport->rxirq, imx_rxint, 0,
636 DRIVER_NAME, sport);
637 if (retval)
638 goto error_out1;
639
640 retval = request_irq(sport->txirq, imx_txint, 0,
641 DRIVER_NAME, sport);
642 if (retval)
643 goto error_out2;
644
645 /* do not use RTS IRQ on IrDA */
646 if (!USE_IRDA(sport)) {
647 retval = request_irq(sport->rtsirq, imx_rtsint,
648 (sport->rtsirq < MAX_INTERNAL_IRQ) ? 0 :
649 IRQF_TRIGGER_FALLING |
650 IRQF_TRIGGER_RISING,
651 DRIVER_NAME, sport);
652 if (retval)
653 goto error_out3;
654 }
655 } else {
656 retval = request_irq(sport->port.irq, imx_int, 0,
657 DRIVER_NAME, sport);
658 if (retval) {
659 free_irq(sport->port.irq, sport);
660 goto error_out1;
661 }
662 }
663
664 /*
665 * Finally, clear and enable interrupts
666 */
667 writel(USR1_RTSD, sport->port.membase + USR1);
668
669 temp = readl(sport->port.membase + UCR1);
670 temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
671
672 if (USE_IRDA(sport)) {
673 temp |= UCR1_IREN;
674 temp &= ~(UCR1_RTSDEN);
675 }
676
677 writel(temp, sport->port.membase + UCR1);
678
679 temp = readl(sport->port.membase + UCR2);
680 temp |= (UCR2_RXEN | UCR2_TXEN);
681 writel(temp, sport->port.membase + UCR2);
682
683 if (USE_IRDA(sport)) {
684 /* clear RX-FIFO */
685 int i = 64;
686 while ((--i > 0) &&
687 (readl(sport->port.membase + URXD0) & URXD_CHARRDY)) {
688 barrier();
689 }
690 }
691
692 if (!cpu_is_mx1()) {
693 temp = readl(sport->port.membase + UCR3);
694 temp |= MX2_UCR3_RXDMUXSEL;
695 writel(temp, sport->port.membase + UCR3);
696 }
697
698 if (USE_IRDA(sport)) {
699 temp = readl(sport->port.membase + UCR4);
700 if (sport->irda_inv_rx)
701 temp |= UCR4_INVR;
702 else
703 temp &= ~(UCR4_INVR);
704 writel(temp | UCR4_DREN, sport->port.membase + UCR4);
705
706 temp = readl(sport->port.membase + UCR3);
707 if (sport->irda_inv_tx)
708 temp |= UCR3_INVT;
709 else
710 temp &= ~(UCR3_INVT);
711 writel(temp, sport->port.membase + UCR3);
712 }
713
714 /*
715 * Enable modem status interrupts
716 */
717 spin_lock_irqsave(&sport->port.lock,flags);
718 imx_enable_ms(&sport->port);
719 spin_unlock_irqrestore(&sport->port.lock,flags);
720
721 if (USE_IRDA(sport)) {
722 struct imxuart_platform_data *pdata;
723 pdata = sport->port.dev->platform_data;
724 sport->irda_inv_rx = pdata->irda_inv_rx;
725 sport->irda_inv_tx = pdata->irda_inv_tx;
726 sport->trcv_delay = pdata->transceiver_delay;
727 if (pdata->irda_enable)
728 pdata->irda_enable(1);
729 }
730
731 return 0;
732
733error_out3:
734 if (sport->txirq)
735 free_irq(sport->txirq, sport);
736error_out2:
737 if (sport->rxirq)
738 free_irq(sport->rxirq, sport);
739error_out1:
740 return retval;
741}
742
743static void imx_shutdown(struct uart_port *port)
744{
745 struct imx_port *sport = (struct imx_port *)port;
746 unsigned long temp;
747
748 temp = readl(sport->port.membase + UCR2);
749 temp &= ~(UCR2_TXEN);
750 writel(temp, sport->port.membase + UCR2);
751
752 if (USE_IRDA(sport)) {
753 struct imxuart_platform_data *pdata;
754 pdata = sport->port.dev->platform_data;
755 if (pdata->irda_enable)
756 pdata->irda_enable(0);
757 }
758
759 /*
760 * Stop our timer.
761 */
762 del_timer_sync(&sport->timer);
763
764 /*
765 * Free the interrupts
766 */
767 if (sport->txirq > 0) {
768 if (!USE_IRDA(sport))
769 free_irq(sport->rtsirq, sport);
770 free_irq(sport->txirq, sport);
771 free_irq(sport->rxirq, sport);
772 } else
773 free_irq(sport->port.irq, sport);
774
775 /*
776 * Disable all interrupts, port and break condition.
777 */
778
779 temp = readl(sport->port.membase + UCR1);
780 temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
781 if (USE_IRDA(sport))
782 temp &= ~(UCR1_IREN);
783
784 writel(temp, sport->port.membase + UCR1);
785}
786
787static void
788imx_set_termios(struct uart_port *port, struct ktermios *termios,
789 struct ktermios *old)
790{
791 struct imx_port *sport = (struct imx_port *)port;
792 unsigned long flags;
793 unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
794 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
795 unsigned int div, ufcr;
796 unsigned long num, denom;
797 uint64_t tdiv64;
798
799 /*
800 * If we don't support modem control lines, don't allow
801 * these to be set.
802 */
803 if (0) {
804 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
805 termios->c_cflag |= CLOCAL;
806 }
807
808 /*
809 * We only support CS7 and CS8.
810 */
811 while ((termios->c_cflag & CSIZE) != CS7 &&
812 (termios->c_cflag & CSIZE) != CS8) {
813 termios->c_cflag &= ~CSIZE;
814 termios->c_cflag |= old_csize;
815 old_csize = CS8;
816 }
817
818 if ((termios->c_cflag & CSIZE) == CS8)
819 ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
820 else
821 ucr2 = UCR2_SRST | UCR2_IRTS;
822
823 if (termios->c_cflag & CRTSCTS) {
824 if( sport->have_rtscts ) {
825 ucr2 &= ~UCR2_IRTS;
826 ucr2 |= UCR2_CTSC;
827 } else {
828 termios->c_cflag &= ~CRTSCTS;
829 }
830 }
831
832 if (termios->c_cflag & CSTOPB)
833 ucr2 |= UCR2_STPB;
834 if (termios->c_cflag & PARENB) {
835 ucr2 |= UCR2_PREN;
836 if (termios->c_cflag & PARODD)
837 ucr2 |= UCR2_PROE;
838 }
839
840 /*
841 * Ask the core to calculate the divisor for us.
842 */
843 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
844 quot = uart_get_divisor(port, baud);
845
846 spin_lock_irqsave(&sport->port.lock, flags);
847
848 sport->port.read_status_mask = 0;
849 if (termios->c_iflag & INPCK)
850 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
851 if (termios->c_iflag & (BRKINT | PARMRK))
852 sport->port.read_status_mask |= URXD_BRK;
853
854 /*
855 * Characters to ignore
856 */
857 sport->port.ignore_status_mask = 0;
858 if (termios->c_iflag & IGNPAR)
859 sport->port.ignore_status_mask |= URXD_PRERR;
860 if (termios->c_iflag & IGNBRK) {
861 sport->port.ignore_status_mask |= URXD_BRK;
862 /*
863 * If we're ignoring parity and break indicators,
864 * ignore overruns too (for real raw support).
865 */
866 if (termios->c_iflag & IGNPAR)
867 sport->port.ignore_status_mask |= URXD_OVRRUN;
868 }
869
870 del_timer_sync(&sport->timer);
871
872 /*
873 * Update the per-port timeout.
874 */
875 uart_update_timeout(port, termios->c_cflag, baud);
876
877 /*
878 * disable interrupts and drain transmitter
879 */
880 old_ucr1 = readl(sport->port.membase + UCR1);
881 writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
882 sport->port.membase + UCR1);
883
884 while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
885 barrier();
886
887 /* then, disable everything */
888 old_txrxen = readl(sport->port.membase + UCR2);
889 writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
890 sport->port.membase + UCR2);
891 old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
892
893 if (USE_IRDA(sport)) {
894 /*
895 * use maximum available submodule frequency to
896 * avoid missing short pulses due to low sampling rate
897 */
898 div = 1;
899 } else {
900 div = sport->port.uartclk / (baud * 16);
901 if (div > 7)
902 div = 7;
903 if (!div)
904 div = 1;
905 }
906
907 rational_best_approximation(16 * div * baud, sport->port.uartclk,
908 1 << 16, 1 << 16, &num, &denom);
909
910 tdiv64 = sport->port.uartclk;
911 tdiv64 *= num;
912 do_div(tdiv64, denom * 16 * div);
913 tty_termios_encode_baud_rate(termios,
914 (speed_t)tdiv64, (speed_t)tdiv64);
915
916 num -= 1;
917 denom -= 1;
918
919 ufcr = readl(sport->port.membase + UFCR);
920 ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
921 writel(ufcr, sport->port.membase + UFCR);
922
923 writel(num, sport->port.membase + UBIR);
924 writel(denom, sport->port.membase + UBMR);
925
926 if (!cpu_is_mx1())
927 writel(sport->port.uartclk / div / 1000,
928 sport->port.membase + MX2_ONEMS);
929
930 writel(old_ucr1, sport->port.membase + UCR1);
931
932 /* set the parity, stop bits and data size */
933 writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
934
935 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
936 imx_enable_ms(&sport->port);
937
938 spin_unlock_irqrestore(&sport->port.lock, flags);
939}
940
941static const char *imx_type(struct uart_port *port)
942{
943 struct imx_port *sport = (struct imx_port *)port;
944
945 return sport->port.type == PORT_IMX ? "IMX" : NULL;
946}
947
948/*
949 * Release the memory region(s) being used by 'port'.
950 */
951static void imx_release_port(struct uart_port *port)
952{
953 struct platform_device *pdev = to_platform_device(port->dev);
954 struct resource *mmres;
955
956 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
957 release_mem_region(mmres->start, mmres->end - mmres->start + 1);
958}
959
960/*
961 * Request the memory region(s) being used by 'port'.
962 */
963static int imx_request_port(struct uart_port *port)
964{
965 struct platform_device *pdev = to_platform_device(port->dev);
966 struct resource *mmres;
967 void *ret;
968
969 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
970 if (!mmres)
971 return -ENODEV;
972
973 ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
974 "imx-uart");
975
976 return ret ? 0 : -EBUSY;
977}
978
979/*
980 * Configure/autoconfigure the port.
981 */
982static void imx_config_port(struct uart_port *port, int flags)
983{
984 struct imx_port *sport = (struct imx_port *)port;
985
986 if (flags & UART_CONFIG_TYPE &&
987 imx_request_port(&sport->port) == 0)
988 sport->port.type = PORT_IMX;
989}
990
991/*
992 * Verify the new serial_struct (for TIOCSSERIAL).
993 * The only change we allow are to the flags and type, and
994 * even then only between PORT_IMX and PORT_UNKNOWN
995 */
996static int
997imx_verify_port(struct uart_port *port, struct serial_struct *ser)
998{
999 struct imx_port *sport = (struct imx_port *)port;
1000 int ret = 0;
1001
1002 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
1003 ret = -EINVAL;
1004 if (sport->port.irq != ser->irq)
1005 ret = -EINVAL;
1006 if (ser->io_type != UPIO_MEM)
1007 ret = -EINVAL;
1008 if (sport->port.uartclk / 16 != ser->baud_base)
1009 ret = -EINVAL;
1010 if ((void *)sport->port.mapbase != ser->iomem_base)
1011 ret = -EINVAL;
1012 if (sport->port.iobase != ser->port)
1013 ret = -EINVAL;
1014 if (ser->hub6 != 0)
1015 ret = -EINVAL;
1016 return ret;
1017}
1018
1019static struct uart_ops imx_pops = {
1020 .tx_empty = imx_tx_empty,
1021 .set_mctrl = imx_set_mctrl,
1022 .get_mctrl = imx_get_mctrl,
1023 .stop_tx = imx_stop_tx,
1024 .start_tx = imx_start_tx,
1025 .stop_rx = imx_stop_rx,
1026 .enable_ms = imx_enable_ms,
1027 .break_ctl = imx_break_ctl,
1028 .startup = imx_startup,
1029 .shutdown = imx_shutdown,
1030 .set_termios = imx_set_termios,
1031 .type = imx_type,
1032 .release_port = imx_release_port,
1033 .request_port = imx_request_port,
1034 .config_port = imx_config_port,
1035 .verify_port = imx_verify_port,
1036};
1037
1038static struct imx_port *imx_ports[UART_NR];
1039
1040#ifdef CONFIG_SERIAL_IMX_CONSOLE
1041static void imx_console_putchar(struct uart_port *port, int ch)
1042{
1043 struct imx_port *sport = (struct imx_port *)port;
1044
1045 while (readl(sport->port.membase + UTS) & UTS_TXFULL)
1046 barrier();
1047
1048 writel(ch, sport->port.membase + URTX0);
1049}
1050
1051/*
1052 * Interrupts are disabled on entering
1053 */
1054static void
1055imx_console_write(struct console *co, const char *s, unsigned int count)
1056{
1057 struct imx_port *sport = imx_ports[co->index];
1058 unsigned int old_ucr1, old_ucr2, ucr1;
1059
1060 /*
1061 * First, save UCR1/2 and then disable interrupts
1062 */
1063 ucr1 = old_ucr1 = readl(sport->port.membase + UCR1);
1064 old_ucr2 = readl(sport->port.membase + UCR2);
1065
1066 if (cpu_is_mx1())
1067 ucr1 |= MX1_UCR1_UARTCLKEN;
1068 ucr1 |= UCR1_UARTEN;
1069 ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
1070
1071 writel(ucr1, sport->port.membase + UCR1);
1072
1073 writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
1074
1075 uart_console_write(&sport->port, s, count, imx_console_putchar);
1076
1077 /*
1078 * Finally, wait for transmitter to become empty
1079 * and restore UCR1/2
1080 */
1081 while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
1082
1083 writel(old_ucr1, sport->port.membase + UCR1);
1084 writel(old_ucr2, sport->port.membase + UCR2);
1085}
1086
1087/*
1088 * If the port was already initialised (eg, by a boot loader),
1089 * try to determine the current setup.
1090 */
1091static void __init
1092imx_console_get_options(struct imx_port *sport, int *baud,
1093 int *parity, int *bits)
1094{
1095
1096 if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
1097 /* ok, the port was enabled */
1098 unsigned int ucr2, ubir,ubmr, uartclk;
1099 unsigned int baud_raw;
1100 unsigned int ucfr_rfdiv;
1101
1102 ucr2 = readl(sport->port.membase + UCR2);
1103
1104 *parity = 'n';
1105 if (ucr2 & UCR2_PREN) {
1106 if (ucr2 & UCR2_PROE)
1107 *parity = 'o';
1108 else
1109 *parity = 'e';
1110 }
1111
1112 if (ucr2 & UCR2_WS)
1113 *bits = 8;
1114 else
1115 *bits = 7;
1116
1117 ubir = readl(sport->port.membase + UBIR) & 0xffff;
1118 ubmr = readl(sport->port.membase + UBMR) & 0xffff;
1119
1120 ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
1121 if (ucfr_rfdiv == 6)
1122 ucfr_rfdiv = 7;
1123 else
1124 ucfr_rfdiv = 6 - ucfr_rfdiv;
1125
1126 uartclk = clk_get_rate(sport->clk);
1127 uartclk /= ucfr_rfdiv;
1128
1129 { /*
1130 * The next code provides exact computation of
1131 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
1132 * without need of float support or long long division,
1133 * which would be required to prevent 32bit arithmetic overflow
1134 */
1135 unsigned int mul = ubir + 1;
1136 unsigned int div = 16 * (ubmr + 1);
1137 unsigned int rem = uartclk % div;
1138
1139 baud_raw = (uartclk / div) * mul;
1140 baud_raw += (rem * mul + div / 2) / div;
1141 *baud = (baud_raw + 50) / 100 * 100;
1142 }
1143
1144 if(*baud != baud_raw)
1145 printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
1146 baud_raw, *baud);
1147 }
1148}
1149
1150static int __init
1151imx_console_setup(struct console *co, char *options)
1152{
1153 struct imx_port *sport;
1154 int baud = 9600;
1155 int bits = 8;
1156 int parity = 'n';
1157 int flow = 'n';
1158
1159 /*
1160 * Check whether an invalid uart number has been specified, and
1161 * if so, search for the first available port that does have
1162 * console support.
1163 */
1164 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
1165 co->index = 0;
1166 sport = imx_ports[co->index];
1167 if(sport == NULL)
1168 return -ENODEV;
1169
1170 if (options)
1171 uart_parse_options(options, &baud, &parity, &bits, &flow);
1172 else
1173 imx_console_get_options(sport, &baud, &parity, &bits);
1174
1175 imx_setup_ufcr(sport, 0);
1176
1177 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
1178}
1179
1180static struct uart_driver imx_reg;
1181static struct console imx_console = {
1182 .name = DEV_NAME,
1183 .write = imx_console_write,
1184 .device = uart_console_device,
1185 .setup = imx_console_setup,
1186 .flags = CON_PRINTBUFFER,
1187 .index = -1,
1188 .data = &imx_reg,
1189};
1190
1191#define IMX_CONSOLE &imx_console
1192#else
1193#define IMX_CONSOLE NULL
1194#endif
1195
1196static struct uart_driver imx_reg = {
1197 .owner = THIS_MODULE,
1198 .driver_name = DRIVER_NAME,
1199 .dev_name = DEV_NAME,
1200 .major = SERIAL_IMX_MAJOR,
1201 .minor = MINOR_START,
1202 .nr = ARRAY_SIZE(imx_ports),
1203 .cons = IMX_CONSOLE,
1204};
1205
1206static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
1207{
1208 struct imx_port *sport = platform_get_drvdata(dev);
1209
1210 if (sport)
1211 uart_suspend_port(&imx_reg, &sport->port);
1212
1213 return 0;
1214}
1215
1216static int serial_imx_resume(struct platform_device *dev)
1217{
1218 struct imx_port *sport = platform_get_drvdata(dev);
1219
1220 if (sport)
1221 uart_resume_port(&imx_reg, &sport->port);
1222
1223 return 0;
1224}
1225
1226static int serial_imx_probe(struct platform_device *pdev)
1227{
1228 struct imx_port *sport;
1229 struct imxuart_platform_data *pdata;
1230 void __iomem *base;
1231 int ret = 0;
1232 struct resource *res;
1233
1234 sport = kzalloc(sizeof(*sport), GFP_KERNEL);
1235 if (!sport)
1236 return -ENOMEM;
1237
1238 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1239 if (!res) {
1240 ret = -ENODEV;
1241 goto free;
1242 }
1243
1244 base = ioremap(res->start, PAGE_SIZE);
1245 if (!base) {
1246 ret = -ENOMEM;
1247 goto free;
1248 }
1249
1250 sport->port.dev = &pdev->dev;
1251 sport->port.mapbase = res->start;
1252 sport->port.membase = base;
1253 sport->port.type = PORT_IMX,
1254 sport->port.iotype = UPIO_MEM;
1255 sport->port.irq = platform_get_irq(pdev, 0);
1256 sport->rxirq = platform_get_irq(pdev, 0);
1257 sport->txirq = platform_get_irq(pdev, 1);
1258 sport->rtsirq = platform_get_irq(pdev, 2);
1259 sport->port.fifosize = 32;
1260 sport->port.ops = &imx_pops;
1261 sport->port.flags = UPF_BOOT_AUTOCONF;
1262 sport->port.line = pdev->id;
1263 init_timer(&sport->timer);
1264 sport->timer.function = imx_timeout;
1265 sport->timer.data = (unsigned long)sport;
1266
1267 sport->clk = clk_get(&pdev->dev, "uart");
1268 if (IS_ERR(sport->clk)) {
1269 ret = PTR_ERR(sport->clk);
1270 goto unmap;
1271 }
1272 clk_enable(sport->clk);
1273
1274 sport->port.uartclk = clk_get_rate(sport->clk);
1275
1276 imx_ports[pdev->id] = sport;
1277
1278 pdata = pdev->dev.platform_data;
1279 if (pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
1280 sport->have_rtscts = 1;
1281
1282#ifdef CONFIG_IRDA
1283 if (pdata && (pdata->flags & IMXUART_IRDA))
1284 sport->use_irda = 1;
1285#endif
1286
1287 if (pdata && pdata->init) {
1288 ret = pdata->init(pdev);
1289 if (ret)
1290 goto clkput;
1291 }
1292
1293 ret = uart_add_one_port(&imx_reg, &sport->port);
1294 if (ret)
1295 goto deinit;
1296 platform_set_drvdata(pdev, &sport->port);
1297
1298 return 0;
1299deinit:
1300 if (pdata && pdata->exit)
1301 pdata->exit(pdev);
1302clkput:
1303 clk_put(sport->clk);
1304 clk_disable(sport->clk);
1305unmap:
1306 iounmap(sport->port.membase);
1307free:
1308 kfree(sport);
1309
1310 return ret;
1311}
1312
1313static int serial_imx_remove(struct platform_device *pdev)
1314{
1315 struct imxuart_platform_data *pdata;
1316 struct imx_port *sport = platform_get_drvdata(pdev);
1317
1318 pdata = pdev->dev.platform_data;
1319
1320 platform_set_drvdata(pdev, NULL);
1321
1322 if (sport) {
1323 uart_remove_one_port(&imx_reg, &sport->port);
1324 clk_put(sport->clk);
1325 }
1326
1327 clk_disable(sport->clk);
1328
1329 if (pdata && pdata->exit)
1330 pdata->exit(pdev);
1331
1332 iounmap(sport->port.membase);
1333 kfree(sport);
1334
1335 return 0;
1336}
1337
1338static struct platform_driver serial_imx_driver = {
1339 .probe = serial_imx_probe,
1340 .remove = serial_imx_remove,
1341
1342 .suspend = serial_imx_suspend,
1343 .resume = serial_imx_resume,
1344 .driver = {
1345 .name = "imx-uart",
1346 .owner = THIS_MODULE,
1347 },
1348};
1349
1350static int __init imx_serial_init(void)
1351{
1352 int ret;
1353
1354 printk(KERN_INFO "Serial: IMX driver\n");
1355
1356 ret = uart_register_driver(&imx_reg);
1357 if (ret)
1358 return ret;
1359
1360 ret = platform_driver_register(&serial_imx_driver);
1361 if (ret != 0)
1362 uart_unregister_driver(&imx_reg);
1363
1364 return 0;
1365}
1366
1367static void __exit imx_serial_exit(void)
1368{
1369 platform_driver_unregister(&serial_imx_driver);
1370 uart_unregister_driver(&imx_reg);
1371}
1372
1373module_init(imx_serial_init);
1374module_exit(imx_serial_exit);
1375
1376MODULE_AUTHOR("Sascha Hauer");
1377MODULE_DESCRIPTION("IMX generic serial port driver");
1378MODULE_LICENSE("GPL");
1379MODULE_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..7b1cda59ebb5
--- /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 becoming 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..b704c8ce0d71
--- /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 - Receive 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..96da17868cf3
--- /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, pci_resource_len(pdev, 0));
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..4538c3e3646e
--- /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 guaranteed
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 occasionally 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..87e7e6c876d4
--- /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/lantiq.c b/drivers/tty/serial/lantiq.c
new file mode 100644
index 000000000000..58cf279ed879
--- /dev/null
+++ b/drivers/tty/serial/lantiq.c
@@ -0,0 +1,756 @@
1/*
2 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
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 * Copyright (C) 2004 Infineon IFAP DC COM CPE
18 * Copyright (C) 2007 Felix Fietkau <nbd@openwrt.org>
19 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
20 * Copyright (C) 2010 Thomas Langer, <thomas.langer@lantiq.com>
21 */
22
23#include <linux/slab.h>
24#include <linux/module.h>
25#include <linux/ioport.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/sysrq.h>
29#include <linux/device.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial_core.h>
33#include <linux/serial.h>
34#include <linux/platform_device.h>
35#include <linux/io.h>
36#include <linux/clk.h>
37
38#include <lantiq_soc.h>
39
40#define PORT_LTQ_ASC 111
41#define MAXPORTS 2
42#define UART_DUMMY_UER_RX 1
43#define DRVNAME "ltq_asc"
44#ifdef __BIG_ENDIAN
45#define LTQ_ASC_TBUF (0x0020 + 3)
46#define LTQ_ASC_RBUF (0x0024 + 3)
47#else
48#define LTQ_ASC_TBUF 0x0020
49#define LTQ_ASC_RBUF 0x0024
50#endif
51#define LTQ_ASC_FSTAT 0x0048
52#define LTQ_ASC_WHBSTATE 0x0018
53#define LTQ_ASC_STATE 0x0014
54#define LTQ_ASC_IRNCR 0x00F8
55#define LTQ_ASC_CLC 0x0000
56#define LTQ_ASC_ID 0x0008
57#define LTQ_ASC_PISEL 0x0004
58#define LTQ_ASC_TXFCON 0x0044
59#define LTQ_ASC_RXFCON 0x0040
60#define LTQ_ASC_CON 0x0010
61#define LTQ_ASC_BG 0x0050
62#define LTQ_ASC_IRNREN 0x00F4
63
64#define ASC_IRNREN_TX 0x1
65#define ASC_IRNREN_RX 0x2
66#define ASC_IRNREN_ERR 0x4
67#define ASC_IRNREN_TX_BUF 0x8
68#define ASC_IRNCR_TIR 0x1
69#define ASC_IRNCR_RIR 0x2
70#define ASC_IRNCR_EIR 0x4
71
72#define ASCOPT_CSIZE 0x3
73#define TXFIFO_FL 1
74#define RXFIFO_FL 1
75#define ASCCLC_DISS 0x2
76#define ASCCLC_RMCMASK 0x0000FF00
77#define ASCCLC_RMCOFFSET 8
78#define ASCCON_M_8ASYNC 0x0
79#define ASCCON_M_7ASYNC 0x2
80#define ASCCON_ODD 0x00000020
81#define ASCCON_STP 0x00000080
82#define ASCCON_BRS 0x00000100
83#define ASCCON_FDE 0x00000200
84#define ASCCON_R 0x00008000
85#define ASCCON_FEN 0x00020000
86#define ASCCON_ROEN 0x00080000
87#define ASCCON_TOEN 0x00100000
88#define ASCSTATE_PE 0x00010000
89#define ASCSTATE_FE 0x00020000
90#define ASCSTATE_ROE 0x00080000
91#define ASCSTATE_ANY (ASCSTATE_ROE|ASCSTATE_PE|ASCSTATE_FE)
92#define ASCWHBSTATE_CLRREN 0x00000001
93#define ASCWHBSTATE_SETREN 0x00000002
94#define ASCWHBSTATE_CLRPE 0x00000004
95#define ASCWHBSTATE_CLRFE 0x00000008
96#define ASCWHBSTATE_CLRROE 0x00000020
97#define ASCTXFCON_TXFEN 0x0001
98#define ASCTXFCON_TXFFLU 0x0002
99#define ASCTXFCON_TXFITLMASK 0x3F00
100#define ASCTXFCON_TXFITLOFF 8
101#define ASCRXFCON_RXFEN 0x0001
102#define ASCRXFCON_RXFFLU 0x0002
103#define ASCRXFCON_RXFITLMASK 0x3F00
104#define ASCRXFCON_RXFITLOFF 8
105#define ASCFSTAT_RXFFLMASK 0x003F
106#define ASCFSTAT_TXFFLMASK 0x3F00
107#define ASCFSTAT_TXFREEMASK 0x3F000000
108#define ASCFSTAT_TXFREEOFF 24
109
110static void lqasc_tx_chars(struct uart_port *port);
111static struct ltq_uart_port *lqasc_port[MAXPORTS];
112static struct uart_driver lqasc_reg;
113static DEFINE_SPINLOCK(ltq_asc_lock);
114
115struct ltq_uart_port {
116 struct uart_port port;
117 struct clk *clk;
118 unsigned int tx_irq;
119 unsigned int rx_irq;
120 unsigned int err_irq;
121};
122
123static inline struct
124ltq_uart_port *to_ltq_uart_port(struct uart_port *port)
125{
126 return container_of(port, struct ltq_uart_port, port);
127}
128
129static void
130lqasc_stop_tx(struct uart_port *port)
131{
132 return;
133}
134
135static void
136lqasc_start_tx(struct uart_port *port)
137{
138 unsigned long flags;
139 spin_lock_irqsave(&ltq_asc_lock, flags);
140 lqasc_tx_chars(port);
141 spin_unlock_irqrestore(&ltq_asc_lock, flags);
142 return;
143}
144
145static void
146lqasc_stop_rx(struct uart_port *port)
147{
148 ltq_w32(ASCWHBSTATE_CLRREN, port->membase + LTQ_ASC_WHBSTATE);
149}
150
151static void
152lqasc_enable_ms(struct uart_port *port)
153{
154}
155
156static int
157lqasc_rx_chars(struct uart_port *port)
158{
159 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
160 unsigned int ch = 0, rsr = 0, fifocnt;
161
162 if (!tty) {
163 dev_dbg(port->dev, "%s:tty is busy now", __func__);
164 return -EBUSY;
165 }
166 fifocnt =
167 ltq_r32(port->membase + LTQ_ASC_FSTAT) & ASCFSTAT_RXFFLMASK;
168 while (fifocnt--) {
169 u8 flag = TTY_NORMAL;
170 ch = ltq_r8(port->membase + LTQ_ASC_RBUF);
171 rsr = (ltq_r32(port->membase + LTQ_ASC_STATE)
172 & ASCSTATE_ANY) | UART_DUMMY_UER_RX;
173 tty_flip_buffer_push(tty);
174 port->icount.rx++;
175
176 /*
177 * Note that the error handling code is
178 * out of the main execution path
179 */
180 if (rsr & ASCSTATE_ANY) {
181 if (rsr & ASCSTATE_PE) {
182 port->icount.parity++;
183 ltq_w32_mask(0, ASCWHBSTATE_CLRPE,
184 port->membase + LTQ_ASC_WHBSTATE);
185 } else if (rsr & ASCSTATE_FE) {
186 port->icount.frame++;
187 ltq_w32_mask(0, ASCWHBSTATE_CLRFE,
188 port->membase + LTQ_ASC_WHBSTATE);
189 }
190 if (rsr & ASCSTATE_ROE) {
191 port->icount.overrun++;
192 ltq_w32_mask(0, ASCWHBSTATE_CLRROE,
193 port->membase + LTQ_ASC_WHBSTATE);
194 }
195
196 rsr &= port->read_status_mask;
197
198 if (rsr & ASCSTATE_PE)
199 flag = TTY_PARITY;
200 else if (rsr & ASCSTATE_FE)
201 flag = TTY_FRAME;
202 }
203
204 if ((rsr & port->ignore_status_mask) == 0)
205 tty_insert_flip_char(tty, ch, flag);
206
207 if (rsr & ASCSTATE_ROE)
208 /*
209 * Overrun is special, since it's reported
210 * immediately, and doesn't affect the current
211 * character
212 */
213 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
214 }
215 if (ch != 0)
216 tty_flip_buffer_push(tty);
217 tty_kref_put(tty);
218 return 0;
219}
220
221static void
222lqasc_tx_chars(struct uart_port *port)
223{
224 struct circ_buf *xmit = &port->state->xmit;
225 if (uart_tx_stopped(port)) {
226 lqasc_stop_tx(port);
227 return;
228 }
229
230 while (((ltq_r32(port->membase + LTQ_ASC_FSTAT) &
231 ASCFSTAT_TXFREEMASK) >> ASCFSTAT_TXFREEOFF) != 0) {
232 if (port->x_char) {
233 ltq_w8(port->x_char, port->membase + LTQ_ASC_TBUF);
234 port->icount.tx++;
235 port->x_char = 0;
236 continue;
237 }
238
239 if (uart_circ_empty(xmit))
240 break;
241
242 ltq_w8(port->state->xmit.buf[port->state->xmit.tail],
243 port->membase + LTQ_ASC_TBUF);
244 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
245 port->icount.tx++;
246 }
247
248 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
249 uart_write_wakeup(port);
250}
251
252static irqreturn_t
253lqasc_tx_int(int irq, void *_port)
254{
255 unsigned long flags;
256 struct uart_port *port = (struct uart_port *)_port;
257 spin_lock_irqsave(&ltq_asc_lock, flags);
258 ltq_w32(ASC_IRNCR_TIR, port->membase + LTQ_ASC_IRNCR);
259 spin_unlock_irqrestore(&ltq_asc_lock, flags);
260 lqasc_start_tx(port);
261 return IRQ_HANDLED;
262}
263
264static irqreturn_t
265lqasc_err_int(int irq, void *_port)
266{
267 unsigned long flags;
268 struct uart_port *port = (struct uart_port *)_port;
269 spin_lock_irqsave(&ltq_asc_lock, flags);
270 /* clear any pending interrupts */
271 ltq_w32_mask(0, ASCWHBSTATE_CLRPE | ASCWHBSTATE_CLRFE |
272 ASCWHBSTATE_CLRROE, port->membase + LTQ_ASC_WHBSTATE);
273 spin_unlock_irqrestore(&ltq_asc_lock, flags);
274 return IRQ_HANDLED;
275}
276
277static irqreturn_t
278lqasc_rx_int(int irq, void *_port)
279{
280 unsigned long flags;
281 struct uart_port *port = (struct uart_port *)_port;
282 spin_lock_irqsave(&ltq_asc_lock, flags);
283 ltq_w32(ASC_IRNCR_RIR, port->membase + LTQ_ASC_IRNCR);
284 lqasc_rx_chars(port);
285 spin_unlock_irqrestore(&ltq_asc_lock, flags);
286 return IRQ_HANDLED;
287}
288
289static unsigned int
290lqasc_tx_empty(struct uart_port *port)
291{
292 int status;
293 status = ltq_r32(port->membase + LTQ_ASC_FSTAT) & ASCFSTAT_TXFFLMASK;
294 return status ? 0 : TIOCSER_TEMT;
295}
296
297static unsigned int
298lqasc_get_mctrl(struct uart_port *port)
299{
300 return TIOCM_CTS | TIOCM_CAR | TIOCM_DSR;
301}
302
303static void
304lqasc_set_mctrl(struct uart_port *port, u_int mctrl)
305{
306}
307
308static void
309lqasc_break_ctl(struct uart_port *port, int break_state)
310{
311}
312
313static int
314lqasc_startup(struct uart_port *port)
315{
316 struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
317 int retval;
318
319 port->uartclk = clk_get_rate(ltq_port->clk);
320
321 ltq_w32_mask(ASCCLC_DISS | ASCCLC_RMCMASK, (1 << ASCCLC_RMCOFFSET),
322 port->membase + LTQ_ASC_CLC);
323
324 ltq_w32(0, port->membase + LTQ_ASC_PISEL);
325 ltq_w32(
326 ((TXFIFO_FL << ASCTXFCON_TXFITLOFF) & ASCTXFCON_TXFITLMASK) |
327 ASCTXFCON_TXFEN | ASCTXFCON_TXFFLU,
328 port->membase + LTQ_ASC_TXFCON);
329 ltq_w32(
330 ((RXFIFO_FL << ASCRXFCON_RXFITLOFF) & ASCRXFCON_RXFITLMASK)
331 | ASCRXFCON_RXFEN | ASCRXFCON_RXFFLU,
332 port->membase + LTQ_ASC_RXFCON);
333 /* make sure other settings are written to hardware before
334 * setting enable bits
335 */
336 wmb();
337 ltq_w32_mask(0, ASCCON_M_8ASYNC | ASCCON_FEN | ASCCON_TOEN |
338 ASCCON_ROEN, port->membase + LTQ_ASC_CON);
339
340 retval = request_irq(ltq_port->tx_irq, lqasc_tx_int,
341 IRQF_DISABLED, "asc_tx", port);
342 if (retval) {
343 pr_err("failed to request lqasc_tx_int\n");
344 return retval;
345 }
346
347 retval = request_irq(ltq_port->rx_irq, lqasc_rx_int,
348 IRQF_DISABLED, "asc_rx", port);
349 if (retval) {
350 pr_err("failed to request lqasc_rx_int\n");
351 goto err1;
352 }
353
354 retval = request_irq(ltq_port->err_irq, lqasc_err_int,
355 IRQF_DISABLED, "asc_err", port);
356 if (retval) {
357 pr_err("failed to request lqasc_err_int\n");
358 goto err2;
359 }
360
361 ltq_w32(ASC_IRNREN_RX | ASC_IRNREN_ERR | ASC_IRNREN_TX,
362 port->membase + LTQ_ASC_IRNREN);
363 return 0;
364
365err2:
366 free_irq(ltq_port->rx_irq, port);
367err1:
368 free_irq(ltq_port->tx_irq, port);
369 return retval;
370}
371
372static void
373lqasc_shutdown(struct uart_port *port)
374{
375 struct ltq_uart_port *ltq_port = to_ltq_uart_port(port);
376 free_irq(ltq_port->tx_irq, port);
377 free_irq(ltq_port->rx_irq, port);
378 free_irq(ltq_port->err_irq, port);
379
380 ltq_w32(0, port->membase + LTQ_ASC_CON);
381 ltq_w32_mask(ASCRXFCON_RXFEN, ASCRXFCON_RXFFLU,
382 port->membase + LTQ_ASC_RXFCON);
383 ltq_w32_mask(ASCTXFCON_TXFEN, ASCTXFCON_TXFFLU,
384 port->membase + LTQ_ASC_TXFCON);
385}
386
387static void
388lqasc_set_termios(struct uart_port *port,
389 struct ktermios *new, struct ktermios *old)
390{
391 unsigned int cflag;
392 unsigned int iflag;
393 unsigned int divisor;
394 unsigned int baud;
395 unsigned int con = 0;
396 unsigned long flags;
397
398 cflag = new->c_cflag;
399 iflag = new->c_iflag;
400
401 switch (cflag & CSIZE) {
402 case CS7:
403 con = ASCCON_M_7ASYNC;
404 break;
405
406 case CS5:
407 case CS6:
408 default:
409 new->c_cflag &= ~ CSIZE;
410 new->c_cflag |= CS8;
411 con = ASCCON_M_8ASYNC;
412 break;
413 }
414
415 cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
416
417 if (cflag & CSTOPB)
418 con |= ASCCON_STP;
419
420 if (cflag & PARENB) {
421 if (!(cflag & PARODD))
422 con &= ~ASCCON_ODD;
423 else
424 con |= ASCCON_ODD;
425 }
426
427 port->read_status_mask = ASCSTATE_ROE;
428 if (iflag & INPCK)
429 port->read_status_mask |= ASCSTATE_FE | ASCSTATE_PE;
430
431 port->ignore_status_mask = 0;
432 if (iflag & IGNPAR)
433 port->ignore_status_mask |= ASCSTATE_FE | ASCSTATE_PE;
434
435 if (iflag & IGNBRK) {
436 /*
437 * If we're ignoring parity and break indicators,
438 * ignore overruns too (for real raw support).
439 */
440 if (iflag & IGNPAR)
441 port->ignore_status_mask |= ASCSTATE_ROE;
442 }
443
444 if ((cflag & CREAD) == 0)
445 port->ignore_status_mask |= UART_DUMMY_UER_RX;
446
447 /* set error signals - framing, parity and overrun, enable receiver */
448 con |= ASCCON_FEN | ASCCON_TOEN | ASCCON_ROEN;
449
450 spin_lock_irqsave(&ltq_asc_lock, flags);
451
452 /* set up CON */
453 ltq_w32_mask(0, con, port->membase + LTQ_ASC_CON);
454
455 /* Set baud rate - take a divider of 2 into account */
456 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
457 divisor = uart_get_divisor(port, baud);
458 divisor = divisor / 2 - 1;
459
460 /* disable the baudrate generator */
461 ltq_w32_mask(ASCCON_R, 0, port->membase + LTQ_ASC_CON);
462
463 /* make sure the fractional divider is off */
464 ltq_w32_mask(ASCCON_FDE, 0, port->membase + LTQ_ASC_CON);
465
466 /* set up to use divisor of 2 */
467 ltq_w32_mask(ASCCON_BRS, 0, port->membase + LTQ_ASC_CON);
468
469 /* now we can write the new baudrate into the register */
470 ltq_w32(divisor, port->membase + LTQ_ASC_BG);
471
472 /* turn the baudrate generator back on */
473 ltq_w32_mask(0, ASCCON_R, port->membase + LTQ_ASC_CON);
474
475 /* enable rx */
476 ltq_w32(ASCWHBSTATE_SETREN, port->membase + LTQ_ASC_WHBSTATE);
477
478 spin_unlock_irqrestore(&ltq_asc_lock, flags);
479
480 /* Don't rewrite B0 */
481 if (tty_termios_baud_rate(new))
482 tty_termios_encode_baud_rate(new, baud, baud);
483}
484
485static const char*
486lqasc_type(struct uart_port *port)
487{
488 if (port->type == PORT_LTQ_ASC)
489 return DRVNAME;
490 else
491 return NULL;
492}
493
494static void
495lqasc_release_port(struct uart_port *port)
496{
497 if (port->flags & UPF_IOREMAP) {
498 iounmap(port->membase);
499 port->membase = NULL;
500 }
501}
502
503static int
504lqasc_request_port(struct uart_port *port)
505{
506 struct platform_device *pdev = to_platform_device(port->dev);
507 struct resource *res;
508 int size;
509
510 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
511 if (!res) {
512 dev_err(&pdev->dev, "cannot obtain I/O memory region");
513 return -ENODEV;
514 }
515 size = resource_size(res);
516
517 res = devm_request_mem_region(&pdev->dev, res->start,
518 size, dev_name(&pdev->dev));
519 if (!res) {
520 dev_err(&pdev->dev, "cannot request I/O memory region");
521 return -EBUSY;
522 }
523
524 if (port->flags & UPF_IOREMAP) {
525 port->membase = devm_ioremap_nocache(&pdev->dev,
526 port->mapbase, size);
527 if (port->membase == NULL)
528 return -ENOMEM;
529 }
530 return 0;
531}
532
533static void
534lqasc_config_port(struct uart_port *port, int flags)
535{
536 if (flags & UART_CONFIG_TYPE) {
537 port->type = PORT_LTQ_ASC;
538 lqasc_request_port(port);
539 }
540}
541
542static int
543lqasc_verify_port(struct uart_port *port,
544 struct serial_struct *ser)
545{
546 int ret = 0;
547 if (ser->type != PORT_UNKNOWN && ser->type != PORT_LTQ_ASC)
548 ret = -EINVAL;
549 if (ser->irq < 0 || ser->irq >= NR_IRQS)
550 ret = -EINVAL;
551 if (ser->baud_base < 9600)
552 ret = -EINVAL;
553 return ret;
554}
555
556static struct uart_ops lqasc_pops = {
557 .tx_empty = lqasc_tx_empty,
558 .set_mctrl = lqasc_set_mctrl,
559 .get_mctrl = lqasc_get_mctrl,
560 .stop_tx = lqasc_stop_tx,
561 .start_tx = lqasc_start_tx,
562 .stop_rx = lqasc_stop_rx,
563 .enable_ms = lqasc_enable_ms,
564 .break_ctl = lqasc_break_ctl,
565 .startup = lqasc_startup,
566 .shutdown = lqasc_shutdown,
567 .set_termios = lqasc_set_termios,
568 .type = lqasc_type,
569 .release_port = lqasc_release_port,
570 .request_port = lqasc_request_port,
571 .config_port = lqasc_config_port,
572 .verify_port = lqasc_verify_port,
573};
574
575static void
576lqasc_console_putchar(struct uart_port *port, int ch)
577{
578 int fifofree;
579
580 if (!port->membase)
581 return;
582
583 do {
584 fifofree = (ltq_r32(port->membase + LTQ_ASC_FSTAT)
585 & ASCFSTAT_TXFREEMASK) >> ASCFSTAT_TXFREEOFF;
586 } while (fifofree == 0);
587 ltq_w8(ch, port->membase + LTQ_ASC_TBUF);
588}
589
590
591static void
592lqasc_console_write(struct console *co, const char *s, u_int count)
593{
594 struct ltq_uart_port *ltq_port;
595 struct uart_port *port;
596 unsigned long flags;
597
598 if (co->index >= MAXPORTS)
599 return;
600
601 ltq_port = lqasc_port[co->index];
602 if (!ltq_port)
603 return;
604
605 port = &ltq_port->port;
606
607 spin_lock_irqsave(&ltq_asc_lock, flags);
608 uart_console_write(port, s, count, lqasc_console_putchar);
609 spin_unlock_irqrestore(&ltq_asc_lock, flags);
610}
611
612static int __init
613lqasc_console_setup(struct console *co, char *options)
614{
615 struct ltq_uart_port *ltq_port;
616 struct uart_port *port;
617 int baud = 115200;
618 int bits = 8;
619 int parity = 'n';
620 int flow = 'n';
621
622 if (co->index >= MAXPORTS)
623 return -ENODEV;
624
625 ltq_port = lqasc_port[co->index];
626 if (!ltq_port)
627 return -ENODEV;
628
629 port = &ltq_port->port;
630
631 port->uartclk = clk_get_rate(ltq_port->clk);
632
633 if (options)
634 uart_parse_options(options, &baud, &parity, &bits, &flow);
635 return uart_set_options(port, co, baud, parity, bits, flow);
636}
637
638static struct console lqasc_console = {
639 .name = "ttyLTQ",
640 .write = lqasc_console_write,
641 .device = uart_console_device,
642 .setup = lqasc_console_setup,
643 .flags = CON_PRINTBUFFER,
644 .index = -1,
645 .data = &lqasc_reg,
646};
647
648static int __init
649lqasc_console_init(void)
650{
651 register_console(&lqasc_console);
652 return 0;
653}
654console_initcall(lqasc_console_init);
655
656static struct uart_driver lqasc_reg = {
657 .owner = THIS_MODULE,
658 .driver_name = DRVNAME,
659 .dev_name = "ttyLTQ",
660 .major = 0,
661 .minor = 0,
662 .nr = MAXPORTS,
663 .cons = &lqasc_console,
664};
665
666static int __init
667lqasc_probe(struct platform_device *pdev)
668{
669 struct ltq_uart_port *ltq_port;
670 struct uart_port *port;
671 struct resource *mmres, *irqres;
672 int tx_irq, rx_irq, err_irq;
673 struct clk *clk;
674 int ret;
675
676 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
677 irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
678 if (!mmres || !irqres)
679 return -ENODEV;
680
681 if (pdev->id >= MAXPORTS)
682 return -EBUSY;
683
684 if (lqasc_port[pdev->id] != NULL)
685 return -EBUSY;
686
687 clk = clk_get(&pdev->dev, "fpi");
688 if (IS_ERR(clk)) {
689 pr_err("failed to get fpi clk\n");
690 return -ENOENT;
691 }
692
693 tx_irq = platform_get_irq_byname(pdev, "tx");
694 rx_irq = platform_get_irq_byname(pdev, "rx");
695 err_irq = platform_get_irq_byname(pdev, "err");
696 if ((tx_irq < 0) | (rx_irq < 0) | (err_irq < 0))
697 return -ENODEV;
698
699 ltq_port = kzalloc(sizeof(struct ltq_uart_port), GFP_KERNEL);
700 if (!ltq_port)
701 return -ENOMEM;
702
703 port = &ltq_port->port;
704
705 port->iotype = SERIAL_IO_MEM;
706 port->flags = ASYNC_BOOT_AUTOCONF | UPF_IOREMAP;
707 port->ops = &lqasc_pops;
708 port->fifosize = 16;
709 port->type = PORT_LTQ_ASC,
710 port->line = pdev->id;
711 port->dev = &pdev->dev;
712
713 port->irq = tx_irq; /* unused, just to be backward-compatibe */
714 port->mapbase = mmres->start;
715
716 ltq_port->clk = clk;
717
718 ltq_port->tx_irq = tx_irq;
719 ltq_port->rx_irq = rx_irq;
720 ltq_port->err_irq = err_irq;
721
722 lqasc_port[pdev->id] = ltq_port;
723 platform_set_drvdata(pdev, ltq_port);
724
725 ret = uart_add_one_port(&lqasc_reg, port);
726
727 return ret;
728}
729
730static struct platform_driver lqasc_driver = {
731 .driver = {
732 .name = DRVNAME,
733 .owner = THIS_MODULE,
734 },
735};
736
737int __init
738init_lqasc(void)
739{
740 int ret;
741
742 ret = uart_register_driver(&lqasc_reg);
743 if (ret != 0)
744 return ret;
745
746 ret = platform_driver_probe(&lqasc_driver, lqasc_probe);
747 if (ret != 0)
748 uart_unregister_driver(&lqasc_reg);
749
750 return ret;
751}
752
753module_init(init_lqasc);
754
755MODULE_DESCRIPTION("Lantiq serial port driver");
756MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/m32r_sio.c b/drivers/tty/serial/m32r_sio.c
new file mode 100644
index 000000000000..84db7321cce8
--- /dev/null
+++ b/drivers/tty/serial/m32r_sio.c
@@ -0,0 +1,1193 @@
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 unused)
911{
912 struct uart_sio_port *up = (struct uart_sio_port *)port;
913 unsigned long flags;
914
915 spin_lock_irqsave(&up->port.lock, flags);
916
917 up->port.type = (PORT_M32R_SIO - PORT_M32R_BASE + 1);
918 up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
919
920 spin_unlock_irqrestore(&up->port.lock, flags);
921}
922
923static int
924m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser)
925{
926 if (ser->irq >= nr_irqs || ser->irq < 0 ||
927 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
928 ser->type >= ARRAY_SIZE(uart_config))
929 return -EINVAL;
930 return 0;
931}
932
933static const char *
934m32r_sio_type(struct uart_port *port)
935{
936 int type = port->type;
937
938 if (type >= ARRAY_SIZE(uart_config))
939 type = 0;
940 return uart_config[type].name;
941}
942
943static struct uart_ops m32r_sio_pops = {
944 .tx_empty = m32r_sio_tx_empty,
945 .set_mctrl = m32r_sio_set_mctrl,
946 .get_mctrl = m32r_sio_get_mctrl,
947 .stop_tx = m32r_sio_stop_tx,
948 .start_tx = m32r_sio_start_tx,
949 .stop_rx = m32r_sio_stop_rx,
950 .enable_ms = m32r_sio_enable_ms,
951 .break_ctl = m32r_sio_break_ctl,
952 .startup = m32r_sio_startup,
953 .shutdown = m32r_sio_shutdown,
954 .set_termios = m32r_sio_set_termios,
955 .pm = m32r_sio_pm,
956 .type = m32r_sio_type,
957 .release_port = m32r_sio_release_port,
958 .request_port = m32r_sio_request_port,
959 .config_port = m32r_sio_config_port,
960 .verify_port = m32r_sio_verify_port,
961};
962
963static struct uart_sio_port m32r_sio_ports[UART_NR];
964
965static void __init m32r_sio_init_ports(void)
966{
967 struct uart_sio_port *up;
968 static int first = 1;
969 int i;
970
971 if (!first)
972 return;
973 first = 0;
974
975 for (i = 0, up = m32r_sio_ports; i < ARRAY_SIZE(old_serial_port);
976 i++, up++) {
977 up->port.iobase = old_serial_port[i].port;
978 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
979 up->port.uartclk = old_serial_port[i].baud_base * 16;
980 up->port.flags = old_serial_port[i].flags;
981 up->port.membase = old_serial_port[i].iomem_base;
982 up->port.iotype = old_serial_port[i].io_type;
983 up->port.regshift = old_serial_port[i].iomem_reg_shift;
984 up->port.ops = &m32r_sio_pops;
985 }
986}
987
988static void __init m32r_sio_register_ports(struct uart_driver *drv)
989{
990 int i;
991
992 m32r_sio_init_ports();
993
994 for (i = 0; i < UART_NR; i++) {
995 struct uart_sio_port *up = &m32r_sio_ports[i];
996
997 up->port.line = i;
998 up->port.ops = &m32r_sio_pops;
999 init_timer(&up->timer);
1000 up->timer.function = m32r_sio_timeout;
1001
1002 /*
1003 * ALPHA_KLUDGE_MCR needs to be killed.
1004 */
1005 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
1006 up->mcr_force = ALPHA_KLUDGE_MCR;
1007
1008 uart_add_one_port(drv, &up->port);
1009 }
1010}
1011
1012#ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
1013
1014/*
1015 * Wait for transmitter & holding register to empty
1016 */
1017static inline void wait_for_xmitr(struct uart_sio_port *up)
1018{
1019 unsigned int status, tmout = 10000;
1020
1021 /* Wait up to 10ms for the character(s) to be sent. */
1022 do {
1023 status = sio_in(up, SIOSTS);
1024
1025 if (--tmout == 0)
1026 break;
1027 udelay(1);
1028 } while ((status & UART_EMPTY) != UART_EMPTY);
1029
1030 /* Wait up to 1s for flow control if necessary */
1031 if (up->port.flags & UPF_CONS_FLOW) {
1032 tmout = 1000000;
1033 while (--tmout)
1034 udelay(1);
1035 }
1036}
1037
1038static void m32r_sio_console_putchar(struct uart_port *port, int ch)
1039{
1040 struct uart_sio_port *up = (struct uart_sio_port *)port;
1041
1042 wait_for_xmitr(up);
1043 sio_out(up, SIOTXB, ch);
1044}
1045
1046/*
1047 * Print a string to the serial port trying not to disturb
1048 * any possible real use of the port...
1049 *
1050 * The console_lock must be held when we get here.
1051 */
1052static void m32r_sio_console_write(struct console *co, const char *s,
1053 unsigned int count)
1054{
1055 struct uart_sio_port *up = &m32r_sio_ports[co->index];
1056 unsigned int ier;
1057
1058 /*
1059 * First save the UER then disable the interrupts
1060 */
1061 ier = sio_in(up, SIOTRCR);
1062 sio_out(up, SIOTRCR, 0);
1063
1064 uart_console_write(&up->port, s, count, m32r_sio_console_putchar);
1065
1066 /*
1067 * Finally, wait for transmitter to become empty
1068 * and restore the IER
1069 */
1070 wait_for_xmitr(up);
1071 sio_out(up, SIOTRCR, ier);
1072}
1073
1074static int __init m32r_sio_console_setup(struct console *co, char *options)
1075{
1076 struct uart_port *port;
1077 int baud = 9600;
1078 int bits = 8;
1079 int parity = 'n';
1080 int flow = 'n';
1081
1082 /*
1083 * Check whether an invalid uart number has been specified, and
1084 * if so, search for the first available port that does have
1085 * console support.
1086 */
1087 if (co->index >= UART_NR)
1088 co->index = 0;
1089 port = &m32r_sio_ports[co->index].port;
1090
1091 /*
1092 * Temporary fix.
1093 */
1094 spin_lock_init(&port->lock);
1095
1096 if (options)
1097 uart_parse_options(options, &baud, &parity, &bits, &flow);
1098
1099 return uart_set_options(port, co, baud, parity, bits, flow);
1100}
1101
1102static struct uart_driver m32r_sio_reg;
1103static struct console m32r_sio_console = {
1104 .name = "ttyS",
1105 .write = m32r_sio_console_write,
1106 .device = uart_console_device,
1107 .setup = m32r_sio_console_setup,
1108 .flags = CON_PRINTBUFFER,
1109 .index = -1,
1110 .data = &m32r_sio_reg,
1111};
1112
1113static int __init m32r_sio_console_init(void)
1114{
1115 sio_reset();
1116 sio_init();
1117 m32r_sio_init_ports();
1118 register_console(&m32r_sio_console);
1119 return 0;
1120}
1121console_initcall(m32r_sio_console_init);
1122
1123#define M32R_SIO_CONSOLE &m32r_sio_console
1124#else
1125#define M32R_SIO_CONSOLE NULL
1126#endif
1127
1128static struct uart_driver m32r_sio_reg = {
1129 .owner = THIS_MODULE,
1130 .driver_name = "sio",
1131 .dev_name = "ttyS",
1132 .major = TTY_MAJOR,
1133 .minor = 64,
1134 .nr = UART_NR,
1135 .cons = M32R_SIO_CONSOLE,
1136};
1137
1138/**
1139 * m32r_sio_suspend_port - suspend one serial port
1140 * @line: serial line number
1141 *
1142 * Suspend one serial port.
1143 */
1144void m32r_sio_suspend_port(int line)
1145{
1146 uart_suspend_port(&m32r_sio_reg, &m32r_sio_ports[line].port);
1147}
1148
1149/**
1150 * m32r_sio_resume_port - resume one serial port
1151 * @line: serial line number
1152 *
1153 * Resume one serial port.
1154 */
1155void m32r_sio_resume_port(int line)
1156{
1157 uart_resume_port(&m32r_sio_reg, &m32r_sio_ports[line].port);
1158}
1159
1160static int __init m32r_sio_init(void)
1161{
1162 int ret, i;
1163
1164 printk(KERN_INFO "Serial: M32R SIO driver\n");
1165
1166 for (i = 0; i < nr_irqs; i++)
1167 spin_lock_init(&irq_lists[i].lock);
1168
1169 ret = uart_register_driver(&m32r_sio_reg);
1170 if (ret >= 0)
1171 m32r_sio_register_ports(&m32r_sio_reg);
1172
1173 return ret;
1174}
1175
1176static void __exit m32r_sio_exit(void)
1177{
1178 int i;
1179
1180 for (i = 0; i < UART_NR; i++)
1181 uart_remove_one_port(&m32r_sio_reg, &m32r_sio_ports[i].port);
1182
1183 uart_unregister_driver(&m32r_sio_reg);
1184}
1185
1186module_init(m32r_sio_init);
1187module_exit(m32r_sio_exit);
1188
1189EXPORT_SYMBOL(m32r_sio_suspend_port);
1190EXPORT_SYMBOL(m32r_sio_resume_port);
1191
1192MODULE_LICENSE("GPL");
1193MODULE_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..7b951adac54b
--- /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_freezable_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..750b4f627315
--- /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_freezable_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..8415fc723b96
--- /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 structure */
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..cab52f4a88b0
--- /dev/null
+++ b/drivers/tty/serial/mfd.c
@@ -0,0 +1,1470 @@
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. The RI/DSR/DCD/DTR are not pinned out, DCD & DSR are always
20 * asserted, only when the HW is reset the DDCD and DDSR will
21 * be triggered
22 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/console.h>
27#include <linux/sysrq.h>
28#include <linux/slab.h>
29#include <linux/serial_reg.h>
30#include <linux/circ_buf.h>
31#include <linux/delay.h>
32#include <linux/interrupt.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/serial_core.h>
36#include <linux/serial_mfd.h>
37#include <linux/dma-mapping.h>
38#include <linux/pci.h>
39#include <linux/io.h>
40#include <linux/debugfs.h>
41
42#define HSU_DMA_BUF_SIZE 2048
43
44#define chan_readl(chan, offset) readl(chan->reg + offset)
45#define chan_writel(chan, offset, val) writel(val, chan->reg + offset)
46
47#define mfd_readl(obj, offset) readl(obj->reg + offset)
48#define mfd_writel(obj, offset, val) writel(val, obj->reg + offset)
49
50static int hsu_dma_enable;
51module_param(hsu_dma_enable, int, 0);
52MODULE_PARM_DESC(hsu_dma_enable,
53 "It is a bitmap to set working mode, if bit[x] is 1, then port[x] will work in DMA mode, otherwise in PIO mode.");
54
55struct hsu_dma_buffer {
56 u8 *buf;
57 dma_addr_t dma_addr;
58 u32 dma_size;
59 u32 ofs;
60};
61
62struct hsu_dma_chan {
63 u32 id;
64 enum dma_data_direction dirt;
65 struct uart_hsu_port *uport;
66 void __iomem *reg;
67};
68
69struct uart_hsu_port {
70 struct uart_port port;
71 unsigned char ier;
72 unsigned char lcr;
73 unsigned char mcr;
74 unsigned int lsr_break_flag;
75 char name[12];
76 int index;
77 struct device *dev;
78
79 struct hsu_dma_chan *txc;
80 struct hsu_dma_chan *rxc;
81 struct hsu_dma_buffer txbuf;
82 struct hsu_dma_buffer rxbuf;
83 int use_dma; /* flag for DMA/PIO */
84 int running;
85 int dma_tx_on;
86};
87
88/* Top level data structure of HSU */
89struct hsu_port {
90 void __iomem *reg;
91 unsigned long paddr;
92 unsigned long iolen;
93 u32 irq;
94
95 struct uart_hsu_port port[3];
96 struct hsu_dma_chan chans[10];
97
98 struct dentry *debugfs;
99};
100
101static inline unsigned int serial_in(struct uart_hsu_port *up, int offset)
102{
103 unsigned int val;
104
105 if (offset > UART_MSR) {
106 offset <<= 2;
107 val = readl(up->port.membase + offset);
108 } else
109 val = (unsigned int)readb(up->port.membase + offset);
110
111 return val;
112}
113
114static inline void serial_out(struct uart_hsu_port *up, int offset, int value)
115{
116 if (offset > UART_MSR) {
117 offset <<= 2;
118 writel(value, up->port.membase + offset);
119 } else {
120 unsigned char val = value & 0xff;
121 writeb(val, up->port.membase + offset);
122 }
123}
124
125#ifdef CONFIG_DEBUG_FS
126
127#define HSU_REGS_BUFSIZE 1024
128
129static int hsu_show_regs_open(struct inode *inode, struct file *file)
130{
131 file->private_data = inode->i_private;
132 return 0;
133}
134
135static ssize_t port_show_regs(struct file *file, char __user *user_buf,
136 size_t count, loff_t *ppos)
137{
138 struct uart_hsu_port *up = file->private_data;
139 char *buf;
140 u32 len = 0;
141 ssize_t ret;
142
143 buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
144 if (!buf)
145 return 0;
146
147 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
148 "MFD HSU port[%d] regs:\n", up->index);
149
150 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
151 "=================================\n");
152 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
153 "IER: \t\t0x%08x\n", serial_in(up, UART_IER));
154 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
155 "IIR: \t\t0x%08x\n", serial_in(up, UART_IIR));
156 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
157 "LCR: \t\t0x%08x\n", serial_in(up, UART_LCR));
158 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
159 "MCR: \t\t0x%08x\n", serial_in(up, UART_MCR));
160 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
161 "LSR: \t\t0x%08x\n", serial_in(up, UART_LSR));
162 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
163 "MSR: \t\t0x%08x\n", serial_in(up, UART_MSR));
164 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
165 "FOR: \t\t0x%08x\n", serial_in(up, UART_FOR));
166 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
167 "PS: \t\t0x%08x\n", serial_in(up, UART_PS));
168 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
169 "MUL: \t\t0x%08x\n", serial_in(up, UART_MUL));
170 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
171 "DIV: \t\t0x%08x\n", serial_in(up, UART_DIV));
172
173 if (len > HSU_REGS_BUFSIZE)
174 len = HSU_REGS_BUFSIZE;
175
176 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
177 kfree(buf);
178 return ret;
179}
180
181static ssize_t dma_show_regs(struct file *file, char __user *user_buf,
182 size_t count, loff_t *ppos)
183{
184 struct hsu_dma_chan *chan = file->private_data;
185 char *buf;
186 u32 len = 0;
187 ssize_t ret;
188
189 buf = kzalloc(HSU_REGS_BUFSIZE, GFP_KERNEL);
190 if (!buf)
191 return 0;
192
193 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
194 "MFD HSU DMA channel [%d] regs:\n", chan->id);
195
196 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
197 "=================================\n");
198 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
199 "CR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_CR));
200 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
201 "DCR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_DCR));
202 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
203 "BSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_BSR));
204 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
205 "MOTSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_MOTSR));
206 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
207 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0SAR));
208 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
209 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D0TSR));
210 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
211 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1SAR));
212 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
213 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D1TSR));
214 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
215 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2SAR));
216 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
217 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D2TSR));
218 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
219 "D0SAR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3SAR));
220 len += snprintf(buf + len, HSU_REGS_BUFSIZE - len,
221 "D0TSR: \t\t0x%08x\n", chan_readl(chan, HSU_CH_D3TSR));
222
223 if (len > HSU_REGS_BUFSIZE)
224 len = HSU_REGS_BUFSIZE;
225
226 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
227 kfree(buf);
228 return ret;
229}
230
231static const struct file_operations port_regs_ops = {
232 .owner = THIS_MODULE,
233 .open = hsu_show_regs_open,
234 .read = port_show_regs,
235 .llseek = default_llseek,
236};
237
238static const struct file_operations dma_regs_ops = {
239 .owner = THIS_MODULE,
240 .open = hsu_show_regs_open,
241 .read = dma_show_regs,
242 .llseek = default_llseek,
243};
244
245static int hsu_debugfs_init(struct hsu_port *hsu)
246{
247 int i;
248 char name[32];
249
250 hsu->debugfs = debugfs_create_dir("hsu", NULL);
251 if (!hsu->debugfs)
252 return -ENOMEM;
253
254 for (i = 0; i < 3; i++) {
255 snprintf(name, sizeof(name), "port_%d_regs", i);
256 debugfs_create_file(name, S_IFREG | S_IRUGO,
257 hsu->debugfs, (void *)(&hsu->port[i]), &port_regs_ops);
258 }
259
260 for (i = 0; i < 6; i++) {
261 snprintf(name, sizeof(name), "dma_chan_%d_regs", i);
262 debugfs_create_file(name, S_IFREG | S_IRUGO,
263 hsu->debugfs, (void *)&hsu->chans[i], &dma_regs_ops);
264 }
265
266 return 0;
267}
268
269static void hsu_debugfs_remove(struct hsu_port *hsu)
270{
271 if (hsu->debugfs)
272 debugfs_remove_recursive(hsu->debugfs);
273}
274
275#else
276static inline int hsu_debugfs_init(struct hsu_port *hsu)
277{
278 return 0;
279}
280
281static inline void hsu_debugfs_remove(struct hsu_port *hsu)
282{
283}
284#endif /* CONFIG_DEBUG_FS */
285
286static void serial_hsu_enable_ms(struct uart_port *port)
287{
288 struct uart_hsu_port *up =
289 container_of(port, struct uart_hsu_port, port);
290
291 up->ier |= UART_IER_MSI;
292 serial_out(up, UART_IER, up->ier);
293}
294
295void hsu_dma_tx(struct uart_hsu_port *up)
296{
297 struct circ_buf *xmit = &up->port.state->xmit;
298 struct hsu_dma_buffer *dbuf = &up->txbuf;
299 int count;
300
301 /* test_and_set_bit may be better, but anyway it's in lock protected mode */
302 if (up->dma_tx_on)
303 return;
304
305 /* Update the circ buf info */
306 xmit->tail += dbuf->ofs;
307 xmit->tail &= UART_XMIT_SIZE - 1;
308
309 up->port.icount.tx += dbuf->ofs;
310 dbuf->ofs = 0;
311
312 /* Disable the channel */
313 chan_writel(up->txc, HSU_CH_CR, 0x0);
314
315 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&up->port)) {
316 dma_sync_single_for_device(up->port.dev,
317 dbuf->dma_addr,
318 dbuf->dma_size,
319 DMA_TO_DEVICE);
320
321 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
322 dbuf->ofs = count;
323
324 /* Reprogram the channel */
325 chan_writel(up->txc, HSU_CH_D0SAR, dbuf->dma_addr + xmit->tail);
326 chan_writel(up->txc, HSU_CH_D0TSR, count);
327
328 /* Reenable the channel */
329 chan_writel(up->txc, HSU_CH_DCR, 0x1
330 | (0x1 << 8)
331 | (0x1 << 16)
332 | (0x1 << 24));
333 up->dma_tx_on = 1;
334 chan_writel(up->txc, HSU_CH_CR, 0x1);
335 }
336
337 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
338 uart_write_wakeup(&up->port);
339}
340
341/* The buffer is already cache coherent */
342void hsu_dma_start_rx_chan(struct hsu_dma_chan *rxc, struct hsu_dma_buffer *dbuf)
343{
344 dbuf->ofs = 0;
345
346 chan_writel(rxc, HSU_CH_BSR, 32);
347 chan_writel(rxc, HSU_CH_MOTSR, 4);
348
349 chan_writel(rxc, HSU_CH_D0SAR, dbuf->dma_addr);
350 chan_writel(rxc, HSU_CH_D0TSR, dbuf->dma_size);
351 chan_writel(rxc, HSU_CH_DCR, 0x1 | (0x1 << 8)
352 | (0x1 << 16)
353 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
354 );
355 chan_writel(rxc, HSU_CH_CR, 0x3);
356}
357
358/* Protected by spin_lock_irqsave(port->lock) */
359static void serial_hsu_start_tx(struct uart_port *port)
360{
361 struct uart_hsu_port *up =
362 container_of(port, struct uart_hsu_port, port);
363
364 if (up->use_dma) {
365 hsu_dma_tx(up);
366 } else if (!(up->ier & UART_IER_THRI)) {
367 up->ier |= UART_IER_THRI;
368 serial_out(up, UART_IER, up->ier);
369 }
370}
371
372static void serial_hsu_stop_tx(struct uart_port *port)
373{
374 struct uart_hsu_port *up =
375 container_of(port, struct uart_hsu_port, port);
376 struct hsu_dma_chan *txc = up->txc;
377
378 if (up->use_dma)
379 chan_writel(txc, HSU_CH_CR, 0x0);
380 else if (up->ier & UART_IER_THRI) {
381 up->ier &= ~UART_IER_THRI;
382 serial_out(up, UART_IER, up->ier);
383 }
384}
385
386/* This is always called in spinlock protected mode, so
387 * modify timeout timer is safe here */
388void hsu_dma_rx(struct uart_hsu_port *up, u32 int_sts)
389{
390 struct hsu_dma_buffer *dbuf = &up->rxbuf;
391 struct hsu_dma_chan *chan = up->rxc;
392 struct uart_port *port = &up->port;
393 struct tty_struct *tty = port->state->port.tty;
394 int count;
395
396 if (!tty)
397 return;
398
399 /*
400 * First need to know how many is already transferred,
401 * then check if its a timeout DMA irq, and return
402 * the trail bytes out, push them up and reenable the
403 * channel
404 */
405
406 /* Timeout IRQ, need wait some time, see Errata 2 */
407 if (int_sts & 0xf00)
408 udelay(2);
409
410 /* Stop the channel */
411 chan_writel(chan, HSU_CH_CR, 0x0);
412
413 count = chan_readl(chan, HSU_CH_D0SAR) - dbuf->dma_addr;
414 if (!count) {
415 /* Restart the channel before we leave */
416 chan_writel(chan, HSU_CH_CR, 0x3);
417 return;
418 }
419
420 dma_sync_single_for_cpu(port->dev, dbuf->dma_addr,
421 dbuf->dma_size, DMA_FROM_DEVICE);
422
423 /*
424 * Head will only wrap around when we recycle
425 * the DMA buffer, and when that happens, we
426 * explicitly set tail to 0. So head will
427 * always be greater than tail.
428 */
429 tty_insert_flip_string(tty, dbuf->buf, count);
430 port->icount.rx += count;
431
432 dma_sync_single_for_device(up->port.dev, dbuf->dma_addr,
433 dbuf->dma_size, DMA_FROM_DEVICE);
434
435 /* Reprogram the channel */
436 chan_writel(chan, HSU_CH_D0SAR, dbuf->dma_addr);
437 chan_writel(chan, HSU_CH_D0TSR, dbuf->dma_size);
438 chan_writel(chan, HSU_CH_DCR, 0x1
439 | (0x1 << 8)
440 | (0x1 << 16)
441 | (0x1 << 24) /* timeout bit, see HSU Errata 1 */
442 );
443 tty_flip_buffer_push(tty);
444
445 chan_writel(chan, HSU_CH_CR, 0x3);
446
447}
448
449static void serial_hsu_stop_rx(struct uart_port *port)
450{
451 struct uart_hsu_port *up =
452 container_of(port, struct uart_hsu_port, port);
453 struct hsu_dma_chan *chan = up->rxc;
454
455 if (up->use_dma)
456 chan_writel(chan, HSU_CH_CR, 0x2);
457 else {
458 up->ier &= ~UART_IER_RLSI;
459 up->port.read_status_mask &= ~UART_LSR_DR;
460 serial_out(up, UART_IER, up->ier);
461 }
462}
463
464static inline void receive_chars(struct uart_hsu_port *up, int *status)
465{
466 struct tty_struct *tty = up->port.state->port.tty;
467 unsigned int ch, flag;
468 unsigned int max_count = 256;
469
470 if (!tty)
471 return;
472
473 do {
474 ch = serial_in(up, UART_RX);
475 flag = TTY_NORMAL;
476 up->port.icount.rx++;
477
478 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
479 UART_LSR_FE | UART_LSR_OE))) {
480
481 dev_warn(up->dev, "We really rush into ERR/BI case"
482 "status = 0x%02x", *status);
483 /* For statistics only */
484 if (*status & UART_LSR_BI) {
485 *status &= ~(UART_LSR_FE | UART_LSR_PE);
486 up->port.icount.brk++;
487 /*
488 * We do the SysRQ and SAK checking
489 * here because otherwise the break
490 * may get masked by ignore_status_mask
491 * or read_status_mask.
492 */
493 if (uart_handle_break(&up->port))
494 goto ignore_char;
495 } else if (*status & UART_LSR_PE)
496 up->port.icount.parity++;
497 else if (*status & UART_LSR_FE)
498 up->port.icount.frame++;
499 if (*status & UART_LSR_OE)
500 up->port.icount.overrun++;
501
502 /* Mask off conditions which should be ignored. */
503 *status &= up->port.read_status_mask;
504
505#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
506 if (up->port.cons &&
507 up->port.cons->index == up->port.line) {
508 /* Recover the break flag from console xmit */
509 *status |= up->lsr_break_flag;
510 up->lsr_break_flag = 0;
511 }
512#endif
513 if (*status & UART_LSR_BI) {
514 flag = TTY_BREAK;
515 } else if (*status & UART_LSR_PE)
516 flag = TTY_PARITY;
517 else if (*status & UART_LSR_FE)
518 flag = TTY_FRAME;
519 }
520
521 if (uart_handle_sysrq_char(&up->port, ch))
522 goto ignore_char;
523
524 uart_insert_char(&up->port, *status, UART_LSR_OE, ch, flag);
525 ignore_char:
526 *status = serial_in(up, UART_LSR);
527 } while ((*status & UART_LSR_DR) && max_count--);
528 tty_flip_buffer_push(tty);
529}
530
531static void transmit_chars(struct uart_hsu_port *up)
532{
533 struct circ_buf *xmit = &up->port.state->xmit;
534 int count;
535
536 if (up->port.x_char) {
537 serial_out(up, UART_TX, up->port.x_char);
538 up->port.icount.tx++;
539 up->port.x_char = 0;
540 return;
541 }
542 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
543 serial_hsu_stop_tx(&up->port);
544 return;
545 }
546
547 /* The IRQ is for TX FIFO half-empty */
548 count = up->port.fifosize / 2;
549
550 do {
551 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
552 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
553
554 up->port.icount.tx++;
555 if (uart_circ_empty(xmit))
556 break;
557 } while (--count > 0);
558
559 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
560 uart_write_wakeup(&up->port);
561
562 if (uart_circ_empty(xmit))
563 serial_hsu_stop_tx(&up->port);
564}
565
566static inline void check_modem_status(struct uart_hsu_port *up)
567{
568 int status;
569
570 status = serial_in(up, UART_MSR);
571
572 if ((status & UART_MSR_ANY_DELTA) == 0)
573 return;
574
575 if (status & UART_MSR_TERI)
576 up->port.icount.rng++;
577 if (status & UART_MSR_DDSR)
578 up->port.icount.dsr++;
579 /* We may only get DDCD when HW init and reset */
580 if (status & UART_MSR_DDCD)
581 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
582 /* Will start/stop_tx accordingly */
583 if (status & UART_MSR_DCTS)
584 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
585
586 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
587}
588
589/*
590 * This handles the interrupt from one port.
591 */
592static irqreturn_t port_irq(int irq, void *dev_id)
593{
594 struct uart_hsu_port *up = dev_id;
595 unsigned int iir, lsr;
596 unsigned long flags;
597
598 if (unlikely(!up->running))
599 return IRQ_NONE;
600
601 spin_lock_irqsave(&up->port.lock, flags);
602 if (up->use_dma) {
603 lsr = serial_in(up, UART_LSR);
604 if (unlikely(lsr & (UART_LSR_BI | UART_LSR_PE |
605 UART_LSR_FE | UART_LSR_OE)))
606 dev_warn(up->dev,
607 "Got lsr irq while using DMA, lsr = 0x%2x\n",
608 lsr);
609 check_modem_status(up);
610 spin_unlock_irqrestore(&up->port.lock, flags);
611 return IRQ_HANDLED;
612 }
613
614 iir = serial_in(up, UART_IIR);
615 if (iir & UART_IIR_NO_INT) {
616 spin_unlock_irqrestore(&up->port.lock, flags);
617 return IRQ_NONE;
618 }
619
620 lsr = serial_in(up, UART_LSR);
621 if (lsr & UART_LSR_DR)
622 receive_chars(up, &lsr);
623 check_modem_status(up);
624
625 /* lsr will be renewed during the receive_chars */
626 if (lsr & UART_LSR_THRE)
627 transmit_chars(up);
628
629 spin_unlock_irqrestore(&up->port.lock, flags);
630 return IRQ_HANDLED;
631}
632
633static inline void dma_chan_irq(struct hsu_dma_chan *chan)
634{
635 struct uart_hsu_port *up = chan->uport;
636 unsigned long flags;
637 u32 int_sts;
638
639 spin_lock_irqsave(&up->port.lock, flags);
640
641 if (!up->use_dma || !up->running)
642 goto exit;
643
644 /*
645 * No matter what situation, need read clear the IRQ status
646 * There is a bug, see Errata 5, HSD 2900918
647 */
648 int_sts = chan_readl(chan, HSU_CH_SR);
649
650 /* Rx channel */
651 if (chan->dirt == DMA_FROM_DEVICE)
652 hsu_dma_rx(up, int_sts);
653
654 /* Tx channel */
655 if (chan->dirt == DMA_TO_DEVICE) {
656 chan_writel(chan, HSU_CH_CR, 0x0);
657 up->dma_tx_on = 0;
658 hsu_dma_tx(up);
659 }
660
661exit:
662 spin_unlock_irqrestore(&up->port.lock, flags);
663 return;
664}
665
666static irqreturn_t dma_irq(int irq, void *dev_id)
667{
668 struct hsu_port *hsu = dev_id;
669 u32 int_sts, i;
670
671 int_sts = mfd_readl(hsu, HSU_GBL_DMAISR);
672
673 /* Currently we only have 6 channels may be used */
674 for (i = 0; i < 6; i++) {
675 if (int_sts & 0x1)
676 dma_chan_irq(&hsu->chans[i]);
677 int_sts >>= 1;
678 }
679
680 return IRQ_HANDLED;
681}
682
683static unsigned int serial_hsu_tx_empty(struct uart_port *port)
684{
685 struct uart_hsu_port *up =
686 container_of(port, struct uart_hsu_port, port);
687 unsigned long flags;
688 unsigned int ret;
689
690 spin_lock_irqsave(&up->port.lock, flags);
691 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
692 spin_unlock_irqrestore(&up->port.lock, flags);
693
694 return ret;
695}
696
697static unsigned int serial_hsu_get_mctrl(struct uart_port *port)
698{
699 struct uart_hsu_port *up =
700 container_of(port, struct uart_hsu_port, port);
701 unsigned char status;
702 unsigned int ret;
703
704 status = serial_in(up, UART_MSR);
705
706 ret = 0;
707 if (status & UART_MSR_DCD)
708 ret |= TIOCM_CAR;
709 if (status & UART_MSR_RI)
710 ret |= TIOCM_RNG;
711 if (status & UART_MSR_DSR)
712 ret |= TIOCM_DSR;
713 if (status & UART_MSR_CTS)
714 ret |= TIOCM_CTS;
715 return ret;
716}
717
718static void serial_hsu_set_mctrl(struct uart_port *port, unsigned int mctrl)
719{
720 struct uart_hsu_port *up =
721 container_of(port, struct uart_hsu_port, port);
722 unsigned char mcr = 0;
723
724 if (mctrl & TIOCM_RTS)
725 mcr |= UART_MCR_RTS;
726 if (mctrl & TIOCM_DTR)
727 mcr |= UART_MCR_DTR;
728 if (mctrl & TIOCM_OUT1)
729 mcr |= UART_MCR_OUT1;
730 if (mctrl & TIOCM_OUT2)
731 mcr |= UART_MCR_OUT2;
732 if (mctrl & TIOCM_LOOP)
733 mcr |= UART_MCR_LOOP;
734
735 mcr |= up->mcr;
736
737 serial_out(up, UART_MCR, mcr);
738}
739
740static void serial_hsu_break_ctl(struct uart_port *port, int break_state)
741{
742 struct uart_hsu_port *up =
743 container_of(port, struct uart_hsu_port, port);
744 unsigned long flags;
745
746 spin_lock_irqsave(&up->port.lock, flags);
747 if (break_state == -1)
748 up->lcr |= UART_LCR_SBC;
749 else
750 up->lcr &= ~UART_LCR_SBC;
751 serial_out(up, UART_LCR, up->lcr);
752 spin_unlock_irqrestore(&up->port.lock, flags);
753}
754
755/*
756 * What special to do:
757 * 1. chose the 64B fifo mode
758 * 2. start dma or pio depends on configuration
759 * 3. we only allocate dma memory when needed
760 */
761static int serial_hsu_startup(struct uart_port *port)
762{
763 struct uart_hsu_port *up =
764 container_of(port, struct uart_hsu_port, port);
765 unsigned long flags;
766
767 /*
768 * Clear the FIFO buffers and disable them.
769 * (they will be reenabled in set_termios())
770 */
771 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
772 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
773 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
774 serial_out(up, UART_FCR, 0);
775
776 /* Clear the interrupt registers. */
777 (void) serial_in(up, UART_LSR);
778 (void) serial_in(up, UART_RX);
779 (void) serial_in(up, UART_IIR);
780 (void) serial_in(up, UART_MSR);
781
782 /* Now, initialize the UART, default is 8n1 */
783 serial_out(up, UART_LCR, UART_LCR_WLEN8);
784
785 spin_lock_irqsave(&up->port.lock, flags);
786
787 up->port.mctrl |= TIOCM_OUT2;
788 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
789
790 /*
791 * Finally, enable interrupts. Note: Modem status interrupts
792 * are set via set_termios(), which will be occurring imminently
793 * anyway, so we don't enable them here.
794 */
795 if (!up->use_dma)
796 up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE;
797 else
798 up->ier = 0;
799 serial_out(up, UART_IER, up->ier);
800
801 spin_unlock_irqrestore(&up->port.lock, flags);
802
803 /* DMA init */
804 if (up->use_dma) {
805 struct hsu_dma_buffer *dbuf;
806 struct circ_buf *xmit = &port->state->xmit;
807
808 up->dma_tx_on = 0;
809
810 /* First allocate the RX buffer */
811 dbuf = &up->rxbuf;
812 dbuf->buf = kzalloc(HSU_DMA_BUF_SIZE, GFP_KERNEL);
813 if (!dbuf->buf) {
814 up->use_dma = 0;
815 goto exit;
816 }
817 dbuf->dma_addr = dma_map_single(port->dev,
818 dbuf->buf,
819 HSU_DMA_BUF_SIZE,
820 DMA_FROM_DEVICE);
821 dbuf->dma_size = HSU_DMA_BUF_SIZE;
822
823 /* Start the RX channel right now */
824 hsu_dma_start_rx_chan(up->rxc, dbuf);
825
826 /* Next init the TX DMA */
827 dbuf = &up->txbuf;
828 dbuf->buf = xmit->buf;
829 dbuf->dma_addr = dma_map_single(port->dev,
830 dbuf->buf,
831 UART_XMIT_SIZE,
832 DMA_TO_DEVICE);
833 dbuf->dma_size = UART_XMIT_SIZE;
834
835 /* This should not be changed all around */
836 chan_writel(up->txc, HSU_CH_BSR, 32);
837 chan_writel(up->txc, HSU_CH_MOTSR, 4);
838 dbuf->ofs = 0;
839 }
840
841exit:
842 /* And clear the interrupt registers again for luck. */
843 (void) serial_in(up, UART_LSR);
844 (void) serial_in(up, UART_RX);
845 (void) serial_in(up, UART_IIR);
846 (void) serial_in(up, UART_MSR);
847
848 up->running = 1;
849 return 0;
850}
851
852static void serial_hsu_shutdown(struct uart_port *port)
853{
854 struct uart_hsu_port *up =
855 container_of(port, struct uart_hsu_port, port);
856 unsigned long flags;
857
858 /* Disable interrupts from this port */
859 up->ier = 0;
860 serial_out(up, UART_IER, 0);
861 up->running = 0;
862
863 spin_lock_irqsave(&up->port.lock, flags);
864 up->port.mctrl &= ~TIOCM_OUT2;
865 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
866 spin_unlock_irqrestore(&up->port.lock, flags);
867
868 /* Disable break condition and FIFOs */
869 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
870 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
871 UART_FCR_CLEAR_RCVR |
872 UART_FCR_CLEAR_XMIT);
873 serial_out(up, UART_FCR, 0);
874}
875
876static void
877serial_hsu_set_termios(struct uart_port *port, struct ktermios *termios,
878 struct ktermios *old)
879{
880 struct uart_hsu_port *up =
881 container_of(port, struct uart_hsu_port, port);
882 struct tty_struct *tty = port->state->port.tty;
883 unsigned char cval, fcr = 0;
884 unsigned long flags;
885 unsigned int baud, quot;
886 u32 ps, mul;
887
888 switch (termios->c_cflag & CSIZE) {
889 case CS5:
890 cval = UART_LCR_WLEN5;
891 break;
892 case CS6:
893 cval = UART_LCR_WLEN6;
894 break;
895 case CS7:
896 cval = UART_LCR_WLEN7;
897 break;
898 default:
899 case CS8:
900 cval = UART_LCR_WLEN8;
901 break;
902 }
903
904 /* CMSPAR isn't supported by this driver */
905 if (tty)
906 tty->termios->c_cflag &= ~CMSPAR;
907
908 if (termios->c_cflag & CSTOPB)
909 cval |= UART_LCR_STOP;
910 if (termios->c_cflag & PARENB)
911 cval |= UART_LCR_PARITY;
912 if (!(termios->c_cflag & PARODD))
913 cval |= UART_LCR_EPAR;
914
915 /*
916 * The base clk is 50Mhz, and the baud rate come from:
917 * baud = 50M * MUL / (DIV * PS * DLAB)
918 *
919 * For those basic low baud rate we can get the direct
920 * scalar from 2746800, like 115200 = 2746800/24. For those
921 * higher baud rate, we handle them case by case, mainly by
922 * adjusting the MUL/PS registers, and DIV register is kept
923 * as default value 0x3d09 to make things simple
924 */
925 baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
926
927 quot = 1;
928 ps = 0x10;
929 mul = 0x3600;
930 switch (baud) {
931 case 3500000:
932 mul = 0x3345;
933 ps = 0xC;
934 break;
935 case 1843200:
936 mul = 0x2400;
937 break;
938 case 3000000:
939 case 2500000:
940 case 2000000:
941 case 1500000:
942 case 1000000:
943 case 500000:
944 /* mul/ps/quot = 0x9C4/0x10/0x1 will make a 500000 bps */
945 mul = baud / 500000 * 0x9C4;
946 break;
947 default:
948 /* Use uart_get_divisor to get quot for other baud rates */
949 quot = 0;
950 }
951
952 if (!quot)
953 quot = uart_get_divisor(port, baud);
954
955 if ((up->port.uartclk / quot) < (2400 * 16))
956 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_1B;
957 else if ((up->port.uartclk / quot) < (230400 * 16))
958 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_16B;
959 else
960 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_HSU_64_32B;
961
962 fcr |= UART_FCR_HSU_64B_FIFO;
963
964 /*
965 * Ok, we're now changing the port state. Do it with
966 * interrupts disabled.
967 */
968 spin_lock_irqsave(&up->port.lock, flags);
969
970 /* Update the per-port timeout */
971 uart_update_timeout(port, termios->c_cflag, baud);
972
973 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
974 if (termios->c_iflag & INPCK)
975 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
976 if (termios->c_iflag & (BRKINT | PARMRK))
977 up->port.read_status_mask |= UART_LSR_BI;
978
979 /* Characters to ignore */
980 up->port.ignore_status_mask = 0;
981 if (termios->c_iflag & IGNPAR)
982 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
983 if (termios->c_iflag & IGNBRK) {
984 up->port.ignore_status_mask |= UART_LSR_BI;
985 /*
986 * If we're ignoring parity and break indicators,
987 * ignore overruns too (for real raw support).
988 */
989 if (termios->c_iflag & IGNPAR)
990 up->port.ignore_status_mask |= UART_LSR_OE;
991 }
992
993 /* Ignore all characters if CREAD is not set */
994 if ((termios->c_cflag & CREAD) == 0)
995 up->port.ignore_status_mask |= UART_LSR_DR;
996
997 /*
998 * CTS flow control flag and modem status interrupts, disable
999 * MSI by default
1000 */
1001 up->ier &= ~UART_IER_MSI;
1002 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
1003 up->ier |= UART_IER_MSI;
1004
1005 serial_out(up, UART_IER, up->ier);
1006
1007 if (termios->c_cflag & CRTSCTS)
1008 up->mcr |= UART_MCR_AFE | UART_MCR_RTS;
1009 else
1010 up->mcr &= ~UART_MCR_AFE;
1011
1012 serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
1013 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
1014 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
1015 serial_out(up, UART_LCR, cval); /* reset DLAB */
1016 serial_out(up, UART_MUL, mul); /* set MUL */
1017 serial_out(up, UART_PS, ps); /* set PS */
1018 up->lcr = cval; /* Save LCR */
1019 serial_hsu_set_mctrl(&up->port, up->port.mctrl);
1020 serial_out(up, UART_FCR, fcr);
1021 spin_unlock_irqrestore(&up->port.lock, flags);
1022}
1023
1024static void
1025serial_hsu_pm(struct uart_port *port, unsigned int state,
1026 unsigned int oldstate)
1027{
1028}
1029
1030static void serial_hsu_release_port(struct uart_port *port)
1031{
1032}
1033
1034static int serial_hsu_request_port(struct uart_port *port)
1035{
1036 return 0;
1037}
1038
1039static void serial_hsu_config_port(struct uart_port *port, int flags)
1040{
1041 struct uart_hsu_port *up =
1042 container_of(port, struct uart_hsu_port, port);
1043 up->port.type = PORT_MFD;
1044}
1045
1046static int
1047serial_hsu_verify_port(struct uart_port *port, struct serial_struct *ser)
1048{
1049 /* We don't want the core code to modify any port params */
1050 return -EINVAL;
1051}
1052
1053static const char *
1054serial_hsu_type(struct uart_port *port)
1055{
1056 struct uart_hsu_port *up =
1057 container_of(port, struct uart_hsu_port, port);
1058 return up->name;
1059}
1060
1061/* Mainly for uart console use */
1062static struct uart_hsu_port *serial_hsu_ports[3];
1063static struct uart_driver serial_hsu_reg;
1064
1065#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
1066
1067#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1068
1069/* Wait for transmitter & holding register to empty */
1070static inline void wait_for_xmitr(struct uart_hsu_port *up)
1071{
1072 unsigned int status, tmout = 1000;
1073
1074 /* Wait up to 1ms for the character to be sent. */
1075 do {
1076 status = serial_in(up, UART_LSR);
1077
1078 if (status & UART_LSR_BI)
1079 up->lsr_break_flag = UART_LSR_BI;
1080
1081 if (--tmout == 0)
1082 break;
1083 udelay(1);
1084 } while (!(status & BOTH_EMPTY));
1085
1086 /* Wait up to 1s for flow control if necessary */
1087 if (up->port.flags & UPF_CONS_FLOW) {
1088 tmout = 1000000;
1089 while (--tmout &&
1090 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
1091 udelay(1);
1092 }
1093}
1094
1095static void serial_hsu_console_putchar(struct uart_port *port, int ch)
1096{
1097 struct uart_hsu_port *up =
1098 container_of(port, struct uart_hsu_port, port);
1099
1100 wait_for_xmitr(up);
1101 serial_out(up, UART_TX, ch);
1102}
1103
1104/*
1105 * Print a string to the serial port trying not to disturb
1106 * any possible real use of the port...
1107 *
1108 * The console_lock must be held when we get here.
1109 */
1110static void
1111serial_hsu_console_write(struct console *co, const char *s, unsigned int count)
1112{
1113 struct uart_hsu_port *up = serial_hsu_ports[co->index];
1114 unsigned long flags;
1115 unsigned int ier;
1116 int locked = 1;
1117
1118 local_irq_save(flags);
1119 if (up->port.sysrq)
1120 locked = 0;
1121 else if (oops_in_progress) {
1122 locked = spin_trylock(&up->port.lock);
1123 } else
1124 spin_lock(&up->port.lock);
1125
1126 /* First save the IER then disable the interrupts */
1127 ier = serial_in(up, UART_IER);
1128 serial_out(up, UART_IER, 0);
1129
1130 uart_console_write(&up->port, s, count, serial_hsu_console_putchar);
1131
1132 /*
1133 * Finally, wait for transmitter to become empty
1134 * and restore the IER
1135 */
1136 wait_for_xmitr(up);
1137 serial_out(up, UART_IER, ier);
1138
1139 if (locked)
1140 spin_unlock(&up->port.lock);
1141 local_irq_restore(flags);
1142}
1143
1144static struct console serial_hsu_console;
1145
1146static int __init
1147serial_hsu_console_setup(struct console *co, char *options)
1148{
1149 struct uart_hsu_port *up;
1150 int baud = 115200;
1151 int bits = 8;
1152 int parity = 'n';
1153 int flow = 'n';
1154 int ret;
1155
1156 if (co->index == -1 || co->index >= serial_hsu_reg.nr)
1157 co->index = 0;
1158 up = serial_hsu_ports[co->index];
1159 if (!up)
1160 return -ENODEV;
1161
1162 if (options)
1163 uart_parse_options(options, &baud, &parity, &bits, &flow);
1164
1165 ret = uart_set_options(&up->port, co, baud, parity, bits, flow);
1166
1167 return ret;
1168}
1169
1170static struct console serial_hsu_console = {
1171 .name = "ttyMFD",
1172 .write = serial_hsu_console_write,
1173 .device = uart_console_device,
1174 .setup = serial_hsu_console_setup,
1175 .flags = CON_PRINTBUFFER,
1176 .index = 2,
1177 .data = &serial_hsu_reg,
1178};
1179#endif
1180
1181struct uart_ops serial_hsu_pops = {
1182 .tx_empty = serial_hsu_tx_empty,
1183 .set_mctrl = serial_hsu_set_mctrl,
1184 .get_mctrl = serial_hsu_get_mctrl,
1185 .stop_tx = serial_hsu_stop_tx,
1186 .start_tx = serial_hsu_start_tx,
1187 .stop_rx = serial_hsu_stop_rx,
1188 .enable_ms = serial_hsu_enable_ms,
1189 .break_ctl = serial_hsu_break_ctl,
1190 .startup = serial_hsu_startup,
1191 .shutdown = serial_hsu_shutdown,
1192 .set_termios = serial_hsu_set_termios,
1193 .pm = serial_hsu_pm,
1194 .type = serial_hsu_type,
1195 .release_port = serial_hsu_release_port,
1196 .request_port = serial_hsu_request_port,
1197 .config_port = serial_hsu_config_port,
1198 .verify_port = serial_hsu_verify_port,
1199};
1200
1201static struct uart_driver serial_hsu_reg = {
1202 .owner = THIS_MODULE,
1203 .driver_name = "MFD serial",
1204 .dev_name = "ttyMFD",
1205 .major = TTY_MAJOR,
1206 .minor = 128,
1207 .nr = 3,
1208};
1209
1210#ifdef CONFIG_PM
1211static int serial_hsu_suspend(struct pci_dev *pdev, pm_message_t state)
1212{
1213 void *priv = pci_get_drvdata(pdev);
1214 struct uart_hsu_port *up;
1215
1216 /* Make sure this is not the internal dma controller */
1217 if (priv && (pdev->device != 0x081E)) {
1218 up = priv;
1219 uart_suspend_port(&serial_hsu_reg, &up->port);
1220 }
1221
1222 pci_save_state(pdev);
1223 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1224 return 0;
1225}
1226
1227static int serial_hsu_resume(struct pci_dev *pdev)
1228{
1229 void *priv = pci_get_drvdata(pdev);
1230 struct uart_hsu_port *up;
1231 int ret;
1232
1233 pci_set_power_state(pdev, PCI_D0);
1234 pci_restore_state(pdev);
1235
1236 ret = pci_enable_device(pdev);
1237 if (ret)
1238 dev_warn(&pdev->dev,
1239 "HSU: can't re-enable device, try to continue\n");
1240
1241 if (priv && (pdev->device != 0x081E)) {
1242 up = priv;
1243 uart_resume_port(&serial_hsu_reg, &up->port);
1244 }
1245 return 0;
1246}
1247#else
1248#define serial_hsu_suspend NULL
1249#define serial_hsu_resume NULL
1250#endif
1251
1252/* temp global pointer before we settle down on using one or four PCI dev */
1253static struct hsu_port *phsu;
1254
1255static int serial_hsu_probe(struct pci_dev *pdev,
1256 const struct pci_device_id *ent)
1257{
1258 struct uart_hsu_port *uport;
1259 int index, ret;
1260
1261 printk(KERN_INFO "HSU: found PCI Serial controller(ID: %04x:%04x)\n",
1262 pdev->vendor, pdev->device);
1263
1264 switch (pdev->device) {
1265 case 0x081B:
1266 index = 0;
1267 break;
1268 case 0x081C:
1269 index = 1;
1270 break;
1271 case 0x081D:
1272 index = 2;
1273 break;
1274 case 0x081E:
1275 /* internal DMA controller */
1276 index = 3;
1277 break;
1278 default:
1279 dev_err(&pdev->dev, "HSU: out of index!");
1280 return -ENODEV;
1281 }
1282
1283 ret = pci_enable_device(pdev);
1284 if (ret)
1285 return ret;
1286
1287 if (index == 3) {
1288 /* DMA controller */
1289 ret = request_irq(pdev->irq, dma_irq, 0, "hsu_dma", phsu);
1290 if (ret) {
1291 dev_err(&pdev->dev, "can not get IRQ\n");
1292 goto err_disable;
1293 }
1294 pci_set_drvdata(pdev, phsu);
1295 } else {
1296 /* UART port 0~2 */
1297 uport = &phsu->port[index];
1298 uport->port.irq = pdev->irq;
1299 uport->port.dev = &pdev->dev;
1300 uport->dev = &pdev->dev;
1301
1302 ret = request_irq(pdev->irq, port_irq, 0, uport->name, uport);
1303 if (ret) {
1304 dev_err(&pdev->dev, "can not get IRQ\n");
1305 goto err_disable;
1306 }
1307 uart_add_one_port(&serial_hsu_reg, &uport->port);
1308
1309#ifdef CONFIG_SERIAL_MFD_HSU_CONSOLE
1310 if (index == 2) {
1311 register_console(&serial_hsu_console);
1312 uport->port.cons = &serial_hsu_console;
1313 }
1314#endif
1315 pci_set_drvdata(pdev, uport);
1316 }
1317
1318 return 0;
1319
1320err_disable:
1321 pci_disable_device(pdev);
1322 return ret;
1323}
1324
1325static void hsu_global_init(void)
1326{
1327 struct hsu_port *hsu;
1328 struct uart_hsu_port *uport;
1329 struct hsu_dma_chan *dchan;
1330 int i, ret;
1331
1332 hsu = kzalloc(sizeof(struct hsu_port), GFP_KERNEL);
1333 if (!hsu)
1334 return;
1335
1336 /* Get basic io resource and map it */
1337 hsu->paddr = 0xffa28000;
1338 hsu->iolen = 0x1000;
1339
1340 if (!(request_mem_region(hsu->paddr, hsu->iolen, "HSU global")))
1341 pr_warning("HSU: error in request mem region\n");
1342
1343 hsu->reg = ioremap_nocache((unsigned long)hsu->paddr, hsu->iolen);
1344 if (!hsu->reg) {
1345 pr_err("HSU: error in ioremap\n");
1346 ret = -ENOMEM;
1347 goto err_free_region;
1348 }
1349
1350 /* Initialise the 3 UART ports */
1351 uport = hsu->port;
1352 for (i = 0; i < 3; i++) {
1353 uport->port.type = PORT_MFD;
1354 uport->port.iotype = UPIO_MEM;
1355 uport->port.mapbase = (resource_size_t)hsu->paddr
1356 + HSU_PORT_REG_OFFSET
1357 + i * HSU_PORT_REG_LENGTH;
1358 uport->port.membase = hsu->reg + HSU_PORT_REG_OFFSET
1359 + i * HSU_PORT_REG_LENGTH;
1360
1361 sprintf(uport->name, "hsu_port%d", i);
1362 uport->port.fifosize = 64;
1363 uport->port.ops = &serial_hsu_pops;
1364 uport->port.line = i;
1365 uport->port.flags = UPF_IOREMAP;
1366 /* set the scalable maxim support rate to 2746800 bps */
1367 uport->port.uartclk = 115200 * 24 * 16;
1368
1369 uport->running = 0;
1370 uport->txc = &hsu->chans[i * 2];
1371 uport->rxc = &hsu->chans[i * 2 + 1];
1372
1373 serial_hsu_ports[i] = uport;
1374 uport->index = i;
1375
1376 if (hsu_dma_enable & (1<<i))
1377 uport->use_dma = 1;
1378 else
1379 uport->use_dma = 0;
1380
1381 uport++;
1382 }
1383
1384 /* Initialise 6 dma channels */
1385 dchan = hsu->chans;
1386 for (i = 0; i < 6; i++) {
1387 dchan->id = i;
1388 dchan->dirt = (i & 0x1) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1389 dchan->uport = &hsu->port[i/2];
1390 dchan->reg = hsu->reg + HSU_DMA_CHANS_REG_OFFSET +
1391 i * HSU_DMA_CHANS_REG_LENGTH;
1392
1393 dchan++;
1394 }
1395
1396 phsu = hsu;
1397 hsu_debugfs_init(hsu);
1398 return;
1399
1400err_free_region:
1401 release_mem_region(hsu->paddr, hsu->iolen);
1402 kfree(hsu);
1403 return;
1404}
1405
1406static void serial_hsu_remove(struct pci_dev *pdev)
1407{
1408 void *priv = pci_get_drvdata(pdev);
1409 struct uart_hsu_port *up;
1410
1411 if (!priv)
1412 return;
1413
1414 /* For port 0/1/2, priv is the address of uart_hsu_port */
1415 if (pdev->device != 0x081E) {
1416 up = priv;
1417 uart_remove_one_port(&serial_hsu_reg, &up->port);
1418 }
1419
1420 pci_set_drvdata(pdev, NULL);
1421 free_irq(pdev->irq, priv);
1422 pci_disable_device(pdev);
1423}
1424
1425/* First 3 are UART ports, and the 4th is the DMA */
1426static const struct pci_device_id pci_ids[] __devinitdata = {
1427 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081B) },
1428 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081C) },
1429 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081D) },
1430 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x081E) },
1431 {},
1432};
1433
1434static struct pci_driver hsu_pci_driver = {
1435 .name = "HSU serial",
1436 .id_table = pci_ids,
1437 .probe = serial_hsu_probe,
1438 .remove = __devexit_p(serial_hsu_remove),
1439 .suspend = serial_hsu_suspend,
1440 .resume = serial_hsu_resume,
1441};
1442
1443static int __init hsu_pci_init(void)
1444{
1445 int ret;
1446
1447 hsu_global_init();
1448
1449 ret = uart_register_driver(&serial_hsu_reg);
1450 if (ret)
1451 return ret;
1452
1453 return pci_register_driver(&hsu_pci_driver);
1454}
1455
1456static void __exit hsu_pci_exit(void)
1457{
1458 pci_unregister_driver(&hsu_pci_driver);
1459 uart_unregister_driver(&serial_hsu_reg);
1460
1461 hsu_debugfs_remove(phsu);
1462
1463 kfree(phsu);
1464}
1465
1466module_init(hsu_pci_init);
1467module_exit(hsu_pci_exit);
1468
1469MODULE_LICENSE("GPL v2");
1470MODULE_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..a0bcd8a3758d
--- /dev/null
+++ b/drivers/tty/serial/mpc52xx_uart.c
@@ -0,0 +1,1524 @@
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 mpc52xx_uart_of_probe(struct platform_device *op)
1306{
1307 int idx = -1;
1308 unsigned int uartclk;
1309 struct uart_port *port = NULL;
1310 struct resource res;
1311 int ret;
1312
1313 /* Check validity & presence */
1314 for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
1315 if (mpc52xx_uart_nodes[idx] == op->dev.of_node)
1316 break;
1317 if (idx >= MPC52xx_PSC_MAXNUM)
1318 return -EINVAL;
1319 pr_debug("Found %s assigned to ttyPSC%x\n",
1320 mpc52xx_uart_nodes[idx]->full_name, idx);
1321
1322 /* set the uart clock to the input clock of the psc, the different
1323 * prescalers are taken into account in the set_baudrate() methods
1324 * of the respective chip */
1325 uartclk = mpc5xxx_get_bus_frequency(op->dev.of_node);
1326 if (uartclk == 0) {
1327 dev_dbg(&op->dev, "Could not find uart clock frequency!\n");
1328 return -EINVAL;
1329 }
1330
1331 /* Init the port structure */
1332 port = &mpc52xx_uart_ports[idx];
1333
1334 spin_lock_init(&port->lock);
1335 port->uartclk = uartclk;
1336 port->fifosize = 512;
1337 port->iotype = UPIO_MEM;
1338 port->flags = UPF_BOOT_AUTOCONF |
1339 (uart_console(port) ? 0 : UPF_IOREMAP);
1340 port->line = idx;
1341 port->ops = &mpc52xx_uart_ops;
1342 port->dev = &op->dev;
1343
1344 /* Search for IRQ and mapbase */
1345 ret = of_address_to_resource(op->dev.of_node, 0, &res);
1346 if (ret)
1347 return ret;
1348
1349 port->mapbase = res.start;
1350 if (!port->mapbase) {
1351 dev_dbg(&op->dev, "Could not allocate resources for PSC\n");
1352 return -EINVAL;
1353 }
1354
1355 psc_ops->get_irq(port, op->dev.of_node);
1356 if (port->irq == NO_IRQ) {
1357 dev_dbg(&op->dev, "Could not get irq\n");
1358 return -EINVAL;
1359 }
1360
1361 dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
1362 (void *)port->mapbase, port->irq, port->uartclk);
1363
1364 /* Add the port to the uart sub-system */
1365 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
1366 if (ret)
1367 return ret;
1368
1369 dev_set_drvdata(&op->dev, (void *)port);
1370 return 0;
1371}
1372
1373static int
1374mpc52xx_uart_of_remove(struct platform_device *op)
1375{
1376 struct uart_port *port = dev_get_drvdata(&op->dev);
1377 dev_set_drvdata(&op->dev, NULL);
1378
1379 if (port)
1380 uart_remove_one_port(&mpc52xx_uart_driver, port);
1381
1382 return 0;
1383}
1384
1385#ifdef CONFIG_PM
1386static int
1387mpc52xx_uart_of_suspend(struct platform_device *op, pm_message_t state)
1388{
1389 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1390
1391 if (port)
1392 uart_suspend_port(&mpc52xx_uart_driver, port);
1393
1394 return 0;
1395}
1396
1397static int
1398mpc52xx_uart_of_resume(struct platform_device *op)
1399{
1400 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1401
1402 if (port)
1403 uart_resume_port(&mpc52xx_uart_driver, port);
1404
1405 return 0;
1406}
1407#endif
1408
1409static void
1410mpc52xx_uart_of_assign(struct device_node *np)
1411{
1412 int i;
1413
1414 /* Find the first free PSC number */
1415 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1416 if (mpc52xx_uart_nodes[i] == NULL) {
1417 of_node_get(np);
1418 mpc52xx_uart_nodes[i] = np;
1419 return;
1420 }
1421 }
1422}
1423
1424static void
1425mpc52xx_uart_of_enumerate(void)
1426{
1427 static int enum_done;
1428 struct device_node *np;
1429 const struct of_device_id *match;
1430 int i;
1431
1432 if (enum_done)
1433 return;
1434
1435 /* Assign index to each PSC in device tree */
1436 for_each_matching_node(np, mpc52xx_uart_of_match) {
1437 match = of_match_node(mpc52xx_uart_of_match, np);
1438 psc_ops = match->data;
1439 mpc52xx_uart_of_assign(np);
1440 }
1441
1442 enum_done = 1;
1443
1444 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1445 if (mpc52xx_uart_nodes[i])
1446 pr_debug("%s assigned to ttyPSC%x\n",
1447 mpc52xx_uart_nodes[i]->full_name, i);
1448 }
1449}
1450
1451MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
1452
1453static struct platform_driver mpc52xx_uart_of_driver = {
1454 .probe = mpc52xx_uart_of_probe,
1455 .remove = mpc52xx_uart_of_remove,
1456#ifdef CONFIG_PM
1457 .suspend = mpc52xx_uart_of_suspend,
1458 .resume = mpc52xx_uart_of_resume,
1459#endif
1460 .driver = {
1461 .name = "mpc52xx-psc-uart",
1462 .owner = THIS_MODULE,
1463 .of_match_table = mpc52xx_uart_of_match,
1464 },
1465};
1466
1467
1468/* ======================================================================== */
1469/* Module */
1470/* ======================================================================== */
1471
1472static int __init
1473mpc52xx_uart_init(void)
1474{
1475 int ret;
1476
1477 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
1478
1479 ret = uart_register_driver(&mpc52xx_uart_driver);
1480 if (ret) {
1481 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
1482 __FILE__, ret);
1483 return ret;
1484 }
1485
1486 mpc52xx_uart_of_enumerate();
1487
1488 /*
1489 * Map the PSC FIFO Controller and init if on MPC512x.
1490 */
1491 if (psc_ops && psc_ops->fifoc_init) {
1492 ret = psc_ops->fifoc_init();
1493 if (ret)
1494 return ret;
1495 }
1496
1497 ret = platform_driver_register(&mpc52xx_uart_of_driver);
1498 if (ret) {
1499 printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
1500 __FILE__, ret);
1501 uart_unregister_driver(&mpc52xx_uart_driver);
1502 return ret;
1503 }
1504
1505 return 0;
1506}
1507
1508static void __exit
1509mpc52xx_uart_exit(void)
1510{
1511 if (psc_ops->fifoc_uninit)
1512 psc_ops->fifoc_uninit();
1513
1514 platform_driver_unregister(&mpc52xx_uart_of_driver);
1515 uart_unregister_driver(&mpc52xx_uart_driver);
1516}
1517
1518
1519module_init(mpc52xx_uart_init);
1520module_exit(mpc52xx_uart_exit);
1521
1522MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
1523MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
1524MODULE_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..a764bf99743b
--- /dev/null
+++ b/drivers/tty/serial/mrst_max3110.c
@@ -0,0 +1,920 @@
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 available 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[SPI_NAME_SIZE];
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 pr_info(PR_FMT "start main thread\n");
425
426 do {
427 wait_event_interruptible(*wq, max->uart_flags || kthread_should_stop());
428
429 mutex_lock(&max->thread_mutex);
430
431 if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags))
432 max3110_con_receive(max);
433
434 /* first handle console output */
435 if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags))
436 send_circ_buf(max, xmit);
437
438 /* handle uart output */
439 if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags))
440 transmit_char(max);
441
442 mutex_unlock(&max->thread_mutex);
443
444 } while (!kthread_should_stop());
445
446 return ret;
447}
448
449static irqreturn_t serial_m3110_irq(int irq, void *dev_id)
450{
451 struct uart_max3110 *max = dev_id;
452
453 /* max3110's irq is a falling edge, not level triggered,
454 * so no need to disable the irq */
455 if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags))
456 wake_up_process(max->main_thread);
457
458 return IRQ_HANDLED;
459}
460
461/* if don't use RX IRQ, then need a thread to polling read */
462static int max3110_read_thread(void *_max)
463{
464 struct uart_max3110 *max = _max;
465
466 pr_info(PR_FMT "start read thread\n");
467 do {
468 /*
469 * If can't acquire the mutex, it means the main thread
470 * is running which will also perform the rx job
471 */
472 if (mutex_trylock(&max->thread_mutex)) {
473 max3110_con_receive(max);
474 mutex_unlock(&max->thread_mutex);
475 }
476
477 set_current_state(TASK_INTERRUPTIBLE);
478 schedule_timeout(HZ / 20);
479 } while (!kthread_should_stop());
480
481 return 0;
482}
483
484static int serial_m3110_startup(struct uart_port *port)
485{
486 struct uart_max3110 *max =
487 container_of(port, struct uart_max3110, port);
488 u16 config = 0;
489 int ret = 0;
490
491 if (port->line != 0) {
492 pr_err(PR_FMT "uart port startup failed\n");
493 return -1;
494 }
495
496 /* Disable all IRQ and config it to 115200, 8n1 */
497 config = WC_TAG | WC_FIFO_ENABLE
498 | WC_1_STOPBITS
499 | WC_8BIT_WORD
500 | WC_BAUD_DR2;
501
502 /* as we use thread to handle tx/rx, need set low latency */
503 port->state->port.tty->low_latency = 1;
504
505 if (max->irq) {
506 max->read_thread = NULL;
507 ret = request_irq(max->irq, serial_m3110_irq,
508 IRQ_TYPE_EDGE_FALLING, "max3110", max);
509 if (ret) {
510 max->irq = 0;
511 pr_err(PR_FMT "unable to allocate IRQ, polling\n");
512 } else {
513 /* Enable RX IRQ only */
514 config |= WC_RXA_IRQ_ENABLE;
515 }
516 }
517
518 if (max->irq == 0) {
519 /* If IRQ is disabled, start a read thread for input data */
520 max->read_thread =
521 kthread_run(max3110_read_thread, max, "max3110_read");
522 if (IS_ERR(max->read_thread)) {
523 ret = PTR_ERR(max->read_thread);
524 max->read_thread = NULL;
525 pr_err(PR_FMT "Can't create read thread!\n");
526 return ret;
527 }
528 }
529
530 ret = max3110_out(max, config);
531 if (ret) {
532 if (max->irq)
533 free_irq(max->irq, max);
534 if (max->read_thread)
535 kthread_stop(max->read_thread);
536 max->read_thread = NULL;
537 return ret;
538 }
539
540 max->cur_conf = config;
541 return 0;
542}
543
544static void serial_m3110_shutdown(struct uart_port *port)
545{
546 struct uart_max3110 *max =
547 container_of(port, struct uart_max3110, port);
548 u16 config;
549
550 if (max->read_thread) {
551 kthread_stop(max->read_thread);
552 max->read_thread = NULL;
553 }
554
555 if (max->irq)
556 free_irq(max->irq, max);
557
558 /* Disable interrupts from this port */
559 config = WC_TAG | WC_SW_SHDI;
560 max3110_out(max, config);
561}
562
563static void serial_m3110_release_port(struct uart_port *port)
564{
565}
566
567static int serial_m3110_request_port(struct uart_port *port)
568{
569 return 0;
570}
571
572static void serial_m3110_config_port(struct uart_port *port, int flags)
573{
574 port->type = PORT_MAX3100;
575}
576
577static int
578serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
579{
580 /* we don't want the core code to modify any port params */
581 return -EINVAL;
582}
583
584
585static const char *serial_m3110_type(struct uart_port *port)
586{
587 struct uart_max3110 *max =
588 container_of(port, struct uart_max3110, port);
589 return max->name;
590}
591
592static void
593serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
594 struct ktermios *old)
595{
596 struct uart_max3110 *max =
597 container_of(port, struct uart_max3110, port);
598 unsigned char cval;
599 unsigned int baud, parity = 0;
600 int clk_div = -1;
601 u16 new_conf = max->cur_conf;
602
603 switch (termios->c_cflag & CSIZE) {
604 case CS7:
605 cval = UART_LCR_WLEN7;
606 new_conf |= WC_7BIT_WORD;
607 break;
608 default:
609 /* We only support CS7 & CS8 */
610 termios->c_cflag &= ~CSIZE;
611 termios->c_cflag |= CS8;
612 case CS8:
613 cval = UART_LCR_WLEN8;
614 new_conf |= WC_8BIT_WORD;
615 break;
616 }
617
618 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
619
620 /* First calc the div for 1.8MHZ clock case */
621 switch (baud) {
622 case 300:
623 clk_div = WC_BAUD_DR384;
624 break;
625 case 600:
626 clk_div = WC_BAUD_DR192;
627 break;
628 case 1200:
629 clk_div = WC_BAUD_DR96;
630 break;
631 case 2400:
632 clk_div = WC_BAUD_DR48;
633 break;
634 case 4800:
635 clk_div = WC_BAUD_DR24;
636 break;
637 case 9600:
638 clk_div = WC_BAUD_DR12;
639 break;
640 case 19200:
641 clk_div = WC_BAUD_DR6;
642 break;
643 case 38400:
644 clk_div = WC_BAUD_DR3;
645 break;
646 case 57600:
647 clk_div = WC_BAUD_DR2;
648 break;
649 case 115200:
650 clk_div = WC_BAUD_DR1;
651 break;
652 case 230400:
653 if (max->clock & MAX3110_HIGH_CLK)
654 break;
655 default:
656 /* Pick the previous baud rate */
657 baud = max->baud;
658 clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
659 tty_termios_encode_baud_rate(termios, baud, baud);
660 }
661
662 if (max->clock & MAX3110_HIGH_CLK) {
663 clk_div += 1;
664 /* High clk version max3110 doesn't support B300 */
665 if (baud == 300) {
666 baud = 600;
667 clk_div = WC_BAUD_DR384;
668 }
669 if (baud == 230400)
670 clk_div = WC_BAUD_DR1;
671 tty_termios_encode_baud_rate(termios, baud, baud);
672 }
673
674 new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
675
676 if (unlikely(termios->c_cflag & CMSPAR))
677 termios->c_cflag &= ~CMSPAR;
678
679 if (termios->c_cflag & CSTOPB)
680 new_conf |= WC_2_STOPBITS;
681 else
682 new_conf &= ~WC_2_STOPBITS;
683
684 if (termios->c_cflag & PARENB) {
685 new_conf |= WC_PARITY_ENABLE;
686 parity |= UART_LCR_PARITY;
687 } else
688 new_conf &= ~WC_PARITY_ENABLE;
689
690 if (!(termios->c_cflag & PARODD))
691 parity |= UART_LCR_EPAR;
692 max->parity = parity;
693
694 uart_update_timeout(port, termios->c_cflag, baud);
695
696 new_conf |= WC_TAG;
697 if (new_conf != max->cur_conf) {
698 if (!max3110_out(max, new_conf)) {
699 max->cur_conf = new_conf;
700 max->baud = baud;
701 }
702 }
703}
704
705/* Don't handle hw handshaking */
706static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
707{
708 return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
709}
710
711static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
712{
713}
714
715static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
716{
717}
718
719static void serial_m3110_pm(struct uart_port *port, unsigned int state,
720 unsigned int oldstate)
721{
722}
723
724static void serial_m3110_enable_ms(struct uart_port *port)
725{
726}
727
728struct uart_ops serial_m3110_ops = {
729 .tx_empty = serial_m3110_tx_empty,
730 .set_mctrl = serial_m3110_set_mctrl,
731 .get_mctrl = serial_m3110_get_mctrl,
732 .stop_tx = serial_m3110_stop_tx,
733 .start_tx = serial_m3110_start_tx,
734 .stop_rx = serial_m3110_stop_rx,
735 .enable_ms = serial_m3110_enable_ms,
736 .break_ctl = serial_m3110_break_ctl,
737 .startup = serial_m3110_startup,
738 .shutdown = serial_m3110_shutdown,
739 .set_termios = serial_m3110_set_termios,
740 .pm = serial_m3110_pm,
741 .type = serial_m3110_type,
742 .release_port = serial_m3110_release_port,
743 .request_port = serial_m3110_request_port,
744 .config_port = serial_m3110_config_port,
745 .verify_port = serial_m3110_verify_port,
746};
747
748static struct uart_driver serial_m3110_reg = {
749 .owner = THIS_MODULE,
750 .driver_name = "MRST serial",
751 .dev_name = "ttyS",
752 .major = TTY_MAJOR,
753 .minor = 64,
754 .nr = 1,
755 .cons = &serial_m3110_console,
756};
757
758#ifdef CONFIG_PM
759static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state)
760{
761 struct uart_max3110 *max = spi_get_drvdata(spi);
762
763 disable_irq(max->irq);
764 uart_suspend_port(&serial_m3110_reg, &max->port);
765 max3110_out(max, max->cur_conf | WC_SW_SHDI);
766 return 0;
767}
768
769static int serial_m3110_resume(struct spi_device *spi)
770{
771 struct uart_max3110 *max = spi_get_drvdata(spi);
772
773 max3110_out(max, max->cur_conf);
774 uart_resume_port(&serial_m3110_reg, &max->port);
775 enable_irq(max->irq);
776 return 0;
777}
778#else
779#define serial_m3110_suspend NULL
780#define serial_m3110_resume NULL
781#endif
782
783static int __devinit serial_m3110_probe(struct spi_device *spi)
784{
785 struct uart_max3110 *max;
786 void *buffer;
787 u16 res;
788 int ret = 0;
789
790 max = kzalloc(sizeof(*max), GFP_KERNEL);
791 if (!max)
792 return -ENOMEM;
793
794 /* Set spi info */
795 spi->bits_per_word = 16;
796 max->clock = MAX3110_HIGH_CLK;
797
798 spi_setup(spi);
799
800 max->port.type = PORT_MAX3100;
801 max->port.fifosize = 2; /* Only have 16b buffer */
802 max->port.ops = &serial_m3110_ops;
803 max->port.line = 0;
804 max->port.dev = &spi->dev;
805 max->port.uartclk = 115200;
806
807 max->spi = spi;
808 strcpy(max->name, spi->modalias);
809 max->irq = (u16)spi->irq;
810
811 mutex_init(&max->thread_mutex);
812
813 max->word_7bits = 0;
814 max->parity = 0;
815 max->baud = 0;
816
817 max->cur_conf = 0;
818 max->uart_flags = 0;
819
820 /* Check if reading configuration register returns something sane */
821
822 res = RC_TAG;
823 ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0);
824 if (ret < 0 || res == 0 || res == 0xffff) {
825 dev_dbg(&spi->dev, "MAX3111 deemed not present (conf reg %04x)",
826 res);
827 ret = -ENODEV;
828 goto err_get_page;
829 }
830
831 buffer = (void *)__get_free_page(GFP_KERNEL);
832 if (!buffer) {
833 ret = -ENOMEM;
834 goto err_get_page;
835 }
836 max->con_xmit.buf = buffer;
837 max->con_xmit.head = 0;
838 max->con_xmit.tail = 0;
839
840 init_waitqueue_head(&max->wq);
841
842 max->main_thread = kthread_run(max3110_main_thread,
843 max, "max3110_main");
844 if (IS_ERR(max->main_thread)) {
845 ret = PTR_ERR(max->main_thread);
846 goto err_kthread;
847 }
848
849 spi_set_drvdata(spi, max);
850 pmax = max;
851
852 /* Give membase a psudo value to pass serial_core's check */
853 max->port.membase = (void *)0xff110000;
854 uart_add_one_port(&serial_m3110_reg, &max->port);
855
856 return 0;
857
858err_kthread:
859 free_page((unsigned long)buffer);
860err_get_page:
861 kfree(max);
862 return ret;
863}
864
865static int __devexit serial_m3110_remove(struct spi_device *dev)
866{
867 struct uart_max3110 *max = spi_get_drvdata(dev);
868
869 if (!max)
870 return 0;
871
872 uart_remove_one_port(&serial_m3110_reg, &max->port);
873
874 free_page((unsigned long)max->con_xmit.buf);
875
876 if (max->main_thread)
877 kthread_stop(max->main_thread);
878
879 kfree(max);
880 return 0;
881}
882
883static struct spi_driver uart_max3110_driver = {
884 .driver = {
885 .name = "spi_max3111",
886 .bus = &spi_bus_type,
887 .owner = THIS_MODULE,
888 },
889 .probe = serial_m3110_probe,
890 .remove = __devexit_p(serial_m3110_remove),
891 .suspend = serial_m3110_suspend,
892 .resume = serial_m3110_resume,
893};
894
895static int __init serial_m3110_init(void)
896{
897 int ret = 0;
898
899 ret = uart_register_driver(&serial_m3110_reg);
900 if (ret)
901 return ret;
902
903 ret = spi_register_driver(&uart_max3110_driver);
904 if (ret)
905 uart_unregister_driver(&serial_m3110_reg);
906
907 return ret;
908}
909
910static void __exit serial_m3110_exit(void)
911{
912 spi_unregister_driver(&uart_max3110_driver);
913 uart_unregister_driver(&serial_m3110_reg);
914}
915
916module_init(serial_m3110_init);
917module_exit(serial_m3110_exit);
918
919MODULE_LICENSE("GPL v2");
920MODULE_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..c37ea48c825a
--- /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 available 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..e6ba83876508
--- /dev/null
+++ b/drivers/tty/serial/msm_serial.c
@@ -0,0 +1,966 @@
1/*
2 * Driver for msm7k serial device and console
3 *
4 * Copyright (C) 2007 Google, Inc.
5 * Author: Robert Love <rlove@google.com>
6 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#if defined(CONFIG_SERIAL_MSM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19# define SUPPORT_SYSRQ
20#endif
21
22#include <linux/hrtimer.h>
23#include <linux/module.h>
24#include <linux/io.h>
25#include <linux/ioport.h>
26#include <linux/irq.h>
27#include <linux/init.h>
28#include <linux/console.h>
29#include <linux/tty.h>
30#include <linux/tty_flip.h>
31#include <linux/serial_core.h>
32#include <linux/serial.h>
33#include <linux/clk.h>
34#include <linux/platform_device.h>
35#include <linux/delay.h>
36
37#include "msm_serial.h"
38
39struct msm_port {
40 struct uart_port uart;
41 char name[16];
42 struct clk *clk;
43 struct clk *pclk;
44 unsigned int imr;
45 unsigned int *gsbi_base;
46 int is_uartdm;
47 unsigned int old_snap_state;
48};
49
50static inline void wait_for_xmitr(struct uart_port *port, int bits)
51{
52 if (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY))
53 while ((msm_read(port, UART_ISR) & bits) != bits)
54 cpu_relax();
55}
56
57static void msm_stop_tx(struct uart_port *port)
58{
59 struct msm_port *msm_port = UART_TO_MSM(port);
60
61 msm_port->imr &= ~UART_IMR_TXLEV;
62 msm_write(port, msm_port->imr, UART_IMR);
63}
64
65static void msm_start_tx(struct uart_port *port)
66{
67 struct msm_port *msm_port = UART_TO_MSM(port);
68
69 msm_port->imr |= UART_IMR_TXLEV;
70 msm_write(port, msm_port->imr, UART_IMR);
71}
72
73static void msm_stop_rx(struct uart_port *port)
74{
75 struct msm_port *msm_port = UART_TO_MSM(port);
76
77 msm_port->imr &= ~(UART_IMR_RXLEV | UART_IMR_RXSTALE);
78 msm_write(port, msm_port->imr, UART_IMR);
79}
80
81static void msm_enable_ms(struct uart_port *port)
82{
83 struct msm_port *msm_port = UART_TO_MSM(port);
84
85 msm_port->imr |= UART_IMR_DELTA_CTS;
86 msm_write(port, msm_port->imr, UART_IMR);
87}
88
89static void handle_rx_dm(struct uart_port *port, unsigned int misr)
90{
91 struct tty_struct *tty = port->state->port.tty;
92 unsigned int sr;
93 int count = 0;
94 struct msm_port *msm_port = UART_TO_MSM(port);
95
96 if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
97 port->icount.overrun++;
98 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
99 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
100 }
101
102 if (misr & UART_IMR_RXSTALE) {
103 count = msm_read(port, UARTDM_RX_TOTAL_SNAP) -
104 msm_port->old_snap_state;
105 msm_port->old_snap_state = 0;
106 } else {
107 count = 4 * (msm_read(port, UART_RFWR));
108 msm_port->old_snap_state += count;
109 }
110
111 /* TODO: Precise error reporting */
112
113 port->icount.rx += count;
114
115 while (count > 0) {
116 unsigned int c;
117
118 sr = msm_read(port, UART_SR);
119 if ((sr & UART_SR_RX_READY) == 0) {
120 msm_port->old_snap_state -= count;
121 break;
122 }
123 c = msm_read(port, UARTDM_RF);
124 if (sr & UART_SR_RX_BREAK) {
125 port->icount.brk++;
126 if (uart_handle_break(port))
127 continue;
128 } else if (sr & UART_SR_PAR_FRAME_ERR)
129 port->icount.frame++;
130
131 /* TODO: handle sysrq */
132 tty_insert_flip_string(tty, (char *) &c,
133 (count > 4) ? 4 : count);
134 count -= 4;
135 }
136
137 tty_flip_buffer_push(tty);
138 if (misr & (UART_IMR_RXSTALE))
139 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
140 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
141 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
142}
143
144static void handle_rx(struct uart_port *port)
145{
146 struct tty_struct *tty = port->state->port.tty;
147 unsigned int sr;
148
149 /*
150 * Handle overrun. My understanding of the hardware is that overrun
151 * is not tied to the RX buffer, so we handle the case out of band.
152 */
153 if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) {
154 port->icount.overrun++;
155 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
156 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
157 }
158
159 /* and now the main RX loop */
160 while ((sr = msm_read(port, UART_SR)) & UART_SR_RX_READY) {
161 unsigned int c;
162 char flag = TTY_NORMAL;
163
164 c = msm_read(port, UART_RF);
165
166 if (sr & UART_SR_RX_BREAK) {
167 port->icount.brk++;
168 if (uart_handle_break(port))
169 continue;
170 } else if (sr & UART_SR_PAR_FRAME_ERR) {
171 port->icount.frame++;
172 } else {
173 port->icount.rx++;
174 }
175
176 /* Mask conditions we're ignorning. */
177 sr &= port->read_status_mask;
178
179 if (sr & UART_SR_RX_BREAK) {
180 flag = TTY_BREAK;
181 } else if (sr & UART_SR_PAR_FRAME_ERR) {
182 flag = TTY_FRAME;
183 }
184
185 if (!uart_handle_sysrq_char(port, c))
186 tty_insert_flip_char(tty, c, flag);
187 }
188
189 tty_flip_buffer_push(tty);
190}
191
192static void reset_dm_count(struct uart_port *port)
193{
194 wait_for_xmitr(port, UART_ISR_TX_READY);
195 msm_write(port, 1, UARTDM_NCF_TX);
196}
197
198static void handle_tx(struct uart_port *port)
199{
200 struct circ_buf *xmit = &port->state->xmit;
201 struct msm_port *msm_port = UART_TO_MSM(port);
202 int sent_tx;
203
204 if (port->x_char) {
205 if (msm_port->is_uartdm)
206 reset_dm_count(port);
207
208 msm_write(port, port->x_char,
209 msm_port->is_uartdm ? UARTDM_TF : UART_TF);
210 port->icount.tx++;
211 port->x_char = 0;
212 }
213
214 if (msm_port->is_uartdm)
215 reset_dm_count(port);
216
217 while (msm_read(port, UART_SR) & UART_SR_TX_READY) {
218 if (uart_circ_empty(xmit)) {
219 /* disable tx interrupts */
220 msm_port->imr &= ~UART_IMR_TXLEV;
221 msm_write(port, msm_port->imr, UART_IMR);
222 break;
223 }
224 msm_write(port, xmit->buf[xmit->tail],
225 msm_port->is_uartdm ? UARTDM_TF : UART_TF);
226
227 if (msm_port->is_uartdm)
228 reset_dm_count(port);
229
230 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
231 port->icount.tx++;
232 sent_tx = 1;
233 }
234
235 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
236 uart_write_wakeup(port);
237}
238
239static void handle_delta_cts(struct uart_port *port)
240{
241 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
242 port->icount.cts++;
243 wake_up_interruptible(&port->state->port.delta_msr_wait);
244}
245
246static irqreturn_t msm_irq(int irq, void *dev_id)
247{
248 struct uart_port *port = dev_id;
249 struct msm_port *msm_port = UART_TO_MSM(port);
250 unsigned int misr;
251
252 spin_lock(&port->lock);
253 misr = msm_read(port, UART_MISR);
254 msm_write(port, 0, UART_IMR); /* disable interrupt */
255
256 if (misr & (UART_IMR_RXLEV | UART_IMR_RXSTALE)) {
257 if (msm_port->is_uartdm)
258 handle_rx_dm(port, misr);
259 else
260 handle_rx(port);
261 }
262 if (misr & UART_IMR_TXLEV)
263 handle_tx(port);
264 if (misr & UART_IMR_DELTA_CTS)
265 handle_delta_cts(port);
266
267 msm_write(port, msm_port->imr, UART_IMR); /* restore interrupt */
268 spin_unlock(&port->lock);
269
270 return IRQ_HANDLED;
271}
272
273static unsigned int msm_tx_empty(struct uart_port *port)
274{
275 return (msm_read(port, UART_SR) & UART_SR_TX_EMPTY) ? TIOCSER_TEMT : 0;
276}
277
278static unsigned int msm_get_mctrl(struct uart_port *port)
279{
280 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR | TIOCM_RTS;
281}
282
283
284static void msm_reset(struct uart_port *port)
285{
286 /* reset everything */
287 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
288 msm_write(port, UART_CR_CMD_RESET_TX, UART_CR);
289 msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR);
290 msm_write(port, UART_CR_CMD_RESET_BREAK_INT, UART_CR);
291 msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR);
292 msm_write(port, UART_CR_CMD_SET_RFR, UART_CR);
293}
294
295void msm_set_mctrl(struct uart_port *port, unsigned int mctrl)
296{
297 unsigned int mr;
298 mr = msm_read(port, UART_MR1);
299
300 if (!(mctrl & TIOCM_RTS)) {
301 mr &= ~UART_MR1_RX_RDY_CTL;
302 msm_write(port, mr, UART_MR1);
303 msm_write(port, UART_CR_CMD_RESET_RFR, UART_CR);
304 } else {
305 mr |= UART_MR1_RX_RDY_CTL;
306 msm_write(port, mr, UART_MR1);
307 }
308}
309
310static void msm_break_ctl(struct uart_port *port, int break_ctl)
311{
312 if (break_ctl)
313 msm_write(port, UART_CR_CMD_START_BREAK, UART_CR);
314 else
315 msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR);
316}
317
318static int msm_set_baud_rate(struct uart_port *port, unsigned int baud)
319{
320 unsigned int baud_code, rxstale, watermark;
321 struct msm_port *msm_port = UART_TO_MSM(port);
322
323 switch (baud) {
324 case 300:
325 baud_code = UART_CSR_300;
326 rxstale = 1;
327 break;
328 case 600:
329 baud_code = UART_CSR_600;
330 rxstale = 1;
331 break;
332 case 1200:
333 baud_code = UART_CSR_1200;
334 rxstale = 1;
335 break;
336 case 2400:
337 baud_code = UART_CSR_2400;
338 rxstale = 1;
339 break;
340 case 4800:
341 baud_code = UART_CSR_4800;
342 rxstale = 1;
343 break;
344 case 9600:
345 baud_code = UART_CSR_9600;
346 rxstale = 2;
347 break;
348 case 14400:
349 baud_code = UART_CSR_14400;
350 rxstale = 3;
351 break;
352 case 19200:
353 baud_code = UART_CSR_19200;
354 rxstale = 4;
355 break;
356 case 28800:
357 baud_code = UART_CSR_28800;
358 rxstale = 6;
359 break;
360 case 38400:
361 baud_code = UART_CSR_38400;
362 rxstale = 8;
363 break;
364 case 57600:
365 baud_code = UART_CSR_57600;
366 rxstale = 16;
367 break;
368 case 115200:
369 default:
370 baud_code = UART_CSR_115200;
371 baud = 115200;
372 rxstale = 31;
373 break;
374 }
375
376 if (msm_port->is_uartdm)
377 msm_write(port, UART_CR_CMD_RESET_RX, UART_CR);
378
379 msm_write(port, baud_code, UART_CSR);
380
381 /* RX stale watermark */
382 watermark = UART_IPR_STALE_LSB & rxstale;
383 watermark |= UART_IPR_RXSTALE_LAST;
384 watermark |= UART_IPR_STALE_TIMEOUT_MSB & (rxstale << 2);
385 msm_write(port, watermark, UART_IPR);
386
387 /* set RX watermark */
388 watermark = (port->fifosize * 3) / 4;
389 msm_write(port, watermark, UART_RFWR);
390
391 /* set TX watermark */
392 msm_write(port, 10, UART_TFWR);
393
394 if (msm_port->is_uartdm) {
395 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
396 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
397 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
398 }
399
400 return baud;
401}
402
403
404static void msm_init_clock(struct uart_port *port)
405{
406 struct msm_port *msm_port = UART_TO_MSM(port);
407
408 clk_enable(msm_port->clk);
409 if (!IS_ERR(msm_port->pclk))
410 clk_enable(msm_port->pclk);
411 msm_serial_set_mnd_regs(port);
412}
413
414static int msm_startup(struct uart_port *port)
415{
416 struct msm_port *msm_port = UART_TO_MSM(port);
417 unsigned int data, rfr_level;
418 int ret;
419
420 snprintf(msm_port->name, sizeof(msm_port->name),
421 "msm_serial%d", port->line);
422
423 ret = request_irq(port->irq, msm_irq, IRQF_TRIGGER_HIGH,
424 msm_port->name, port);
425 if (unlikely(ret))
426 return ret;
427
428 msm_init_clock(port);
429
430 if (likely(port->fifosize > 12))
431 rfr_level = port->fifosize - 12;
432 else
433 rfr_level = port->fifosize;
434
435 /* set automatic RFR level */
436 data = msm_read(port, UART_MR1);
437 data &= ~UART_MR1_AUTO_RFR_LEVEL1;
438 data &= ~UART_MR1_AUTO_RFR_LEVEL0;
439 data |= UART_MR1_AUTO_RFR_LEVEL1 & (rfr_level << 2);
440 data |= UART_MR1_AUTO_RFR_LEVEL0 & rfr_level;
441 msm_write(port, data, UART_MR1);
442
443 /* make sure that RXSTALE count is non-zero */
444 data = msm_read(port, UART_IPR);
445 if (unlikely(!data)) {
446 data |= UART_IPR_RXSTALE_LAST;
447 data |= UART_IPR_STALE_LSB;
448 msm_write(port, data, UART_IPR);
449 }
450
451 data = 0;
452 if (!port->cons || (port->cons && !(port->cons->flags & CON_ENABLED))) {
453 msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
454 msm_reset(port);
455 data = UART_CR_TX_ENABLE;
456 }
457
458 data |= UART_CR_RX_ENABLE;
459 msm_write(port, data, UART_CR); /* enable TX & RX */
460
461 /* Make sure IPR is not 0 to start with*/
462 if (msm_port->is_uartdm)
463 msm_write(port, UART_IPR_STALE_LSB, UART_IPR);
464
465 /* turn on RX and CTS interrupts */
466 msm_port->imr = UART_IMR_RXLEV | UART_IMR_RXSTALE |
467 UART_IMR_CURRENT_CTS;
468
469 if (msm_port->is_uartdm) {
470 msm_write(port, 0xFFFFFF, UARTDM_DMRX);
471 msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR);
472 msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR);
473 }
474
475 msm_write(port, msm_port->imr, UART_IMR);
476 return 0;
477}
478
479static void msm_shutdown(struct uart_port *port)
480{
481 struct msm_port *msm_port = UART_TO_MSM(port);
482
483 msm_port->imr = 0;
484 msm_write(port, 0, UART_IMR); /* disable interrupts */
485
486 clk_disable(msm_port->clk);
487
488 free_irq(port->irq, port);
489}
490
491static void msm_set_termios(struct uart_port *port, struct ktermios *termios,
492 struct ktermios *old)
493{
494 unsigned long flags;
495 unsigned int baud, mr;
496
497 spin_lock_irqsave(&port->lock, flags);
498
499 /* calculate and set baud rate */
500 baud = uart_get_baud_rate(port, termios, old, 300, 115200);
501 baud = msm_set_baud_rate(port, baud);
502 if (tty_termios_baud_rate(termios))
503 tty_termios_encode_baud_rate(termios, baud, baud);
504
505 /* calculate parity */
506 mr = msm_read(port, UART_MR2);
507 mr &= ~UART_MR2_PARITY_MODE;
508 if (termios->c_cflag & PARENB) {
509 if (termios->c_cflag & PARODD)
510 mr |= UART_MR2_PARITY_MODE_ODD;
511 else if (termios->c_cflag & CMSPAR)
512 mr |= UART_MR2_PARITY_MODE_SPACE;
513 else
514 mr |= UART_MR2_PARITY_MODE_EVEN;
515 }
516
517 /* calculate bits per char */
518 mr &= ~UART_MR2_BITS_PER_CHAR;
519 switch (termios->c_cflag & CSIZE) {
520 case CS5:
521 mr |= UART_MR2_BITS_PER_CHAR_5;
522 break;
523 case CS6:
524 mr |= UART_MR2_BITS_PER_CHAR_6;
525 break;
526 case CS7:
527 mr |= UART_MR2_BITS_PER_CHAR_7;
528 break;
529 case CS8:
530 default:
531 mr |= UART_MR2_BITS_PER_CHAR_8;
532 break;
533 }
534
535 /* calculate stop bits */
536 mr &= ~(UART_MR2_STOP_BIT_LEN_ONE | UART_MR2_STOP_BIT_LEN_TWO);
537 if (termios->c_cflag & CSTOPB)
538 mr |= UART_MR2_STOP_BIT_LEN_TWO;
539 else
540 mr |= UART_MR2_STOP_BIT_LEN_ONE;
541
542 /* set parity, bits per char, and stop bit */
543 msm_write(port, mr, UART_MR2);
544
545 /* calculate and set hardware flow control */
546 mr = msm_read(port, UART_MR1);
547 mr &= ~(UART_MR1_CTS_CTL | UART_MR1_RX_RDY_CTL);
548 if (termios->c_cflag & CRTSCTS) {
549 mr |= UART_MR1_CTS_CTL;
550 mr |= UART_MR1_RX_RDY_CTL;
551 }
552 msm_write(port, mr, UART_MR1);
553
554 /* Configure status bits to ignore based on termio flags. */
555 port->read_status_mask = 0;
556 if (termios->c_iflag & INPCK)
557 port->read_status_mask |= UART_SR_PAR_FRAME_ERR;
558 if (termios->c_iflag & (BRKINT | PARMRK))
559 port->read_status_mask |= UART_SR_RX_BREAK;
560
561 uart_update_timeout(port, termios->c_cflag, baud);
562
563 spin_unlock_irqrestore(&port->lock, flags);
564}
565
566static const char *msm_type(struct uart_port *port)
567{
568 return "MSM";
569}
570
571static void msm_release_port(struct uart_port *port)
572{
573 struct platform_device *pdev = to_platform_device(port->dev);
574 struct msm_port *msm_port = UART_TO_MSM(port);
575 struct resource *uart_resource;
576 struct resource *gsbi_resource;
577 resource_size_t size;
578
579 uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
580 if (unlikely(!uart_resource))
581 return;
582 size = resource_size(uart_resource);
583
584 release_mem_region(port->mapbase, size);
585 iounmap(port->membase);
586 port->membase = NULL;
587
588 if (msm_port->gsbi_base) {
589 iowrite32(GSBI_PROTOCOL_IDLE, msm_port->gsbi_base +
590 GSBI_CONTROL);
591
592 gsbi_resource = platform_get_resource_byname(pdev,
593 IORESOURCE_MEM,
594 "gsbi_resource");
595
596 if (unlikely(!gsbi_resource))
597 return;
598
599 size = resource_size(gsbi_resource);
600 release_mem_region(gsbi_resource->start, size);
601 iounmap(msm_port->gsbi_base);
602 msm_port->gsbi_base = NULL;
603 }
604}
605
606static int msm_request_port(struct uart_port *port)
607{
608 struct msm_port *msm_port = UART_TO_MSM(port);
609 struct platform_device *pdev = to_platform_device(port->dev);
610 struct resource *uart_resource;
611 struct resource *gsbi_resource;
612 resource_size_t size;
613 int ret;
614
615 uart_resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
616 "uart_resource");
617 if (unlikely(!uart_resource))
618 return -ENXIO;
619
620 size = resource_size(uart_resource);
621
622 if (!request_mem_region(port->mapbase, size, "msm_serial"))
623 return -EBUSY;
624
625 port->membase = ioremap(port->mapbase, size);
626 if (!port->membase) {
627 ret = -EBUSY;
628 goto fail_release_port;
629 }
630
631 gsbi_resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
632 "gsbi_resource");
633 /* Is this a GSBI-based port? */
634 if (gsbi_resource) {
635 size = resource_size(gsbi_resource);
636
637 if (!request_mem_region(gsbi_resource->start, size,
638 "msm_serial")) {
639 ret = -EBUSY;
640 goto fail_release_port;
641 }
642
643 msm_port->gsbi_base = ioremap(gsbi_resource->start, size);
644 if (!msm_port->gsbi_base) {
645 ret = -EBUSY;
646 goto fail_release_gsbi;
647 }
648 }
649
650 return 0;
651
652fail_release_gsbi:
653 release_mem_region(gsbi_resource->start, size);
654fail_release_port:
655 release_mem_region(port->mapbase, size);
656 return ret;
657}
658
659static void msm_config_port(struct uart_port *port, int flags)
660{
661 struct msm_port *msm_port = UART_TO_MSM(port);
662 int ret;
663 if (flags & UART_CONFIG_TYPE) {
664 port->type = PORT_MSM;
665 ret = msm_request_port(port);
666 if (ret)
667 return;
668 }
669
670 if (msm_port->is_uartdm)
671 iowrite32(GSBI_PROTOCOL_UART, msm_port->gsbi_base +
672 GSBI_CONTROL);
673}
674
675static int msm_verify_port(struct uart_port *port, struct serial_struct *ser)
676{
677 if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_MSM))
678 return -EINVAL;
679 if (unlikely(port->irq != ser->irq))
680 return -EINVAL;
681 return 0;
682}
683
684static void msm_power(struct uart_port *port, unsigned int state,
685 unsigned int oldstate)
686{
687 struct msm_port *msm_port = UART_TO_MSM(port);
688
689 switch (state) {
690 case 0:
691 clk_enable(msm_port->clk);
692 if (!IS_ERR(msm_port->pclk))
693 clk_enable(msm_port->pclk);
694 break;
695 case 3:
696 clk_disable(msm_port->clk);
697 if (!IS_ERR(msm_port->pclk))
698 clk_disable(msm_port->pclk);
699 break;
700 default:
701 printk(KERN_ERR "msm_serial: Unknown PM state %d\n", state);
702 }
703}
704
705static struct uart_ops msm_uart_pops = {
706 .tx_empty = msm_tx_empty,
707 .set_mctrl = msm_set_mctrl,
708 .get_mctrl = msm_get_mctrl,
709 .stop_tx = msm_stop_tx,
710 .start_tx = msm_start_tx,
711 .stop_rx = msm_stop_rx,
712 .enable_ms = msm_enable_ms,
713 .break_ctl = msm_break_ctl,
714 .startup = msm_startup,
715 .shutdown = msm_shutdown,
716 .set_termios = msm_set_termios,
717 .type = msm_type,
718 .release_port = msm_release_port,
719 .request_port = msm_request_port,
720 .config_port = msm_config_port,
721 .verify_port = msm_verify_port,
722 .pm = msm_power,
723};
724
725static struct msm_port msm_uart_ports[] = {
726 {
727 .uart = {
728 .iotype = UPIO_MEM,
729 .ops = &msm_uart_pops,
730 .flags = UPF_BOOT_AUTOCONF,
731 .fifosize = 64,
732 .line = 0,
733 },
734 },
735 {
736 .uart = {
737 .iotype = UPIO_MEM,
738 .ops = &msm_uart_pops,
739 .flags = UPF_BOOT_AUTOCONF,
740 .fifosize = 64,
741 .line = 1,
742 },
743 },
744 {
745 .uart = {
746 .iotype = UPIO_MEM,
747 .ops = &msm_uart_pops,
748 .flags = UPF_BOOT_AUTOCONF,
749 .fifosize = 64,
750 .line = 2,
751 },
752 },
753};
754
755#define UART_NR ARRAY_SIZE(msm_uart_ports)
756
757static inline struct uart_port *get_port_from_line(unsigned int line)
758{
759 return &msm_uart_ports[line].uart;
760}
761
762#ifdef CONFIG_SERIAL_MSM_CONSOLE
763
764static void msm_console_putchar(struct uart_port *port, int c)
765{
766 struct msm_port *msm_port = UART_TO_MSM(port);
767
768 if (msm_port->is_uartdm)
769 reset_dm_count(port);
770
771 while (!(msm_read(port, UART_SR) & UART_SR_TX_READY))
772 ;
773 msm_write(port, c, msm_port->is_uartdm ? UARTDM_TF : UART_TF);
774}
775
776static void msm_console_write(struct console *co, const char *s,
777 unsigned int count)
778{
779 struct uart_port *port;
780 struct msm_port *msm_port;
781
782 BUG_ON(co->index < 0 || co->index >= UART_NR);
783
784 port = get_port_from_line(co->index);
785 msm_port = UART_TO_MSM(port);
786
787 spin_lock(&port->lock);
788 uart_console_write(port, s, count, msm_console_putchar);
789 spin_unlock(&port->lock);
790}
791
792static int __init msm_console_setup(struct console *co, char *options)
793{
794 struct uart_port *port;
795 struct msm_port *msm_port;
796 int baud, flow, bits, parity;
797
798 if (unlikely(co->index >= UART_NR || co->index < 0))
799 return -ENXIO;
800
801 port = get_port_from_line(co->index);
802 msm_port = UART_TO_MSM(port);
803
804 if (unlikely(!port->membase))
805 return -ENXIO;
806
807 port->cons = co;
808
809 msm_init_clock(port);
810
811 if (options)
812 uart_parse_options(options, &baud, &parity, &bits, &flow);
813
814 bits = 8;
815 parity = 'n';
816 flow = 'n';
817 msm_write(port, UART_MR2_BITS_PER_CHAR_8 | UART_MR2_STOP_BIT_LEN_ONE,
818 UART_MR2); /* 8N1 */
819
820 if (baud < 300 || baud > 115200)
821 baud = 115200;
822 msm_set_baud_rate(port, baud);
823
824 msm_reset(port);
825
826 if (msm_port->is_uartdm) {
827 msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR);
828 msm_write(port, UART_CR_TX_ENABLE, UART_CR);
829 }
830
831 printk(KERN_INFO "msm_serial: console setup on port #%d\n", port->line);
832
833 return uart_set_options(port, co, baud, parity, bits, flow);
834}
835
836static struct uart_driver msm_uart_driver;
837
838static struct console msm_console = {
839 .name = "ttyMSM",
840 .write = msm_console_write,
841 .device = uart_console_device,
842 .setup = msm_console_setup,
843 .flags = CON_PRINTBUFFER,
844 .index = -1,
845 .data = &msm_uart_driver,
846};
847
848#define MSM_CONSOLE (&msm_console)
849
850#else
851#define MSM_CONSOLE NULL
852#endif
853
854static struct uart_driver msm_uart_driver = {
855 .owner = THIS_MODULE,
856 .driver_name = "msm_serial",
857 .dev_name = "ttyMSM",
858 .nr = UART_NR,
859 .cons = MSM_CONSOLE,
860};
861
862static int __init msm_serial_probe(struct platform_device *pdev)
863{
864 struct msm_port *msm_port;
865 struct resource *resource;
866 struct uart_port *port;
867 int irq;
868
869 if (unlikely(pdev->id < 0 || pdev->id >= UART_NR))
870 return -ENXIO;
871
872 printk(KERN_INFO "msm_serial: detected port #%d\n", pdev->id);
873
874 port = get_port_from_line(pdev->id);
875 port->dev = &pdev->dev;
876 msm_port = UART_TO_MSM(port);
877
878 if (platform_get_resource_byname(pdev, IORESOURCE_MEM, "gsbi_resource"))
879 msm_port->is_uartdm = 1;
880 else
881 msm_port->is_uartdm = 0;
882
883 if (msm_port->is_uartdm) {
884 msm_port->clk = clk_get(&pdev->dev, "gsbi_uart_clk");
885 msm_port->pclk = clk_get(&pdev->dev, "gsbi_pclk");
886 } else {
887 msm_port->clk = clk_get(&pdev->dev, "uart_clk");
888 msm_port->pclk = ERR_PTR(-ENOENT);
889 }
890
891 if (unlikely(IS_ERR(msm_port->clk) || (IS_ERR(msm_port->pclk) &&
892 msm_port->is_uartdm)))
893 return PTR_ERR(msm_port->clk);
894
895 if (msm_port->is_uartdm)
896 clk_set_rate(msm_port->clk, 7372800);
897
898 port->uartclk = clk_get_rate(msm_port->clk);
899 printk(KERN_INFO "uartclk = %d\n", port->uartclk);
900
901
902 resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
903 "uart_resource");
904 if (unlikely(!resource))
905 return -ENXIO;
906 port->mapbase = resource->start;
907
908 irq = platform_get_irq(pdev, 0);
909 if (unlikely(irq < 0))
910 return -ENXIO;
911 port->irq = irq;
912
913 platform_set_drvdata(pdev, port);
914
915 return uart_add_one_port(&msm_uart_driver, port);
916}
917
918static int __devexit msm_serial_remove(struct platform_device *pdev)
919{
920 struct msm_port *msm_port = platform_get_drvdata(pdev);
921
922 clk_put(msm_port->clk);
923
924 return 0;
925}
926
927static struct platform_driver msm_platform_driver = {
928 .remove = msm_serial_remove,
929 .driver = {
930 .name = "msm_serial",
931 .owner = THIS_MODULE,
932 },
933};
934
935static int __init msm_serial_init(void)
936{
937 int ret;
938
939 ret = uart_register_driver(&msm_uart_driver);
940 if (unlikely(ret))
941 return ret;
942
943 ret = platform_driver_probe(&msm_platform_driver, msm_serial_probe);
944 if (unlikely(ret))
945 uart_unregister_driver(&msm_uart_driver);
946
947 printk(KERN_INFO "msm_serial: driver initialized\n");
948
949 return ret;
950}
951
952static void __exit msm_serial_exit(void)
953{
954#ifdef CONFIG_SERIAL_MSM_CONSOLE
955 unregister_console(&msm_console);
956#endif
957 platform_driver_unregister(&msm_platform_driver);
958 uart_unregister_driver(&msm_uart_driver);
959}
960
961module_init(msm_serial_init);
962module_exit(msm_serial_exit);
963
964MODULE_AUTHOR("Robert Love <rlove@google.com>");
965MODULE_DESCRIPTION("Driver for msm7x serial device");
966MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/msm_serial.h b/drivers/tty/serial/msm_serial.h
new file mode 100644
index 000000000000..e4acef5de77e
--- /dev/null
+++ b/drivers/tty/serial/msm_serial.h
@@ -0,0 +1,187 @@
1/*
2 * Copyright (C) 2007 Google, Inc.
3 * Author: Robert Love <rlove@google.com>
4 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
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
16#ifndef __DRIVERS_SERIAL_MSM_SERIAL_H
17#define __DRIVERS_SERIAL_MSM_SERIAL_H
18
19#define UART_MR1 0x0000
20
21#define UART_MR1_AUTO_RFR_LEVEL0 0x3F
22#define UART_MR1_AUTO_RFR_LEVEL1 0x3FF00
23#define UART_MR1_RX_RDY_CTL (1 << 7)
24#define UART_MR1_CTS_CTL (1 << 6)
25
26#define UART_MR2 0x0004
27#define UART_MR2_ERROR_MODE (1 << 6)
28#define UART_MR2_BITS_PER_CHAR 0x30
29#define UART_MR2_BITS_PER_CHAR_5 (0x0 << 4)
30#define UART_MR2_BITS_PER_CHAR_6 (0x1 << 4)
31#define UART_MR2_BITS_PER_CHAR_7 (0x2 << 4)
32#define UART_MR2_BITS_PER_CHAR_8 (0x3 << 4)
33#define UART_MR2_STOP_BIT_LEN_ONE (0x1 << 2)
34#define UART_MR2_STOP_BIT_LEN_TWO (0x3 << 2)
35#define UART_MR2_PARITY_MODE_NONE 0x0
36#define UART_MR2_PARITY_MODE_ODD 0x1
37#define UART_MR2_PARITY_MODE_EVEN 0x2
38#define UART_MR2_PARITY_MODE_SPACE 0x3
39#define UART_MR2_PARITY_MODE 0x3
40
41#define UART_CSR 0x0008
42#define UART_CSR_115200 0xFF
43#define UART_CSR_57600 0xEE
44#define UART_CSR_38400 0xDD
45#define UART_CSR_28800 0xCC
46#define UART_CSR_19200 0xBB
47#define UART_CSR_14400 0xAA
48#define UART_CSR_9600 0x99
49#define UART_CSR_4800 0x77
50#define UART_CSR_2400 0x55
51#define UART_CSR_1200 0x44
52#define UART_CSR_600 0x33
53#define UART_CSR_300 0x22
54
55#define UART_TF 0x000C
56#define UARTDM_TF 0x0070
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_RESET_STALE_INT (8 << 4)
68#define UART_CR_CMD_PACKET_MODE (9 << 4)
69#define UART_CR_CMD_MODE_RESET (12 << 4)
70#define UART_CR_CMD_SET_RFR (13 << 4)
71#define UART_CR_CMD_RESET_RFR (14 << 4)
72#define UART_CR_CMD_PROTECTION_EN (16 << 4)
73#define UART_CR_CMD_STALE_EVENT_ENABLE (80 << 4)
74#define UART_CR_TX_DISABLE (1 << 3)
75#define UART_CR_TX_ENABLE (1 << 2)
76#define UART_CR_RX_DISABLE (1 << 1)
77#define UART_CR_RX_ENABLE (1 << 0)
78
79#define UART_IMR 0x0014
80#define UART_IMR_TXLEV (1 << 0)
81#define UART_IMR_RXSTALE (1 << 3)
82#define UART_IMR_RXLEV (1 << 4)
83#define UART_IMR_DELTA_CTS (1 << 5)
84#define UART_IMR_CURRENT_CTS (1 << 6)
85
86#define UART_IPR_RXSTALE_LAST 0x20
87#define UART_IPR_STALE_LSB 0x1F
88#define UART_IPR_STALE_TIMEOUT_MSB 0x3FF80
89
90#define UART_IPR 0x0018
91#define UART_TFWR 0x001C
92#define UART_RFWR 0x0020
93#define UART_HCR 0x0024
94
95#define UART_MREG 0x0028
96#define UART_NREG 0x002C
97#define UART_DREG 0x0030
98#define UART_MNDREG 0x0034
99#define UART_IRDA 0x0038
100#define UART_MISR_MODE 0x0040
101#define UART_MISR_RESET 0x0044
102#define UART_MISR_EXPORT 0x0048
103#define UART_MISR_VAL 0x004C
104#define UART_TEST_CTRL 0x0050
105
106#define UART_SR 0x0008
107#define UART_SR_HUNT_CHAR (1 << 7)
108#define UART_SR_RX_BREAK (1 << 6)
109#define UART_SR_PAR_FRAME_ERR (1 << 5)
110#define UART_SR_OVERRUN (1 << 4)
111#define UART_SR_TX_EMPTY (1 << 3)
112#define UART_SR_TX_READY (1 << 2)
113#define UART_SR_RX_FULL (1 << 1)
114#define UART_SR_RX_READY (1 << 0)
115
116#define UART_RF 0x000C
117#define UARTDM_RF 0x0070
118#define UART_MISR 0x0010
119#define UART_ISR 0x0014
120#define UART_ISR_TX_READY (1 << 7)
121
122#define GSBI_CONTROL 0x0
123#define GSBI_PROTOCOL_CODE 0x30
124#define GSBI_PROTOCOL_UART 0x40
125#define GSBI_PROTOCOL_IDLE 0x0
126
127#define UARTDM_DMRX 0x34
128#define UARTDM_NCF_TX 0x40
129#define UARTDM_RX_TOTAL_SNAP 0x38
130
131#define UART_TO_MSM(uart_port) ((struct msm_port *) uart_port)
132
133static inline
134void msm_write(struct uart_port *port, unsigned int val, unsigned int off)
135{
136 __raw_writel(val, port->membase + off);
137}
138
139static inline
140unsigned int msm_read(struct uart_port *port, unsigned int off)
141{
142 return __raw_readl(port->membase + off);
143}
144
145/*
146 * Setup the MND registers to use the TCXO clock.
147 */
148static inline void msm_serial_set_mnd_regs_tcxo(struct uart_port *port)
149{
150 msm_write(port, 0x06, UART_MREG);
151 msm_write(port, 0xF1, UART_NREG);
152 msm_write(port, 0x0F, UART_DREG);
153 msm_write(port, 0x1A, UART_MNDREG);
154}
155
156/*
157 * Setup the MND registers to use the TCXO clock divided by 4.
158 */
159static inline void msm_serial_set_mnd_regs_tcxoby4(struct uart_port *port)
160{
161 msm_write(port, 0x18, UART_MREG);
162 msm_write(port, 0xF6, UART_NREG);
163 msm_write(port, 0x0F, UART_DREG);
164 msm_write(port, 0x0A, UART_MNDREG);
165}
166
167static inline
168void msm_serial_set_mnd_regs_from_uartclk(struct uart_port *port)
169{
170 if (port->uartclk == 19200000)
171 msm_serial_set_mnd_regs_tcxo(port);
172 else
173 msm_serial_set_mnd_regs_tcxoby4(port);
174}
175
176/*
177 * TROUT has a specific defect that makes it report it's uartclk
178 * as 19.2Mhz (TCXO) when it's actually 4.8Mhz (TCXO/4). This special
179 * cases TROUT to use the right clock.
180 */
181#ifdef CONFIG_MACH_TROUT
182#define msm_serial_set_mnd_regs msm_serial_set_mnd_regs_tcxoby4
183#else
184#define msm_serial_set_mnd_regs msm_serial_set_mnd_regs_from_uartclk
185#endif
186
187#endif /* __DRIVERS_SERIAL_MSM_SERIAL_H */
diff --git a/drivers/tty/serial/msm_serial_hs.c b/drivers/tty/serial/msm_serial_hs.c
new file mode 100644
index 000000000000..624701f8138a
--- /dev/null
+++ b/drivers/tty/serial/msm_serial_hs.c
@@ -0,0 +1,1880 @@
1/*
2 * MSM 7k/8k High speed uart driver
3 *
4 * Copyright (c) 2007-2011, Code Aurora Forum. All rights reserved.
5 * Copyright (c) 2008 Google Inc.
6 * Modified: Nick Pelly <npelly@google.com>
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 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
16 *
17 * Has optional support for uart power management independent of linux
18 * suspend/resume:
19 *
20 * RX wakeup.
21 * UART wakeup can be triggered by RX activity (using a wakeup GPIO on the
22 * UART RX pin). This should only be used if there is not a wakeup
23 * GPIO on the UART CTS, and the first RX byte is known (for example, with the
24 * Bluetooth Texas Instruments HCILL protocol), since the first RX byte will
25 * always be lost. RTS will be asserted even while the UART is off in this mode
26 * of operation. See msm_serial_hs_platform_data.rx_wakeup_irq.
27 */
28
29#include <linux/module.h>
30
31#include <linux/serial.h>
32#include <linux/serial_core.h>
33#include <linux/slab.h>
34#include <linux/init.h>
35#include <linux/interrupt.h>
36#include <linux/irq.h>
37#include <linux/io.h>
38#include <linux/ioport.h>
39#include <linux/kernel.h>
40#include <linux/timer.h>
41#include <linux/clk.h>
42#include <linux/platform_device.h>
43#include <linux/pm_runtime.h>
44#include <linux/dma-mapping.h>
45#include <linux/dmapool.h>
46#include <linux/wait.h>
47#include <linux/workqueue.h>
48
49#include <linux/atomic.h>
50#include <asm/irq.h>
51#include <asm/system.h>
52
53#include <mach/hardware.h>
54#include <mach/dma.h>
55#include <linux/platform_data/msm_serial_hs.h>
56
57/* HSUART Registers */
58#define UARTDM_MR1_ADDR 0x0
59#define UARTDM_MR2_ADDR 0x4
60
61/* Data Mover result codes */
62#define RSLT_FIFO_CNTR_BMSK (0xE << 28)
63#define RSLT_VLD BIT(1)
64
65/* write only register */
66#define UARTDM_CSR_ADDR 0x8
67#define UARTDM_CSR_115200 0xFF
68#define UARTDM_CSR_57600 0xEE
69#define UARTDM_CSR_38400 0xDD
70#define UARTDM_CSR_28800 0xCC
71#define UARTDM_CSR_19200 0xBB
72#define UARTDM_CSR_14400 0xAA
73#define UARTDM_CSR_9600 0x99
74#define UARTDM_CSR_7200 0x88
75#define UARTDM_CSR_4800 0x77
76#define UARTDM_CSR_3600 0x66
77#define UARTDM_CSR_2400 0x55
78#define UARTDM_CSR_1200 0x44
79#define UARTDM_CSR_600 0x33
80#define UARTDM_CSR_300 0x22
81#define UARTDM_CSR_150 0x11
82#define UARTDM_CSR_75 0x00
83
84/* write only register */
85#define UARTDM_TF_ADDR 0x70
86#define UARTDM_TF2_ADDR 0x74
87#define UARTDM_TF3_ADDR 0x78
88#define UARTDM_TF4_ADDR 0x7C
89
90/* write only register */
91#define UARTDM_CR_ADDR 0x10
92#define UARTDM_IMR_ADDR 0x14
93
94#define UARTDM_IPR_ADDR 0x18
95#define UARTDM_TFWR_ADDR 0x1c
96#define UARTDM_RFWR_ADDR 0x20
97#define UARTDM_HCR_ADDR 0x24
98#define UARTDM_DMRX_ADDR 0x34
99#define UARTDM_IRDA_ADDR 0x38
100#define UARTDM_DMEN_ADDR 0x3c
101
102/* UART_DM_NO_CHARS_FOR_TX */
103#define UARTDM_NCF_TX_ADDR 0x40
104
105#define UARTDM_BADR_ADDR 0x44
106
107#define UARTDM_SIM_CFG_ADDR 0x80
108/* Read Only register */
109#define UARTDM_SR_ADDR 0x8
110
111/* Read Only register */
112#define UARTDM_RF_ADDR 0x70
113#define UARTDM_RF2_ADDR 0x74
114#define UARTDM_RF3_ADDR 0x78
115#define UARTDM_RF4_ADDR 0x7C
116
117/* Read Only register */
118#define UARTDM_MISR_ADDR 0x10
119
120/* Read Only register */
121#define UARTDM_ISR_ADDR 0x14
122#define UARTDM_RX_TOTAL_SNAP_ADDR 0x38
123
124#define UARTDM_RXFS_ADDR 0x50
125
126/* Register field Mask Mapping */
127#define UARTDM_SR_PAR_FRAME_BMSK BIT(5)
128#define UARTDM_SR_OVERRUN_BMSK BIT(4)
129#define UARTDM_SR_TXEMT_BMSK BIT(3)
130#define UARTDM_SR_TXRDY_BMSK BIT(2)
131#define UARTDM_SR_RXRDY_BMSK BIT(0)
132
133#define UARTDM_CR_TX_DISABLE_BMSK BIT(3)
134#define UARTDM_CR_RX_DISABLE_BMSK BIT(1)
135#define UARTDM_CR_TX_EN_BMSK BIT(2)
136#define UARTDM_CR_RX_EN_BMSK BIT(0)
137
138/* UARTDM_CR channel_comman bit value (register field is bits 8:4) */
139#define RESET_RX 0x10
140#define RESET_TX 0x20
141#define RESET_ERROR_STATUS 0x30
142#define RESET_BREAK_INT 0x40
143#define START_BREAK 0x50
144#define STOP_BREAK 0x60
145#define RESET_CTS 0x70
146#define RESET_STALE_INT 0x80
147#define RFR_LOW 0xD0
148#define RFR_HIGH 0xE0
149#define CR_PROTECTION_EN 0x100
150#define STALE_EVENT_ENABLE 0x500
151#define STALE_EVENT_DISABLE 0x600
152#define FORCE_STALE_EVENT 0x400
153#define CLEAR_TX_READY 0x300
154#define RESET_TX_ERROR 0x800
155#define RESET_TX_DONE 0x810
156
157#define UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK 0xffffff00
158#define UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK 0x3f
159#define UARTDM_MR1_CTS_CTL_BMSK 0x40
160#define UARTDM_MR1_RX_RDY_CTL_BMSK 0x80
161
162#define UARTDM_MR2_ERROR_MODE_BMSK 0x40
163#define UARTDM_MR2_BITS_PER_CHAR_BMSK 0x30
164
165/* bits per character configuration */
166#define FIVE_BPC (0 << 4)
167#define SIX_BPC (1 << 4)
168#define SEVEN_BPC (2 << 4)
169#define EIGHT_BPC (3 << 4)
170
171#define UARTDM_MR2_STOP_BIT_LEN_BMSK 0xc
172#define STOP_BIT_ONE (1 << 2)
173#define STOP_BIT_TWO (3 << 2)
174
175#define UARTDM_MR2_PARITY_MODE_BMSK 0x3
176
177/* Parity configuration */
178#define NO_PARITY 0x0
179#define EVEN_PARITY 0x1
180#define ODD_PARITY 0x2
181#define SPACE_PARITY 0x3
182
183#define UARTDM_IPR_STALE_TIMEOUT_MSB_BMSK 0xffffff80
184#define UARTDM_IPR_STALE_LSB_BMSK 0x1f
185
186/* These can be used for both ISR and IMR register */
187#define UARTDM_ISR_TX_READY_BMSK BIT(7)
188#define UARTDM_ISR_CURRENT_CTS_BMSK BIT(6)
189#define UARTDM_ISR_DELTA_CTS_BMSK BIT(5)
190#define UARTDM_ISR_RXLEV_BMSK BIT(4)
191#define UARTDM_ISR_RXSTALE_BMSK BIT(3)
192#define UARTDM_ISR_RXBREAK_BMSK BIT(2)
193#define UARTDM_ISR_RXHUNT_BMSK BIT(1)
194#define UARTDM_ISR_TXLEV_BMSK BIT(0)
195
196/* Field definitions for UART_DM_DMEN*/
197#define UARTDM_TX_DM_EN_BMSK 0x1
198#define UARTDM_RX_DM_EN_BMSK 0x2
199
200#define UART_FIFOSIZE 64
201#define UARTCLK 7372800
202
203/* Rx DMA request states */
204enum flush_reason {
205 FLUSH_NONE,
206 FLUSH_DATA_READY,
207 FLUSH_DATA_INVALID, /* values after this indicate invalid data */
208 FLUSH_IGNORE = FLUSH_DATA_INVALID,
209 FLUSH_STOP,
210 FLUSH_SHUTDOWN,
211};
212
213/* UART clock states */
214enum msm_hs_clk_states_e {
215 MSM_HS_CLK_PORT_OFF, /* port not in use */
216 MSM_HS_CLK_OFF, /* clock disabled */
217 MSM_HS_CLK_REQUEST_OFF, /* disable after TX and RX flushed */
218 MSM_HS_CLK_ON, /* clock enabled */
219};
220
221/* Track the forced RXSTALE flush during clock off sequence.
222 * These states are only valid during MSM_HS_CLK_REQUEST_OFF */
223enum msm_hs_clk_req_off_state_e {
224 CLK_REQ_OFF_START,
225 CLK_REQ_OFF_RXSTALE_ISSUED,
226 CLK_REQ_OFF_FLUSH_ISSUED,
227 CLK_REQ_OFF_RXSTALE_FLUSHED,
228};
229
230/**
231 * struct msm_hs_tx
232 * @tx_ready_int_en: ok to dma more tx?
233 * @dma_in_flight: tx dma in progress
234 * @xfer: top level DMA command pointer structure
235 * @command_ptr: third level command struct pointer
236 * @command_ptr_ptr: second level command list struct pointer
237 * @mapped_cmd_ptr: DMA view of third level command struct
238 * @mapped_cmd_ptr_ptr: DMA view of second level command list struct
239 * @tx_count: number of bytes to transfer in DMA transfer
240 * @dma_base: DMA view of UART xmit buffer
241 *
242 * This structure describes a single Tx DMA transaction. MSM DMA
243 * commands have two levels of indirection. The top level command
244 * ptr points to a list of command ptr which in turn points to a
245 * single DMA 'command'. In our case each Tx transaction consists
246 * of a single second level pointer pointing to a 'box type' command.
247 */
248struct msm_hs_tx {
249 unsigned int tx_ready_int_en;
250 unsigned int dma_in_flight;
251 struct msm_dmov_cmd xfer;
252 dmov_box *command_ptr;
253 u32 *command_ptr_ptr;
254 dma_addr_t mapped_cmd_ptr;
255 dma_addr_t mapped_cmd_ptr_ptr;
256 int tx_count;
257 dma_addr_t dma_base;
258};
259
260/**
261 * struct msm_hs_rx
262 * @flush: Rx DMA request state
263 * @xfer: top level DMA command pointer structure
264 * @cmdptr_dmaaddr: DMA view of second level command structure
265 * @command_ptr: third level DMA command pointer structure
266 * @command_ptr_ptr: second level DMA command list pointer
267 * @mapped_cmd_ptr: DMA view of the third level command structure
268 * @wait: wait for DMA completion before shutdown
269 * @buffer: destination buffer for RX DMA
270 * @rbuffer: DMA view of buffer
271 * @pool: dma pool out of which coherent rx buffer is allocated
272 * @tty_work: private work-queue for tty flip buffer push task
273 *
274 * This structure describes a single Rx DMA transaction. Rx DMA
275 * transactions use box mode DMA commands.
276 */
277struct msm_hs_rx {
278 enum flush_reason flush;
279 struct msm_dmov_cmd xfer;
280 dma_addr_t cmdptr_dmaaddr;
281 dmov_box *command_ptr;
282 u32 *command_ptr_ptr;
283 dma_addr_t mapped_cmd_ptr;
284 wait_queue_head_t wait;
285 dma_addr_t rbuffer;
286 unsigned char *buffer;
287 struct dma_pool *pool;
288 struct work_struct tty_work;
289};
290
291/**
292 * struct msm_hs_rx_wakeup
293 * @irq: IRQ line to be configured as interrupt source on Rx activity
294 * @ignore: boolean value. 1 = ignore the wakeup interrupt
295 * @rx_to_inject: extra character to be inserted to Rx tty on wakeup
296 * @inject_rx: 1 = insert rx_to_inject. 0 = do not insert extra character
297 *
298 * This is an optional structure required for UART Rx GPIO IRQ based
299 * wakeup from low power state. UART wakeup can be triggered by RX activity
300 * (using a wakeup GPIO on the UART RX pin). This should only be used if
301 * there is not a wakeup GPIO on the UART CTS, and the first RX byte is
302 * known (eg., with the Bluetooth Texas Instruments HCILL protocol),
303 * since the first RX byte will always be lost. RTS will be asserted even
304 * while the UART is clocked off in this mode of operation.
305 */
306struct msm_hs_rx_wakeup {
307 int irq; /* < 0 indicates low power wakeup disabled */
308 unsigned char ignore;
309 unsigned char inject_rx;
310 char rx_to_inject;
311};
312
313/**
314 * struct msm_hs_port
315 * @uport: embedded uart port structure
316 * @imr_reg: shadow value of UARTDM_IMR
317 * @clk: uart input clock handle
318 * @tx: Tx transaction related data structure
319 * @rx: Rx transaction related data structure
320 * @dma_tx_channel: Tx DMA command channel
321 * @dma_rx_channel Rx DMA command channel
322 * @dma_tx_crci: Tx channel rate control interface number
323 * @dma_rx_crci: Rx channel rate control interface number
324 * @clk_off_timer: Timer to poll DMA event completion before clock off
325 * @clk_off_delay: clk_off_timer poll interval
326 * @clk_state: overall clock state
327 * @clk_req_off_state: post flush clock states
328 * @rx_wakeup: optional rx_wakeup feature related data
329 * @exit_lpm_cb: optional callback to exit low power mode
330 *
331 * Low level serial port structure.
332 */
333struct msm_hs_port {
334 struct uart_port uport;
335 unsigned long imr_reg;
336 struct clk *clk;
337 struct msm_hs_tx tx;
338 struct msm_hs_rx rx;
339
340 int dma_tx_channel;
341 int dma_rx_channel;
342 int dma_tx_crci;
343 int dma_rx_crci;
344
345 struct hrtimer clk_off_timer;
346 ktime_t clk_off_delay;
347 enum msm_hs_clk_states_e clk_state;
348 enum msm_hs_clk_req_off_state_e clk_req_off_state;
349
350 struct msm_hs_rx_wakeup rx_wakeup;
351 void (*exit_lpm_cb)(struct uart_port *);
352};
353
354#define MSM_UARTDM_BURST_SIZE 16 /* DM burst size (in bytes) */
355#define UARTDM_TX_BUF_SIZE UART_XMIT_SIZE
356#define UARTDM_RX_BUF_SIZE 512
357
358#define UARTDM_NR 2
359
360static struct msm_hs_port q_uart_port[UARTDM_NR];
361static struct platform_driver msm_serial_hs_platform_driver;
362static struct uart_driver msm_hs_driver;
363static struct uart_ops msm_hs_ops;
364static struct workqueue_struct *msm_hs_workqueue;
365
366#define UARTDM_TO_MSM(uart_port) \
367 container_of((uart_port), struct msm_hs_port, uport)
368
369static unsigned int use_low_power_rx_wakeup(struct msm_hs_port
370 *msm_uport)
371{
372 return (msm_uport->rx_wakeup.irq >= 0);
373}
374
375static unsigned int msm_hs_read(struct uart_port *uport,
376 unsigned int offset)
377{
378 return ioread32(uport->membase + offset);
379}
380
381static void msm_hs_write(struct uart_port *uport, unsigned int offset,
382 unsigned int value)
383{
384 iowrite32(value, uport->membase + offset);
385}
386
387static void msm_hs_release_port(struct uart_port *port)
388{
389 iounmap(port->membase);
390}
391
392static int msm_hs_request_port(struct uart_port *port)
393{
394 port->membase = ioremap(port->mapbase, PAGE_SIZE);
395 if (unlikely(!port->membase))
396 return -ENOMEM;
397
398 /* configure the CR Protection to Enable */
399 msm_hs_write(port, UARTDM_CR_ADDR, CR_PROTECTION_EN);
400 return 0;
401}
402
403static int __devexit msm_hs_remove(struct platform_device *pdev)
404{
405
406 struct msm_hs_port *msm_uport;
407 struct device *dev;
408
409 if (pdev->id < 0 || pdev->id >= UARTDM_NR) {
410 printk(KERN_ERR "Invalid plaform device ID = %d\n", pdev->id);
411 return -EINVAL;
412 }
413
414 msm_uport = &q_uart_port[pdev->id];
415 dev = msm_uport->uport.dev;
416
417 dma_unmap_single(dev, msm_uport->rx.mapped_cmd_ptr, sizeof(dmov_box),
418 DMA_TO_DEVICE);
419 dma_pool_free(msm_uport->rx.pool, msm_uport->rx.buffer,
420 msm_uport->rx.rbuffer);
421 dma_pool_destroy(msm_uport->rx.pool);
422
423 dma_unmap_single(dev, msm_uport->rx.cmdptr_dmaaddr, sizeof(u32 *),
424 DMA_TO_DEVICE);
425 dma_unmap_single(dev, msm_uport->tx.mapped_cmd_ptr_ptr, sizeof(u32 *),
426 DMA_TO_DEVICE);
427 dma_unmap_single(dev, msm_uport->tx.mapped_cmd_ptr, sizeof(dmov_box),
428 DMA_TO_DEVICE);
429
430 uart_remove_one_port(&msm_hs_driver, &msm_uport->uport);
431 clk_put(msm_uport->clk);
432
433 /* Free the tx resources */
434 kfree(msm_uport->tx.command_ptr);
435 kfree(msm_uport->tx.command_ptr_ptr);
436
437 /* Free the rx resources */
438 kfree(msm_uport->rx.command_ptr);
439 kfree(msm_uport->rx.command_ptr_ptr);
440
441 iounmap(msm_uport->uport.membase);
442
443 return 0;
444}
445
446static int msm_hs_init_clk_locked(struct uart_port *uport)
447{
448 int ret;
449 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
450
451 ret = clk_enable(msm_uport->clk);
452 if (ret) {
453 printk(KERN_ERR "Error could not turn on UART clk\n");
454 return ret;
455 }
456
457 /* Set up the MREG/NREG/DREG/MNDREG */
458 ret = clk_set_rate(msm_uport->clk, uport->uartclk);
459 if (ret) {
460 printk(KERN_WARNING "Error setting clock rate on UART\n");
461 clk_disable(msm_uport->clk);
462 return ret;
463 }
464
465 msm_uport->clk_state = MSM_HS_CLK_ON;
466 return 0;
467}
468
469/* Enable and Disable clocks (Used for power management) */
470static void msm_hs_pm(struct uart_port *uport, unsigned int state,
471 unsigned int oldstate)
472{
473 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
474
475 if (use_low_power_rx_wakeup(msm_uport) ||
476 msm_uport->exit_lpm_cb)
477 return; /* ignore linux PM states,
478 use msm_hs_request_clock API */
479
480 switch (state) {
481 case 0:
482 clk_enable(msm_uport->clk);
483 break;
484 case 3:
485 clk_disable(msm_uport->clk);
486 break;
487 default:
488 dev_err(uport->dev, "msm_serial: Unknown PM state %d\n",
489 state);
490 }
491}
492
493/*
494 * programs the UARTDM_CSR register with correct bit rates
495 *
496 * Interrupts should be disabled before we are called, as
497 * we modify Set Baud rate
498 * Set receive stale interrupt level, dependent on Bit Rate
499 * Goal is to have around 8 ms before indicate stale.
500 * roundup (((Bit Rate * .008) / 10) + 1
501 */
502static void msm_hs_set_bps_locked(struct uart_port *uport,
503 unsigned int bps)
504{
505 unsigned long rxstale;
506 unsigned long data;
507 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
508
509 switch (bps) {
510 case 300:
511 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_75);
512 rxstale = 1;
513 break;
514 case 600:
515 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_150);
516 rxstale = 1;
517 break;
518 case 1200:
519 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_300);
520 rxstale = 1;
521 break;
522 case 2400:
523 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_600);
524 rxstale = 1;
525 break;
526 case 4800:
527 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_1200);
528 rxstale = 1;
529 break;
530 case 9600:
531 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_2400);
532 rxstale = 2;
533 break;
534 case 14400:
535 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_3600);
536 rxstale = 3;
537 break;
538 case 19200:
539 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_4800);
540 rxstale = 4;
541 break;
542 case 28800:
543 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_7200);
544 rxstale = 6;
545 break;
546 case 38400:
547 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_9600);
548 rxstale = 8;
549 break;
550 case 57600:
551 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_14400);
552 rxstale = 16;
553 break;
554 case 76800:
555 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_19200);
556 rxstale = 16;
557 break;
558 case 115200:
559 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_28800);
560 rxstale = 31;
561 break;
562 case 230400:
563 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_57600);
564 rxstale = 31;
565 break;
566 case 460800:
567 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_115200);
568 rxstale = 31;
569 break;
570 case 4000000:
571 case 3686400:
572 case 3200000:
573 case 3500000:
574 case 3000000:
575 case 2500000:
576 case 1500000:
577 case 1152000:
578 case 1000000:
579 case 921600:
580 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_115200);
581 rxstale = 31;
582 break;
583 default:
584 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_2400);
585 /* default to 9600 */
586 bps = 9600;
587 rxstale = 2;
588 break;
589 }
590 if (bps > 460800)
591 uport->uartclk = bps * 16;
592 else
593 uport->uartclk = UARTCLK;
594
595 if (clk_set_rate(msm_uport->clk, uport->uartclk)) {
596 printk(KERN_WARNING "Error setting clock rate on UART\n");
597 return;
598 }
599
600 data = rxstale & UARTDM_IPR_STALE_LSB_BMSK;
601 data |= UARTDM_IPR_STALE_TIMEOUT_MSB_BMSK & (rxstale << 2);
602
603 msm_hs_write(uport, UARTDM_IPR_ADDR, data);
604}
605
606/*
607 * termios : new ktermios
608 * oldtermios: old ktermios previous setting
609 *
610 * Configure the serial port
611 */
612static void msm_hs_set_termios(struct uart_port *uport,
613 struct ktermios *termios,
614 struct ktermios *oldtermios)
615{
616 unsigned int bps;
617 unsigned long data;
618 unsigned long flags;
619 unsigned int c_cflag = termios->c_cflag;
620 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
621
622 spin_lock_irqsave(&uport->lock, flags);
623 clk_enable(msm_uport->clk);
624
625 /* 300 is the minimum baud support by the driver */
626 bps = uart_get_baud_rate(uport, termios, oldtermios, 200, 4000000);
627
628 /* Temporary remapping 200 BAUD to 3.2 mbps */
629 if (bps == 200)
630 bps = 3200000;
631
632 msm_hs_set_bps_locked(uport, bps);
633
634 data = msm_hs_read(uport, UARTDM_MR2_ADDR);
635 data &= ~UARTDM_MR2_PARITY_MODE_BMSK;
636 /* set parity */
637 if (PARENB == (c_cflag & PARENB)) {
638 if (PARODD == (c_cflag & PARODD))
639 data |= ODD_PARITY;
640 else if (CMSPAR == (c_cflag & CMSPAR))
641 data |= SPACE_PARITY;
642 else
643 data |= EVEN_PARITY;
644 }
645
646 /* Set bits per char */
647 data &= ~UARTDM_MR2_BITS_PER_CHAR_BMSK;
648
649 switch (c_cflag & CSIZE) {
650 case CS5:
651 data |= FIVE_BPC;
652 break;
653 case CS6:
654 data |= SIX_BPC;
655 break;
656 case CS7:
657 data |= SEVEN_BPC;
658 break;
659 default:
660 data |= EIGHT_BPC;
661 break;
662 }
663 /* stop bits */
664 if (c_cflag & CSTOPB) {
665 data |= STOP_BIT_TWO;
666 } else {
667 /* otherwise 1 stop bit */
668 data |= STOP_BIT_ONE;
669 }
670 data |= UARTDM_MR2_ERROR_MODE_BMSK;
671 /* write parity/bits per char/stop bit configuration */
672 msm_hs_write(uport, UARTDM_MR2_ADDR, data);
673
674 /* Configure HW flow control */
675 data = msm_hs_read(uport, UARTDM_MR1_ADDR);
676
677 data &= ~(UARTDM_MR1_CTS_CTL_BMSK | UARTDM_MR1_RX_RDY_CTL_BMSK);
678
679 if (c_cflag & CRTSCTS) {
680 data |= UARTDM_MR1_CTS_CTL_BMSK;
681 data |= UARTDM_MR1_RX_RDY_CTL_BMSK;
682 }
683
684 msm_hs_write(uport, UARTDM_MR1_ADDR, data);
685
686 uport->ignore_status_mask = termios->c_iflag & INPCK;
687 uport->ignore_status_mask |= termios->c_iflag & IGNPAR;
688 uport->read_status_mask = (termios->c_cflag & CREAD);
689
690 msm_hs_write(uport, UARTDM_IMR_ADDR, 0);
691
692 /* Set Transmit software time out */
693 uart_update_timeout(uport, c_cflag, bps);
694
695 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
696 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_TX);
697
698 if (msm_uport->rx.flush == FLUSH_NONE) {
699 msm_uport->rx.flush = FLUSH_IGNORE;
700 msm_dmov_stop_cmd(msm_uport->dma_rx_channel, NULL, 1);
701 }
702
703 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
704
705 clk_disable(msm_uport->clk);
706 spin_unlock_irqrestore(&uport->lock, flags);
707}
708
709/*
710 * Standard API, Transmitter
711 * Any character in the transmit shift register is sent
712 */
713static unsigned int msm_hs_tx_empty(struct uart_port *uport)
714{
715 unsigned int data;
716 unsigned int ret = 0;
717 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
718
719 clk_enable(msm_uport->clk);
720
721 data = msm_hs_read(uport, UARTDM_SR_ADDR);
722 if (data & UARTDM_SR_TXEMT_BMSK)
723 ret = TIOCSER_TEMT;
724
725 clk_disable(msm_uport->clk);
726
727 return ret;
728}
729
730/*
731 * Standard API, Stop transmitter.
732 * Any character in the transmit shift register is sent as
733 * well as the current data mover transfer .
734 */
735static void msm_hs_stop_tx_locked(struct uart_port *uport)
736{
737 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
738
739 msm_uport->tx.tx_ready_int_en = 0;
740}
741
742/*
743 * Standard API, Stop receiver as soon as possible.
744 *
745 * Function immediately terminates the operation of the
746 * channel receiver and any incoming characters are lost. None
747 * of the receiver status bits are affected by this command and
748 * characters that are already in the receive FIFO there.
749 */
750static void msm_hs_stop_rx_locked(struct uart_port *uport)
751{
752 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
753 unsigned int data;
754
755 clk_enable(msm_uport->clk);
756
757 /* disable dlink */
758 data = msm_hs_read(uport, UARTDM_DMEN_ADDR);
759 data &= ~UARTDM_RX_DM_EN_BMSK;
760 msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
761
762 /* Disable the receiver */
763 if (msm_uport->rx.flush == FLUSH_NONE)
764 msm_dmov_stop_cmd(msm_uport->dma_rx_channel, NULL, 1);
765
766 if (msm_uport->rx.flush != FLUSH_SHUTDOWN)
767 msm_uport->rx.flush = FLUSH_STOP;
768
769 clk_disable(msm_uport->clk);
770}
771
772/* Transmit the next chunk of data */
773static void msm_hs_submit_tx_locked(struct uart_port *uport)
774{
775 int left;
776 int tx_count;
777 dma_addr_t src_addr;
778 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
779 struct msm_hs_tx *tx = &msm_uport->tx;
780 struct circ_buf *tx_buf = &msm_uport->uport.state->xmit;
781
782 if (uart_circ_empty(tx_buf) || uport->state->port.tty->stopped) {
783 msm_hs_stop_tx_locked(uport);
784 return;
785 }
786
787 tx->dma_in_flight = 1;
788
789 tx_count = uart_circ_chars_pending(tx_buf);
790
791 if (UARTDM_TX_BUF_SIZE < tx_count)
792 tx_count = UARTDM_TX_BUF_SIZE;
793
794 left = UART_XMIT_SIZE - tx_buf->tail;
795
796 if (tx_count > left)
797 tx_count = left;
798
799 src_addr = tx->dma_base + tx_buf->tail;
800 dma_sync_single_for_device(uport->dev, src_addr, tx_count,
801 DMA_TO_DEVICE);
802
803 tx->command_ptr->num_rows = (((tx_count + 15) >> 4) << 16) |
804 ((tx_count + 15) >> 4);
805 tx->command_ptr->src_row_addr = src_addr;
806
807 dma_sync_single_for_device(uport->dev, tx->mapped_cmd_ptr,
808 sizeof(dmov_box), DMA_TO_DEVICE);
809
810 *tx->command_ptr_ptr = CMD_PTR_LP | DMOV_CMD_ADDR(tx->mapped_cmd_ptr);
811
812 dma_sync_single_for_device(uport->dev, tx->mapped_cmd_ptr_ptr,
813 sizeof(u32 *), DMA_TO_DEVICE);
814
815 /* Save tx_count to use in Callback */
816 tx->tx_count = tx_count;
817 msm_hs_write(uport, UARTDM_NCF_TX_ADDR, tx_count);
818
819 /* Disable the tx_ready interrupt */
820 msm_uport->imr_reg &= ~UARTDM_ISR_TX_READY_BMSK;
821 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
822 msm_dmov_enqueue_cmd(msm_uport->dma_tx_channel, &tx->xfer);
823}
824
825/* Start to receive the next chunk of data */
826static void msm_hs_start_rx_locked(struct uart_port *uport)
827{
828 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
829
830 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
831 msm_hs_write(uport, UARTDM_DMRX_ADDR, UARTDM_RX_BUF_SIZE);
832 msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_ENABLE);
833 msm_uport->imr_reg |= UARTDM_ISR_RXLEV_BMSK;
834 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
835
836 msm_uport->rx.flush = FLUSH_NONE;
837 msm_dmov_enqueue_cmd(msm_uport->dma_rx_channel, &msm_uport->rx.xfer);
838
839 /* might have finished RX and be ready to clock off */
840 hrtimer_start(&msm_uport->clk_off_timer, msm_uport->clk_off_delay,
841 HRTIMER_MODE_REL);
842}
843
844/* Enable the transmitter Interrupt */
845static void msm_hs_start_tx_locked(struct uart_port *uport)
846{
847 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
848
849 clk_enable(msm_uport->clk);
850
851 if (msm_uport->exit_lpm_cb)
852 msm_uport->exit_lpm_cb(uport);
853
854 if (msm_uport->tx.tx_ready_int_en == 0) {
855 msm_uport->tx.tx_ready_int_en = 1;
856 msm_hs_submit_tx_locked(uport);
857 }
858
859 clk_disable(msm_uport->clk);
860}
861
862/*
863 * This routine is called when we are done with a DMA transfer
864 *
865 * This routine is registered with Data mover when we set
866 * up a Data Mover transfer. It is called from Data mover ISR
867 * when the DMA transfer is done.
868 */
869static void msm_hs_dmov_tx_callback(struct msm_dmov_cmd *cmd_ptr,
870 unsigned int result,
871 struct msm_dmov_errdata *err)
872{
873 unsigned long flags;
874 struct msm_hs_port *msm_uport;
875
876 /* DMA did not finish properly */
877 WARN_ON((((result & RSLT_FIFO_CNTR_BMSK) >> 28) == 1) &&
878 !(result & RSLT_VLD));
879
880 msm_uport = container_of(cmd_ptr, struct msm_hs_port, tx.xfer);
881
882 spin_lock_irqsave(&msm_uport->uport.lock, flags);
883 clk_enable(msm_uport->clk);
884
885 msm_uport->imr_reg |= UARTDM_ISR_TX_READY_BMSK;
886 msm_hs_write(&msm_uport->uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
887
888 clk_disable(msm_uport->clk);
889 spin_unlock_irqrestore(&msm_uport->uport.lock, flags);
890}
891
892/*
893 * This routine is called when we are done with a DMA transfer or the
894 * a flush has been sent to the data mover driver.
895 *
896 * This routine is registered with Data mover when we set up a Data Mover
897 * transfer. It is called from Data mover ISR when the DMA transfer is done.
898 */
899static void msm_hs_dmov_rx_callback(struct msm_dmov_cmd *cmd_ptr,
900 unsigned int result,
901 struct msm_dmov_errdata *err)
902{
903 int retval;
904 int rx_count;
905 unsigned long status;
906 unsigned int error_f = 0;
907 unsigned long flags;
908 unsigned int flush;
909 struct tty_struct *tty;
910 struct uart_port *uport;
911 struct msm_hs_port *msm_uport;
912
913 msm_uport = container_of(cmd_ptr, struct msm_hs_port, rx.xfer);
914 uport = &msm_uport->uport;
915
916 spin_lock_irqsave(&uport->lock, flags);
917 clk_enable(msm_uport->clk);
918
919 tty = uport->state->port.tty;
920
921 msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_DISABLE);
922
923 status = msm_hs_read(uport, UARTDM_SR_ADDR);
924
925 /* overflow is not connect to data in a FIFO */
926 if (unlikely((status & UARTDM_SR_OVERRUN_BMSK) &&
927 (uport->read_status_mask & CREAD))) {
928 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
929 uport->icount.buf_overrun++;
930 error_f = 1;
931 }
932
933 if (!(uport->ignore_status_mask & INPCK))
934 status = status & ~(UARTDM_SR_PAR_FRAME_BMSK);
935
936 if (unlikely(status & UARTDM_SR_PAR_FRAME_BMSK)) {
937 /* Can not tell difference between parity & frame error */
938 uport->icount.parity++;
939 error_f = 1;
940 if (uport->ignore_status_mask & IGNPAR)
941 tty_insert_flip_char(tty, 0, TTY_PARITY);
942 }
943
944 if (error_f)
945 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_ERROR_STATUS);
946
947 if (msm_uport->clk_req_off_state == CLK_REQ_OFF_FLUSH_ISSUED)
948 msm_uport->clk_req_off_state = CLK_REQ_OFF_RXSTALE_FLUSHED;
949
950 flush = msm_uport->rx.flush;
951 if (flush == FLUSH_IGNORE)
952 msm_hs_start_rx_locked(uport);
953 if (flush == FLUSH_STOP)
954 msm_uport->rx.flush = FLUSH_SHUTDOWN;
955 if (flush >= FLUSH_DATA_INVALID)
956 goto out;
957
958 rx_count = msm_hs_read(uport, UARTDM_RX_TOTAL_SNAP_ADDR);
959
960 if (0 != (uport->read_status_mask & CREAD)) {
961 retval = tty_insert_flip_string(tty, msm_uport->rx.buffer,
962 rx_count);
963 BUG_ON(retval != rx_count);
964 }
965
966 msm_hs_start_rx_locked(uport);
967
968out:
969 clk_disable(msm_uport->clk);
970
971 spin_unlock_irqrestore(&uport->lock, flags);
972
973 if (flush < FLUSH_DATA_INVALID)
974 queue_work(msm_hs_workqueue, &msm_uport->rx.tty_work);
975}
976
977static void msm_hs_tty_flip_buffer_work(struct work_struct *work)
978{
979 struct msm_hs_port *msm_uport =
980 container_of(work, struct msm_hs_port, rx.tty_work);
981 struct tty_struct *tty = msm_uport->uport.state->port.tty;
982
983 tty_flip_buffer_push(tty);
984}
985
986/*
987 * Standard API, Current states of modem control inputs
988 *
989 * Since CTS can be handled entirely by HARDWARE we always
990 * indicate clear to send and count on the TX FIFO to block when
991 * it fills up.
992 *
993 * - TIOCM_DCD
994 * - TIOCM_CTS
995 * - TIOCM_DSR
996 * - TIOCM_RI
997 * (Unsupported) DCD and DSR will return them high. RI will return low.
998 */
999static unsigned int msm_hs_get_mctrl_locked(struct uart_port *uport)
1000{
1001 return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
1002}
1003
1004/*
1005 * True enables UART auto RFR, which indicates we are ready for data if the RX
1006 * buffer is not full. False disables auto RFR, and deasserts RFR to indicate
1007 * we are not ready for data. Must be called with UART clock on.
1008 */
1009static void set_rfr_locked(struct uart_port *uport, int auto_rfr)
1010{
1011 unsigned int data;
1012
1013 data = msm_hs_read(uport, UARTDM_MR1_ADDR);
1014
1015 if (auto_rfr) {
1016 /* enable auto ready-for-receiving */
1017 data |= UARTDM_MR1_RX_RDY_CTL_BMSK;
1018 msm_hs_write(uport, UARTDM_MR1_ADDR, data);
1019 } else {
1020 /* disable auto ready-for-receiving */
1021 data &= ~UARTDM_MR1_RX_RDY_CTL_BMSK;
1022 msm_hs_write(uport, UARTDM_MR1_ADDR, data);
1023 /* RFR is active low, set high */
1024 msm_hs_write(uport, UARTDM_CR_ADDR, RFR_HIGH);
1025 }
1026}
1027
1028/*
1029 * Standard API, used to set or clear RFR
1030 */
1031static void msm_hs_set_mctrl_locked(struct uart_port *uport,
1032 unsigned int mctrl)
1033{
1034 unsigned int auto_rfr;
1035 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1036
1037 clk_enable(msm_uport->clk);
1038
1039 auto_rfr = TIOCM_RTS & mctrl ? 1 : 0;
1040 set_rfr_locked(uport, auto_rfr);
1041
1042 clk_disable(msm_uport->clk);
1043}
1044
1045/* Standard API, Enable modem status (CTS) interrupt */
1046static void msm_hs_enable_ms_locked(struct uart_port *uport)
1047{
1048 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1049
1050 clk_enable(msm_uport->clk);
1051
1052 /* Enable DELTA_CTS Interrupt */
1053 msm_uport->imr_reg |= UARTDM_ISR_DELTA_CTS_BMSK;
1054 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1055
1056 clk_disable(msm_uport->clk);
1057
1058}
1059
1060/*
1061 * Standard API, Break Signal
1062 *
1063 * Control the transmission of a break signal. ctl eq 0 => break
1064 * signal terminate ctl ne 0 => start break signal
1065 */
1066static void msm_hs_break_ctl(struct uart_port *uport, int ctl)
1067{
1068 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1069
1070 clk_enable(msm_uport->clk);
1071 msm_hs_write(uport, UARTDM_CR_ADDR, ctl ? START_BREAK : STOP_BREAK);
1072 clk_disable(msm_uport->clk);
1073}
1074
1075static void msm_hs_config_port(struct uart_port *uport, int cfg_flags)
1076{
1077 unsigned long flags;
1078
1079 spin_lock_irqsave(&uport->lock, flags);
1080 if (cfg_flags & UART_CONFIG_TYPE) {
1081 uport->type = PORT_MSM;
1082 msm_hs_request_port(uport);
1083 }
1084 spin_unlock_irqrestore(&uport->lock, flags);
1085}
1086
1087/* Handle CTS changes (Called from interrupt handler) */
1088static void msm_hs_handle_delta_cts(struct uart_port *uport)
1089{
1090 unsigned long flags;
1091 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1092
1093 spin_lock_irqsave(&uport->lock, flags);
1094 clk_enable(msm_uport->clk);
1095
1096 /* clear interrupt */
1097 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_CTS);
1098 uport->icount.cts++;
1099
1100 clk_disable(msm_uport->clk);
1101 spin_unlock_irqrestore(&uport->lock, flags);
1102
1103 /* clear the IOCTL TIOCMIWAIT if called */
1104 wake_up_interruptible(&uport->state->port.delta_msr_wait);
1105}
1106
1107/* check if the TX path is flushed, and if so clock off
1108 * returns 0 did not clock off, need to retry (still sending final byte)
1109 * -1 did not clock off, do not retry
1110 * 1 if we clocked off
1111 */
1112static int msm_hs_check_clock_off_locked(struct uart_port *uport)
1113{
1114 unsigned long sr_status;
1115 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1116 struct circ_buf *tx_buf = &uport->state->xmit;
1117
1118 /* Cancel if tx tty buffer is not empty, dma is in flight,
1119 * or tx fifo is not empty, or rx fifo is not empty */
1120 if (msm_uport->clk_state != MSM_HS_CLK_REQUEST_OFF ||
1121 !uart_circ_empty(tx_buf) || msm_uport->tx.dma_in_flight ||
1122 (msm_uport->imr_reg & UARTDM_ISR_TXLEV_BMSK) ||
1123 !(msm_uport->imr_reg & UARTDM_ISR_RXLEV_BMSK)) {
1124 return -1;
1125 }
1126
1127 /* Make sure the uart is finished with the last byte */
1128 sr_status = msm_hs_read(uport, UARTDM_SR_ADDR);
1129 if (!(sr_status & UARTDM_SR_TXEMT_BMSK))
1130 return 0; /* retry */
1131
1132 /* Make sure forced RXSTALE flush complete */
1133 switch (msm_uport->clk_req_off_state) {
1134 case CLK_REQ_OFF_START:
1135 msm_uport->clk_req_off_state = CLK_REQ_OFF_RXSTALE_ISSUED;
1136 msm_hs_write(uport, UARTDM_CR_ADDR, FORCE_STALE_EVENT);
1137 return 0; /* RXSTALE flush not complete - retry */
1138 case CLK_REQ_OFF_RXSTALE_ISSUED:
1139 case CLK_REQ_OFF_FLUSH_ISSUED:
1140 return 0; /* RXSTALE flush not complete - retry */
1141 case CLK_REQ_OFF_RXSTALE_FLUSHED:
1142 break; /* continue */
1143 }
1144
1145 if (msm_uport->rx.flush != FLUSH_SHUTDOWN) {
1146 if (msm_uport->rx.flush == FLUSH_NONE)
1147 msm_hs_stop_rx_locked(uport);
1148 return 0; /* come back later to really clock off */
1149 }
1150
1151 /* we really want to clock off */
1152 clk_disable(msm_uport->clk);
1153 msm_uport->clk_state = MSM_HS_CLK_OFF;
1154
1155 if (use_low_power_rx_wakeup(msm_uport)) {
1156 msm_uport->rx_wakeup.ignore = 1;
1157 enable_irq(msm_uport->rx_wakeup.irq);
1158 }
1159 return 1;
1160}
1161
1162static enum hrtimer_restart msm_hs_clk_off_retry(struct hrtimer *timer)
1163{
1164 unsigned long flags;
1165 int ret = HRTIMER_NORESTART;
1166 struct msm_hs_port *msm_uport = container_of(timer, struct msm_hs_port,
1167 clk_off_timer);
1168 struct uart_port *uport = &msm_uport->uport;
1169
1170 spin_lock_irqsave(&uport->lock, flags);
1171
1172 if (!msm_hs_check_clock_off_locked(uport)) {
1173 hrtimer_forward_now(timer, msm_uport->clk_off_delay);
1174 ret = HRTIMER_RESTART;
1175 }
1176
1177 spin_unlock_irqrestore(&uport->lock, flags);
1178
1179 return ret;
1180}
1181
1182static irqreturn_t msm_hs_isr(int irq, void *dev)
1183{
1184 unsigned long flags;
1185 unsigned long isr_status;
1186 struct msm_hs_port *msm_uport = dev;
1187 struct uart_port *uport = &msm_uport->uport;
1188 struct circ_buf *tx_buf = &uport->state->xmit;
1189 struct msm_hs_tx *tx = &msm_uport->tx;
1190 struct msm_hs_rx *rx = &msm_uport->rx;
1191
1192 spin_lock_irqsave(&uport->lock, flags);
1193
1194 isr_status = msm_hs_read(uport, UARTDM_MISR_ADDR);
1195
1196 /* Uart RX starting */
1197 if (isr_status & UARTDM_ISR_RXLEV_BMSK) {
1198 msm_uport->imr_reg &= ~UARTDM_ISR_RXLEV_BMSK;
1199 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1200 }
1201 /* Stale rx interrupt */
1202 if (isr_status & UARTDM_ISR_RXSTALE_BMSK) {
1203 msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_DISABLE);
1204 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
1205
1206 if (msm_uport->clk_req_off_state == CLK_REQ_OFF_RXSTALE_ISSUED)
1207 msm_uport->clk_req_off_state =
1208 CLK_REQ_OFF_FLUSH_ISSUED;
1209 if (rx->flush == FLUSH_NONE) {
1210 rx->flush = FLUSH_DATA_READY;
1211 msm_dmov_stop_cmd(msm_uport->dma_rx_channel, NULL, 1);
1212 }
1213 }
1214 /* tx ready interrupt */
1215 if (isr_status & UARTDM_ISR_TX_READY_BMSK) {
1216 /* Clear TX Ready */
1217 msm_hs_write(uport, UARTDM_CR_ADDR, CLEAR_TX_READY);
1218
1219 if (msm_uport->clk_state == MSM_HS_CLK_REQUEST_OFF) {
1220 msm_uport->imr_reg |= UARTDM_ISR_TXLEV_BMSK;
1221 msm_hs_write(uport, UARTDM_IMR_ADDR,
1222 msm_uport->imr_reg);
1223 }
1224
1225 /* Complete DMA TX transactions and submit new transactions */
1226 tx_buf->tail = (tx_buf->tail + tx->tx_count) & ~UART_XMIT_SIZE;
1227
1228 tx->dma_in_flight = 0;
1229
1230 uport->icount.tx += tx->tx_count;
1231 if (tx->tx_ready_int_en)
1232 msm_hs_submit_tx_locked(uport);
1233
1234 if (uart_circ_chars_pending(tx_buf) < WAKEUP_CHARS)
1235 uart_write_wakeup(uport);
1236 }
1237 if (isr_status & UARTDM_ISR_TXLEV_BMSK) {
1238 /* TX FIFO is empty */
1239 msm_uport->imr_reg &= ~UARTDM_ISR_TXLEV_BMSK;
1240 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1241 if (!msm_hs_check_clock_off_locked(uport))
1242 hrtimer_start(&msm_uport->clk_off_timer,
1243 msm_uport->clk_off_delay,
1244 HRTIMER_MODE_REL);
1245 }
1246
1247 /* Change in CTS interrupt */
1248 if (isr_status & UARTDM_ISR_DELTA_CTS_BMSK)
1249 msm_hs_handle_delta_cts(uport);
1250
1251 spin_unlock_irqrestore(&uport->lock, flags);
1252
1253 return IRQ_HANDLED;
1254}
1255
1256void msm_hs_request_clock_off_locked(struct uart_port *uport)
1257{
1258 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1259
1260 if (msm_uport->clk_state == MSM_HS_CLK_ON) {
1261 msm_uport->clk_state = MSM_HS_CLK_REQUEST_OFF;
1262 msm_uport->clk_req_off_state = CLK_REQ_OFF_START;
1263 if (!use_low_power_rx_wakeup(msm_uport))
1264 set_rfr_locked(uport, 0);
1265 msm_uport->imr_reg |= UARTDM_ISR_TXLEV_BMSK;
1266 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1267 }
1268}
1269
1270/**
1271 * msm_hs_request_clock_off - request to (i.e. asynchronously) turn off uart
1272 * clock once pending TX is flushed and Rx DMA command is terminated.
1273 * @uport: uart_port structure for the device instance.
1274 *
1275 * This functions puts the device into a partially active low power mode. It
1276 * waits to complete all pending tx transactions, flushes ongoing Rx DMA
1277 * command and terminates UART side Rx transaction, puts UART HW in non DMA
1278 * mode and then clocks off the device. A client calls this when no UART
1279 * data is expected. msm_request_clock_on() must be called before any further
1280 * UART can be sent or received.
1281 */
1282void msm_hs_request_clock_off(struct uart_port *uport)
1283{
1284 unsigned long flags;
1285
1286 spin_lock_irqsave(&uport->lock, flags);
1287 msm_hs_request_clock_off_locked(uport);
1288 spin_unlock_irqrestore(&uport->lock, flags);
1289}
1290
1291void msm_hs_request_clock_on_locked(struct uart_port *uport)
1292{
1293 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1294 unsigned int data;
1295
1296 switch (msm_uport->clk_state) {
1297 case MSM_HS_CLK_OFF:
1298 clk_enable(msm_uport->clk);
1299 disable_irq_nosync(msm_uport->rx_wakeup.irq);
1300 /* fall-through */
1301 case MSM_HS_CLK_REQUEST_OFF:
1302 if (msm_uport->rx.flush == FLUSH_STOP ||
1303 msm_uport->rx.flush == FLUSH_SHUTDOWN) {
1304 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
1305 data = msm_hs_read(uport, UARTDM_DMEN_ADDR);
1306 data |= UARTDM_RX_DM_EN_BMSK;
1307 msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
1308 }
1309 hrtimer_try_to_cancel(&msm_uport->clk_off_timer);
1310 if (msm_uport->rx.flush == FLUSH_SHUTDOWN)
1311 msm_hs_start_rx_locked(uport);
1312 if (!use_low_power_rx_wakeup(msm_uport))
1313 set_rfr_locked(uport, 1);
1314 if (msm_uport->rx.flush == FLUSH_STOP)
1315 msm_uport->rx.flush = FLUSH_IGNORE;
1316 msm_uport->clk_state = MSM_HS_CLK_ON;
1317 break;
1318 case MSM_HS_CLK_ON:
1319 break;
1320 case MSM_HS_CLK_PORT_OFF:
1321 break;
1322 }
1323}
1324
1325/**
1326 * msm_hs_request_clock_on - Switch the device from partially active low
1327 * power mode to fully active (i.e. clock on) mode.
1328 * @uport: uart_port structure for the device.
1329 *
1330 * This function switches on the input clock, puts UART HW into DMA mode
1331 * and enqueues an Rx DMA command if the device was in partially active
1332 * mode. It has no effect if called with the device in inactive state.
1333 */
1334void msm_hs_request_clock_on(struct uart_port *uport)
1335{
1336 unsigned long flags;
1337
1338 spin_lock_irqsave(&uport->lock, flags);
1339 msm_hs_request_clock_on_locked(uport);
1340 spin_unlock_irqrestore(&uport->lock, flags);
1341}
1342
1343static irqreturn_t msm_hs_rx_wakeup_isr(int irq, void *dev)
1344{
1345 unsigned int wakeup = 0;
1346 unsigned long flags;
1347 struct msm_hs_port *msm_uport = dev;
1348 struct uart_port *uport = &msm_uport->uport;
1349 struct tty_struct *tty = NULL;
1350
1351 spin_lock_irqsave(&uport->lock, flags);
1352 if (msm_uport->clk_state == MSM_HS_CLK_OFF) {
1353 /* ignore the first irq - it is a pending irq that occurred
1354 * before enable_irq() */
1355 if (msm_uport->rx_wakeup.ignore)
1356 msm_uport->rx_wakeup.ignore = 0;
1357 else
1358 wakeup = 1;
1359 }
1360
1361 if (wakeup) {
1362 /* the uart was clocked off during an rx, wake up and
1363 * optionally inject char into tty rx */
1364 msm_hs_request_clock_on_locked(uport);
1365 if (msm_uport->rx_wakeup.inject_rx) {
1366 tty = uport->state->port.tty;
1367 tty_insert_flip_char(tty,
1368 msm_uport->rx_wakeup.rx_to_inject,
1369 TTY_NORMAL);
1370 queue_work(msm_hs_workqueue, &msm_uport->rx.tty_work);
1371 }
1372 }
1373
1374 spin_unlock_irqrestore(&uport->lock, flags);
1375
1376 return IRQ_HANDLED;
1377}
1378
1379static const char *msm_hs_type(struct uart_port *port)
1380{
1381 return (port->type == PORT_MSM) ? "MSM_HS_UART" : NULL;
1382}
1383
1384/* Called when port is opened */
1385static int msm_hs_startup(struct uart_port *uport)
1386{
1387 int ret;
1388 int rfr_level;
1389 unsigned long flags;
1390 unsigned int data;
1391 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1392 struct circ_buf *tx_buf = &uport->state->xmit;
1393 struct msm_hs_tx *tx = &msm_uport->tx;
1394 struct msm_hs_rx *rx = &msm_uport->rx;
1395
1396 rfr_level = uport->fifosize;
1397 if (rfr_level > 16)
1398 rfr_level -= 16;
1399
1400 tx->dma_base = dma_map_single(uport->dev, tx_buf->buf, UART_XMIT_SIZE,
1401 DMA_TO_DEVICE);
1402
1403 /* do not let tty layer execute RX in global workqueue, use a
1404 * dedicated workqueue managed by this driver */
1405 uport->state->port.tty->low_latency = 1;
1406
1407 /* turn on uart clk */
1408 ret = msm_hs_init_clk_locked(uport);
1409 if (unlikely(ret)) {
1410 printk(KERN_ERR "Turning uartclk failed!\n");
1411 goto err_msm_hs_init_clk;
1412 }
1413
1414 /* Set auto RFR Level */
1415 data = msm_hs_read(uport, UARTDM_MR1_ADDR);
1416 data &= ~UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK;
1417 data &= ~UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK;
1418 data |= (UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK & (rfr_level << 2));
1419 data |= (UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK & rfr_level);
1420 msm_hs_write(uport, UARTDM_MR1_ADDR, data);
1421
1422 /* Make sure RXSTALE count is non-zero */
1423 data = msm_hs_read(uport, UARTDM_IPR_ADDR);
1424 if (!data) {
1425 data |= 0x1f & UARTDM_IPR_STALE_LSB_BMSK;
1426 msm_hs_write(uport, UARTDM_IPR_ADDR, data);
1427 }
1428
1429 /* Enable Data Mover Mode */
1430 data = UARTDM_TX_DM_EN_BMSK | UARTDM_RX_DM_EN_BMSK;
1431 msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
1432
1433 /* Reset TX */
1434 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_TX);
1435 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
1436 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_ERROR_STATUS);
1437 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_BREAK_INT);
1438 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
1439 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_CTS);
1440 msm_hs_write(uport, UARTDM_CR_ADDR, RFR_LOW);
1441 /* Turn on Uart Receiver */
1442 msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_RX_EN_BMSK);
1443
1444 /* Turn on Uart Transmitter */
1445 msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_TX_EN_BMSK);
1446
1447 /* Initialize the tx */
1448 tx->tx_ready_int_en = 0;
1449 tx->dma_in_flight = 0;
1450
1451 tx->xfer.complete_func = msm_hs_dmov_tx_callback;
1452 tx->xfer.execute_func = NULL;
1453
1454 tx->command_ptr->cmd = CMD_LC |
1455 CMD_DST_CRCI(msm_uport->dma_tx_crci) | CMD_MODE_BOX;
1456
1457 tx->command_ptr->src_dst_len = (MSM_UARTDM_BURST_SIZE << 16)
1458 | (MSM_UARTDM_BURST_SIZE);
1459
1460 tx->command_ptr->row_offset = (MSM_UARTDM_BURST_SIZE << 16);
1461
1462 tx->command_ptr->dst_row_addr =
1463 msm_uport->uport.mapbase + UARTDM_TF_ADDR;
1464
1465
1466 /* Turn on Uart Receive */
1467 rx->xfer.complete_func = msm_hs_dmov_rx_callback;
1468 rx->xfer.execute_func = NULL;
1469
1470 rx->command_ptr->cmd = CMD_LC |
1471 CMD_SRC_CRCI(msm_uport->dma_rx_crci) | CMD_MODE_BOX;
1472
1473 rx->command_ptr->src_dst_len = (MSM_UARTDM_BURST_SIZE << 16)
1474 | (MSM_UARTDM_BURST_SIZE);
1475 rx->command_ptr->row_offset = MSM_UARTDM_BURST_SIZE;
1476 rx->command_ptr->src_row_addr = uport->mapbase + UARTDM_RF_ADDR;
1477
1478
1479 msm_uport->imr_reg |= UARTDM_ISR_RXSTALE_BMSK;
1480 /* Enable reading the current CTS, no harm even if CTS is ignored */
1481 msm_uport->imr_reg |= UARTDM_ISR_CURRENT_CTS_BMSK;
1482
1483 msm_hs_write(uport, UARTDM_TFWR_ADDR, 0); /* TXLEV on empty TX fifo */
1484
1485
1486 ret = request_irq(uport->irq, msm_hs_isr, IRQF_TRIGGER_HIGH,
1487 "msm_hs_uart", msm_uport);
1488 if (unlikely(ret)) {
1489 printk(KERN_ERR "Request msm_hs_uart IRQ failed!\n");
1490 goto err_request_irq;
1491 }
1492 if (use_low_power_rx_wakeup(msm_uport)) {
1493 ret = request_irq(msm_uport->rx_wakeup.irq,
1494 msm_hs_rx_wakeup_isr,
1495 IRQF_TRIGGER_FALLING,
1496 "msm_hs_rx_wakeup", msm_uport);
1497 if (unlikely(ret)) {
1498 printk(KERN_ERR "Request msm_hs_rx_wakeup IRQ failed!\n");
1499 free_irq(uport->irq, msm_uport);
1500 goto err_request_irq;
1501 }
1502 disable_irq(msm_uport->rx_wakeup.irq);
1503 }
1504
1505 spin_lock_irqsave(&uport->lock, flags);
1506
1507 msm_hs_write(uport, UARTDM_RFWR_ADDR, 0);
1508 msm_hs_start_rx_locked(uport);
1509
1510 spin_unlock_irqrestore(&uport->lock, flags);
1511 ret = pm_runtime_set_active(uport->dev);
1512 if (ret)
1513 dev_err(uport->dev, "set active error:%d\n", ret);
1514 pm_runtime_enable(uport->dev);
1515
1516 return 0;
1517
1518err_request_irq:
1519err_msm_hs_init_clk:
1520 dma_unmap_single(uport->dev, tx->dma_base,
1521 UART_XMIT_SIZE, DMA_TO_DEVICE);
1522 return ret;
1523}
1524
1525/* Initialize tx and rx data structures */
1526static int __devinit uartdm_init_port(struct uart_port *uport)
1527{
1528 int ret = 0;
1529 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1530 struct msm_hs_tx *tx = &msm_uport->tx;
1531 struct msm_hs_rx *rx = &msm_uport->rx;
1532
1533 /* Allocate the command pointer. Needs to be 64 bit aligned */
1534 tx->command_ptr = kmalloc(sizeof(dmov_box), GFP_KERNEL | __GFP_DMA);
1535 if (!tx->command_ptr)
1536 return -ENOMEM;
1537
1538 tx->command_ptr_ptr = kmalloc(sizeof(u32 *), GFP_KERNEL | __GFP_DMA);
1539 if (!tx->command_ptr_ptr) {
1540 ret = -ENOMEM;
1541 goto err_tx_command_ptr_ptr;
1542 }
1543
1544 tx->mapped_cmd_ptr = dma_map_single(uport->dev, tx->command_ptr,
1545 sizeof(dmov_box), DMA_TO_DEVICE);
1546 tx->mapped_cmd_ptr_ptr = dma_map_single(uport->dev,
1547 tx->command_ptr_ptr,
1548 sizeof(u32 *), DMA_TO_DEVICE);
1549 tx->xfer.cmdptr = DMOV_CMD_ADDR(tx->mapped_cmd_ptr_ptr);
1550
1551 init_waitqueue_head(&rx->wait);
1552
1553 rx->pool = dma_pool_create("rx_buffer_pool", uport->dev,
1554 UARTDM_RX_BUF_SIZE, 16, 0);
1555 if (!rx->pool) {
1556 pr_err("%s(): cannot allocate rx_buffer_pool", __func__);
1557 ret = -ENOMEM;
1558 goto err_dma_pool_create;
1559 }
1560
1561 rx->buffer = dma_pool_alloc(rx->pool, GFP_KERNEL, &rx->rbuffer);
1562 if (!rx->buffer) {
1563 pr_err("%s(): cannot allocate rx->buffer", __func__);
1564 ret = -ENOMEM;
1565 goto err_dma_pool_alloc;
1566 }
1567
1568 /* Allocate the command pointer. Needs to be 64 bit aligned */
1569 rx->command_ptr = kmalloc(sizeof(dmov_box), GFP_KERNEL | __GFP_DMA);
1570 if (!rx->command_ptr) {
1571 pr_err("%s(): cannot allocate rx->command_ptr", __func__);
1572 ret = -ENOMEM;
1573 goto err_rx_command_ptr;
1574 }
1575
1576 rx->command_ptr_ptr = kmalloc(sizeof(u32 *), GFP_KERNEL | __GFP_DMA);
1577 if (!rx->command_ptr_ptr) {
1578 pr_err("%s(): cannot allocate rx->command_ptr_ptr", __func__);
1579 ret = -ENOMEM;
1580 goto err_rx_command_ptr_ptr;
1581 }
1582
1583 rx->command_ptr->num_rows = ((UARTDM_RX_BUF_SIZE >> 4) << 16) |
1584 (UARTDM_RX_BUF_SIZE >> 4);
1585
1586 rx->command_ptr->dst_row_addr = rx->rbuffer;
1587
1588 rx->mapped_cmd_ptr = dma_map_single(uport->dev, rx->command_ptr,
1589 sizeof(dmov_box), DMA_TO_DEVICE);
1590
1591 *rx->command_ptr_ptr = CMD_PTR_LP | DMOV_CMD_ADDR(rx->mapped_cmd_ptr);
1592
1593 rx->cmdptr_dmaaddr = dma_map_single(uport->dev, rx->command_ptr_ptr,
1594 sizeof(u32 *), DMA_TO_DEVICE);
1595 rx->xfer.cmdptr = DMOV_CMD_ADDR(rx->cmdptr_dmaaddr);
1596
1597 INIT_WORK(&rx->tty_work, msm_hs_tty_flip_buffer_work);
1598
1599 return ret;
1600
1601err_rx_command_ptr_ptr:
1602 kfree(rx->command_ptr);
1603err_rx_command_ptr:
1604 dma_pool_free(msm_uport->rx.pool, msm_uport->rx.buffer,
1605 msm_uport->rx.rbuffer);
1606err_dma_pool_alloc:
1607 dma_pool_destroy(msm_uport->rx.pool);
1608err_dma_pool_create:
1609 dma_unmap_single(uport->dev, msm_uport->tx.mapped_cmd_ptr_ptr,
1610 sizeof(u32 *), DMA_TO_DEVICE);
1611 dma_unmap_single(uport->dev, msm_uport->tx.mapped_cmd_ptr,
1612 sizeof(dmov_box), DMA_TO_DEVICE);
1613 kfree(msm_uport->tx.command_ptr_ptr);
1614err_tx_command_ptr_ptr:
1615 kfree(msm_uport->tx.command_ptr);
1616 return ret;
1617}
1618
1619static int __devinit msm_hs_probe(struct platform_device *pdev)
1620{
1621 int ret;
1622 struct uart_port *uport;
1623 struct msm_hs_port *msm_uport;
1624 struct resource *resource;
1625 const struct msm_serial_hs_platform_data *pdata =
1626 pdev->dev.platform_data;
1627
1628 if (pdev->id < 0 || pdev->id >= UARTDM_NR) {
1629 printk(KERN_ERR "Invalid plaform device ID = %d\n", pdev->id);
1630 return -EINVAL;
1631 }
1632
1633 msm_uport = &q_uart_port[pdev->id];
1634 uport = &msm_uport->uport;
1635
1636 uport->dev = &pdev->dev;
1637
1638 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1639 if (unlikely(!resource))
1640 return -ENXIO;
1641
1642 uport->mapbase = resource->start;
1643 uport->irq = platform_get_irq(pdev, 0);
1644 if (unlikely(uport->irq < 0))
1645 return -ENXIO;
1646
1647 if (unlikely(irq_set_irq_wake(uport->irq, 1)))
1648 return -ENXIO;
1649
1650 if (pdata == NULL || pdata->rx_wakeup_irq < 0)
1651 msm_uport->rx_wakeup.irq = -1;
1652 else {
1653 msm_uport->rx_wakeup.irq = pdata->rx_wakeup_irq;
1654 msm_uport->rx_wakeup.ignore = 1;
1655 msm_uport->rx_wakeup.inject_rx = pdata->inject_rx_on_wakeup;
1656 msm_uport->rx_wakeup.rx_to_inject = pdata->rx_to_inject;
1657
1658 if (unlikely(msm_uport->rx_wakeup.irq < 0))
1659 return -ENXIO;
1660
1661 if (unlikely(irq_set_irq_wake(msm_uport->rx_wakeup.irq, 1)))
1662 return -ENXIO;
1663 }
1664
1665 if (pdata == NULL)
1666 msm_uport->exit_lpm_cb = NULL;
1667 else
1668 msm_uport->exit_lpm_cb = pdata->exit_lpm_cb;
1669
1670 resource = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1671 "uartdm_channels");
1672 if (unlikely(!resource))
1673 return -ENXIO;
1674
1675 msm_uport->dma_tx_channel = resource->start;
1676 msm_uport->dma_rx_channel = resource->end;
1677
1678 resource = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1679 "uartdm_crci");
1680 if (unlikely(!resource))
1681 return -ENXIO;
1682
1683 msm_uport->dma_tx_crci = resource->start;
1684 msm_uport->dma_rx_crci = resource->end;
1685
1686 uport->iotype = UPIO_MEM;
1687 uport->fifosize = UART_FIFOSIZE;
1688 uport->ops = &msm_hs_ops;
1689 uport->flags = UPF_BOOT_AUTOCONF;
1690 uport->uartclk = UARTCLK;
1691 msm_uport->imr_reg = 0x0;
1692 msm_uport->clk = clk_get(&pdev->dev, "uartdm_clk");
1693 if (IS_ERR(msm_uport->clk))
1694 return PTR_ERR(msm_uport->clk);
1695
1696 ret = uartdm_init_port(uport);
1697 if (unlikely(ret))
1698 return ret;
1699
1700 msm_uport->clk_state = MSM_HS_CLK_PORT_OFF;
1701 hrtimer_init(&msm_uport->clk_off_timer, CLOCK_MONOTONIC,
1702 HRTIMER_MODE_REL);
1703 msm_uport->clk_off_timer.function = msm_hs_clk_off_retry;
1704 msm_uport->clk_off_delay = ktime_set(0, 1000000); /* 1ms */
1705
1706 uport->line = pdev->id;
1707 return uart_add_one_port(&msm_hs_driver, uport);
1708}
1709
1710static int __init msm_serial_hs_init(void)
1711{
1712 int ret, i;
1713
1714 /* Init all UARTS as non-configured */
1715 for (i = 0; i < UARTDM_NR; i++)
1716 q_uart_port[i].uport.type = PORT_UNKNOWN;
1717
1718 msm_hs_workqueue = create_singlethread_workqueue("msm_serial_hs");
1719 if (unlikely(!msm_hs_workqueue))
1720 return -ENOMEM;
1721
1722 ret = uart_register_driver(&msm_hs_driver);
1723 if (unlikely(ret)) {
1724 printk(KERN_ERR "%s failed to load\n", __func__);
1725 goto err_uart_register_driver;
1726 }
1727
1728 ret = platform_driver_register(&msm_serial_hs_platform_driver);
1729 if (ret) {
1730 printk(KERN_ERR "%s failed to load\n", __func__);
1731 goto err_platform_driver_register;
1732 }
1733
1734 return ret;
1735
1736err_platform_driver_register:
1737 uart_unregister_driver(&msm_hs_driver);
1738err_uart_register_driver:
1739 destroy_workqueue(msm_hs_workqueue);
1740 return ret;
1741}
1742module_init(msm_serial_hs_init);
1743
1744/*
1745 * Called by the upper layer when port is closed.
1746 * - Disables the port
1747 * - Unhook the ISR
1748 */
1749static void msm_hs_shutdown(struct uart_port *uport)
1750{
1751 unsigned long flags;
1752 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1753
1754 BUG_ON(msm_uport->rx.flush < FLUSH_STOP);
1755
1756 spin_lock_irqsave(&uport->lock, flags);
1757 clk_enable(msm_uport->clk);
1758
1759 /* Disable the transmitter */
1760 msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_TX_DISABLE_BMSK);
1761 /* Disable the receiver */
1762 msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_RX_DISABLE_BMSK);
1763
1764 pm_runtime_disable(uport->dev);
1765 pm_runtime_set_suspended(uport->dev);
1766
1767 /* Free the interrupt */
1768 free_irq(uport->irq, msm_uport);
1769 if (use_low_power_rx_wakeup(msm_uport))
1770 free_irq(msm_uport->rx_wakeup.irq, msm_uport);
1771
1772 msm_uport->imr_reg = 0;
1773 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1774
1775 wait_event(msm_uport->rx.wait, msm_uport->rx.flush == FLUSH_SHUTDOWN);
1776
1777 clk_disable(msm_uport->clk); /* to balance local clk_enable() */
1778 if (msm_uport->clk_state != MSM_HS_CLK_OFF)
1779 clk_disable(msm_uport->clk); /* to balance clk_state */
1780 msm_uport->clk_state = MSM_HS_CLK_PORT_OFF;
1781
1782 dma_unmap_single(uport->dev, msm_uport->tx.dma_base,
1783 UART_XMIT_SIZE, DMA_TO_DEVICE);
1784
1785 spin_unlock_irqrestore(&uport->lock, flags);
1786
1787 if (cancel_work_sync(&msm_uport->rx.tty_work))
1788 msm_hs_tty_flip_buffer_work(&msm_uport->rx.tty_work);
1789}
1790
1791static void __exit msm_serial_hs_exit(void)
1792{
1793 flush_workqueue(msm_hs_workqueue);
1794 destroy_workqueue(msm_hs_workqueue);
1795 platform_driver_unregister(&msm_serial_hs_platform_driver);
1796 uart_unregister_driver(&msm_hs_driver);
1797}
1798module_exit(msm_serial_hs_exit);
1799
1800#ifdef CONFIG_PM_RUNTIME
1801static int msm_hs_runtime_idle(struct device *dev)
1802{
1803 /*
1804 * returning success from idle results in runtime suspend to be
1805 * called
1806 */
1807 return 0;
1808}
1809
1810static int msm_hs_runtime_resume(struct device *dev)
1811{
1812 struct platform_device *pdev = container_of(dev, struct
1813 platform_device, dev);
1814 struct msm_hs_port *msm_uport = &q_uart_port[pdev->id];
1815
1816 msm_hs_request_clock_on(&msm_uport->uport);
1817 return 0;
1818}
1819
1820static int msm_hs_runtime_suspend(struct device *dev)
1821{
1822 struct platform_device *pdev = container_of(dev, struct
1823 platform_device, dev);
1824 struct msm_hs_port *msm_uport = &q_uart_port[pdev->id];
1825
1826 msm_hs_request_clock_off(&msm_uport->uport);
1827 return 0;
1828}
1829#else
1830#define msm_hs_runtime_idle NULL
1831#define msm_hs_runtime_resume NULL
1832#define msm_hs_runtime_suspend NULL
1833#endif
1834
1835static const struct dev_pm_ops msm_hs_dev_pm_ops = {
1836 .runtime_suspend = msm_hs_runtime_suspend,
1837 .runtime_resume = msm_hs_runtime_resume,
1838 .runtime_idle = msm_hs_runtime_idle,
1839};
1840
1841static struct platform_driver msm_serial_hs_platform_driver = {
1842 .probe = msm_hs_probe,
1843 .remove = __devexit_p(msm_hs_remove),
1844 .driver = {
1845 .name = "msm_serial_hs",
1846 .owner = THIS_MODULE,
1847 .pm = &msm_hs_dev_pm_ops,
1848 },
1849};
1850
1851static struct uart_driver msm_hs_driver = {
1852 .owner = THIS_MODULE,
1853 .driver_name = "msm_serial_hs",
1854 .dev_name = "ttyHS",
1855 .nr = UARTDM_NR,
1856 .cons = 0,
1857};
1858
1859static struct uart_ops msm_hs_ops = {
1860 .tx_empty = msm_hs_tx_empty,
1861 .set_mctrl = msm_hs_set_mctrl_locked,
1862 .get_mctrl = msm_hs_get_mctrl_locked,
1863 .stop_tx = msm_hs_stop_tx_locked,
1864 .start_tx = msm_hs_start_tx_locked,
1865 .stop_rx = msm_hs_stop_rx_locked,
1866 .enable_ms = msm_hs_enable_ms_locked,
1867 .break_ctl = msm_hs_break_ctl,
1868 .startup = msm_hs_startup,
1869 .shutdown = msm_hs_shutdown,
1870 .set_termios = msm_hs_set_termios,
1871 .pm = msm_hs_pm,
1872 .type = msm_hs_type,
1873 .config_port = msm_hs_config_port,
1874 .release_port = msm_hs_release_port,
1875 .request_port = msm_hs_request_port,
1876};
1877
1878MODULE_DESCRIPTION("High Speed UART Driver for the MSM chipset");
1879MODULE_VERSION("1.2");
1880MODULE_LICENSE("GPL v2");
diff --git a/drivers/tty/serial/msm_smd_tty.c b/drivers/tty/serial/msm_smd_tty.c
new file mode 100644
index 000000000000..4f41dcdcb771
--- /dev/null
+++ b/drivers/tty/serial/msm_smd_tty.c
@@ -0,0 +1,235 @@
1/*
2 * Copyright (C) 2007 Google, Inc.
3 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
4 * Author: Brian Swetland <swetland@google.com>
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
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 */
16
17#include <linux/module.h>
18#include <linux/fs.h>
19#include <linux/cdev.h>
20#include <linux/device.h>
21#include <linux/wait.h>
22
23#include <linux/tty.h>
24#include <linux/tty_driver.h>
25#include <linux/tty_flip.h>
26
27#include <mach/msm_smd.h>
28
29#define MAX_SMD_TTYS 32
30
31struct smd_tty_info {
32 struct tty_port port;
33 smd_channel_t *ch;
34};
35
36struct smd_tty_channel_desc {
37 int id;
38 const char *name;
39};
40
41static struct smd_tty_info smd_tty[MAX_SMD_TTYS];
42
43static const struct smd_tty_channel_desc smd_default_tty_channels[] = {
44 { .id = 0, .name = "SMD_DS" },
45 { .id = 27, .name = "SMD_GPSNMEA" },
46};
47
48static const struct smd_tty_channel_desc *smd_tty_channels =
49 smd_default_tty_channels;
50static int smd_tty_channels_len = ARRAY_SIZE(smd_default_tty_channels);
51
52static void smd_tty_notify(void *priv, unsigned event)
53{
54 unsigned char *ptr;
55 int avail;
56 struct smd_tty_info *info = priv;
57 struct tty_struct *tty;
58
59 if (event != SMD_EVENT_DATA)
60 return;
61
62 tty = tty_port_tty_get(&info->port);
63 if (!tty)
64 return;
65
66 for (;;) {
67 if (test_bit(TTY_THROTTLED, &tty->flags))
68 break;
69 avail = smd_read_avail(info->ch);
70 if (avail == 0)
71 break;
72
73 avail = tty_prepare_flip_string(tty, &ptr, avail);
74
75 if (smd_read(info->ch, ptr, avail) != avail) {
76 /* shouldn't be possible since we're in interrupt
77 ** context here and nobody else could 'steal' our
78 ** characters.
79 */
80 pr_err("OOPS - smd_tty_buffer mismatch?!");
81 }
82
83 tty_flip_buffer_push(tty);
84 }
85
86 /* XXX only when writable and necessary */
87 tty_wakeup(tty);
88 tty_kref_put(tty);
89}
90
91static int smd_tty_port_activate(struct tty_port *tport, struct tty_struct *tty)
92{
93 int i, res = 0;
94 int n = tty->index;
95 const char *name = NULL;
96 struct smd_tty_info *info = smd_tty + n;
97
98 for (i = 0; i < smd_tty_channels_len; i++) {
99 if (smd_tty_channels[i].id == n) {
100 name = smd_tty_channels[i].name;
101 break;
102 }
103 }
104 if (!name)
105 return -ENODEV;
106
107 if (info->ch)
108 smd_kick(info->ch);
109 else
110 res = smd_open(name, &info->ch, info, smd_tty_notify);
111
112 if (!res)
113 tty->driver_data = info;
114
115 return res;
116}
117
118static void smd_tty_port_shutdown(struct tty_port *tport)
119{
120 struct smd_tty_info *info;
121 struct tty_struct *tty = tty_port_tty_get(tport);
122
123 info = tty->driver_data;
124 if (info->ch) {
125 smd_close(info->ch);
126 info->ch = 0;
127 }
128
129 tty->driver_data = 0;
130 tty_kref_put(tty);
131}
132
133static int smd_tty_open(struct tty_struct *tty, struct file *f)
134{
135 struct smd_tty_info *info = smd_tty + tty->index;
136
137 return tty_port_open(&info->port, tty, f);
138}
139
140static void smd_tty_close(struct tty_struct *tty, struct file *f)
141{
142 struct smd_tty_info *info = tty->driver_data;
143
144 tty_port_close(&info->port, tty, f);
145}
146
147static int smd_tty_write(struct tty_struct *tty,
148 const unsigned char *buf, int len)
149{
150 struct smd_tty_info *info = tty->driver_data;
151 int avail;
152
153 /* if we're writing to a packet channel we will
154 ** never be able to write more data than there
155 ** is currently space for
156 */
157 avail = smd_write_avail(info->ch);
158 if (len > avail)
159 len = avail;
160
161 return smd_write(info->ch, buf, len);
162}
163
164static int smd_tty_write_room(struct tty_struct *tty)
165{
166 struct smd_tty_info *info = tty->driver_data;
167 return smd_write_avail(info->ch);
168}
169
170static int smd_tty_chars_in_buffer(struct tty_struct *tty)
171{
172 struct smd_tty_info *info = tty->driver_data;
173 return smd_read_avail(info->ch);
174}
175
176static void smd_tty_unthrottle(struct tty_struct *tty)
177{
178 struct smd_tty_info *info = tty->driver_data;
179 smd_kick(info->ch);
180}
181
182static const struct tty_port_operations smd_tty_port_ops = {
183 .shutdown = smd_tty_port_shutdown,
184 .activate = smd_tty_port_activate,
185};
186
187static const struct tty_operations smd_tty_ops = {
188 .open = smd_tty_open,
189 .close = smd_tty_close,
190 .write = smd_tty_write,
191 .write_room = smd_tty_write_room,
192 .chars_in_buffer = smd_tty_chars_in_buffer,
193 .unthrottle = smd_tty_unthrottle,
194};
195
196static struct tty_driver *smd_tty_driver;
197
198static int __init smd_tty_init(void)
199{
200 int ret, i;
201
202 smd_tty_driver = alloc_tty_driver(MAX_SMD_TTYS);
203 if (smd_tty_driver == 0)
204 return -ENOMEM;
205
206 smd_tty_driver->owner = THIS_MODULE;
207 smd_tty_driver->driver_name = "smd_tty_driver";
208 smd_tty_driver->name = "smd";
209 smd_tty_driver->major = 0;
210 smd_tty_driver->minor_start = 0;
211 smd_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
212 smd_tty_driver->subtype = SERIAL_TYPE_NORMAL;
213 smd_tty_driver->init_termios = tty_std_termios;
214 smd_tty_driver->init_termios.c_iflag = 0;
215 smd_tty_driver->init_termios.c_oflag = 0;
216 smd_tty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
217 smd_tty_driver->init_termios.c_lflag = 0;
218 smd_tty_driver->flags = TTY_DRIVER_RESET_TERMIOS |
219 TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
220 tty_set_operations(smd_tty_driver, &smd_tty_ops);
221
222 ret = tty_register_driver(smd_tty_driver);
223 if (ret)
224 return ret;
225
226 for (i = 0; i < smd_tty_channels_len; i++) {
227 tty_port_init(&smd_tty[smd_tty_channels[i].id].port);
228 smd_tty[smd_tty_channels[i].id].port.ops = &smd_tty_port_ops;
229 tty_register_device(smd_tty_driver, smd_tty_channels[i].id, 0);
230 }
231
232 return 0;
233}
234
235module_init(smd_tty_init);
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/mxs-auart.c b/drivers/tty/serial/mxs-auart.c
new file mode 100644
index 000000000000..7e02c9c344fe
--- /dev/null
+++ b/drivers/tty/serial/mxs-auart.c
@@ -0,0 +1,798 @@
1/*
2 * Freescale STMP37XX/STMP378X Application UART driver
3 *
4 * Author: dmitry pervushin <dimka@embeddedalley.com>
5 *
6 * Copyright 2008-2010 Freescale Semiconductor, Inc.
7 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
8 *
9 * The code contained herein is licensed under the GNU General Public
10 * License. You may obtain a copy of the GNU General Public License
11 * Version 2 or later at the following locations:
12 *
13 * http://www.opensource.org/licenses/gpl-license.html
14 * http://www.gnu.org/copyleft/gpl.html
15 */
16
17#include <linux/kernel.h>
18#include <linux/errno.h>
19#include <linux/init.h>
20#include <linux/console.h>
21#include <linux/interrupt.h>
22#include <linux/module.h>
23#include <linux/slab.h>
24#include <linux/wait.h>
25#include <linux/tty.h>
26#include <linux/tty_driver.h>
27#include <linux/tty_flip.h>
28#include <linux/serial.h>
29#include <linux/serial_core.h>
30#include <linux/platform_device.h>
31#include <linux/device.h>
32#include <linux/clk.h>
33#include <linux/delay.h>
34#include <linux/io.h>
35
36#include <asm/cacheflush.h>
37
38#define MXS_AUART_PORTS 5
39
40#define AUART_CTRL0 0x00000000
41#define AUART_CTRL0_SET 0x00000004
42#define AUART_CTRL0_CLR 0x00000008
43#define AUART_CTRL0_TOG 0x0000000c
44#define AUART_CTRL1 0x00000010
45#define AUART_CTRL1_SET 0x00000014
46#define AUART_CTRL1_CLR 0x00000018
47#define AUART_CTRL1_TOG 0x0000001c
48#define AUART_CTRL2 0x00000020
49#define AUART_CTRL2_SET 0x00000024
50#define AUART_CTRL2_CLR 0x00000028
51#define AUART_CTRL2_TOG 0x0000002c
52#define AUART_LINECTRL 0x00000030
53#define AUART_LINECTRL_SET 0x00000034
54#define AUART_LINECTRL_CLR 0x00000038
55#define AUART_LINECTRL_TOG 0x0000003c
56#define AUART_LINECTRL2 0x00000040
57#define AUART_LINECTRL2_SET 0x00000044
58#define AUART_LINECTRL2_CLR 0x00000048
59#define AUART_LINECTRL2_TOG 0x0000004c
60#define AUART_INTR 0x00000050
61#define AUART_INTR_SET 0x00000054
62#define AUART_INTR_CLR 0x00000058
63#define AUART_INTR_TOG 0x0000005c
64#define AUART_DATA 0x00000060
65#define AUART_STAT 0x00000070
66#define AUART_DEBUG 0x00000080
67#define AUART_VERSION 0x00000090
68#define AUART_AUTOBAUD 0x000000a0
69
70#define AUART_CTRL0_SFTRST (1 << 31)
71#define AUART_CTRL0_CLKGATE (1 << 30)
72
73#define AUART_CTRL2_CTSEN (1 << 15)
74#define AUART_CTRL2_RTS (1 << 11)
75#define AUART_CTRL2_RXE (1 << 9)
76#define AUART_CTRL2_TXE (1 << 8)
77#define AUART_CTRL2_UARTEN (1 << 0)
78
79#define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
80#define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
81#define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
82#define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
83#define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
84#define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
85#define AUART_LINECTRL_WLEN_MASK 0x00000060
86#define AUART_LINECTRL_WLEN(v) (((v) & 0x3) << 5)
87#define AUART_LINECTRL_FEN (1 << 4)
88#define AUART_LINECTRL_STP2 (1 << 3)
89#define AUART_LINECTRL_EPS (1 << 2)
90#define AUART_LINECTRL_PEN (1 << 1)
91#define AUART_LINECTRL_BRK (1 << 0)
92
93#define AUART_INTR_RTIEN (1 << 22)
94#define AUART_INTR_TXIEN (1 << 21)
95#define AUART_INTR_RXIEN (1 << 20)
96#define AUART_INTR_CTSMIEN (1 << 17)
97#define AUART_INTR_RTIS (1 << 6)
98#define AUART_INTR_TXIS (1 << 5)
99#define AUART_INTR_RXIS (1 << 4)
100#define AUART_INTR_CTSMIS (1 << 1)
101
102#define AUART_STAT_BUSY (1 << 29)
103#define AUART_STAT_CTS (1 << 28)
104#define AUART_STAT_TXFE (1 << 27)
105#define AUART_STAT_TXFF (1 << 25)
106#define AUART_STAT_RXFE (1 << 24)
107#define AUART_STAT_OERR (1 << 19)
108#define AUART_STAT_BERR (1 << 18)
109#define AUART_STAT_PERR (1 << 17)
110#define AUART_STAT_FERR (1 << 16)
111
112static struct uart_driver auart_driver;
113
114struct mxs_auart_port {
115 struct uart_port port;
116
117 unsigned int flags;
118 unsigned int ctrl;
119
120 unsigned int irq;
121
122 struct clk *clk;
123 struct device *dev;
124};
125
126static void mxs_auart_stop_tx(struct uart_port *u);
127
128#define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
129
130static inline void mxs_auart_tx_chars(struct mxs_auart_port *s)
131{
132 struct circ_buf *xmit = &s->port.state->xmit;
133
134 while (!(readl(s->port.membase + AUART_STAT) &
135 AUART_STAT_TXFF)) {
136 if (s->port.x_char) {
137 s->port.icount.tx++;
138 writel(s->port.x_char,
139 s->port.membase + AUART_DATA);
140 s->port.x_char = 0;
141 continue;
142 }
143 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
144 s->port.icount.tx++;
145 writel(xmit->buf[xmit->tail],
146 s->port.membase + AUART_DATA);
147 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
148 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
149 uart_write_wakeup(&s->port);
150 } else
151 break;
152 }
153 if (uart_circ_empty(&(s->port.state->xmit)))
154 writel(AUART_INTR_TXIEN,
155 s->port.membase + AUART_INTR_CLR);
156 else
157 writel(AUART_INTR_TXIEN,
158 s->port.membase + AUART_INTR_SET);
159
160 if (uart_tx_stopped(&s->port))
161 mxs_auart_stop_tx(&s->port);
162}
163
164static void mxs_auart_rx_char(struct mxs_auart_port *s)
165{
166 int flag;
167 u32 stat;
168 u8 c;
169
170 c = readl(s->port.membase + AUART_DATA);
171 stat = readl(s->port.membase + AUART_STAT);
172
173 flag = TTY_NORMAL;
174 s->port.icount.rx++;
175
176 if (stat & AUART_STAT_BERR) {
177 s->port.icount.brk++;
178 if (uart_handle_break(&s->port))
179 goto out;
180 } else if (stat & AUART_STAT_PERR) {
181 s->port.icount.parity++;
182 } else if (stat & AUART_STAT_FERR) {
183 s->port.icount.frame++;
184 }
185
186 /*
187 * Mask off conditions which should be ingored.
188 */
189 stat &= s->port.read_status_mask;
190
191 if (stat & AUART_STAT_BERR) {
192 flag = TTY_BREAK;
193 } else if (stat & AUART_STAT_PERR)
194 flag = TTY_PARITY;
195 else if (stat & AUART_STAT_FERR)
196 flag = TTY_FRAME;
197
198 if (stat & AUART_STAT_OERR)
199 s->port.icount.overrun++;
200
201 if (uart_handle_sysrq_char(&s->port, c))
202 goto out;
203
204 uart_insert_char(&s->port, stat, AUART_STAT_OERR, c, flag);
205out:
206 writel(stat, s->port.membase + AUART_STAT);
207}
208
209static void mxs_auart_rx_chars(struct mxs_auart_port *s)
210{
211 struct tty_struct *tty = s->port.state->port.tty;
212 u32 stat = 0;
213
214 for (;;) {
215 stat = readl(s->port.membase + AUART_STAT);
216 if (stat & AUART_STAT_RXFE)
217 break;
218 mxs_auart_rx_char(s);
219 }
220
221 writel(stat, s->port.membase + AUART_STAT);
222 tty_flip_buffer_push(tty);
223}
224
225static int mxs_auart_request_port(struct uart_port *u)
226{
227 return 0;
228}
229
230static int mxs_auart_verify_port(struct uart_port *u,
231 struct serial_struct *ser)
232{
233 if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
234 return -EINVAL;
235 return 0;
236}
237
238static void mxs_auart_config_port(struct uart_port *u, int flags)
239{
240}
241
242static const char *mxs_auart_type(struct uart_port *u)
243{
244 struct mxs_auart_port *s = to_auart_port(u);
245
246 return dev_name(s->dev);
247}
248
249static void mxs_auart_release_port(struct uart_port *u)
250{
251}
252
253static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
254{
255 struct mxs_auart_port *s = to_auart_port(u);
256
257 u32 ctrl = readl(u->membase + AUART_CTRL2);
258
259 ctrl &= ~AUART_CTRL2_RTS;
260 if (mctrl & TIOCM_RTS)
261 ctrl |= AUART_CTRL2_RTS;
262 s->ctrl = mctrl;
263 writel(ctrl, u->membase + AUART_CTRL2);
264}
265
266static u32 mxs_auart_get_mctrl(struct uart_port *u)
267{
268 struct mxs_auart_port *s = to_auart_port(u);
269 u32 stat = readl(u->membase + AUART_STAT);
270 int ctrl2 = readl(u->membase + AUART_CTRL2);
271 u32 mctrl = s->ctrl;
272
273 mctrl &= ~TIOCM_CTS;
274 if (stat & AUART_STAT_CTS)
275 mctrl |= TIOCM_CTS;
276
277 if (ctrl2 & AUART_CTRL2_RTS)
278 mctrl |= TIOCM_RTS;
279
280 return mctrl;
281}
282
283static void mxs_auart_settermios(struct uart_port *u,
284 struct ktermios *termios,
285 struct ktermios *old)
286{
287 u32 bm, ctrl, ctrl2, div;
288 unsigned int cflag, baud;
289
290 cflag = termios->c_cflag;
291
292 ctrl = AUART_LINECTRL_FEN;
293 ctrl2 = readl(u->membase + AUART_CTRL2);
294
295 /* byte size */
296 switch (cflag & CSIZE) {
297 case CS5:
298 bm = 0;
299 break;
300 case CS6:
301 bm = 1;
302 break;
303 case CS7:
304 bm = 2;
305 break;
306 case CS8:
307 bm = 3;
308 break;
309 default:
310 return;
311 }
312
313 ctrl |= AUART_LINECTRL_WLEN(bm);
314
315 /* parity */
316 if (cflag & PARENB) {
317 ctrl |= AUART_LINECTRL_PEN;
318 if ((cflag & PARODD) == 0)
319 ctrl |= AUART_LINECTRL_EPS;
320 }
321
322 u->read_status_mask = 0;
323
324 if (termios->c_iflag & INPCK)
325 u->read_status_mask |= AUART_STAT_PERR;
326 if (termios->c_iflag & (BRKINT | PARMRK))
327 u->read_status_mask |= AUART_STAT_BERR;
328
329 /*
330 * Characters to ignore
331 */
332 u->ignore_status_mask = 0;
333 if (termios->c_iflag & IGNPAR)
334 u->ignore_status_mask |= AUART_STAT_PERR;
335 if (termios->c_iflag & IGNBRK) {
336 u->ignore_status_mask |= AUART_STAT_BERR;
337 /*
338 * If we're ignoring parity and break indicators,
339 * ignore overruns too (for real raw support).
340 */
341 if (termios->c_iflag & IGNPAR)
342 u->ignore_status_mask |= AUART_STAT_OERR;
343 }
344
345 /*
346 * ignore all characters if CREAD is not set
347 */
348 if (cflag & CREAD)
349 ctrl2 |= AUART_CTRL2_RXE;
350 else
351 ctrl2 &= ~AUART_CTRL2_RXE;
352
353 /* figure out the stop bits requested */
354 if (cflag & CSTOPB)
355 ctrl |= AUART_LINECTRL_STP2;
356
357 /* figure out the hardware flow control settings */
358 if (cflag & CRTSCTS)
359 ctrl2 |= AUART_CTRL2_CTSEN;
360 else
361 ctrl2 &= ~AUART_CTRL2_CTSEN;
362
363 /* set baud rate */
364 baud = uart_get_baud_rate(u, termios, old, 0, u->uartclk);
365 div = u->uartclk * 32 / baud;
366 ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
367 ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
368
369 writel(ctrl, u->membase + AUART_LINECTRL);
370 writel(ctrl2, u->membase + AUART_CTRL2);
371}
372
373static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
374{
375 u32 istatus, istat;
376 struct mxs_auart_port *s = context;
377 u32 stat = readl(s->port.membase + AUART_STAT);
378
379 istatus = istat = readl(s->port.membase + AUART_INTR);
380
381 if (istat & AUART_INTR_CTSMIS) {
382 uart_handle_cts_change(&s->port, stat & AUART_STAT_CTS);
383 writel(AUART_INTR_CTSMIS,
384 s->port.membase + AUART_INTR_CLR);
385 istat &= ~AUART_INTR_CTSMIS;
386 }
387
388 if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
389 mxs_auart_rx_chars(s);
390 istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
391 }
392
393 if (istat & AUART_INTR_TXIS) {
394 mxs_auart_tx_chars(s);
395 istat &= ~AUART_INTR_TXIS;
396 }
397
398 writel(istatus & (AUART_INTR_RTIS
399 | AUART_INTR_TXIS
400 | AUART_INTR_RXIS
401 | AUART_INTR_CTSMIS),
402 s->port.membase + AUART_INTR_CLR);
403
404 return IRQ_HANDLED;
405}
406
407static void mxs_auart_reset(struct uart_port *u)
408{
409 int i;
410 unsigned int reg;
411
412 writel(AUART_CTRL0_SFTRST, u->membase + AUART_CTRL0_CLR);
413
414 for (i = 0; i < 10000; i++) {
415 reg = readl(u->membase + AUART_CTRL0);
416 if (!(reg & AUART_CTRL0_SFTRST))
417 break;
418 udelay(3);
419 }
420 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
421}
422
423static int mxs_auart_startup(struct uart_port *u)
424{
425 struct mxs_auart_port *s = to_auart_port(u);
426
427 clk_enable(s->clk);
428
429 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
430
431 writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_SET);
432
433 writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
434 u->membase + AUART_INTR);
435
436 /*
437 * Enable fifo so all four bytes of a DMA word are written to
438 * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
439 */
440 writel(AUART_LINECTRL_FEN, u->membase + AUART_LINECTRL_SET);
441
442 return 0;
443}
444
445static void mxs_auart_shutdown(struct uart_port *u)
446{
447 struct mxs_auart_port *s = to_auart_port(u);
448
449 writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_CLR);
450
451 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_SET);
452
453 writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
454 u->membase + AUART_INTR_CLR);
455
456 clk_disable(s->clk);
457}
458
459static unsigned int mxs_auart_tx_empty(struct uart_port *u)
460{
461 if (readl(u->membase + AUART_STAT) & AUART_STAT_TXFE)
462 return TIOCSER_TEMT;
463 else
464 return 0;
465}
466
467static void mxs_auart_start_tx(struct uart_port *u)
468{
469 struct mxs_auart_port *s = to_auart_port(u);
470
471 /* enable transmitter */
472 writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_SET);
473
474 mxs_auart_tx_chars(s);
475}
476
477static void mxs_auart_stop_tx(struct uart_port *u)
478{
479 writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_CLR);
480}
481
482static void mxs_auart_stop_rx(struct uart_port *u)
483{
484 writel(AUART_CTRL2_RXE, u->membase + AUART_CTRL2_CLR);
485}
486
487static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
488{
489 if (ctl)
490 writel(AUART_LINECTRL_BRK,
491 u->membase + AUART_LINECTRL_SET);
492 else
493 writel(AUART_LINECTRL_BRK,
494 u->membase + AUART_LINECTRL_CLR);
495}
496
497static void mxs_auart_enable_ms(struct uart_port *port)
498{
499 /* just empty */
500}
501
502static struct uart_ops mxs_auart_ops = {
503 .tx_empty = mxs_auart_tx_empty,
504 .start_tx = mxs_auart_start_tx,
505 .stop_tx = mxs_auart_stop_tx,
506 .stop_rx = mxs_auart_stop_rx,
507 .enable_ms = mxs_auart_enable_ms,
508 .break_ctl = mxs_auart_break_ctl,
509 .set_mctrl = mxs_auart_set_mctrl,
510 .get_mctrl = mxs_auart_get_mctrl,
511 .startup = mxs_auart_startup,
512 .shutdown = mxs_auart_shutdown,
513 .set_termios = mxs_auart_settermios,
514 .type = mxs_auart_type,
515 .release_port = mxs_auart_release_port,
516 .request_port = mxs_auart_request_port,
517 .config_port = mxs_auart_config_port,
518 .verify_port = mxs_auart_verify_port,
519};
520
521static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
522
523#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
524static void mxs_auart_console_putchar(struct uart_port *port, int ch)
525{
526 unsigned int to = 1000;
527
528 while (readl(port->membase + AUART_STAT) & AUART_STAT_TXFF) {
529 if (!to--)
530 break;
531 udelay(1);
532 }
533
534 writel(ch, port->membase + AUART_DATA);
535}
536
537static void
538auart_console_write(struct console *co, const char *str, unsigned int count)
539{
540 struct mxs_auart_port *s;
541 struct uart_port *port;
542 unsigned int old_ctrl0, old_ctrl2;
543 unsigned int to = 1000;
544
545 if (co->index > MXS_AUART_PORTS || co->index < 0)
546 return;
547
548 s = auart_port[co->index];
549 port = &s->port;
550
551 clk_enable(s->clk);
552
553 /* First save the CR then disable the interrupts */
554 old_ctrl2 = readl(port->membase + AUART_CTRL2);
555 old_ctrl0 = readl(port->membase + AUART_CTRL0);
556
557 writel(AUART_CTRL0_CLKGATE,
558 port->membase + AUART_CTRL0_CLR);
559 writel(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE,
560 port->membase + AUART_CTRL2_SET);
561
562 uart_console_write(port, str, count, mxs_auart_console_putchar);
563
564 /*
565 * Finally, wait for transmitter to become empty
566 * and restore the TCR
567 */
568 while (readl(port->membase + AUART_STAT) & AUART_STAT_BUSY) {
569 if (!to--)
570 break;
571 udelay(1);
572 }
573
574 writel(old_ctrl0, port->membase + AUART_CTRL0);
575 writel(old_ctrl2, port->membase + AUART_CTRL2);
576
577 clk_disable(s->clk);
578}
579
580static void __init
581auart_console_get_options(struct uart_port *port, int *baud,
582 int *parity, int *bits)
583{
584 unsigned int lcr_h, quot;
585
586 if (!(readl(port->membase + AUART_CTRL2) & AUART_CTRL2_UARTEN))
587 return;
588
589 lcr_h = readl(port->membase + AUART_LINECTRL);
590
591 *parity = 'n';
592 if (lcr_h & AUART_LINECTRL_PEN) {
593 if (lcr_h & AUART_LINECTRL_EPS)
594 *parity = 'e';
595 else
596 *parity = 'o';
597 }
598
599 if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(2))
600 *bits = 7;
601 else
602 *bits = 8;
603
604 quot = ((readl(port->membase + AUART_LINECTRL)
605 & AUART_LINECTRL_BAUD_DIVINT_MASK))
606 >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
607 quot |= ((readl(port->membase + AUART_LINECTRL)
608 & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
609 >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
610 if (quot == 0)
611 quot = 1;
612
613 *baud = (port->uartclk << 2) / quot;
614}
615
616static int __init
617auart_console_setup(struct console *co, char *options)
618{
619 struct mxs_auart_port *s;
620 int baud = 9600;
621 int bits = 8;
622 int parity = 'n';
623 int flow = 'n';
624 int ret;
625
626 /*
627 * Check whether an invalid uart number has been specified, and
628 * if so, search for the first available port that does have
629 * console support.
630 */
631 if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
632 co->index = 0;
633 s = auart_port[co->index];
634 if (!s)
635 return -ENODEV;
636
637 clk_enable(s->clk);
638
639 if (options)
640 uart_parse_options(options, &baud, &parity, &bits, &flow);
641 else
642 auart_console_get_options(&s->port, &baud, &parity, &bits);
643
644 ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
645
646 clk_disable(s->clk);
647
648 return ret;
649}
650
651static struct console auart_console = {
652 .name = "ttyAPP",
653 .write = auart_console_write,
654 .device = uart_console_device,
655 .setup = auart_console_setup,
656 .flags = CON_PRINTBUFFER,
657 .index = -1,
658 .data = &auart_driver,
659};
660#endif
661
662static struct uart_driver auart_driver = {
663 .owner = THIS_MODULE,
664 .driver_name = "ttyAPP",
665 .dev_name = "ttyAPP",
666 .major = 0,
667 .minor = 0,
668 .nr = MXS_AUART_PORTS,
669#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
670 .cons = &auart_console,
671#endif
672};
673
674static int __devinit mxs_auart_probe(struct platform_device *pdev)
675{
676 struct mxs_auart_port *s;
677 u32 version;
678 int ret = 0;
679 struct resource *r;
680
681 s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL);
682 if (!s) {
683 ret = -ENOMEM;
684 goto out;
685 }
686
687 s->clk = clk_get(&pdev->dev, NULL);
688 if (IS_ERR(s->clk)) {
689 ret = PTR_ERR(s->clk);
690 goto out_free;
691 }
692
693 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
694 if (!r) {
695 ret = -ENXIO;
696 goto out_free_clk;
697 }
698
699 s->port.mapbase = r->start;
700 s->port.membase = ioremap(r->start, resource_size(r));
701 s->port.ops = &mxs_auart_ops;
702 s->port.iotype = UPIO_MEM;
703 s->port.line = pdev->id < 0 ? 0 : pdev->id;
704 s->port.fifosize = 16;
705 s->port.uartclk = clk_get_rate(s->clk);
706 s->port.type = PORT_IMX;
707 s->port.dev = s->dev = get_device(&pdev->dev);
708
709 s->flags = 0;
710 s->ctrl = 0;
711
712 s->irq = platform_get_irq(pdev, 0);
713 s->port.irq = s->irq;
714 ret = request_irq(s->irq, mxs_auart_irq_handle, 0, dev_name(&pdev->dev), s);
715 if (ret)
716 goto out_free_clk;
717
718 platform_set_drvdata(pdev, s);
719
720 auart_port[pdev->id] = s;
721
722 mxs_auart_reset(&s->port);
723
724 ret = uart_add_one_port(&auart_driver, &s->port);
725 if (ret)
726 goto out_free_irq;
727
728 version = readl(s->port.membase + AUART_VERSION);
729 dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
730 (version >> 24) & 0xff,
731 (version >> 16) & 0xff, version & 0xffff);
732
733 return 0;
734
735out_free_irq:
736 auart_port[pdev->id] = NULL;
737 free_irq(s->irq, s);
738out_free_clk:
739 clk_put(s->clk);
740out_free:
741 kfree(s);
742out:
743 return ret;
744}
745
746static int __devexit mxs_auart_remove(struct platform_device *pdev)
747{
748 struct mxs_auart_port *s = platform_get_drvdata(pdev);
749
750 uart_remove_one_port(&auart_driver, &s->port);
751
752 auart_port[pdev->id] = NULL;
753
754 clk_put(s->clk);
755 free_irq(s->irq, s);
756 kfree(s);
757
758 return 0;
759}
760
761static struct platform_driver mxs_auart_driver = {
762 .probe = mxs_auart_probe,
763 .remove = __devexit_p(mxs_auart_remove),
764 .driver = {
765 .name = "mxs-auart",
766 .owner = THIS_MODULE,
767 },
768};
769
770static int __init mxs_auart_init(void)
771{
772 int r;
773
774 r = uart_register_driver(&auart_driver);
775 if (r)
776 goto out;
777
778 r = platform_driver_register(&mxs_auart_driver);
779 if (r)
780 goto out_err;
781
782 return 0;
783out_err:
784 uart_unregister_driver(&auart_driver);
785out:
786 return r;
787}
788
789static void __exit mxs_auart_exit(void)
790{
791 platform_driver_unregister(&mxs_auart_driver);
792 uart_unregister_driver(&auart_driver);
793}
794
795module_init(mxs_auart_init);
796module_exit(mxs_auart_exit);
797MODULE_LICENSE("GPL");
798MODULE_DESCRIPTION("Freescale MXS application uart driver");
diff --git a/drivers/tty/serial/netx-serial.c b/drivers/tty/serial/netx-serial.c
new file mode 100644
index 000000000000..d40da78e7c85
--- /dev/null
+++ b/drivers/tty/serial/netx-serial.c
@@ -0,0 +1,748 @@
1/*
2 * Copyright (c) 2005 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
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 version 2
6 * as published by the Free Software Foundation.
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
18#if defined(CONFIG_SERIAL_NETX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19#define SUPPORT_SYSRQ
20#endif
21
22#include <linux/device.h>
23#include <linux/module.h>
24#include <linux/ioport.h>
25#include <linux/init.h>
26#include <linux/console.h>
27#include <linux/sysrq.h>
28#include <linux/platform_device.h>
29#include <linux/tty.h>
30#include <linux/tty_flip.h>
31#include <linux/serial_core.h>
32#include <linux/serial.h>
33
34#include <asm/io.h>
35#include <asm/irq.h>
36#include <mach/hardware.h>
37#include <mach/netx-regs.h>
38
39/* We've been assigned a range on the "Low-density serial ports" major */
40#define SERIAL_NX_MAJOR 204
41#define MINOR_START 170
42
43enum uart_regs {
44 UART_DR = 0x00,
45 UART_SR = 0x04,
46 UART_LINE_CR = 0x08,
47 UART_BAUDDIV_MSB = 0x0c,
48 UART_BAUDDIV_LSB = 0x10,
49 UART_CR = 0x14,
50 UART_FR = 0x18,
51 UART_IIR = 0x1c,
52 UART_ILPR = 0x20,
53 UART_RTS_CR = 0x24,
54 UART_RTS_LEAD = 0x28,
55 UART_RTS_TRAIL = 0x2c,
56 UART_DRV_ENABLE = 0x30,
57 UART_BRM_CR = 0x34,
58 UART_RXFIFO_IRQLEVEL = 0x38,
59 UART_TXFIFO_IRQLEVEL = 0x3c,
60};
61
62#define SR_FE (1<<0)
63#define SR_PE (1<<1)
64#define SR_BE (1<<2)
65#define SR_OE (1<<3)
66
67#define LINE_CR_BRK (1<<0)
68#define LINE_CR_PEN (1<<1)
69#define LINE_CR_EPS (1<<2)
70#define LINE_CR_STP2 (1<<3)
71#define LINE_CR_FEN (1<<4)
72#define LINE_CR_5BIT (0<<5)
73#define LINE_CR_6BIT (1<<5)
74#define LINE_CR_7BIT (2<<5)
75#define LINE_CR_8BIT (3<<5)
76#define LINE_CR_BITS_MASK (3<<5)
77
78#define CR_UART_EN (1<<0)
79#define CR_SIREN (1<<1)
80#define CR_SIRLP (1<<2)
81#define CR_MSIE (1<<3)
82#define CR_RIE (1<<4)
83#define CR_TIE (1<<5)
84#define CR_RTIE (1<<6)
85#define CR_LBE (1<<7)
86
87#define FR_CTS (1<<0)
88#define FR_DSR (1<<1)
89#define FR_DCD (1<<2)
90#define FR_BUSY (1<<3)
91#define FR_RXFE (1<<4)
92#define FR_TXFF (1<<5)
93#define FR_RXFF (1<<6)
94#define FR_TXFE (1<<7)
95
96#define IIR_MIS (1<<0)
97#define IIR_RIS (1<<1)
98#define IIR_TIS (1<<2)
99#define IIR_RTIS (1<<3)
100#define IIR_MASK 0xf
101
102#define RTS_CR_AUTO (1<<0)
103#define RTS_CR_RTS (1<<1)
104#define RTS_CR_COUNT (1<<2)
105#define RTS_CR_MOD2 (1<<3)
106#define RTS_CR_RTS_POL (1<<4)
107#define RTS_CR_CTS_CTR (1<<5)
108#define RTS_CR_CTS_POL (1<<6)
109#define RTS_CR_STICK (1<<7)
110
111#define UART_PORT_SIZE 0x40
112#define DRIVER_NAME "netx-uart"
113
114struct netx_port {
115 struct uart_port port;
116};
117
118static void netx_stop_tx(struct uart_port *port)
119{
120 unsigned int val;
121 val = readl(port->membase + UART_CR);
122 writel(val & ~CR_TIE, port->membase + UART_CR);
123}
124
125static void netx_stop_rx(struct uart_port *port)
126{
127 unsigned int val;
128 val = readl(port->membase + UART_CR);
129 writel(val & ~CR_RIE, port->membase + UART_CR);
130}
131
132static void netx_enable_ms(struct uart_port *port)
133{
134 unsigned int val;
135 val = readl(port->membase + UART_CR);
136 writel(val | CR_MSIE, port->membase + UART_CR);
137}
138
139static inline void netx_transmit_buffer(struct uart_port *port)
140{
141 struct circ_buf *xmit = &port->state->xmit;
142
143 if (port->x_char) {
144 writel(port->x_char, port->membase + UART_DR);
145 port->icount.tx++;
146 port->x_char = 0;
147 return;
148 }
149
150 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
151 netx_stop_tx(port);
152 return;
153 }
154
155 do {
156 /* send xmit->buf[xmit->tail]
157 * out the port here */
158 writel(xmit->buf[xmit->tail], port->membase + UART_DR);
159 xmit->tail = (xmit->tail + 1) &
160 (UART_XMIT_SIZE - 1);
161 port->icount.tx++;
162 if (uart_circ_empty(xmit))
163 break;
164 } while (!(readl(port->membase + UART_FR) & FR_TXFF));
165
166 if (uart_circ_empty(xmit))
167 netx_stop_tx(port);
168}
169
170static void netx_start_tx(struct uart_port *port)
171{
172 writel(
173 readl(port->membase + UART_CR) | CR_TIE, port->membase + UART_CR);
174
175 if (!(readl(port->membase + UART_FR) & FR_TXFF))
176 netx_transmit_buffer(port);
177}
178
179static unsigned int netx_tx_empty(struct uart_port *port)
180{
181 return readl(port->membase + UART_FR) & FR_BUSY ? 0 : TIOCSER_TEMT;
182}
183
184static void netx_txint(struct uart_port *port)
185{
186 struct circ_buf *xmit = &port->state->xmit;
187
188 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
189 netx_stop_tx(port);
190 return;
191 }
192
193 netx_transmit_buffer(port);
194
195 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
196 uart_write_wakeup(port);
197}
198
199static void netx_rxint(struct uart_port *port)
200{
201 unsigned char rx, flg, status;
202 struct tty_struct *tty = port->state->port.tty;
203
204 while (!(readl(port->membase + UART_FR) & FR_RXFE)) {
205 rx = readl(port->membase + UART_DR);
206 flg = TTY_NORMAL;
207 port->icount.rx++;
208 status = readl(port->membase + UART_SR);
209 if (status & SR_BE) {
210 writel(0, port->membase + UART_SR);
211 if (uart_handle_break(port))
212 continue;
213 }
214
215 if (unlikely(status & (SR_FE | SR_PE | SR_OE))) {
216
217 if (status & SR_PE)
218 port->icount.parity++;
219 else if (status & SR_FE)
220 port->icount.frame++;
221 if (status & SR_OE)
222 port->icount.overrun++;
223
224 status &= port->read_status_mask;
225
226 if (status & SR_BE)
227 flg = TTY_BREAK;
228 else if (status & SR_PE)
229 flg = TTY_PARITY;
230 else if (status & SR_FE)
231 flg = TTY_FRAME;
232 }
233
234 if (uart_handle_sysrq_char(port, rx))
235 continue;
236
237 uart_insert_char(port, status, SR_OE, rx, flg);
238 }
239
240 tty_flip_buffer_push(tty);
241 return;
242}
243
244static irqreturn_t netx_int(int irq, void *dev_id)
245{
246 struct uart_port *port = dev_id;
247 unsigned long flags;
248 unsigned char status;
249
250 spin_lock_irqsave(&port->lock,flags);
251
252 status = readl(port->membase + UART_IIR) & IIR_MASK;
253 while (status) {
254 if (status & IIR_RIS)
255 netx_rxint(port);
256 if (status & IIR_TIS)
257 netx_txint(port);
258 if (status & IIR_MIS) {
259 if (readl(port->membase + UART_FR) & FR_CTS)
260 uart_handle_cts_change(port, 1);
261 else
262 uart_handle_cts_change(port, 0);
263 }
264 writel(0, port->membase + UART_IIR);
265 status = readl(port->membase + UART_IIR) & IIR_MASK;
266 }
267
268 spin_unlock_irqrestore(&port->lock,flags);
269 return IRQ_HANDLED;
270}
271
272static unsigned int netx_get_mctrl(struct uart_port *port)
273{
274 unsigned int ret = TIOCM_DSR | TIOCM_CAR;
275
276 if (readl(port->membase + UART_FR) & FR_CTS)
277 ret |= TIOCM_CTS;
278
279 return ret;
280}
281
282static void netx_set_mctrl(struct uart_port *port, unsigned int mctrl)
283{
284 unsigned int val;
285
286 /* FIXME: Locking needed ? */
287 if (mctrl & TIOCM_RTS) {
288 val = readl(port->membase + UART_RTS_CR);
289 writel(val | RTS_CR_RTS, port->membase + UART_RTS_CR);
290 }
291}
292
293static void netx_break_ctl(struct uart_port *port, int break_state)
294{
295 unsigned int line_cr;
296 spin_lock_irq(&port->lock);
297
298 line_cr = readl(port->membase + UART_LINE_CR);
299 if (break_state != 0)
300 line_cr |= LINE_CR_BRK;
301 else
302 line_cr &= ~LINE_CR_BRK;
303 writel(line_cr, port->membase + UART_LINE_CR);
304
305 spin_unlock_irq(&port->lock);
306}
307
308static int netx_startup(struct uart_port *port)
309{
310 int ret;
311
312 ret = request_irq(port->irq, netx_int, 0,
313 DRIVER_NAME, port);
314 if (ret) {
315 dev_err(port->dev, "unable to grab irq%d\n",port->irq);
316 goto exit;
317 }
318
319 writel(readl(port->membase + UART_LINE_CR) | LINE_CR_FEN,
320 port->membase + UART_LINE_CR);
321
322 writel(CR_MSIE | CR_RIE | CR_TIE | CR_RTIE | CR_UART_EN,
323 port->membase + UART_CR);
324
325exit:
326 return ret;
327}
328
329static void netx_shutdown(struct uart_port *port)
330{
331 writel(0, port->membase + UART_CR) ;
332
333 free_irq(port->irq, port);
334}
335
336static void
337netx_set_termios(struct uart_port *port, struct ktermios *termios,
338 struct ktermios *old)
339{
340 unsigned int baud, quot;
341 unsigned char old_cr;
342 unsigned char line_cr = LINE_CR_FEN;
343 unsigned char rts_cr = 0;
344
345 switch (termios->c_cflag & CSIZE) {
346 case CS5:
347 line_cr |= LINE_CR_5BIT;
348 break;
349 case CS6:
350 line_cr |= LINE_CR_6BIT;
351 break;
352 case CS7:
353 line_cr |= LINE_CR_7BIT;
354 break;
355 case CS8:
356 line_cr |= LINE_CR_8BIT;
357 break;
358 }
359
360 if (termios->c_cflag & CSTOPB)
361 line_cr |= LINE_CR_STP2;
362
363 if (termios->c_cflag & PARENB) {
364 line_cr |= LINE_CR_PEN;
365 if (!(termios->c_cflag & PARODD))
366 line_cr |= LINE_CR_EPS;
367 }
368
369 if (termios->c_cflag & CRTSCTS)
370 rts_cr = RTS_CR_AUTO | RTS_CR_CTS_CTR | RTS_CR_RTS_POL;
371
372 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
373 quot = baud * 4096;
374 quot /= 1000;
375 quot *= 256;
376 quot /= 100000;
377
378 spin_lock_irq(&port->lock);
379
380 uart_update_timeout(port, termios->c_cflag, baud);
381
382 old_cr = readl(port->membase + UART_CR);
383
384 /* disable interrupts */
385 writel(old_cr & ~(CR_MSIE | CR_RIE | CR_TIE | CR_RTIE),
386 port->membase + UART_CR);
387
388 /* drain transmitter */
389 while (readl(port->membase + UART_FR) & FR_BUSY);
390
391 /* disable UART */
392 writel(old_cr & ~CR_UART_EN, port->membase + UART_CR);
393
394 /* modem status interrupts */
395 old_cr &= ~CR_MSIE;
396 if (UART_ENABLE_MS(port, termios->c_cflag))
397 old_cr |= CR_MSIE;
398
399 writel((quot>>8) & 0xff, port->membase + UART_BAUDDIV_MSB);
400 writel(quot & 0xff, port->membase + UART_BAUDDIV_LSB);
401 writel(line_cr, port->membase + UART_LINE_CR);
402
403 writel(rts_cr, port->membase + UART_RTS_CR);
404
405 /*
406 * Characters to ignore
407 */
408 port->ignore_status_mask = 0;
409 if (termios->c_iflag & IGNPAR)
410 port->ignore_status_mask |= SR_PE;
411 if (termios->c_iflag & IGNBRK) {
412 port->ignore_status_mask |= SR_BE;
413 /*
414 * If we're ignoring parity and break indicators,
415 * ignore overruns too (for real raw support).
416 */
417 if (termios->c_iflag & IGNPAR)
418 port->ignore_status_mask |= SR_PE;
419 }
420
421 port->read_status_mask = 0;
422 if (termios->c_iflag & (BRKINT | PARMRK))
423 port->read_status_mask |= SR_BE;
424 if (termios->c_iflag & INPCK)
425 port->read_status_mask |= SR_PE | SR_FE;
426
427 writel(old_cr, port->membase + UART_CR);
428
429 spin_unlock_irq(&port->lock);
430}
431
432static const char *netx_type(struct uart_port *port)
433{
434 return port->type == PORT_NETX ? "NETX" : NULL;
435}
436
437static void netx_release_port(struct uart_port *port)
438{
439 release_mem_region(port->mapbase, UART_PORT_SIZE);
440}
441
442static int netx_request_port(struct uart_port *port)
443{
444 return request_mem_region(port->mapbase, UART_PORT_SIZE,
445 DRIVER_NAME) != NULL ? 0 : -EBUSY;
446}
447
448static void netx_config_port(struct uart_port *port, int flags)
449{
450 if (flags & UART_CONFIG_TYPE && netx_request_port(port) == 0)
451 port->type = PORT_NETX;
452}
453
454static int
455netx_verify_port(struct uart_port *port, struct serial_struct *ser)
456{
457 int ret = 0;
458
459 if (ser->type != PORT_UNKNOWN && ser->type != PORT_NETX)
460 ret = -EINVAL;
461
462 return ret;
463}
464
465static struct uart_ops netx_pops = {
466 .tx_empty = netx_tx_empty,
467 .set_mctrl = netx_set_mctrl,
468 .get_mctrl = netx_get_mctrl,
469 .stop_tx = netx_stop_tx,
470 .start_tx = netx_start_tx,
471 .stop_rx = netx_stop_rx,
472 .enable_ms = netx_enable_ms,
473 .break_ctl = netx_break_ctl,
474 .startup = netx_startup,
475 .shutdown = netx_shutdown,
476 .set_termios = netx_set_termios,
477 .type = netx_type,
478 .release_port = netx_release_port,
479 .request_port = netx_request_port,
480 .config_port = netx_config_port,
481 .verify_port = netx_verify_port,
482};
483
484static struct netx_port netx_ports[] = {
485 {
486 .port = {
487 .type = PORT_NETX,
488 .iotype = UPIO_MEM,
489 .membase = (char __iomem *)io_p2v(NETX_PA_UART0),
490 .mapbase = NETX_PA_UART0,
491 .irq = NETX_IRQ_UART0,
492 .uartclk = 100000000,
493 .fifosize = 16,
494 .flags = UPF_BOOT_AUTOCONF,
495 .ops = &netx_pops,
496 .line = 0,
497 },
498 }, {
499 .port = {
500 .type = PORT_NETX,
501 .iotype = UPIO_MEM,
502 .membase = (char __iomem *)io_p2v(NETX_PA_UART1),
503 .mapbase = NETX_PA_UART1,
504 .irq = NETX_IRQ_UART1,
505 .uartclk = 100000000,
506 .fifosize = 16,
507 .flags = UPF_BOOT_AUTOCONF,
508 .ops = &netx_pops,
509 .line = 1,
510 },
511 }, {
512 .port = {
513 .type = PORT_NETX,
514 .iotype = UPIO_MEM,
515 .membase = (char __iomem *)io_p2v(NETX_PA_UART2),
516 .mapbase = NETX_PA_UART2,
517 .irq = NETX_IRQ_UART2,
518 .uartclk = 100000000,
519 .fifosize = 16,
520 .flags = UPF_BOOT_AUTOCONF,
521 .ops = &netx_pops,
522 .line = 2,
523 },
524 }
525};
526
527#ifdef CONFIG_SERIAL_NETX_CONSOLE
528
529static void netx_console_putchar(struct uart_port *port, int ch)
530{
531 while (readl(port->membase + UART_FR) & FR_BUSY);
532 writel(ch, port->membase + UART_DR);
533}
534
535static void
536netx_console_write(struct console *co, const char *s, unsigned int count)
537{
538 struct uart_port *port = &netx_ports[co->index].port;
539 unsigned char cr_save;
540
541 cr_save = readl(port->membase + UART_CR);
542 writel(cr_save | CR_UART_EN, port->membase + UART_CR);
543
544 uart_console_write(port, s, count, netx_console_putchar);
545
546 while (readl(port->membase + UART_FR) & FR_BUSY);
547 writel(cr_save, port->membase + UART_CR);
548}
549
550static void __init
551netx_console_get_options(struct uart_port *port, int *baud,
552 int *parity, int *bits, int *flow)
553{
554 unsigned char line_cr;
555
556 *baud = (readl(port->membase + UART_BAUDDIV_MSB) << 8) |
557 readl(port->membase + UART_BAUDDIV_LSB);
558 *baud *= 1000;
559 *baud /= 4096;
560 *baud *= 1000;
561 *baud /= 256;
562 *baud *= 100;
563
564 line_cr = readl(port->membase + UART_LINE_CR);
565 *parity = 'n';
566 if (line_cr & LINE_CR_PEN) {
567 if (line_cr & LINE_CR_EPS)
568 *parity = 'e';
569 else
570 *parity = 'o';
571 }
572
573 switch (line_cr & LINE_CR_BITS_MASK) {
574 case LINE_CR_8BIT:
575 *bits = 8;
576 break;
577 case LINE_CR_7BIT:
578 *bits = 7;
579 break;
580 case LINE_CR_6BIT:
581 *bits = 6;
582 break;
583 case LINE_CR_5BIT:
584 *bits = 5;
585 break;
586 }
587
588 if (readl(port->membase + UART_RTS_CR) & RTS_CR_AUTO)
589 *flow = 'r';
590}
591
592static int __init
593netx_console_setup(struct console *co, char *options)
594{
595 struct netx_port *sport;
596 int baud = 9600;
597 int bits = 8;
598 int parity = 'n';
599 int flow = 'n';
600
601 /*
602 * Check whether an invalid uart number has been specified, and
603 * if so, search for the first available port that does have
604 * console support.
605 */
606 if (co->index == -1 || co->index >= ARRAY_SIZE(netx_ports))
607 co->index = 0;
608 sport = &netx_ports[co->index];
609
610 if (options) {
611 uart_parse_options(options, &baud, &parity, &bits, &flow);
612 } else {
613 /* if the UART is enabled, assume it has been correctly setup
614 * by the bootloader and get the options
615 */
616 if (readl(sport->port.membase + UART_CR) & CR_UART_EN) {
617 netx_console_get_options(&sport->port, &baud,
618 &parity, &bits, &flow);
619 }
620
621 }
622
623 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
624}
625
626static struct uart_driver netx_reg;
627static struct console netx_console = {
628 .name = "ttyNX",
629 .write = netx_console_write,
630 .device = uart_console_device,
631 .setup = netx_console_setup,
632 .flags = CON_PRINTBUFFER,
633 .index = -1,
634 .data = &netx_reg,
635};
636
637static int __init netx_console_init(void)
638{
639 register_console(&netx_console);
640 return 0;
641}
642console_initcall(netx_console_init);
643
644#define NETX_CONSOLE &netx_console
645#else
646#define NETX_CONSOLE NULL
647#endif
648
649static struct uart_driver netx_reg = {
650 .owner = THIS_MODULE,
651 .driver_name = DRIVER_NAME,
652 .dev_name = "ttyNX",
653 .major = SERIAL_NX_MAJOR,
654 .minor = MINOR_START,
655 .nr = ARRAY_SIZE(netx_ports),
656 .cons = NETX_CONSOLE,
657};
658
659static int serial_netx_suspend(struct platform_device *pdev, pm_message_t state)
660{
661 struct netx_port *sport = platform_get_drvdata(pdev);
662
663 if (sport)
664 uart_suspend_port(&netx_reg, &sport->port);
665
666 return 0;
667}
668
669static int serial_netx_resume(struct platform_device *pdev)
670{
671 struct netx_port *sport = platform_get_drvdata(pdev);
672
673 if (sport)
674 uart_resume_port(&netx_reg, &sport->port);
675
676 return 0;
677}
678
679static int serial_netx_probe(struct platform_device *pdev)
680{
681 struct uart_port *port = &netx_ports[pdev->id].port;
682
683 dev_info(&pdev->dev, "initialising\n");
684
685 port->dev = &pdev->dev;
686
687 writel(1, port->membase + UART_RXFIFO_IRQLEVEL);
688 uart_add_one_port(&netx_reg, &netx_ports[pdev->id].port);
689 platform_set_drvdata(pdev, &netx_ports[pdev->id]);
690
691 return 0;
692}
693
694static int serial_netx_remove(struct platform_device *pdev)
695{
696 struct netx_port *sport = platform_get_drvdata(pdev);
697
698 platform_set_drvdata(pdev, NULL);
699
700 if (sport)
701 uart_remove_one_port(&netx_reg, &sport->port);
702
703 return 0;
704}
705
706static struct platform_driver serial_netx_driver = {
707 .probe = serial_netx_probe,
708 .remove = serial_netx_remove,
709
710 .suspend = serial_netx_suspend,
711 .resume = serial_netx_resume,
712
713 .driver = {
714 .name = DRIVER_NAME,
715 .owner = THIS_MODULE,
716 },
717};
718
719static int __init netx_serial_init(void)
720{
721 int ret;
722
723 printk(KERN_INFO "Serial: NetX driver\n");
724
725 ret = uart_register_driver(&netx_reg);
726 if (ret)
727 return ret;
728
729 ret = platform_driver_register(&serial_netx_driver);
730 if (ret != 0)
731 uart_unregister_driver(&netx_reg);
732
733 return 0;
734}
735
736static void __exit netx_serial_exit(void)
737{
738 platform_driver_unregister(&serial_netx_driver);
739 uart_unregister_driver(&netx_reg);
740}
741
742module_init(netx_serial_init);
743module_exit(netx_serial_exit);
744
745MODULE_AUTHOR("Sascha Hauer");
746MODULE_DESCRIPTION("NetX serial port driver");
747MODULE_LICENSE("GPL");
748MODULE_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..c911b2419abb
--- /dev/null
+++ b/drivers/tty/serial/of_serial.c
@@ -0,0 +1,206 @@
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 struct of_device_id of_platform_serial_table[];
84static int __devinit of_platform_serial_probe(struct platform_device *ofdev)
85{
86 const struct of_device_id *match;
87 struct of_serial_info *info;
88 struct uart_port port;
89 int port_type;
90 int ret;
91
92 match = of_match_device(of_platform_serial_table, &ofdev->dev);
93 if (!match)
94 return -EINVAL;
95
96 if (of_find_property(ofdev->dev.of_node, "used-by-rtas", NULL))
97 return -EBUSY;
98
99 info = kmalloc(sizeof(*info), GFP_KERNEL);
100 if (info == NULL)
101 return -ENOMEM;
102
103 port_type = (unsigned long)match->data;
104 ret = of_platform_serial_setup(ofdev, port_type, &port);
105 if (ret)
106 goto out;
107
108 switch (port_type) {
109#ifdef CONFIG_SERIAL_8250
110 case PORT_8250 ... PORT_MAX_8250:
111 ret = serial8250_register_port(&port);
112 break;
113#endif
114#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
115 case PORT_NWPSERIAL:
116 ret = nwpserial_register_port(&port);
117 break;
118#endif
119 default:
120 /* need to add code for these */
121 case PORT_UNKNOWN:
122 dev_info(&ofdev->dev, "Unknown serial port found, ignored\n");
123 ret = -ENODEV;
124 break;
125 }
126 if (ret < 0)
127 goto out;
128
129 info->type = port_type;
130 info->line = ret;
131 dev_set_drvdata(&ofdev->dev, info);
132 return 0;
133out:
134 kfree(info);
135 irq_dispose_mapping(port.irq);
136 return ret;
137}
138
139/*
140 * Release a line
141 */
142static int of_platform_serial_remove(struct platform_device *ofdev)
143{
144 struct of_serial_info *info = dev_get_drvdata(&ofdev->dev);
145 switch (info->type) {
146#ifdef CONFIG_SERIAL_8250
147 case PORT_8250 ... PORT_MAX_8250:
148 serial8250_unregister_port(info->line);
149 break;
150#endif
151#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
152 case PORT_NWPSERIAL:
153 nwpserial_unregister_port(info->line);
154 break;
155#endif
156 default:
157 /* need to add code for these */
158 break;
159 }
160 kfree(info);
161 return 0;
162}
163
164/*
165 * A few common types, add more as needed.
166 */
167static struct of_device_id __devinitdata of_platform_serial_table[] = {
168 { .compatible = "ns8250", .data = (void *)PORT_8250, },
169 { .compatible = "ns16450", .data = (void *)PORT_16450, },
170 { .compatible = "ns16550a", .data = (void *)PORT_16550A, },
171 { .compatible = "ns16550", .data = (void *)PORT_16550, },
172 { .compatible = "ns16750", .data = (void *)PORT_16750, },
173 { .compatible = "ns16850", .data = (void *)PORT_16850, },
174#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL
175 { .compatible = "ibm,qpace-nwp-serial",
176 .data = (void *)PORT_NWPSERIAL, },
177#endif
178 { .type = "serial", .data = (void *)PORT_UNKNOWN, },
179 { /* end of list */ },
180};
181
182static struct platform_driver of_platform_serial_driver = {
183 .driver = {
184 .name = "of_serial",
185 .owner = THIS_MODULE,
186 .of_match_table = of_platform_serial_table,
187 },
188 .probe = of_platform_serial_probe,
189 .remove = of_platform_serial_remove,
190};
191
192static int __init of_platform_serial_init(void)
193{
194 return platform_driver_register(&of_platform_serial_driver);
195}
196module_init(of_platform_serial_init);
197
198static void __exit of_platform_serial_exit(void)
199{
200 return platform_driver_unregister(&of_platform_serial_driver);
201};
202module_exit(of_platform_serial_exit);
203
204MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
205MODULE_LICENSE("GPL");
206MODULE_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..47cadf474149
--- /dev/null
+++ b/drivers/tty/serial/omap-serial.c
@@ -0,0 +1,1362 @@
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 separate 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#if defined(CONFIG_SERIAL_OMAP_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
24#define SUPPORT_SYSRQ
25#endif
26
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/console.h>
30#include <linux/serial_reg.h>
31#include <linux/delay.h>
32#include <linux/slab.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/io.h>
36#include <linux/dma-mapping.h>
37#include <linux/clk.h>
38#include <linux/serial_core.h>
39#include <linux/irq.h>
40
41#include <plat/dma.h>
42#include <plat/dmtimer.h>
43#include <plat/omap-serial.h>
44
45static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
46
47/* Forward declaration of functions */
48static void uart_tx_dma_callback(int lch, u16 ch_status, void *data);
49static void serial_omap_rx_timeout(unsigned long uart_no);
50static int serial_omap_start_rxdma(struct uart_omap_port *up);
51
52static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
53{
54 offset <<= up->port.regshift;
55 return readw(up->port.membase + offset);
56}
57
58static inline void serial_out(struct uart_omap_port *up, int offset, int value)
59{
60 offset <<= up->port.regshift;
61 writew(value, up->port.membase + offset);
62}
63
64static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
65{
66 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
67 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
68 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
69 serial_out(up, UART_FCR, 0);
70}
71
72/*
73 * serial_omap_get_divisor - calculate divisor value
74 * @port: uart port info
75 * @baud: baudrate for which divisor needs to be calculated.
76 *
77 * We have written our own function to get the divisor so as to support
78 * 13x mode. 3Mbps Baudrate as an different divisor.
79 * Reference OMAP TRM Chapter 17:
80 * Table 17-1. UART Mode Baud Rates, Divisor Values, and Error Rates
81 * referring to oversampling - divisor value
82 * baudrate 460,800 to 3,686,400 all have divisor 13
83 * except 3,000,000 which has divisor value 16
84 */
85static unsigned int
86serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
87{
88 unsigned int divisor;
89
90 if (baud > OMAP_MODE13X_SPEED && baud != 3000000)
91 divisor = 13;
92 else
93 divisor = 16;
94 return port->uartclk/(baud * divisor);
95}
96
97static void serial_omap_stop_rxdma(struct uart_omap_port *up)
98{
99 if (up->uart_dma.rx_dma_used) {
100 del_timer(&up->uart_dma.rx_timer);
101 omap_stop_dma(up->uart_dma.rx_dma_channel);
102 omap_free_dma(up->uart_dma.rx_dma_channel);
103 up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
104 up->uart_dma.rx_dma_used = false;
105 }
106}
107
108static void serial_omap_enable_ms(struct uart_port *port)
109{
110 struct uart_omap_port *up = (struct uart_omap_port *)port;
111
112 dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->pdev->id);
113 up->ier |= UART_IER_MSI;
114 serial_out(up, UART_IER, up->ier);
115}
116
117static void serial_omap_stop_tx(struct uart_port *port)
118{
119 struct uart_omap_port *up = (struct uart_omap_port *)port;
120
121 if (up->use_dma &&
122 up->uart_dma.tx_dma_channel != OMAP_UART_DMA_CH_FREE) {
123 /*
124 * Check if dma is still active. If yes do nothing,
125 * return. Else stop dma
126 */
127 if (omap_get_dma_active_status(up->uart_dma.tx_dma_channel))
128 return;
129 omap_stop_dma(up->uart_dma.tx_dma_channel);
130 omap_free_dma(up->uart_dma.tx_dma_channel);
131 up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
132 }
133
134 if (up->ier & UART_IER_THRI) {
135 up->ier &= ~UART_IER_THRI;
136 serial_out(up, UART_IER, up->ier);
137 }
138}
139
140static void serial_omap_stop_rx(struct uart_port *port)
141{
142 struct uart_omap_port *up = (struct uart_omap_port *)port;
143
144 if (up->use_dma)
145 serial_omap_stop_rxdma(up);
146 up->ier &= ~UART_IER_RLSI;
147 up->port.read_status_mask &= ~UART_LSR_DR;
148 serial_out(up, UART_IER, up->ier);
149}
150
151static inline void receive_chars(struct uart_omap_port *up, int *status)
152{
153 struct tty_struct *tty = up->port.state->port.tty;
154 unsigned int flag;
155 unsigned char ch, lsr = *status;
156 int max_count = 256;
157
158 do {
159 if (likely(lsr & UART_LSR_DR))
160 ch = serial_in(up, UART_RX);
161 flag = TTY_NORMAL;
162 up->port.icount.rx++;
163
164 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
165 /*
166 * For statistics only
167 */
168 if (lsr & UART_LSR_BI) {
169 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
170 up->port.icount.brk++;
171 /*
172 * We do the SysRQ and SAK checking
173 * here because otherwise the break
174 * may get masked by ignore_status_mask
175 * or read_status_mask.
176 */
177 if (uart_handle_break(&up->port))
178 goto ignore_char;
179 } else if (lsr & UART_LSR_PE) {
180 up->port.icount.parity++;
181 } else if (lsr & UART_LSR_FE) {
182 up->port.icount.frame++;
183 }
184
185 if (lsr & UART_LSR_OE)
186 up->port.icount.overrun++;
187
188 /*
189 * Mask off conditions which should be ignored.
190 */
191 lsr &= up->port.read_status_mask;
192
193#ifdef CONFIG_SERIAL_OMAP_CONSOLE
194 if (up->port.line == up->port.cons->index) {
195 /* Recover the break flag from console xmit */
196 lsr |= up->lsr_break_flag;
197 }
198#endif
199 if (lsr & UART_LSR_BI)
200 flag = TTY_BREAK;
201 else if (lsr & UART_LSR_PE)
202 flag = TTY_PARITY;
203 else if (lsr & UART_LSR_FE)
204 flag = TTY_FRAME;
205 }
206
207 if (uart_handle_sysrq_char(&up->port, ch))
208 goto ignore_char;
209 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
210ignore_char:
211 lsr = serial_in(up, UART_LSR);
212 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
213 spin_unlock(&up->port.lock);
214 tty_flip_buffer_push(tty);
215 spin_lock(&up->port.lock);
216}
217
218static void transmit_chars(struct uart_omap_port *up)
219{
220 struct circ_buf *xmit = &up->port.state->xmit;
221 int count;
222
223 if (up->port.x_char) {
224 serial_out(up, UART_TX, up->port.x_char);
225 up->port.icount.tx++;
226 up->port.x_char = 0;
227 return;
228 }
229 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
230 serial_omap_stop_tx(&up->port);
231 return;
232 }
233 count = up->port.fifosize / 4;
234 do {
235 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
236 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
237 up->port.icount.tx++;
238 if (uart_circ_empty(xmit))
239 break;
240 } while (--count > 0);
241
242 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
243 uart_write_wakeup(&up->port);
244
245 if (uart_circ_empty(xmit))
246 serial_omap_stop_tx(&up->port);
247}
248
249static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
250{
251 if (!(up->ier & UART_IER_THRI)) {
252 up->ier |= UART_IER_THRI;
253 serial_out(up, UART_IER, up->ier);
254 }
255}
256
257static void serial_omap_start_tx(struct uart_port *port)
258{
259 struct uart_omap_port *up = (struct uart_omap_port *)port;
260 struct circ_buf *xmit;
261 unsigned int start;
262 int ret = 0;
263
264 if (!up->use_dma) {
265 serial_omap_enable_ier_thri(up);
266 return;
267 }
268
269 if (up->uart_dma.tx_dma_used)
270 return;
271
272 xmit = &up->port.state->xmit;
273
274 if (up->uart_dma.tx_dma_channel == OMAP_UART_DMA_CH_FREE) {
275 ret = omap_request_dma(up->uart_dma.uart_dma_tx,
276 "UART Tx DMA",
277 (void *)uart_tx_dma_callback, up,
278 &(up->uart_dma.tx_dma_channel));
279
280 if (ret < 0) {
281 serial_omap_enable_ier_thri(up);
282 return;
283 }
284 }
285 spin_lock(&(up->uart_dma.tx_lock));
286 up->uart_dma.tx_dma_used = true;
287 spin_unlock(&(up->uart_dma.tx_lock));
288
289 start = up->uart_dma.tx_buf_dma_phys +
290 (xmit->tail & (UART_XMIT_SIZE - 1));
291
292 up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
293 /*
294 * It is a circular buffer. See if the buffer has wounded back.
295 * If yes it will have to be transferred in two separate dma
296 * transfers
297 */
298 if (start + up->uart_dma.tx_buf_size >=
299 up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
300 up->uart_dma.tx_buf_size =
301 (up->uart_dma.tx_buf_dma_phys +
302 UART_XMIT_SIZE) - start;
303
304 omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
305 OMAP_DMA_AMODE_CONSTANT,
306 up->uart_dma.uart_base, 0, 0);
307 omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
308 OMAP_DMA_AMODE_POST_INC, start, 0, 0);
309 omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
310 OMAP_DMA_DATA_TYPE_S8,
311 up->uart_dma.tx_buf_size, 1,
312 OMAP_DMA_SYNC_ELEMENT,
313 up->uart_dma.uart_dma_tx, 0);
314 /* FIXME: Cache maintenance needed here? */
315 omap_start_dma(up->uart_dma.tx_dma_channel);
316}
317
318static unsigned int check_modem_status(struct uart_omap_port *up)
319{
320 unsigned int status;
321
322 status = serial_in(up, UART_MSR);
323 status |= up->msr_saved_flags;
324 up->msr_saved_flags = 0;
325 if ((status & UART_MSR_ANY_DELTA) == 0)
326 return status;
327
328 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
329 up->port.state != NULL) {
330 if (status & UART_MSR_TERI)
331 up->port.icount.rng++;
332 if (status & UART_MSR_DDSR)
333 up->port.icount.dsr++;
334 if (status & UART_MSR_DDCD)
335 uart_handle_dcd_change
336 (&up->port, status & UART_MSR_DCD);
337 if (status & UART_MSR_DCTS)
338 uart_handle_cts_change
339 (&up->port, status & UART_MSR_CTS);
340 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
341 }
342
343 return status;
344}
345
346/**
347 * serial_omap_irq() - This handles the interrupt from one port
348 * @irq: uart port irq number
349 * @dev_id: uart port info
350 */
351static inline irqreturn_t serial_omap_irq(int irq, void *dev_id)
352{
353 struct uart_omap_port *up = dev_id;
354 unsigned int iir, lsr;
355 unsigned long flags;
356
357 iir = serial_in(up, UART_IIR);
358 if (iir & UART_IIR_NO_INT)
359 return IRQ_NONE;
360
361 spin_lock_irqsave(&up->port.lock, flags);
362 lsr = serial_in(up, UART_LSR);
363 if (iir & UART_IIR_RLSI) {
364 if (!up->use_dma) {
365 if (lsr & UART_LSR_DR)
366 receive_chars(up, &lsr);
367 } else {
368 up->ier &= ~(UART_IER_RDI | UART_IER_RLSI);
369 serial_out(up, UART_IER, up->ier);
370 if ((serial_omap_start_rxdma(up) != 0) &&
371 (lsr & UART_LSR_DR))
372 receive_chars(up, &lsr);
373 }
374 }
375
376 check_modem_status(up);
377 if ((lsr & UART_LSR_THRE) && (iir & UART_IIR_THRI))
378 transmit_chars(up);
379
380 spin_unlock_irqrestore(&up->port.lock, flags);
381 up->port_activity = jiffies;
382 return IRQ_HANDLED;
383}
384
385static unsigned int serial_omap_tx_empty(struct uart_port *port)
386{
387 struct uart_omap_port *up = (struct uart_omap_port *)port;
388 unsigned long flags = 0;
389 unsigned int ret = 0;
390
391 dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->pdev->id);
392 spin_lock_irqsave(&up->port.lock, flags);
393 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
394 spin_unlock_irqrestore(&up->port.lock, flags);
395
396 return ret;
397}
398
399static unsigned int serial_omap_get_mctrl(struct uart_port *port)
400{
401 struct uart_omap_port *up = (struct uart_omap_port *)port;
402 unsigned char status;
403 unsigned int ret = 0;
404
405 status = check_modem_status(up);
406 dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->pdev->id);
407
408 if (status & UART_MSR_DCD)
409 ret |= TIOCM_CAR;
410 if (status & UART_MSR_RI)
411 ret |= TIOCM_RNG;
412 if (status & UART_MSR_DSR)
413 ret |= TIOCM_DSR;
414 if (status & UART_MSR_CTS)
415 ret |= TIOCM_CTS;
416 return ret;
417}
418
419static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
420{
421 struct uart_omap_port *up = (struct uart_omap_port *)port;
422 unsigned char mcr = 0;
423
424 dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->pdev->id);
425 if (mctrl & TIOCM_RTS)
426 mcr |= UART_MCR_RTS;
427 if (mctrl & TIOCM_DTR)
428 mcr |= UART_MCR_DTR;
429 if (mctrl & TIOCM_OUT1)
430 mcr |= UART_MCR_OUT1;
431 if (mctrl & TIOCM_OUT2)
432 mcr |= UART_MCR_OUT2;
433 if (mctrl & TIOCM_LOOP)
434 mcr |= UART_MCR_LOOP;
435
436 mcr |= up->mcr;
437 serial_out(up, UART_MCR, mcr);
438}
439
440static void serial_omap_break_ctl(struct uart_port *port, int break_state)
441{
442 struct uart_omap_port *up = (struct uart_omap_port *)port;
443 unsigned long flags = 0;
444
445 dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->pdev->id);
446 spin_lock_irqsave(&up->port.lock, flags);
447 if (break_state == -1)
448 up->lcr |= UART_LCR_SBC;
449 else
450 up->lcr &= ~UART_LCR_SBC;
451 serial_out(up, UART_LCR, up->lcr);
452 spin_unlock_irqrestore(&up->port.lock, flags);
453}
454
455static int serial_omap_startup(struct uart_port *port)
456{
457 struct uart_omap_port *up = (struct uart_omap_port *)port;
458 unsigned long flags = 0;
459 int retval;
460
461 /*
462 * Allocate the IRQ
463 */
464 retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
465 up->name, up);
466 if (retval)
467 return retval;
468
469 dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->pdev->id);
470
471 /*
472 * Clear the FIFO buffers and disable them.
473 * (they will be reenabled in set_termios())
474 */
475 serial_omap_clear_fifos(up);
476 /* For Hardware flow control */
477 serial_out(up, UART_MCR, UART_MCR_RTS);
478
479 /*
480 * Clear the interrupt registers.
481 */
482 (void) serial_in(up, UART_LSR);
483 if (serial_in(up, UART_LSR) & UART_LSR_DR)
484 (void) serial_in(up, UART_RX);
485 (void) serial_in(up, UART_IIR);
486 (void) serial_in(up, UART_MSR);
487
488 /*
489 * Now, initialize the UART
490 */
491 serial_out(up, UART_LCR, UART_LCR_WLEN8);
492 spin_lock_irqsave(&up->port.lock, flags);
493 /*
494 * Most PC uarts need OUT2 raised to enable interrupts.
495 */
496 up->port.mctrl |= TIOCM_OUT2;
497 serial_omap_set_mctrl(&up->port, up->port.mctrl);
498 spin_unlock_irqrestore(&up->port.lock, flags);
499
500 up->msr_saved_flags = 0;
501 if (up->use_dma) {
502 free_page((unsigned long)up->port.state->xmit.buf);
503 up->port.state->xmit.buf = dma_alloc_coherent(NULL,
504 UART_XMIT_SIZE,
505 (dma_addr_t *)&(up->uart_dma.tx_buf_dma_phys),
506 0);
507 init_timer(&(up->uart_dma.rx_timer));
508 up->uart_dma.rx_timer.function = serial_omap_rx_timeout;
509 up->uart_dma.rx_timer.data = up->pdev->id;
510 /* Currently the buffer size is 4KB. Can increase it */
511 up->uart_dma.rx_buf = dma_alloc_coherent(NULL,
512 up->uart_dma.rx_buf_size,
513 (dma_addr_t *)&(up->uart_dma.rx_buf_dma_phys), 0);
514 }
515 /*
516 * Finally, enable interrupts. Note: Modem status interrupts
517 * are set via set_termios(), which will be occurring imminently
518 * anyway, so we don't enable them here.
519 */
520 up->ier = UART_IER_RLSI | UART_IER_RDI;
521 serial_out(up, UART_IER, up->ier);
522
523 /* Enable module level wake up */
524 serial_out(up, UART_OMAP_WER, OMAP_UART_WER_MOD_WKUP);
525
526 up->port_activity = jiffies;
527 return 0;
528}
529
530static void serial_omap_shutdown(struct uart_port *port)
531{
532 struct uart_omap_port *up = (struct uart_omap_port *)port;
533 unsigned long flags = 0;
534
535 dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->pdev->id);
536 /*
537 * Disable interrupts from this port
538 */
539 up->ier = 0;
540 serial_out(up, UART_IER, 0);
541
542 spin_lock_irqsave(&up->port.lock, flags);
543 up->port.mctrl &= ~TIOCM_OUT2;
544 serial_omap_set_mctrl(&up->port, up->port.mctrl);
545 spin_unlock_irqrestore(&up->port.lock, flags);
546
547 /*
548 * Disable break condition and FIFOs
549 */
550 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
551 serial_omap_clear_fifos(up);
552
553 /*
554 * Read data port to reset things, and then free the irq
555 */
556 if (serial_in(up, UART_LSR) & UART_LSR_DR)
557 (void) serial_in(up, UART_RX);
558 if (up->use_dma) {
559 dma_free_coherent(up->port.dev,
560 UART_XMIT_SIZE, up->port.state->xmit.buf,
561 up->uart_dma.tx_buf_dma_phys);
562 up->port.state->xmit.buf = NULL;
563 serial_omap_stop_rx(port);
564 dma_free_coherent(up->port.dev,
565 up->uart_dma.rx_buf_size, up->uart_dma.rx_buf,
566 up->uart_dma.rx_buf_dma_phys);
567 up->uart_dma.rx_buf = NULL;
568 }
569 free_irq(up->port.irq, up);
570}
571
572static inline void
573serial_omap_configure_xonxoff
574 (struct uart_omap_port *up, struct ktermios *termios)
575{
576 unsigned char efr = 0;
577
578 up->lcr = serial_in(up, UART_LCR);
579 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
580 up->efr = serial_in(up, UART_EFR);
581 serial_out(up, UART_EFR, up->efr & ~UART_EFR_ECB);
582
583 serial_out(up, UART_XON1, termios->c_cc[VSTART]);
584 serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
585
586 /* clear SW control mode bits */
587 efr = up->efr;
588 efr &= OMAP_UART_SW_CLR;
589
590 /*
591 * IXON Flag:
592 * Enable XON/XOFF flow control on output.
593 * Transmit XON1, XOFF1
594 */
595 if (termios->c_iflag & IXON)
596 efr |= OMAP_UART_SW_TX;
597
598 /*
599 * IXOFF Flag:
600 * Enable XON/XOFF flow control on input.
601 * Receiver compares XON1, XOFF1.
602 */
603 if (termios->c_iflag & IXOFF)
604 efr |= OMAP_UART_SW_RX;
605
606 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
607 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
608
609 up->mcr = serial_in(up, UART_MCR);
610
611 /*
612 * IXANY Flag:
613 * Enable any character to restart output.
614 * Operation resumes after receiving any
615 * character after recognition of the XOFF character
616 */
617 if (termios->c_iflag & IXANY)
618 up->mcr |= UART_MCR_XONANY;
619
620 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
621 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
622 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
623 /* Enable special char function UARTi.EFR_REG[5] and
624 * load the new software flow control mode IXON or IXOFF
625 * and restore the UARTi.EFR_REG[4] ENHANCED_EN value.
626 */
627 serial_out(up, UART_EFR, efr | UART_EFR_SCD);
628 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
629
630 serial_out(up, UART_MCR, up->mcr & ~UART_MCR_TCRTLR);
631 serial_out(up, UART_LCR, up->lcr);
632}
633
634static void
635serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
636 struct ktermios *old)
637{
638 struct uart_omap_port *up = (struct uart_omap_port *)port;
639 unsigned char cval = 0;
640 unsigned char efr = 0;
641 unsigned long flags = 0;
642 unsigned int baud, quot;
643
644 switch (termios->c_cflag & CSIZE) {
645 case CS5:
646 cval = UART_LCR_WLEN5;
647 break;
648 case CS6:
649 cval = UART_LCR_WLEN6;
650 break;
651 case CS7:
652 cval = UART_LCR_WLEN7;
653 break;
654 default:
655 case CS8:
656 cval = UART_LCR_WLEN8;
657 break;
658 }
659
660 if (termios->c_cflag & CSTOPB)
661 cval |= UART_LCR_STOP;
662 if (termios->c_cflag & PARENB)
663 cval |= UART_LCR_PARITY;
664 if (!(termios->c_cflag & PARODD))
665 cval |= UART_LCR_EPAR;
666
667 /*
668 * Ask the core to calculate the divisor for us.
669 */
670
671 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
672 quot = serial_omap_get_divisor(port, baud);
673
674 up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
675 UART_FCR_ENABLE_FIFO;
676 if (up->use_dma)
677 up->fcr |= UART_FCR_DMA_SELECT;
678
679 /*
680 * Ok, we're now changing the port state. Do it with
681 * interrupts disabled.
682 */
683 spin_lock_irqsave(&up->port.lock, flags);
684
685 /*
686 * Update the per-port timeout.
687 */
688 uart_update_timeout(port, termios->c_cflag, baud);
689
690 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
691 if (termios->c_iflag & INPCK)
692 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
693 if (termios->c_iflag & (BRKINT | PARMRK))
694 up->port.read_status_mask |= UART_LSR_BI;
695
696 /*
697 * Characters to ignore
698 */
699 up->port.ignore_status_mask = 0;
700 if (termios->c_iflag & IGNPAR)
701 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
702 if (termios->c_iflag & IGNBRK) {
703 up->port.ignore_status_mask |= UART_LSR_BI;
704 /*
705 * If we're ignoring parity and break indicators,
706 * ignore overruns too (for real raw support).
707 */
708 if (termios->c_iflag & IGNPAR)
709 up->port.ignore_status_mask |= UART_LSR_OE;
710 }
711
712 /*
713 * ignore all characters if CREAD is not set
714 */
715 if ((termios->c_cflag & CREAD) == 0)
716 up->port.ignore_status_mask |= UART_LSR_DR;
717
718 /*
719 * Modem status interrupts
720 */
721 up->ier &= ~UART_IER_MSI;
722 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
723 up->ier |= UART_IER_MSI;
724 serial_out(up, UART_IER, up->ier);
725 serial_out(up, UART_LCR, cval); /* reset DLAB */
726
727 /* FIFOs and DMA Settings */
728
729 /* FCR can be changed only when the
730 * baud clock is not running
731 * DLL_REG and DLH_REG set to 0.
732 */
733 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
734 serial_out(up, UART_DLL, 0);
735 serial_out(up, UART_DLM, 0);
736 serial_out(up, UART_LCR, 0);
737
738 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
739
740 up->efr = serial_in(up, UART_EFR);
741 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
742
743 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
744 up->mcr = serial_in(up, UART_MCR);
745 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
746 /* FIFO ENABLE, DMA MODE */
747 serial_out(up, UART_FCR, up->fcr);
748 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
749
750 if (up->use_dma) {
751 serial_out(up, UART_TI752_TLR, 0);
752 serial_out(up, UART_OMAP_SCR,
753 (UART_FCR_TRIGGER_4 | UART_FCR_TRIGGER_8));
754 }
755
756 serial_out(up, UART_EFR, up->efr);
757 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
758 serial_out(up, UART_MCR, up->mcr);
759
760 /* Protocol, Baud Rate, and Interrupt Settings */
761
762 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
763 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
764
765 up->efr = serial_in(up, UART_EFR);
766 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
767
768 serial_out(up, UART_LCR, 0);
769 serial_out(up, UART_IER, 0);
770 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
771
772 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
773 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
774
775 serial_out(up, UART_LCR, 0);
776 serial_out(up, UART_IER, up->ier);
777 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
778
779 serial_out(up, UART_EFR, up->efr);
780 serial_out(up, UART_LCR, cval);
781
782 if (baud > 230400 && baud != 3000000)
783 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_13X_MODE);
784 else
785 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_16X_MODE);
786
787 /* Hardware Flow Control Configuration */
788
789 if (termios->c_cflag & CRTSCTS) {
790 efr |= (UART_EFR_CTS | UART_EFR_RTS);
791 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
792
793 up->mcr = serial_in(up, UART_MCR);
794 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
795
796 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
797 up->efr = serial_in(up, UART_EFR);
798 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
799
800 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
801 serial_out(up, UART_EFR, efr); /* Enable AUTORTS and AUTOCTS */
802 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
803 serial_out(up, UART_MCR, up->mcr | UART_MCR_RTS);
804 serial_out(up, UART_LCR, cval);
805 }
806
807 serial_omap_set_mctrl(&up->port, up->port.mctrl);
808 /* Software Flow Control Configuration */
809 if (termios->c_iflag & (IXON | IXOFF))
810 serial_omap_configure_xonxoff(up, termios);
811
812 spin_unlock_irqrestore(&up->port.lock, flags);
813 dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->pdev->id);
814}
815
816static void
817serial_omap_pm(struct uart_port *port, unsigned int state,
818 unsigned int oldstate)
819{
820 struct uart_omap_port *up = (struct uart_omap_port *)port;
821 unsigned char efr;
822
823 dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->pdev->id);
824 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
825 efr = serial_in(up, UART_EFR);
826 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
827 serial_out(up, UART_LCR, 0);
828
829 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
830 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
831 serial_out(up, UART_EFR, efr);
832 serial_out(up, UART_LCR, 0);
833}
834
835static void serial_omap_release_port(struct uart_port *port)
836{
837 dev_dbg(port->dev, "serial_omap_release_port+\n");
838}
839
840static int serial_omap_request_port(struct uart_port *port)
841{
842 dev_dbg(port->dev, "serial_omap_request_port+\n");
843 return 0;
844}
845
846static void serial_omap_config_port(struct uart_port *port, int flags)
847{
848 struct uart_omap_port *up = (struct uart_omap_port *)port;
849
850 dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
851 up->pdev->id);
852 up->port.type = PORT_OMAP;
853}
854
855static int
856serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
857{
858 /* we don't want the core code to modify any port params */
859 dev_dbg(port->dev, "serial_omap_verify_port+\n");
860 return -EINVAL;
861}
862
863static const char *
864serial_omap_type(struct uart_port *port)
865{
866 struct uart_omap_port *up = (struct uart_omap_port *)port;
867
868 dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->pdev->id);
869 return up->name;
870}
871
872#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
873
874static inline void wait_for_xmitr(struct uart_omap_port *up)
875{
876 unsigned int status, tmout = 10000;
877
878 /* Wait up to 10ms for the character(s) to be sent. */
879 do {
880 status = serial_in(up, UART_LSR);
881
882 if (status & UART_LSR_BI)
883 up->lsr_break_flag = UART_LSR_BI;
884
885 if (--tmout == 0)
886 break;
887 udelay(1);
888 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
889
890 /* Wait up to 1s for flow control if necessary */
891 if (up->port.flags & UPF_CONS_FLOW) {
892 tmout = 1000000;
893 for (tmout = 1000000; tmout; tmout--) {
894 unsigned int msr = serial_in(up, UART_MSR);
895
896 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
897 if (msr & UART_MSR_CTS)
898 break;
899
900 udelay(1);
901 }
902 }
903}
904
905#ifdef CONFIG_CONSOLE_POLL
906
907static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
908{
909 struct uart_omap_port *up = (struct uart_omap_port *)port;
910 wait_for_xmitr(up);
911 serial_out(up, UART_TX, ch);
912}
913
914static int serial_omap_poll_get_char(struct uart_port *port)
915{
916 struct uart_omap_port *up = (struct uart_omap_port *)port;
917 unsigned int status = serial_in(up, UART_LSR);
918
919 if (!(status & UART_LSR_DR))
920 return NO_POLL_CHAR;
921
922 return serial_in(up, UART_RX);
923}
924
925#endif /* CONFIG_CONSOLE_POLL */
926
927#ifdef CONFIG_SERIAL_OMAP_CONSOLE
928
929static struct uart_omap_port *serial_omap_console_ports[4];
930
931static struct uart_driver serial_omap_reg;
932
933static void serial_omap_console_putchar(struct uart_port *port, int ch)
934{
935 struct uart_omap_port *up = (struct uart_omap_port *)port;
936
937 wait_for_xmitr(up);
938 serial_out(up, UART_TX, ch);
939}
940
941static void
942serial_omap_console_write(struct console *co, const char *s,
943 unsigned int count)
944{
945 struct uart_omap_port *up = serial_omap_console_ports[co->index];
946 unsigned long flags;
947 unsigned int ier;
948 int locked = 1;
949
950 local_irq_save(flags);
951 if (up->port.sysrq)
952 locked = 0;
953 else if (oops_in_progress)
954 locked = spin_trylock(&up->port.lock);
955 else
956 spin_lock(&up->port.lock);
957
958 /*
959 * First save the IER then disable the interrupts
960 */
961 ier = serial_in(up, UART_IER);
962 serial_out(up, UART_IER, 0);
963
964 uart_console_write(&up->port, s, count, serial_omap_console_putchar);
965
966 /*
967 * Finally, wait for transmitter to become empty
968 * and restore the IER
969 */
970 wait_for_xmitr(up);
971 serial_out(up, UART_IER, ier);
972 /*
973 * The receive handling will happen properly because the
974 * receive ready bit will still be set; it is not cleared
975 * on read. However, modem control will not, we must
976 * call it if we have saved something in the saved flags
977 * while processing with interrupts off.
978 */
979 if (up->msr_saved_flags)
980 check_modem_status(up);
981
982 if (locked)
983 spin_unlock(&up->port.lock);
984 local_irq_restore(flags);
985}
986
987static int __init
988serial_omap_console_setup(struct console *co, char *options)
989{
990 struct uart_omap_port *up;
991 int baud = 115200;
992 int bits = 8;
993 int parity = 'n';
994 int flow = 'n';
995
996 if (serial_omap_console_ports[co->index] == NULL)
997 return -ENODEV;
998 up = serial_omap_console_ports[co->index];
999
1000 if (options)
1001 uart_parse_options(options, &baud, &parity, &bits, &flow);
1002
1003 return uart_set_options(&up->port, co, baud, parity, bits, flow);
1004}
1005
1006static struct console serial_omap_console = {
1007 .name = OMAP_SERIAL_NAME,
1008 .write = serial_omap_console_write,
1009 .device = uart_console_device,
1010 .setup = serial_omap_console_setup,
1011 .flags = CON_PRINTBUFFER,
1012 .index = -1,
1013 .data = &serial_omap_reg,
1014};
1015
1016static void serial_omap_add_console_port(struct uart_omap_port *up)
1017{
1018 serial_omap_console_ports[up->pdev->id] = up;
1019}
1020
1021#define OMAP_CONSOLE (&serial_omap_console)
1022
1023#else
1024
1025#define OMAP_CONSOLE NULL
1026
1027static inline void serial_omap_add_console_port(struct uart_omap_port *up)
1028{}
1029
1030#endif
1031
1032static struct uart_ops serial_omap_pops = {
1033 .tx_empty = serial_omap_tx_empty,
1034 .set_mctrl = serial_omap_set_mctrl,
1035 .get_mctrl = serial_omap_get_mctrl,
1036 .stop_tx = serial_omap_stop_tx,
1037 .start_tx = serial_omap_start_tx,
1038 .stop_rx = serial_omap_stop_rx,
1039 .enable_ms = serial_omap_enable_ms,
1040 .break_ctl = serial_omap_break_ctl,
1041 .startup = serial_omap_startup,
1042 .shutdown = serial_omap_shutdown,
1043 .set_termios = serial_omap_set_termios,
1044 .pm = serial_omap_pm,
1045 .type = serial_omap_type,
1046 .release_port = serial_omap_release_port,
1047 .request_port = serial_omap_request_port,
1048 .config_port = serial_omap_config_port,
1049 .verify_port = serial_omap_verify_port,
1050#ifdef CONFIG_CONSOLE_POLL
1051 .poll_put_char = serial_omap_poll_put_char,
1052 .poll_get_char = serial_omap_poll_get_char,
1053#endif
1054};
1055
1056static struct uart_driver serial_omap_reg = {
1057 .owner = THIS_MODULE,
1058 .driver_name = "OMAP-SERIAL",
1059 .dev_name = OMAP_SERIAL_NAME,
1060 .nr = OMAP_MAX_HSUART_PORTS,
1061 .cons = OMAP_CONSOLE,
1062};
1063
1064static int
1065serial_omap_suspend(struct platform_device *pdev, pm_message_t state)
1066{
1067 struct uart_omap_port *up = platform_get_drvdata(pdev);
1068
1069 if (up)
1070 uart_suspend_port(&serial_omap_reg, &up->port);
1071 return 0;
1072}
1073
1074static int serial_omap_resume(struct platform_device *dev)
1075{
1076 struct uart_omap_port *up = platform_get_drvdata(dev);
1077
1078 if (up)
1079 uart_resume_port(&serial_omap_reg, &up->port);
1080 return 0;
1081}
1082
1083static void serial_omap_rx_timeout(unsigned long uart_no)
1084{
1085 struct uart_omap_port *up = ui[uart_no];
1086 unsigned int curr_dma_pos, curr_transmitted_size;
1087 int ret = 0;
1088
1089 curr_dma_pos = omap_get_dma_dst_pos(up->uart_dma.rx_dma_channel);
1090 if ((curr_dma_pos == up->uart_dma.prev_rx_dma_pos) ||
1091 (curr_dma_pos == 0)) {
1092 if (jiffies_to_msecs(jiffies - up->port_activity) <
1093 RX_TIMEOUT) {
1094 mod_timer(&up->uart_dma.rx_timer, jiffies +
1095 usecs_to_jiffies(up->uart_dma.rx_timeout));
1096 } else {
1097 serial_omap_stop_rxdma(up);
1098 up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1099 serial_out(up, UART_IER, up->ier);
1100 }
1101 return;
1102 }
1103
1104 curr_transmitted_size = curr_dma_pos -
1105 up->uart_dma.prev_rx_dma_pos;
1106 up->port.icount.rx += curr_transmitted_size;
1107 tty_insert_flip_string(up->port.state->port.tty,
1108 up->uart_dma.rx_buf +
1109 (up->uart_dma.prev_rx_dma_pos -
1110 up->uart_dma.rx_buf_dma_phys),
1111 curr_transmitted_size);
1112 tty_flip_buffer_push(up->port.state->port.tty);
1113 up->uart_dma.prev_rx_dma_pos = curr_dma_pos;
1114 if (up->uart_dma.rx_buf_size +
1115 up->uart_dma.rx_buf_dma_phys == curr_dma_pos) {
1116 ret = serial_omap_start_rxdma(up);
1117 if (ret < 0) {
1118 serial_omap_stop_rxdma(up);
1119 up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1120 serial_out(up, UART_IER, up->ier);
1121 }
1122 } else {
1123 mod_timer(&up->uart_dma.rx_timer, jiffies +
1124 usecs_to_jiffies(up->uart_dma.rx_timeout));
1125 }
1126 up->port_activity = jiffies;
1127}
1128
1129static void uart_rx_dma_callback(int lch, u16 ch_status, void *data)
1130{
1131 return;
1132}
1133
1134static int serial_omap_start_rxdma(struct uart_omap_port *up)
1135{
1136 int ret = 0;
1137
1138 if (up->uart_dma.rx_dma_channel == -1) {
1139 ret = omap_request_dma(up->uart_dma.uart_dma_rx,
1140 "UART Rx DMA",
1141 (void *)uart_rx_dma_callback, up,
1142 &(up->uart_dma.rx_dma_channel));
1143 if (ret < 0)
1144 return ret;
1145
1146 omap_set_dma_src_params(up->uart_dma.rx_dma_channel, 0,
1147 OMAP_DMA_AMODE_CONSTANT,
1148 up->uart_dma.uart_base, 0, 0);
1149 omap_set_dma_dest_params(up->uart_dma.rx_dma_channel, 0,
1150 OMAP_DMA_AMODE_POST_INC,
1151 up->uart_dma.rx_buf_dma_phys, 0, 0);
1152 omap_set_dma_transfer_params(up->uart_dma.rx_dma_channel,
1153 OMAP_DMA_DATA_TYPE_S8,
1154 up->uart_dma.rx_buf_size, 1,
1155 OMAP_DMA_SYNC_ELEMENT,
1156 up->uart_dma.uart_dma_rx, 0);
1157 }
1158 up->uart_dma.prev_rx_dma_pos = up->uart_dma.rx_buf_dma_phys;
1159 /* FIXME: Cache maintenance needed here? */
1160 omap_start_dma(up->uart_dma.rx_dma_channel);
1161 mod_timer(&up->uart_dma.rx_timer, jiffies +
1162 usecs_to_jiffies(up->uart_dma.rx_timeout));
1163 up->uart_dma.rx_dma_used = true;
1164 return ret;
1165}
1166
1167static void serial_omap_continue_tx(struct uart_omap_port *up)
1168{
1169 struct circ_buf *xmit = &up->port.state->xmit;
1170 unsigned int start = up->uart_dma.tx_buf_dma_phys
1171 + (xmit->tail & (UART_XMIT_SIZE - 1));
1172
1173 if (uart_circ_empty(xmit))
1174 return;
1175
1176 up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
1177 /*
1178 * It is a circular buffer. See if the buffer has wounded back.
1179 * If yes it will have to be transferred in two separate dma
1180 * transfers
1181 */
1182 if (start + up->uart_dma.tx_buf_size >=
1183 up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
1184 up->uart_dma.tx_buf_size =
1185 (up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE) - start;
1186 omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
1187 OMAP_DMA_AMODE_CONSTANT,
1188 up->uart_dma.uart_base, 0, 0);
1189 omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
1190 OMAP_DMA_AMODE_POST_INC, start, 0, 0);
1191 omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
1192 OMAP_DMA_DATA_TYPE_S8,
1193 up->uart_dma.tx_buf_size, 1,
1194 OMAP_DMA_SYNC_ELEMENT,
1195 up->uart_dma.uart_dma_tx, 0);
1196 /* FIXME: Cache maintenance needed here? */
1197 omap_start_dma(up->uart_dma.tx_dma_channel);
1198}
1199
1200static void uart_tx_dma_callback(int lch, u16 ch_status, void *data)
1201{
1202 struct uart_omap_port *up = (struct uart_omap_port *)data;
1203 struct circ_buf *xmit = &up->port.state->xmit;
1204
1205 xmit->tail = (xmit->tail + up->uart_dma.tx_buf_size) & \
1206 (UART_XMIT_SIZE - 1);
1207 up->port.icount.tx += up->uart_dma.tx_buf_size;
1208
1209 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1210 uart_write_wakeup(&up->port);
1211
1212 if (uart_circ_empty(xmit)) {
1213 spin_lock(&(up->uart_dma.tx_lock));
1214 serial_omap_stop_tx(&up->port);
1215 up->uart_dma.tx_dma_used = false;
1216 spin_unlock(&(up->uart_dma.tx_lock));
1217 } else {
1218 omap_stop_dma(up->uart_dma.tx_dma_channel);
1219 serial_omap_continue_tx(up);
1220 }
1221 up->port_activity = jiffies;
1222 return;
1223}
1224
1225static int serial_omap_probe(struct platform_device *pdev)
1226{
1227 struct uart_omap_port *up;
1228 struct resource *mem, *irq, *dma_tx, *dma_rx;
1229 struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data;
1230 int ret = -ENOSPC;
1231
1232 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1233 if (!mem) {
1234 dev_err(&pdev->dev, "no mem resource?\n");
1235 return -ENODEV;
1236 }
1237
1238 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1239 if (!irq) {
1240 dev_err(&pdev->dev, "no irq resource?\n");
1241 return -ENODEV;
1242 }
1243
1244 if (!request_mem_region(mem->start, (mem->end - mem->start) + 1,
1245 pdev->dev.driver->name)) {
1246 dev_err(&pdev->dev, "memory region already claimed\n");
1247 return -EBUSY;
1248 }
1249
1250 dma_rx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1251 if (!dma_rx) {
1252 ret = -EINVAL;
1253 goto err;
1254 }
1255
1256 dma_tx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1257 if (!dma_tx) {
1258 ret = -EINVAL;
1259 goto err;
1260 }
1261
1262 up = kzalloc(sizeof(*up), GFP_KERNEL);
1263 if (up == NULL) {
1264 ret = -ENOMEM;
1265 goto do_release_region;
1266 }
1267 sprintf(up->name, "OMAP UART%d", pdev->id);
1268 up->pdev = pdev;
1269 up->port.dev = &pdev->dev;
1270 up->port.type = PORT_OMAP;
1271 up->port.iotype = UPIO_MEM;
1272 up->port.irq = irq->start;
1273
1274 up->port.regshift = 2;
1275 up->port.fifosize = 64;
1276 up->port.ops = &serial_omap_pops;
1277 up->port.line = pdev->id;
1278
1279 up->port.membase = omap_up_info->membase;
1280 up->port.mapbase = omap_up_info->mapbase;
1281 up->port.flags = omap_up_info->flags;
1282 up->port.irqflags = omap_up_info->irqflags;
1283 up->port.uartclk = omap_up_info->uartclk;
1284 up->uart_dma.uart_base = mem->start;
1285
1286 if (omap_up_info->dma_enabled) {
1287 up->uart_dma.uart_dma_tx = dma_tx->start;
1288 up->uart_dma.uart_dma_rx = dma_rx->start;
1289 up->use_dma = 1;
1290 up->uart_dma.rx_buf_size = 4096;
1291 up->uart_dma.rx_timeout = 2;
1292 spin_lock_init(&(up->uart_dma.tx_lock));
1293 spin_lock_init(&(up->uart_dma.rx_lock));
1294 up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
1295 up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
1296 }
1297
1298 ui[pdev->id] = up;
1299 serial_omap_add_console_port(up);
1300
1301 ret = uart_add_one_port(&serial_omap_reg, &up->port);
1302 if (ret != 0)
1303 goto do_release_region;
1304
1305 platform_set_drvdata(pdev, up);
1306 return 0;
1307err:
1308 dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
1309 pdev->id, __func__, ret);
1310do_release_region:
1311 release_mem_region(mem->start, (mem->end - mem->start) + 1);
1312 return ret;
1313}
1314
1315static int serial_omap_remove(struct platform_device *dev)
1316{
1317 struct uart_omap_port *up = platform_get_drvdata(dev);
1318
1319 platform_set_drvdata(dev, NULL);
1320 if (up) {
1321 uart_remove_one_port(&serial_omap_reg, &up->port);
1322 kfree(up);
1323 }
1324 return 0;
1325}
1326
1327static struct platform_driver serial_omap_driver = {
1328 .probe = serial_omap_probe,
1329 .remove = serial_omap_remove,
1330
1331 .suspend = serial_omap_suspend,
1332 .resume = serial_omap_resume,
1333 .driver = {
1334 .name = DRIVER_NAME,
1335 },
1336};
1337
1338static int __init serial_omap_init(void)
1339{
1340 int ret;
1341
1342 ret = uart_register_driver(&serial_omap_reg);
1343 if (ret != 0)
1344 return ret;
1345 ret = platform_driver_register(&serial_omap_driver);
1346 if (ret != 0)
1347 uart_unregister_driver(&serial_omap_reg);
1348 return ret;
1349}
1350
1351static void __exit serial_omap_exit(void)
1352{
1353 platform_driver_unregister(&serial_omap_driver);
1354 uart_unregister_driver(&serial_omap_reg);
1355}
1356
1357module_init(serial_omap_init);
1358module_exit(serial_omap_exit);
1359
1360MODULE_DESCRIPTION("OMAP High Speed UART driver");
1361MODULE_LICENSE("GPL");
1362MODULE_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..465210930890
--- /dev/null
+++ b/drivers/tty/serial/pch_uart.c
@@ -0,0 +1,1612 @@
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/slab.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#include <linux/dmi.h>
25
26#include <linux/dmaengine.h>
27#include <linux/pch_dma.h>
28
29enum {
30 PCH_UART_HANDLED_RX_INT_SHIFT,
31 PCH_UART_HANDLED_TX_INT_SHIFT,
32 PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
33 PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
34 PCH_UART_HANDLED_MS_INT_SHIFT,
35};
36
37enum {
38 PCH_UART_8LINE,
39 PCH_UART_2LINE,
40};
41
42#define PCH_UART_DRIVER_DEVICE "ttyPCH"
43
44/* Set the max number of UART port
45 * Intel EG20T PCH: 4 port
46 * OKI SEMICONDUCTOR ML7213 IOH: 3 port
47*/
48#define PCH_UART_NR 4
49
50#define PCH_UART_HANDLED_RX_INT (1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
51#define PCH_UART_HANDLED_TX_INT (1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
52#define PCH_UART_HANDLED_RX_ERR_INT (1<<((\
53 PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
54#define PCH_UART_HANDLED_RX_TRG_INT (1<<((\
55 PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
56#define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))
57
58#define PCH_UART_RBR 0x00
59#define PCH_UART_THR 0x00
60
61#define PCH_UART_IER_MASK (PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
62 PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
63#define PCH_UART_IER_ERBFI 0x00000001
64#define PCH_UART_IER_ETBEI 0x00000002
65#define PCH_UART_IER_ELSI 0x00000004
66#define PCH_UART_IER_EDSSI 0x00000008
67
68#define PCH_UART_IIR_IP 0x00000001
69#define PCH_UART_IIR_IID 0x00000006
70#define PCH_UART_IIR_MSI 0x00000000
71#define PCH_UART_IIR_TRI 0x00000002
72#define PCH_UART_IIR_RRI 0x00000004
73#define PCH_UART_IIR_REI 0x00000006
74#define PCH_UART_IIR_TOI 0x00000008
75#define PCH_UART_IIR_FIFO256 0x00000020
76#define PCH_UART_IIR_FIFO64 PCH_UART_IIR_FIFO256
77#define PCH_UART_IIR_FE 0x000000C0
78
79#define PCH_UART_FCR_FIFOE 0x00000001
80#define PCH_UART_FCR_RFR 0x00000002
81#define PCH_UART_FCR_TFR 0x00000004
82#define PCH_UART_FCR_DMS 0x00000008
83#define PCH_UART_FCR_FIFO256 0x00000020
84#define PCH_UART_FCR_RFTL 0x000000C0
85
86#define PCH_UART_FCR_RFTL1 0x00000000
87#define PCH_UART_FCR_RFTL64 0x00000040
88#define PCH_UART_FCR_RFTL128 0x00000080
89#define PCH_UART_FCR_RFTL224 0x000000C0
90#define PCH_UART_FCR_RFTL16 PCH_UART_FCR_RFTL64
91#define PCH_UART_FCR_RFTL32 PCH_UART_FCR_RFTL128
92#define PCH_UART_FCR_RFTL56 PCH_UART_FCR_RFTL224
93#define PCH_UART_FCR_RFTL4 PCH_UART_FCR_RFTL64
94#define PCH_UART_FCR_RFTL8 PCH_UART_FCR_RFTL128
95#define PCH_UART_FCR_RFTL14 PCH_UART_FCR_RFTL224
96#define PCH_UART_FCR_RFTL_SHIFT 6
97
98#define PCH_UART_LCR_WLS 0x00000003
99#define PCH_UART_LCR_STB 0x00000004
100#define PCH_UART_LCR_PEN 0x00000008
101#define PCH_UART_LCR_EPS 0x00000010
102#define PCH_UART_LCR_SP 0x00000020
103#define PCH_UART_LCR_SB 0x00000040
104#define PCH_UART_LCR_DLAB 0x00000080
105#define PCH_UART_LCR_NP 0x00000000
106#define PCH_UART_LCR_OP PCH_UART_LCR_PEN
107#define PCH_UART_LCR_EP (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
108#define PCH_UART_LCR_1P (PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
109#define PCH_UART_LCR_0P (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
110 PCH_UART_LCR_SP)
111
112#define PCH_UART_LCR_5BIT 0x00000000
113#define PCH_UART_LCR_6BIT 0x00000001
114#define PCH_UART_LCR_7BIT 0x00000002
115#define PCH_UART_LCR_8BIT 0x00000003
116
117#define PCH_UART_MCR_DTR 0x00000001
118#define PCH_UART_MCR_RTS 0x00000002
119#define PCH_UART_MCR_OUT 0x0000000C
120#define PCH_UART_MCR_LOOP 0x00000010
121#define PCH_UART_MCR_AFE 0x00000020
122
123#define PCH_UART_LSR_DR 0x00000001
124#define PCH_UART_LSR_ERR (1<<7)
125
126#define PCH_UART_MSR_DCTS 0x00000001
127#define PCH_UART_MSR_DDSR 0x00000002
128#define PCH_UART_MSR_TERI 0x00000004
129#define PCH_UART_MSR_DDCD 0x00000008
130#define PCH_UART_MSR_CTS 0x00000010
131#define PCH_UART_MSR_DSR 0x00000020
132#define PCH_UART_MSR_RI 0x00000040
133#define PCH_UART_MSR_DCD 0x00000080
134#define PCH_UART_MSR_DELTA (PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
135 PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)
136
137#define PCH_UART_DLL 0x00
138#define PCH_UART_DLM 0x01
139
140#define DIV_ROUND(a, b) (((a) + ((b)/2)) / (b))
141
142#define PCH_UART_IID_RLS (PCH_UART_IIR_REI)
143#define PCH_UART_IID_RDR (PCH_UART_IIR_RRI)
144#define PCH_UART_IID_RDR_TO (PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
145#define PCH_UART_IID_THRE (PCH_UART_IIR_TRI)
146#define PCH_UART_IID_MS (PCH_UART_IIR_MSI)
147
148#define PCH_UART_HAL_PARITY_NONE (PCH_UART_LCR_NP)
149#define PCH_UART_HAL_PARITY_ODD (PCH_UART_LCR_OP)
150#define PCH_UART_HAL_PARITY_EVEN (PCH_UART_LCR_EP)
151#define PCH_UART_HAL_PARITY_FIX1 (PCH_UART_LCR_1P)
152#define PCH_UART_HAL_PARITY_FIX0 (PCH_UART_LCR_0P)
153#define PCH_UART_HAL_5BIT (PCH_UART_LCR_5BIT)
154#define PCH_UART_HAL_6BIT (PCH_UART_LCR_6BIT)
155#define PCH_UART_HAL_7BIT (PCH_UART_LCR_7BIT)
156#define PCH_UART_HAL_8BIT (PCH_UART_LCR_8BIT)
157#define PCH_UART_HAL_STB1 0
158#define PCH_UART_HAL_STB2 (PCH_UART_LCR_STB)
159
160#define PCH_UART_HAL_CLR_TX_FIFO (PCH_UART_FCR_TFR)
161#define PCH_UART_HAL_CLR_RX_FIFO (PCH_UART_FCR_RFR)
162#define PCH_UART_HAL_CLR_ALL_FIFO (PCH_UART_HAL_CLR_TX_FIFO | \
163 PCH_UART_HAL_CLR_RX_FIFO)
164
165#define PCH_UART_HAL_DMA_MODE0 0
166#define PCH_UART_HAL_FIFO_DIS 0
167#define PCH_UART_HAL_FIFO16 (PCH_UART_FCR_FIFOE)
168#define PCH_UART_HAL_FIFO256 (PCH_UART_FCR_FIFOE | \
169 PCH_UART_FCR_FIFO256)
170#define PCH_UART_HAL_FIFO64 (PCH_UART_HAL_FIFO256)
171#define PCH_UART_HAL_TRIGGER1 (PCH_UART_FCR_RFTL1)
172#define PCH_UART_HAL_TRIGGER64 (PCH_UART_FCR_RFTL64)
173#define PCH_UART_HAL_TRIGGER128 (PCH_UART_FCR_RFTL128)
174#define PCH_UART_HAL_TRIGGER224 (PCH_UART_FCR_RFTL224)
175#define PCH_UART_HAL_TRIGGER16 (PCH_UART_FCR_RFTL16)
176#define PCH_UART_HAL_TRIGGER32 (PCH_UART_FCR_RFTL32)
177#define PCH_UART_HAL_TRIGGER56 (PCH_UART_FCR_RFTL56)
178#define PCH_UART_HAL_TRIGGER4 (PCH_UART_FCR_RFTL4)
179#define PCH_UART_HAL_TRIGGER8 (PCH_UART_FCR_RFTL8)
180#define PCH_UART_HAL_TRIGGER14 (PCH_UART_FCR_RFTL14)
181#define PCH_UART_HAL_TRIGGER_L (PCH_UART_FCR_RFTL64)
182#define PCH_UART_HAL_TRIGGER_M (PCH_UART_FCR_RFTL128)
183#define PCH_UART_HAL_TRIGGER_H (PCH_UART_FCR_RFTL224)
184
185#define PCH_UART_HAL_RX_INT (PCH_UART_IER_ERBFI)
186#define PCH_UART_HAL_TX_INT (PCH_UART_IER_ETBEI)
187#define PCH_UART_HAL_RX_ERR_INT (PCH_UART_IER_ELSI)
188#define PCH_UART_HAL_MS_INT (PCH_UART_IER_EDSSI)
189#define PCH_UART_HAL_ALL_INT (PCH_UART_IER_MASK)
190
191#define PCH_UART_HAL_DTR (PCH_UART_MCR_DTR)
192#define PCH_UART_HAL_RTS (PCH_UART_MCR_RTS)
193#define PCH_UART_HAL_OUT (PCH_UART_MCR_OUT)
194#define PCH_UART_HAL_LOOP (PCH_UART_MCR_LOOP)
195#define PCH_UART_HAL_AFE (PCH_UART_MCR_AFE)
196
197#define PCI_VENDOR_ID_ROHM 0x10DB
198
199struct pch_uart_buffer {
200 unsigned char *buf;
201 int size;
202};
203
204struct eg20t_port {
205 struct uart_port port;
206 int port_type;
207 void __iomem *membase;
208 resource_size_t mapbase;
209 unsigned int iobase;
210 struct pci_dev *pdev;
211 int fifo_size;
212 int base_baud;
213 int start_tx;
214 int start_rx;
215 int tx_empty;
216 int int_dis_flag;
217 int trigger;
218 int trigger_level;
219 struct pch_uart_buffer rxbuf;
220 unsigned int dmsr;
221 unsigned int fcr;
222 unsigned int mcr;
223 unsigned int use_dma;
224 unsigned int use_dma_flag;
225 struct dma_async_tx_descriptor *desc_tx;
226 struct dma_async_tx_descriptor *desc_rx;
227 struct pch_dma_slave param_tx;
228 struct pch_dma_slave param_rx;
229 struct dma_chan *chan_tx;
230 struct dma_chan *chan_rx;
231 struct scatterlist *sg_tx_p;
232 int nent;
233 struct scatterlist sg_rx;
234 int tx_dma_use;
235 void *rx_buf_virt;
236 dma_addr_t rx_buf_dma;
237};
238
239/**
240 * struct pch_uart_driver_data - private data structure for UART-DMA
241 * @port_type: The number of DMA channel
242 * @line_no: UART port line number (0, 1, 2...)
243 */
244struct pch_uart_driver_data {
245 int port_type;
246 int line_no;
247};
248
249enum pch_uart_num_t {
250 pch_et20t_uart0 = 0,
251 pch_et20t_uart1,
252 pch_et20t_uart2,
253 pch_et20t_uart3,
254 pch_ml7213_uart0,
255 pch_ml7213_uart1,
256 pch_ml7213_uart2,
257 pch_ml7223_uart0,
258 pch_ml7223_uart1,
259};
260
261static struct pch_uart_driver_data drv_dat[] = {
262 [pch_et20t_uart0] = {PCH_UART_8LINE, 0},
263 [pch_et20t_uart1] = {PCH_UART_2LINE, 1},
264 [pch_et20t_uart2] = {PCH_UART_2LINE, 2},
265 [pch_et20t_uart3] = {PCH_UART_2LINE, 3},
266 [pch_ml7213_uart0] = {PCH_UART_8LINE, 0},
267 [pch_ml7213_uart1] = {PCH_UART_2LINE, 1},
268 [pch_ml7213_uart2] = {PCH_UART_2LINE, 2},
269 [pch_ml7223_uart0] = {PCH_UART_8LINE, 0},
270 [pch_ml7223_uart1] = {PCH_UART_2LINE, 1},
271};
272
273static unsigned int default_baud = 9600;
274static const int trigger_level_256[4] = { 1, 64, 128, 224 };
275static const int trigger_level_64[4] = { 1, 16, 32, 56 };
276static const int trigger_level_16[4] = { 1, 4, 8, 14 };
277static const int trigger_level_1[4] = { 1, 1, 1, 1 };
278
279static void pch_uart_hal_request(struct pci_dev *pdev, int fifosize,
280 int base_baud)
281{
282 struct eg20t_port *priv = pci_get_drvdata(pdev);
283
284 priv->trigger_level = 1;
285 priv->fcr = 0;
286}
287
288static unsigned int get_msr(struct eg20t_port *priv, void __iomem *base)
289{
290 unsigned int msr = ioread8(base + UART_MSR);
291 priv->dmsr |= msr & PCH_UART_MSR_DELTA;
292
293 return msr;
294}
295
296static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
297 unsigned int flag)
298{
299 u8 ier = ioread8(priv->membase + UART_IER);
300 ier |= flag & PCH_UART_IER_MASK;
301 iowrite8(ier, priv->membase + UART_IER);
302}
303
304static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
305 unsigned int flag)
306{
307 u8 ier = ioread8(priv->membase + UART_IER);
308 ier &= ~(flag & PCH_UART_IER_MASK);
309 iowrite8(ier, priv->membase + UART_IER);
310}
311
312static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
313 unsigned int parity, unsigned int bits,
314 unsigned int stb)
315{
316 unsigned int dll, dlm, lcr;
317 int div;
318
319 div = DIV_ROUND(priv->base_baud / 16, baud);
320 if (div < 0 || USHRT_MAX <= div) {
321 dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
322 return -EINVAL;
323 }
324
325 dll = (unsigned int)div & 0x00FFU;
326 dlm = ((unsigned int)div >> 8) & 0x00FFU;
327
328 if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
329 dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
330 return -EINVAL;
331 }
332
333 if (bits & ~PCH_UART_LCR_WLS) {
334 dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
335 return -EINVAL;
336 }
337
338 if (stb & ~PCH_UART_LCR_STB) {
339 dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
340 return -EINVAL;
341 }
342
343 lcr = parity;
344 lcr |= bits;
345 lcr |= stb;
346
347 dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
348 __func__, baud, div, lcr, jiffies);
349 iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
350 iowrite8(dll, priv->membase + PCH_UART_DLL);
351 iowrite8(dlm, priv->membase + PCH_UART_DLM);
352 iowrite8(lcr, priv->membase + UART_LCR);
353
354 return 0;
355}
356
357static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
358 unsigned int flag)
359{
360 if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
361 dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
362 __func__, flag);
363 return -EINVAL;
364 }
365
366 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
367 iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
368 priv->membase + UART_FCR);
369 iowrite8(priv->fcr, priv->membase + UART_FCR);
370
371 return 0;
372}
373
374static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
375 unsigned int dmamode,
376 unsigned int fifo_size, unsigned int trigger)
377{
378 u8 fcr;
379
380 if (dmamode & ~PCH_UART_FCR_DMS) {
381 dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
382 __func__, dmamode);
383 return -EINVAL;
384 }
385
386 if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
387 dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
388 __func__, fifo_size);
389 return -EINVAL;
390 }
391
392 if (trigger & ~PCH_UART_FCR_RFTL) {
393 dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
394 __func__, trigger);
395 return -EINVAL;
396 }
397
398 switch (priv->fifo_size) {
399 case 256:
400 priv->trigger_level =
401 trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
402 break;
403 case 64:
404 priv->trigger_level =
405 trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
406 break;
407 case 16:
408 priv->trigger_level =
409 trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
410 break;
411 default:
412 priv->trigger_level =
413 trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
414 break;
415 }
416 fcr =
417 dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
418 iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
419 iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
420 priv->membase + UART_FCR);
421 iowrite8(fcr, priv->membase + UART_FCR);
422 priv->fcr = fcr;
423
424 return 0;
425}
426
427static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
428{
429 priv->dmsr = 0;
430 return get_msr(priv, priv->membase);
431}
432
433static void pch_uart_hal_write(struct eg20t_port *priv,
434 const unsigned char *buf, int tx_size)
435{
436 int i;
437 unsigned int thr;
438
439 for (i = 0; i < tx_size;) {
440 thr = buf[i++];
441 iowrite8(thr, priv->membase + PCH_UART_THR);
442 }
443}
444
445static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
446 int rx_size)
447{
448 int i;
449 u8 rbr, lsr;
450
451 lsr = ioread8(priv->membase + UART_LSR);
452 for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
453 i < rx_size && lsr & UART_LSR_DR;
454 lsr = ioread8(priv->membase + UART_LSR)) {
455 rbr = ioread8(priv->membase + PCH_UART_RBR);
456 buf[i++] = rbr;
457 }
458 return i;
459}
460
461static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
462{
463 unsigned int iir;
464 int ret;
465
466 iir = ioread8(priv->membase + UART_IIR);
467 ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
468 return ret;
469}
470
471static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
472{
473 return ioread8(priv->membase + UART_LSR);
474}
475
476static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
477{
478 unsigned int lcr;
479
480 lcr = ioread8(priv->membase + UART_LCR);
481 if (on)
482 lcr |= PCH_UART_LCR_SB;
483 else
484 lcr &= ~PCH_UART_LCR_SB;
485
486 iowrite8(lcr, priv->membase + UART_LCR);
487}
488
489static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
490 int size)
491{
492 struct uart_port *port;
493 struct tty_struct *tty;
494
495 port = &priv->port;
496 tty = tty_port_tty_get(&port->state->port);
497 if (!tty) {
498 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
499 return -EBUSY;
500 }
501
502 tty_insert_flip_string(tty, buf, size);
503 tty_flip_buffer_push(tty);
504 tty_kref_put(tty);
505
506 return 0;
507}
508
509static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
510{
511 int ret;
512 struct uart_port *port = &priv->port;
513
514 if (port->x_char) {
515 dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
516 __func__, port->x_char, jiffies);
517 buf[0] = port->x_char;
518 port->x_char = 0;
519 ret = 1;
520 } else {
521 ret = 0;
522 }
523
524 return ret;
525}
526
527static int dma_push_rx(struct eg20t_port *priv, int size)
528{
529 struct tty_struct *tty;
530 int room;
531 struct uart_port *port = &priv->port;
532
533 port = &priv->port;
534 tty = tty_port_tty_get(&port->state->port);
535 if (!tty) {
536 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
537 return 0;
538 }
539
540 room = tty_buffer_request_room(tty, size);
541
542 if (room < size)
543 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
544 size - room);
545 if (!room)
546 return room;
547
548 tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
549
550 port->icount.rx += room;
551 tty_kref_put(tty);
552
553 return room;
554}
555
556static void pch_free_dma(struct uart_port *port)
557{
558 struct eg20t_port *priv;
559 priv = container_of(port, struct eg20t_port, port);
560
561 if (priv->chan_tx) {
562 dma_release_channel(priv->chan_tx);
563 priv->chan_tx = NULL;
564 }
565 if (priv->chan_rx) {
566 dma_release_channel(priv->chan_rx);
567 priv->chan_rx = NULL;
568 }
569 if (sg_dma_address(&priv->sg_rx))
570 dma_free_coherent(port->dev, port->fifosize,
571 sg_virt(&priv->sg_rx),
572 sg_dma_address(&priv->sg_rx));
573
574 return;
575}
576
577static bool filter(struct dma_chan *chan, void *slave)
578{
579 struct pch_dma_slave *param = slave;
580
581 if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
582 chan->device->dev)) {
583 chan->private = param;
584 return true;
585 } else {
586 return false;
587 }
588}
589
590static void pch_request_dma(struct uart_port *port)
591{
592 dma_cap_mask_t mask;
593 struct dma_chan *chan;
594 struct pci_dev *dma_dev;
595 struct pch_dma_slave *param;
596 struct eg20t_port *priv =
597 container_of(port, struct eg20t_port, port);
598 dma_cap_zero(mask);
599 dma_cap_set(DMA_SLAVE, mask);
600
601 dma_dev = pci_get_bus_and_slot(2, PCI_DEVFN(0xa, 0)); /* Get DMA's dev
602 information */
603 /* Set Tx DMA */
604 param = &priv->param_tx;
605 param->dma_dev = &dma_dev->dev;
606 param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
607
608 param->tx_reg = port->mapbase + UART_TX;
609 chan = dma_request_channel(mask, filter, param);
610 if (!chan) {
611 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
612 __func__);
613 return;
614 }
615 priv->chan_tx = chan;
616
617 /* Set Rx DMA */
618 param = &priv->param_rx;
619 param->dma_dev = &dma_dev->dev;
620 param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
621
622 param->rx_reg = port->mapbase + UART_RX;
623 chan = dma_request_channel(mask, filter, param);
624 if (!chan) {
625 dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
626 __func__);
627 dma_release_channel(priv->chan_tx);
628 return;
629 }
630
631 /* Get Consistent memory for DMA */
632 priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
633 &priv->rx_buf_dma, GFP_KERNEL);
634 priv->chan_rx = chan;
635}
636
637static void pch_dma_rx_complete(void *arg)
638{
639 struct eg20t_port *priv = arg;
640 struct uart_port *port = &priv->port;
641 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
642 int count;
643
644 if (!tty) {
645 dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
646 return;
647 }
648
649 dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
650 count = dma_push_rx(priv, priv->trigger_level);
651 if (count)
652 tty_flip_buffer_push(tty);
653 tty_kref_put(tty);
654 async_tx_ack(priv->desc_rx);
655 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
656}
657
658static void pch_dma_tx_complete(void *arg)
659{
660 struct eg20t_port *priv = arg;
661 struct uart_port *port = &priv->port;
662 struct circ_buf *xmit = &port->state->xmit;
663 struct scatterlist *sg = priv->sg_tx_p;
664 int i;
665
666 for (i = 0; i < priv->nent; i++, sg++) {
667 xmit->tail += sg_dma_len(sg);
668 port->icount.tx += sg_dma_len(sg);
669 }
670 xmit->tail &= UART_XMIT_SIZE - 1;
671 async_tx_ack(priv->desc_tx);
672 dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
673 priv->tx_dma_use = 0;
674 priv->nent = 0;
675 kfree(priv->sg_tx_p);
676 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
677}
678
679static int pop_tx(struct eg20t_port *priv, int size)
680{
681 int count = 0;
682 struct uart_port *port = &priv->port;
683 struct circ_buf *xmit = &port->state->xmit;
684
685 if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
686 goto pop_tx_end;
687
688 do {
689 int cnt_to_end =
690 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
691 int sz = min(size - count, cnt_to_end);
692 pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
693 xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
694 count += sz;
695 } while (!uart_circ_empty(xmit) && count < size);
696
697pop_tx_end:
698 dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
699 count, size - count, jiffies);
700
701 return count;
702}
703
704static int handle_rx_to(struct eg20t_port *priv)
705{
706 struct pch_uart_buffer *buf;
707 int rx_size;
708 int ret;
709 if (!priv->start_rx) {
710 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
711 return 0;
712 }
713 buf = &priv->rxbuf;
714 do {
715 rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
716 ret = push_rx(priv, buf->buf, rx_size);
717 if (ret)
718 return 0;
719 } while (rx_size == buf->size);
720
721 return PCH_UART_HANDLED_RX_INT;
722}
723
724static int handle_rx(struct eg20t_port *priv)
725{
726 return handle_rx_to(priv);
727}
728
729static int dma_handle_rx(struct eg20t_port *priv)
730{
731 struct uart_port *port = &priv->port;
732 struct dma_async_tx_descriptor *desc;
733 struct scatterlist *sg;
734
735 priv = container_of(port, struct eg20t_port, port);
736 sg = &priv->sg_rx;
737
738 sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
739
740 sg_dma_len(sg) = priv->trigger_level;
741
742 sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
743 sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
744 ~PAGE_MASK);
745
746 sg_dma_address(sg) = priv->rx_buf_dma;
747
748 desc = priv->chan_rx->device->device_prep_slave_sg(priv->chan_rx,
749 sg, 1, DMA_FROM_DEVICE,
750 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
751
752 if (!desc)
753 return 0;
754
755 priv->desc_rx = desc;
756 desc->callback = pch_dma_rx_complete;
757 desc->callback_param = priv;
758 desc->tx_submit(desc);
759 dma_async_issue_pending(priv->chan_rx);
760
761 return PCH_UART_HANDLED_RX_INT;
762}
763
764static unsigned int handle_tx(struct eg20t_port *priv)
765{
766 struct uart_port *port = &priv->port;
767 struct circ_buf *xmit = &port->state->xmit;
768 int fifo_size;
769 int tx_size;
770 int size;
771 int tx_empty;
772
773 if (!priv->start_tx) {
774 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
775 __func__, jiffies);
776 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
777 priv->tx_empty = 1;
778 return 0;
779 }
780
781 fifo_size = max(priv->fifo_size, 1);
782 tx_empty = 1;
783 if (pop_tx_x(priv, xmit->buf)) {
784 pch_uart_hal_write(priv, xmit->buf, 1);
785 port->icount.tx++;
786 tx_empty = 0;
787 fifo_size--;
788 }
789 size = min(xmit->head - xmit->tail, fifo_size);
790 if (size < 0)
791 size = fifo_size;
792
793 tx_size = pop_tx(priv, size);
794 if (tx_size > 0) {
795 port->icount.tx += tx_size;
796 tx_empty = 0;
797 }
798
799 priv->tx_empty = tx_empty;
800
801 if (tx_empty) {
802 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
803 uart_write_wakeup(port);
804 }
805
806 return PCH_UART_HANDLED_TX_INT;
807}
808
809static unsigned int dma_handle_tx(struct eg20t_port *priv)
810{
811 struct uart_port *port = &priv->port;
812 struct circ_buf *xmit = &port->state->xmit;
813 struct scatterlist *sg;
814 int nent;
815 int fifo_size;
816 int tx_empty;
817 struct dma_async_tx_descriptor *desc;
818 int num;
819 int i;
820 int bytes;
821 int size;
822 int rem;
823
824 if (!priv->start_tx) {
825 dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
826 __func__, jiffies);
827 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
828 priv->tx_empty = 1;
829 return 0;
830 }
831
832 if (priv->tx_dma_use) {
833 dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
834 __func__, jiffies);
835 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
836 priv->tx_empty = 1;
837 return 0;
838 }
839
840 fifo_size = max(priv->fifo_size, 1);
841 tx_empty = 1;
842 if (pop_tx_x(priv, xmit->buf)) {
843 pch_uart_hal_write(priv, xmit->buf, 1);
844 port->icount.tx++;
845 tx_empty = 0;
846 fifo_size--;
847 }
848
849 bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
850 UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
851 xmit->tail, UART_XMIT_SIZE));
852 if (!bytes) {
853 dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
854 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
855 uart_write_wakeup(port);
856 return 0;
857 }
858
859 if (bytes > fifo_size) {
860 num = bytes / fifo_size + 1;
861 size = fifo_size;
862 rem = bytes % fifo_size;
863 } else {
864 num = 1;
865 size = bytes;
866 rem = bytes;
867 }
868
869 dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
870 __func__, num, size, rem);
871
872 priv->tx_dma_use = 1;
873
874 priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
875
876 sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
877 sg = priv->sg_tx_p;
878
879 for (i = 0; i < num; i++, sg++) {
880 if (i == (num - 1))
881 sg_set_page(sg, virt_to_page(xmit->buf),
882 rem, fifo_size * i);
883 else
884 sg_set_page(sg, virt_to_page(xmit->buf),
885 size, fifo_size * i);
886 }
887
888 sg = priv->sg_tx_p;
889 nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
890 if (!nent) {
891 dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
892 return 0;
893 }
894 priv->nent = nent;
895
896 for (i = 0; i < nent; i++, sg++) {
897 sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
898 fifo_size * i;
899 sg_dma_address(sg) = (sg_dma_address(sg) &
900 ~(UART_XMIT_SIZE - 1)) + sg->offset;
901 if (i == (nent - 1))
902 sg_dma_len(sg) = rem;
903 else
904 sg_dma_len(sg) = size;
905 }
906
907 desc = priv->chan_tx->device->device_prep_slave_sg(priv->chan_tx,
908 priv->sg_tx_p, nent, DMA_TO_DEVICE,
909 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
910 if (!desc) {
911 dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
912 __func__);
913 return 0;
914 }
915 dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
916 priv->desc_tx = desc;
917 desc->callback = pch_dma_tx_complete;
918 desc->callback_param = priv;
919
920 desc->tx_submit(desc);
921
922 dma_async_issue_pending(priv->chan_tx);
923
924 return PCH_UART_HANDLED_TX_INT;
925}
926
927static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
928{
929 u8 fcr = ioread8(priv->membase + UART_FCR);
930
931 /* Reset FIFO */
932 fcr |= UART_FCR_CLEAR_RCVR;
933 iowrite8(fcr, priv->membase + UART_FCR);
934
935 if (lsr & PCH_UART_LSR_ERR)
936 dev_err(&priv->pdev->dev, "Error data in FIFO\n");
937
938 if (lsr & UART_LSR_FE)
939 dev_err(&priv->pdev->dev, "Framing Error\n");
940
941 if (lsr & UART_LSR_PE)
942 dev_err(&priv->pdev->dev, "Parity Error\n");
943
944 if (lsr & UART_LSR_OE)
945 dev_err(&priv->pdev->dev, "Overrun Error\n");
946}
947
948static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
949{
950 struct eg20t_port *priv = dev_id;
951 unsigned int handled;
952 u8 lsr;
953 int ret = 0;
954 unsigned int iid;
955 unsigned long flags;
956
957 spin_lock_irqsave(&priv->port.lock, flags);
958 handled = 0;
959 while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
960 switch (iid) {
961 case PCH_UART_IID_RLS: /* Receiver Line Status */
962 lsr = pch_uart_hal_get_line_status(priv);
963 if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
964 UART_LSR_PE | UART_LSR_OE)) {
965 pch_uart_err_ir(priv, lsr);
966 ret = PCH_UART_HANDLED_RX_ERR_INT;
967 }
968 break;
969 case PCH_UART_IID_RDR: /* Received Data Ready */
970 if (priv->use_dma) {
971 pch_uart_hal_disable_interrupt(priv,
972 PCH_UART_HAL_RX_INT);
973 ret = dma_handle_rx(priv);
974 if (!ret)
975 pch_uart_hal_enable_interrupt(priv,
976 PCH_UART_HAL_RX_INT);
977 } else {
978 ret = handle_rx(priv);
979 }
980 break;
981 case PCH_UART_IID_RDR_TO: /* Received Data Ready
982 (FIFO Timeout) */
983 ret = handle_rx_to(priv);
984 break;
985 case PCH_UART_IID_THRE: /* Transmitter Holding Register
986 Empty */
987 if (priv->use_dma)
988 ret = dma_handle_tx(priv);
989 else
990 ret = handle_tx(priv);
991 break;
992 case PCH_UART_IID_MS: /* Modem Status */
993 ret = PCH_UART_HANDLED_MS_INT;
994 break;
995 default: /* Never junp to this label */
996 dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__,
997 iid, jiffies);
998 ret = -1;
999 break;
1000 }
1001 handled |= (unsigned int)ret;
1002 }
1003 if (handled == 0 && iid <= 1) {
1004 if (priv->int_dis_flag)
1005 priv->int_dis_flag = 0;
1006 }
1007
1008 spin_unlock_irqrestore(&priv->port.lock, flags);
1009 return IRQ_RETVAL(handled);
1010}
1011
1012/* This function tests whether the transmitter fifo and shifter for the port
1013 described by 'port' is empty. */
1014static unsigned int pch_uart_tx_empty(struct uart_port *port)
1015{
1016 struct eg20t_port *priv;
1017 int ret;
1018 priv = container_of(port, struct eg20t_port, port);
1019 if (priv->tx_empty)
1020 ret = TIOCSER_TEMT;
1021 else
1022 ret = 0;
1023
1024 return ret;
1025}
1026
1027/* Returns the current state of modem control inputs. */
1028static unsigned int pch_uart_get_mctrl(struct uart_port *port)
1029{
1030 struct eg20t_port *priv;
1031 u8 modem;
1032 unsigned int ret = 0;
1033
1034 priv = container_of(port, struct eg20t_port, port);
1035 modem = pch_uart_hal_get_modem(priv);
1036
1037 if (modem & UART_MSR_DCD)
1038 ret |= TIOCM_CAR;
1039
1040 if (modem & UART_MSR_RI)
1041 ret |= TIOCM_RNG;
1042
1043 if (modem & UART_MSR_DSR)
1044 ret |= TIOCM_DSR;
1045
1046 if (modem & UART_MSR_CTS)
1047 ret |= TIOCM_CTS;
1048
1049 return ret;
1050}
1051
1052static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
1053{
1054 u32 mcr = 0;
1055 struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
1056
1057 if (mctrl & TIOCM_DTR)
1058 mcr |= UART_MCR_DTR;
1059 if (mctrl & TIOCM_RTS)
1060 mcr |= UART_MCR_RTS;
1061 if (mctrl & TIOCM_LOOP)
1062 mcr |= UART_MCR_LOOP;
1063
1064 if (priv->mcr & UART_MCR_AFE)
1065 mcr |= UART_MCR_AFE;
1066
1067 if (mctrl)
1068 iowrite8(mcr, priv->membase + UART_MCR);
1069}
1070
1071static void pch_uart_stop_tx(struct uart_port *port)
1072{
1073 struct eg20t_port *priv;
1074 priv = container_of(port, struct eg20t_port, port);
1075 priv->start_tx = 0;
1076 priv->tx_dma_use = 0;
1077}
1078
1079static void pch_uart_start_tx(struct uart_port *port)
1080{
1081 struct eg20t_port *priv;
1082
1083 priv = container_of(port, struct eg20t_port, port);
1084
1085 if (priv->use_dma) {
1086 if (priv->tx_dma_use) {
1087 dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
1088 __func__);
1089 return;
1090 }
1091 }
1092
1093 priv->start_tx = 1;
1094 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
1095}
1096
1097static void pch_uart_stop_rx(struct uart_port *port)
1098{
1099 struct eg20t_port *priv;
1100 priv = container_of(port, struct eg20t_port, port);
1101 priv->start_rx = 0;
1102 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
1103 priv->int_dis_flag = 1;
1104}
1105
1106/* Enable the modem status interrupts. */
1107static void pch_uart_enable_ms(struct uart_port *port)
1108{
1109 struct eg20t_port *priv;
1110 priv = container_of(port, struct eg20t_port, port);
1111 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
1112}
1113
1114/* Control the transmission of a break signal. */
1115static void pch_uart_break_ctl(struct uart_port *port, int ctl)
1116{
1117 struct eg20t_port *priv;
1118 unsigned long flags;
1119
1120 priv = container_of(port, struct eg20t_port, port);
1121 spin_lock_irqsave(&port->lock, flags);
1122 pch_uart_hal_set_break(priv, ctl);
1123 spin_unlock_irqrestore(&port->lock, flags);
1124}
1125
1126/* Grab any interrupt resources and initialise any low level driver state. */
1127static int pch_uart_startup(struct uart_port *port)
1128{
1129 struct eg20t_port *priv;
1130 int ret;
1131 int fifo_size;
1132 int trigger_level;
1133
1134 priv = container_of(port, struct eg20t_port, port);
1135 priv->tx_empty = 1;
1136
1137 if (port->uartclk)
1138 priv->base_baud = port->uartclk;
1139 else
1140 port->uartclk = priv->base_baud;
1141
1142 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1143 ret = pch_uart_hal_set_line(priv, default_baud,
1144 PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
1145 PCH_UART_HAL_STB1);
1146 if (ret)
1147 return ret;
1148
1149 switch (priv->fifo_size) {
1150 case 256:
1151 fifo_size = PCH_UART_HAL_FIFO256;
1152 break;
1153 case 64:
1154 fifo_size = PCH_UART_HAL_FIFO64;
1155 break;
1156 case 16:
1157 fifo_size = PCH_UART_HAL_FIFO16;
1158 case 1:
1159 default:
1160 fifo_size = PCH_UART_HAL_FIFO_DIS;
1161 break;
1162 }
1163
1164 switch (priv->trigger) {
1165 case PCH_UART_HAL_TRIGGER1:
1166 trigger_level = 1;
1167 break;
1168 case PCH_UART_HAL_TRIGGER_L:
1169 trigger_level = priv->fifo_size / 4;
1170 break;
1171 case PCH_UART_HAL_TRIGGER_M:
1172 trigger_level = priv->fifo_size / 2;
1173 break;
1174 case PCH_UART_HAL_TRIGGER_H:
1175 default:
1176 trigger_level = priv->fifo_size - (priv->fifo_size / 8);
1177 break;
1178 }
1179
1180 priv->trigger_level = trigger_level;
1181 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1182 fifo_size, priv->trigger);
1183 if (ret < 0)
1184 return ret;
1185
1186 ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
1187 KBUILD_MODNAME, priv);
1188 if (ret < 0)
1189 return ret;
1190
1191 if (priv->use_dma)
1192 pch_request_dma(port);
1193
1194 priv->start_rx = 1;
1195 pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
1196 uart_update_timeout(port, CS8, default_baud);
1197
1198 return 0;
1199}
1200
1201static void pch_uart_shutdown(struct uart_port *port)
1202{
1203 struct eg20t_port *priv;
1204 int ret;
1205
1206 priv = container_of(port, struct eg20t_port, port);
1207 pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
1208 pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
1209 ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
1210 PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
1211 if (ret)
1212 dev_err(priv->port.dev,
1213 "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
1214
1215 if (priv->use_dma_flag)
1216 pch_free_dma(port);
1217
1218 free_irq(priv->port.irq, priv);
1219}
1220
1221/* Change the port parameters, including word length, parity, stop
1222 *bits. Update read_status_mask and ignore_status_mask to indicate
1223 *the types of events we are interested in receiving. */
1224static void pch_uart_set_termios(struct uart_port *port,
1225 struct ktermios *termios, struct ktermios *old)
1226{
1227 int baud;
1228 int rtn;
1229 unsigned int parity, bits, stb;
1230 struct eg20t_port *priv;
1231 unsigned long flags;
1232
1233 priv = container_of(port, struct eg20t_port, port);
1234 switch (termios->c_cflag & CSIZE) {
1235 case CS5:
1236 bits = PCH_UART_HAL_5BIT;
1237 break;
1238 case CS6:
1239 bits = PCH_UART_HAL_6BIT;
1240 break;
1241 case CS7:
1242 bits = PCH_UART_HAL_7BIT;
1243 break;
1244 default: /* CS8 */
1245 bits = PCH_UART_HAL_8BIT;
1246 break;
1247 }
1248 if (termios->c_cflag & CSTOPB)
1249 stb = PCH_UART_HAL_STB2;
1250 else
1251 stb = PCH_UART_HAL_STB1;
1252
1253 if (termios->c_cflag & PARENB) {
1254 if (!(termios->c_cflag & PARODD))
1255 parity = PCH_UART_HAL_PARITY_ODD;
1256 else
1257 parity = PCH_UART_HAL_PARITY_EVEN;
1258
1259 } else {
1260 parity = PCH_UART_HAL_PARITY_NONE;
1261 }
1262
1263 /* Only UART0 has auto hardware flow function */
1264 if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
1265 priv->mcr |= UART_MCR_AFE;
1266 else
1267 priv->mcr &= ~UART_MCR_AFE;
1268
1269 termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
1270
1271 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1272
1273 spin_lock_irqsave(&port->lock, flags);
1274
1275 uart_update_timeout(port, termios->c_cflag, baud);
1276 rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
1277 if (rtn)
1278 goto out;
1279
1280 /* Don't rewrite B0 */
1281 if (tty_termios_baud_rate(termios))
1282 tty_termios_encode_baud_rate(termios, baud, baud);
1283
1284out:
1285 spin_unlock_irqrestore(&port->lock, flags);
1286}
1287
1288static const char *pch_uart_type(struct uart_port *port)
1289{
1290 return KBUILD_MODNAME;
1291}
1292
1293static void pch_uart_release_port(struct uart_port *port)
1294{
1295 struct eg20t_port *priv;
1296
1297 priv = container_of(port, struct eg20t_port, port);
1298 pci_iounmap(priv->pdev, priv->membase);
1299 pci_release_regions(priv->pdev);
1300}
1301
1302static int pch_uart_request_port(struct uart_port *port)
1303{
1304 struct eg20t_port *priv;
1305 int ret;
1306 void __iomem *membase;
1307
1308 priv = container_of(port, struct eg20t_port, port);
1309 ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
1310 if (ret < 0)
1311 return -EBUSY;
1312
1313 membase = pci_iomap(priv->pdev, 1, 0);
1314 if (!membase) {
1315 pci_release_regions(priv->pdev);
1316 return -EBUSY;
1317 }
1318 priv->membase = port->membase = membase;
1319
1320 return 0;
1321}
1322
1323static void pch_uart_config_port(struct uart_port *port, int type)
1324{
1325 struct eg20t_port *priv;
1326
1327 priv = container_of(port, struct eg20t_port, port);
1328 if (type & UART_CONFIG_TYPE) {
1329 port->type = priv->port_type;
1330 pch_uart_request_port(port);
1331 }
1332}
1333
1334static int pch_uart_verify_port(struct uart_port *port,
1335 struct serial_struct *serinfo)
1336{
1337 struct eg20t_port *priv;
1338
1339 priv = container_of(port, struct eg20t_port, port);
1340 if (serinfo->flags & UPF_LOW_LATENCY) {
1341 dev_info(priv->port.dev,
1342 "PCH UART : Use PIO Mode (without DMA)\n");
1343 priv->use_dma = 0;
1344 serinfo->flags &= ~UPF_LOW_LATENCY;
1345 } else {
1346#ifndef CONFIG_PCH_DMA
1347 dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
1348 __func__);
1349 return -EOPNOTSUPP;
1350#endif
1351 priv->use_dma = 1;
1352 priv->use_dma_flag = 1;
1353 dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
1354 }
1355
1356 return 0;
1357}
1358
1359static struct uart_ops pch_uart_ops = {
1360 .tx_empty = pch_uart_tx_empty,
1361 .set_mctrl = pch_uart_set_mctrl,
1362 .get_mctrl = pch_uart_get_mctrl,
1363 .stop_tx = pch_uart_stop_tx,
1364 .start_tx = pch_uart_start_tx,
1365 .stop_rx = pch_uart_stop_rx,
1366 .enable_ms = pch_uart_enable_ms,
1367 .break_ctl = pch_uart_break_ctl,
1368 .startup = pch_uart_startup,
1369 .shutdown = pch_uart_shutdown,
1370 .set_termios = pch_uart_set_termios,
1371/* .pm = pch_uart_pm, Not supported yet */
1372/* .set_wake = pch_uart_set_wake, Not supported yet */
1373 .type = pch_uart_type,
1374 .release_port = pch_uart_release_port,
1375 .request_port = pch_uart_request_port,
1376 .config_port = pch_uart_config_port,
1377 .verify_port = pch_uart_verify_port
1378};
1379
1380static struct uart_driver pch_uart_driver = {
1381 .owner = THIS_MODULE,
1382 .driver_name = KBUILD_MODNAME,
1383 .dev_name = PCH_UART_DRIVER_DEVICE,
1384 .major = 0,
1385 .minor = 0,
1386 .nr = PCH_UART_NR,
1387};
1388
1389static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
1390 const struct pci_device_id *id)
1391{
1392 struct eg20t_port *priv;
1393 int ret;
1394 unsigned int iobase;
1395 unsigned int mapbase;
1396 unsigned char *rxbuf;
1397 int fifosize, base_baud;
1398 int port_type;
1399 struct pch_uart_driver_data *board;
1400 const char *board_name;
1401
1402 board = &drv_dat[id->driver_data];
1403 port_type = board->port_type;
1404
1405 priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
1406 if (priv == NULL)
1407 goto init_port_alloc_err;
1408
1409 rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
1410 if (!rxbuf)
1411 goto init_port_free_txbuf;
1412
1413 base_baud = 1843200; /* 1.8432MHz */
1414
1415 /* quirk for CM-iTC board */
1416 board_name = dmi_get_system_info(DMI_BOARD_NAME);
1417 if (board_name && strstr(board_name, "CM-iTC"))
1418 base_baud = 192000000; /* 192.0MHz */
1419
1420 switch (port_type) {
1421 case PORT_UNKNOWN:
1422 fifosize = 256; /* EG20T/ML7213: UART0 */
1423 break;
1424 case PORT_8250:
1425 fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
1426 break;
1427 default:
1428 dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
1429 goto init_port_hal_free;
1430 }
1431
1432 iobase = pci_resource_start(pdev, 0);
1433 mapbase = pci_resource_start(pdev, 1);
1434 priv->mapbase = mapbase;
1435 priv->iobase = iobase;
1436 priv->pdev = pdev;
1437 priv->tx_empty = 1;
1438 priv->rxbuf.buf = rxbuf;
1439 priv->rxbuf.size = PAGE_SIZE;
1440
1441 priv->fifo_size = fifosize;
1442 priv->base_baud = base_baud;
1443 priv->port_type = PORT_MAX_8250 + port_type + 1;
1444 priv->port.dev = &pdev->dev;
1445 priv->port.iobase = iobase;
1446 priv->port.membase = NULL;
1447 priv->port.mapbase = mapbase;
1448 priv->port.irq = pdev->irq;
1449 priv->port.iotype = UPIO_PORT;
1450 priv->port.ops = &pch_uart_ops;
1451 priv->port.flags = UPF_BOOT_AUTOCONF;
1452 priv->port.fifosize = fifosize;
1453 priv->port.line = board->line_no;
1454 priv->trigger = PCH_UART_HAL_TRIGGER_M;
1455
1456 spin_lock_init(&priv->port.lock);
1457
1458 pci_set_drvdata(pdev, priv);
1459 pch_uart_hal_request(pdev, fifosize, base_baud);
1460
1461 ret = uart_add_one_port(&pch_uart_driver, &priv->port);
1462 if (ret < 0)
1463 goto init_port_hal_free;
1464
1465 return priv;
1466
1467init_port_hal_free:
1468 free_page((unsigned long)rxbuf);
1469init_port_free_txbuf:
1470 kfree(priv);
1471init_port_alloc_err:
1472
1473 return NULL;
1474}
1475
1476static void pch_uart_exit_port(struct eg20t_port *priv)
1477{
1478 uart_remove_one_port(&pch_uart_driver, &priv->port);
1479 pci_set_drvdata(priv->pdev, NULL);
1480 free_page((unsigned long)priv->rxbuf.buf);
1481}
1482
1483static void pch_uart_pci_remove(struct pci_dev *pdev)
1484{
1485 struct eg20t_port *priv;
1486
1487 priv = (struct eg20t_port *)pci_get_drvdata(pdev);
1488 pch_uart_exit_port(priv);
1489 pci_disable_device(pdev);
1490 kfree(priv);
1491 return;
1492}
1493#ifdef CONFIG_PM
1494static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1495{
1496 struct eg20t_port *priv = pci_get_drvdata(pdev);
1497
1498 uart_suspend_port(&pch_uart_driver, &priv->port);
1499
1500 pci_save_state(pdev);
1501 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1502 return 0;
1503}
1504
1505static int pch_uart_pci_resume(struct pci_dev *pdev)
1506{
1507 struct eg20t_port *priv = pci_get_drvdata(pdev);
1508 int ret;
1509
1510 pci_set_power_state(pdev, PCI_D0);
1511 pci_restore_state(pdev);
1512
1513 ret = pci_enable_device(pdev);
1514 if (ret) {
1515 dev_err(&pdev->dev,
1516 "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
1517 return ret;
1518 }
1519
1520 uart_resume_port(&pch_uart_driver, &priv->port);
1521
1522 return 0;
1523}
1524#else
1525#define pch_uart_pci_suspend NULL
1526#define pch_uart_pci_resume NULL
1527#endif
1528
1529static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
1530 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
1531 .driver_data = pch_et20t_uart0},
1532 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
1533 .driver_data = pch_et20t_uart1},
1534 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
1535 .driver_data = pch_et20t_uart2},
1536 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
1537 .driver_data = pch_et20t_uart3},
1538 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
1539 .driver_data = pch_ml7213_uart0},
1540 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
1541 .driver_data = pch_ml7213_uart1},
1542 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
1543 .driver_data = pch_ml7213_uart2},
1544 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
1545 .driver_data = pch_ml7223_uart0},
1546 {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
1547 .driver_data = pch_ml7223_uart1},
1548 {0,},
1549};
1550
1551static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
1552 const struct pci_device_id *id)
1553{
1554 int ret;
1555 struct eg20t_port *priv;
1556
1557 ret = pci_enable_device(pdev);
1558 if (ret < 0)
1559 goto probe_error;
1560
1561 priv = pch_uart_init_port(pdev, id);
1562 if (!priv) {
1563 ret = -EBUSY;
1564 goto probe_disable_device;
1565 }
1566 pci_set_drvdata(pdev, priv);
1567
1568 return ret;
1569
1570probe_disable_device:
1571 pci_disable_device(pdev);
1572probe_error:
1573 return ret;
1574}
1575
1576static struct pci_driver pch_uart_pci_driver = {
1577 .name = "pch_uart",
1578 .id_table = pch_uart_pci_id,
1579 .probe = pch_uart_pci_probe,
1580 .remove = __devexit_p(pch_uart_pci_remove),
1581 .suspend = pch_uart_pci_suspend,
1582 .resume = pch_uart_pci_resume,
1583};
1584
1585static int __init pch_uart_module_init(void)
1586{
1587 int ret;
1588
1589 /* register as UART driver */
1590 ret = uart_register_driver(&pch_uart_driver);
1591 if (ret < 0)
1592 return ret;
1593
1594 /* register as PCI driver */
1595 ret = pci_register_driver(&pch_uart_pci_driver);
1596 if (ret < 0)
1597 uart_unregister_driver(&pch_uart_driver);
1598
1599 return ret;
1600}
1601module_init(pch_uart_module_init);
1602
1603static void __exit pch_uart_module_exit(void)
1604{
1605 pci_unregister_driver(&pch_uart_pci_driver);
1606 uart_unregister_driver(&pch_uart_driver);
1607}
1608module_exit(pch_uart_module_exit);
1609
1610MODULE_LICENSE("GPL v2");
1611MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
1612module_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..5acd24a27d08
--- /dev/null
+++ b/drivers/tty/serial/pmac_zilog.c
@@ -0,0 +1,2206 @@
1/*
2 * Driver for PowerMac Z85c30 based ESCC cell found in the
3 * "macio" ASICs of various PowerMac models
4 *
5 * Copyright (C) 2003 Ben. Herrenschmidt (benh@kernel.crashing.org)
6 *
7 * Derived from drivers/macintosh/macserial.c by Paul Mackerras
8 * and drivers/serial/sunzilog.c by David S. Miller
9 *
10 * Hrm... actually, I ripped most of sunzilog (Thanks David !) and
11 * adapted special tweaks needed for us. I don't think it's worth
12 * merging back those though. The DMA code still has to get in
13 * and once done, I expect that driver to remain fairly stable in
14 * the long term, unless we change the driver model again...
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * 2004-08-06 Harald Welte <laforge@gnumonks.org>
31 * - Enable BREAK interrupt
32 * - Add support for sysreq
33 *
34 * TODO: - Add DMA support
35 * - Defer port shutdown to a few seconds after close
36 * - maybe put something right into uap->clk_divisor
37 */
38
39#undef DEBUG
40#undef DEBUG_HARD
41#undef USE_CTRL_O_SYSRQ
42
43#include <linux/module.h>
44#include <linux/tty.h>
45
46#include <linux/tty_flip.h>
47#include <linux/major.h>
48#include <linux/string.h>
49#include <linux/fcntl.h>
50#include <linux/mm.h>
51#include <linux/kernel.h>
52#include <linux/delay.h>
53#include <linux/init.h>
54#include <linux/console.h>
55#include <linux/adb.h>
56#include <linux/pmu.h>
57#include <linux/bitops.h>
58#include <linux/sysrq.h>
59#include <linux/mutex.h>
60#include <asm/sections.h>
61#include <asm/io.h>
62#include <asm/irq.h>
63
64#ifdef CONFIG_PPC_PMAC
65#include <asm/prom.h>
66#include <asm/machdep.h>
67#include <asm/pmac_feature.h>
68#include <asm/dbdma.h>
69#include <asm/macio.h>
70#else
71#include <linux/platform_device.h>
72#define of_machine_is_compatible(x) (0)
73#endif
74
75#if defined (CONFIG_SERIAL_PMACZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
76#define SUPPORT_SYSRQ
77#endif
78
79#include <linux/serial.h>
80#include <linux/serial_core.h>
81
82#include "pmac_zilog.h"
83
84/* Not yet implemented */
85#undef HAS_DBDMA
86
87static char version[] __initdata = "pmac_zilog: 0.6 (Benjamin Herrenschmidt <benh@kernel.crashing.org>)";
88MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
89MODULE_DESCRIPTION("Driver for the Mac and PowerMac serial ports.");
90MODULE_LICENSE("GPL");
91
92#ifdef CONFIG_SERIAL_PMACZILOG_TTYS
93#define PMACZILOG_MAJOR TTY_MAJOR
94#define PMACZILOG_MINOR 64
95#define PMACZILOG_NAME "ttyS"
96#else
97#define PMACZILOG_MAJOR 204
98#define PMACZILOG_MINOR 192
99#define PMACZILOG_NAME "ttyPZ"
100#endif
101
102
103/*
104 * For the sake of early serial console, we can do a pre-probe
105 * (optional) of the ports at rather early boot time.
106 */
107static struct uart_pmac_port pmz_ports[MAX_ZS_PORTS];
108static int pmz_ports_count;
109static DEFINE_MUTEX(pmz_irq_mutex);
110
111static struct uart_driver pmz_uart_reg = {
112 .owner = THIS_MODULE,
113 .driver_name = PMACZILOG_NAME,
114 .dev_name = PMACZILOG_NAME,
115 .major = PMACZILOG_MAJOR,
116 .minor = PMACZILOG_MINOR,
117};
118
119
120/*
121 * Load all registers to reprogram the port
122 * This function must only be called when the TX is not busy. The UART
123 * port lock must be held and local interrupts disabled.
124 */
125static void pmz_load_zsregs(struct uart_pmac_port *uap, u8 *regs)
126{
127 int i;
128
129 if (ZS_IS_ASLEEP(uap))
130 return;
131
132 /* Let pending transmits finish. */
133 for (i = 0; i < 1000; i++) {
134 unsigned char stat = read_zsreg(uap, R1);
135 if (stat & ALL_SNT)
136 break;
137 udelay(100);
138 }
139
140 ZS_CLEARERR(uap);
141 zssync(uap);
142 ZS_CLEARFIFO(uap);
143 zssync(uap);
144 ZS_CLEARERR(uap);
145
146 /* Disable all interrupts. */
147 write_zsreg(uap, R1,
148 regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
149
150 /* Set parity, sync config, stop bits, and clock divisor. */
151 write_zsreg(uap, R4, regs[R4]);
152
153 /* Set misc. TX/RX control bits. */
154 write_zsreg(uap, R10, regs[R10]);
155
156 /* Set TX/RX controls sans the enable bits. */
157 write_zsreg(uap, R3, regs[R3] & ~RxENABLE);
158 write_zsreg(uap, R5, regs[R5] & ~TxENABLE);
159
160 /* now set R7 "prime" on ESCC */
161 write_zsreg(uap, R15, regs[R15] | EN85C30);
162 write_zsreg(uap, R7, regs[R7P]);
163
164 /* make sure we use R7 "non-prime" on ESCC */
165 write_zsreg(uap, R15, regs[R15] & ~EN85C30);
166
167 /* Synchronous mode config. */
168 write_zsreg(uap, R6, regs[R6]);
169 write_zsreg(uap, R7, regs[R7]);
170
171 /* Disable baud generator. */
172 write_zsreg(uap, R14, regs[R14] & ~BRENAB);
173
174 /* Clock mode control. */
175 write_zsreg(uap, R11, regs[R11]);
176
177 /* Lower and upper byte of baud rate generator divisor. */
178 write_zsreg(uap, R12, regs[R12]);
179 write_zsreg(uap, R13, regs[R13]);
180
181 /* Now rewrite R14, with BRENAB (if set). */
182 write_zsreg(uap, R14, regs[R14]);
183
184 /* Reset external status interrupts. */
185 write_zsreg(uap, R0, RES_EXT_INT);
186 write_zsreg(uap, R0, RES_EXT_INT);
187
188 /* Rewrite R3/R5, this time without enables masked. */
189 write_zsreg(uap, R3, regs[R3]);
190 write_zsreg(uap, R5, regs[R5]);
191
192 /* Rewrite R1, this time without IRQ enabled masked. */
193 write_zsreg(uap, R1, regs[R1]);
194
195 /* Enable interrupts */
196 write_zsreg(uap, R9, regs[R9]);
197}
198
199/*
200 * We do like sunzilog to avoid disrupting pending Tx
201 * Reprogram the Zilog channel HW registers with the copies found in the
202 * software state struct. If the transmitter is busy, we defer this update
203 * until the next TX complete interrupt. Else, we do it right now.
204 *
205 * The UART port lock must be held and local interrupts disabled.
206 */
207static void pmz_maybe_update_regs(struct uart_pmac_port *uap)
208{
209 if (!ZS_REGS_HELD(uap)) {
210 if (ZS_TX_ACTIVE(uap)) {
211 uap->flags |= PMACZILOG_FLAG_REGS_HELD;
212 } else {
213 pmz_debug("pmz: maybe_update_regs: updating\n");
214 pmz_load_zsregs(uap, uap->curregs);
215 }
216 }
217}
218
219static struct tty_struct *pmz_receive_chars(struct uart_pmac_port *uap)
220{
221 struct tty_struct *tty = NULL;
222 unsigned char ch, r1, drop, error, flag;
223 int loops = 0;
224
225 /* The interrupt can be enabled when the port isn't open, typically
226 * that happens when using one port is open and the other closed (stale
227 * interrupt) or when one port is used as a console.
228 */
229 if (!ZS_IS_OPEN(uap)) {
230 pmz_debug("pmz: draining input\n");
231 /* Port is closed, drain input data */
232 for (;;) {
233 if ((++loops) > 1000)
234 goto flood;
235 (void)read_zsreg(uap, R1);
236 write_zsreg(uap, R0, ERR_RES);
237 (void)read_zsdata(uap);
238 ch = read_zsreg(uap, R0);
239 if (!(ch & Rx_CH_AV))
240 break;
241 }
242 return NULL;
243 }
244
245 /* Sanity check, make sure the old bug is no longer happening */
246 if (uap->port.state == NULL || uap->port.state->port.tty == NULL) {
247 WARN_ON(1);
248 (void)read_zsdata(uap);
249 return NULL;
250 }
251 tty = uap->port.state->port.tty;
252
253 while (1) {
254 error = 0;
255 drop = 0;
256
257 r1 = read_zsreg(uap, R1);
258 ch = read_zsdata(uap);
259
260 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
261 write_zsreg(uap, R0, ERR_RES);
262 zssync(uap);
263 }
264
265 ch &= uap->parity_mask;
266 if (ch == 0 && uap->flags & PMACZILOG_FLAG_BREAK) {
267 uap->flags &= ~PMACZILOG_FLAG_BREAK;
268 }
269
270#if defined(CONFIG_MAGIC_SYSRQ) && defined(CONFIG_SERIAL_CORE_CONSOLE)
271#ifdef USE_CTRL_O_SYSRQ
272 /* Handle the SysRq ^O Hack */
273 if (ch == '\x0f') {
274 uap->port.sysrq = jiffies + HZ*5;
275 goto next_char;
276 }
277#endif /* USE_CTRL_O_SYSRQ */
278 if (uap->port.sysrq) {
279 int swallow;
280 spin_unlock(&uap->port.lock);
281 swallow = uart_handle_sysrq_char(&uap->port, ch);
282 spin_lock(&uap->port.lock);
283 if (swallow)
284 goto next_char;
285 }
286#endif /* CONFIG_MAGIC_SYSRQ && CONFIG_SERIAL_CORE_CONSOLE */
287
288 /* A real serial line, record the character and status. */
289 if (drop)
290 goto next_char;
291
292 flag = TTY_NORMAL;
293 uap->port.icount.rx++;
294
295 if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR | BRK_ABRT)) {
296 error = 1;
297 if (r1 & BRK_ABRT) {
298 pmz_debug("pmz: got break !\n");
299 r1 &= ~(PAR_ERR | CRC_ERR);
300 uap->port.icount.brk++;
301 if (uart_handle_break(&uap->port))
302 goto next_char;
303 }
304 else if (r1 & PAR_ERR)
305 uap->port.icount.parity++;
306 else if (r1 & CRC_ERR)
307 uap->port.icount.frame++;
308 if (r1 & Rx_OVR)
309 uap->port.icount.overrun++;
310 r1 &= uap->port.read_status_mask;
311 if (r1 & BRK_ABRT)
312 flag = TTY_BREAK;
313 else if (r1 & PAR_ERR)
314 flag = TTY_PARITY;
315 else if (r1 & CRC_ERR)
316 flag = TTY_FRAME;
317 }
318
319 if (uap->port.ignore_status_mask == 0xff ||
320 (r1 & uap->port.ignore_status_mask) == 0) {
321 tty_insert_flip_char(tty, ch, flag);
322 }
323 if (r1 & Rx_OVR)
324 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
325 next_char:
326 /* We can get stuck in an infinite loop getting char 0 when the
327 * line is in a wrong HW state, we break that here.
328 * When that happens, I disable the receive side of the driver.
329 * Note that what I've been experiencing is a real irq loop where
330 * I'm getting flooded regardless of the actual port speed.
331 * Something strange is going on with the HW
332 */
333 if ((++loops) > 1000)
334 goto flood;
335 ch = read_zsreg(uap, R0);
336 if (!(ch & Rx_CH_AV))
337 break;
338 }
339
340 return tty;
341 flood:
342 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
343 write_zsreg(uap, R1, uap->curregs[R1]);
344 zssync(uap);
345 pmz_error("pmz: rx irq flood !\n");
346 return tty;
347}
348
349static void pmz_status_handle(struct uart_pmac_port *uap)
350{
351 unsigned char status;
352
353 status = read_zsreg(uap, R0);
354 write_zsreg(uap, R0, RES_EXT_INT);
355 zssync(uap);
356
357 if (ZS_IS_OPEN(uap) && ZS_WANTS_MODEM_STATUS(uap)) {
358 if (status & SYNC_HUNT)
359 uap->port.icount.dsr++;
360
361 /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
362 * But it does not tell us which bit has changed, we have to keep
363 * track of this ourselves.
364 * The CTS input is inverted for some reason. -- paulus
365 */
366 if ((status ^ uap->prev_status) & DCD)
367 uart_handle_dcd_change(&uap->port,
368 (status & DCD));
369 if ((status ^ uap->prev_status) & CTS)
370 uart_handle_cts_change(&uap->port,
371 !(status & CTS));
372
373 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
374 }
375
376 if (status & BRK_ABRT)
377 uap->flags |= PMACZILOG_FLAG_BREAK;
378
379 uap->prev_status = status;
380}
381
382static void pmz_transmit_chars(struct uart_pmac_port *uap)
383{
384 struct circ_buf *xmit;
385
386 if (ZS_IS_ASLEEP(uap))
387 return;
388 if (ZS_IS_CONS(uap)) {
389 unsigned char status = read_zsreg(uap, R0);
390
391 /* TX still busy? Just wait for the next TX done interrupt.
392 *
393 * It can occur because of how we do serial console writes. It would
394 * be nice to transmit console writes just like we normally would for
395 * a TTY line. (ie. buffered and TX interrupt driven). That is not
396 * easy because console writes cannot sleep. One solution might be
397 * to poll on enough port->xmit space becoming free. -DaveM
398 */
399 if (!(status & Tx_BUF_EMP))
400 return;
401 }
402
403 uap->flags &= ~PMACZILOG_FLAG_TX_ACTIVE;
404
405 if (ZS_REGS_HELD(uap)) {
406 pmz_load_zsregs(uap, uap->curregs);
407 uap->flags &= ~PMACZILOG_FLAG_REGS_HELD;
408 }
409
410 if (ZS_TX_STOPPED(uap)) {
411 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
412 goto ack_tx_int;
413 }
414
415 /* Under some circumstances, we see interrupts reported for
416 * a closed channel. The interrupt mask in R1 is clear, but
417 * R3 still signals the interrupts and we see them when taking
418 * an interrupt for the other channel (this could be a qemu
419 * bug but since the ESCC doc doesn't specify precsiely whether
420 * R3 interrup status bits are masked by R1 interrupt enable
421 * bits, better safe than sorry). --BenH.
422 */
423 if (!ZS_IS_OPEN(uap))
424 goto ack_tx_int;
425
426 if (uap->port.x_char) {
427 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
428 write_zsdata(uap, uap->port.x_char);
429 zssync(uap);
430 uap->port.icount.tx++;
431 uap->port.x_char = 0;
432 return;
433 }
434
435 if (uap->port.state == NULL)
436 goto ack_tx_int;
437 xmit = &uap->port.state->xmit;
438 if (uart_circ_empty(xmit)) {
439 uart_write_wakeup(&uap->port);
440 goto ack_tx_int;
441 }
442 if (uart_tx_stopped(&uap->port))
443 goto ack_tx_int;
444
445 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
446 write_zsdata(uap, xmit->buf[xmit->tail]);
447 zssync(uap);
448
449 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
450 uap->port.icount.tx++;
451
452 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
453 uart_write_wakeup(&uap->port);
454
455 return;
456
457ack_tx_int:
458 write_zsreg(uap, R0, RES_Tx_P);
459 zssync(uap);
460}
461
462/* Hrm... we register that twice, fixme later.... */
463static irqreturn_t pmz_interrupt(int irq, void *dev_id)
464{
465 struct uart_pmac_port *uap = dev_id;
466 struct uart_pmac_port *uap_a;
467 struct uart_pmac_port *uap_b;
468 int rc = IRQ_NONE;
469 struct tty_struct *tty;
470 u8 r3;
471
472 uap_a = pmz_get_port_A(uap);
473 uap_b = uap_a->mate;
474
475 spin_lock(&uap_a->port.lock);
476 r3 = read_zsreg(uap_a, R3);
477
478#ifdef DEBUG_HARD
479 pmz_debug("irq, r3: %x\n", r3);
480#endif
481 /* Channel A */
482 tty = NULL;
483 if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
484 write_zsreg(uap_a, R0, RES_H_IUS);
485 zssync(uap_a);
486 if (r3 & CHAEXT)
487 pmz_status_handle(uap_a);
488 if (r3 & CHARxIP)
489 tty = pmz_receive_chars(uap_a);
490 if (r3 & CHATxIP)
491 pmz_transmit_chars(uap_a);
492 rc = IRQ_HANDLED;
493 }
494 spin_unlock(&uap_a->port.lock);
495 if (tty != NULL)
496 tty_flip_buffer_push(tty);
497
498 if (uap_b->node == NULL)
499 goto out;
500
501 spin_lock(&uap_b->port.lock);
502 tty = NULL;
503 if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
504 write_zsreg(uap_b, R0, RES_H_IUS);
505 zssync(uap_b);
506 if (r3 & CHBEXT)
507 pmz_status_handle(uap_b);
508 if (r3 & CHBRxIP)
509 tty = pmz_receive_chars(uap_b);
510 if (r3 & CHBTxIP)
511 pmz_transmit_chars(uap_b);
512 rc = IRQ_HANDLED;
513 }
514 spin_unlock(&uap_b->port.lock);
515 if (tty != NULL)
516 tty_flip_buffer_push(tty);
517
518 out:
519#ifdef DEBUG_HARD
520 pmz_debug("irq done.\n");
521#endif
522 return rc;
523}
524
525/*
526 * Peek the status register, lock not held by caller
527 */
528static inline u8 pmz_peek_status(struct uart_pmac_port *uap)
529{
530 unsigned long flags;
531 u8 status;
532
533 spin_lock_irqsave(&uap->port.lock, flags);
534 status = read_zsreg(uap, R0);
535 spin_unlock_irqrestore(&uap->port.lock, flags);
536
537 return status;
538}
539
540/*
541 * Check if transmitter is empty
542 * The port lock is not held.
543 */
544static unsigned int pmz_tx_empty(struct uart_port *port)
545{
546 struct uart_pmac_port *uap = to_pmz(port);
547 unsigned char status;
548
549 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
550 return TIOCSER_TEMT;
551
552 status = pmz_peek_status(to_pmz(port));
553 if (status & Tx_BUF_EMP)
554 return TIOCSER_TEMT;
555 return 0;
556}
557
558/*
559 * Set Modem Control (RTS & DTR) bits
560 * The port lock is held and interrupts are disabled.
561 * Note: Shall we really filter out RTS on external ports or
562 * should that be dealt at higher level only ?
563 */
564static void pmz_set_mctrl(struct uart_port *port, unsigned int mctrl)
565{
566 struct uart_pmac_port *uap = to_pmz(port);
567 unsigned char set_bits, clear_bits;
568
569 /* Do nothing for irda for now... */
570 if (ZS_IS_IRDA(uap))
571 return;
572 /* We get called during boot with a port not up yet */
573 if (ZS_IS_ASLEEP(uap) ||
574 !(ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)))
575 return;
576
577 set_bits = clear_bits = 0;
578
579 if (ZS_IS_INTMODEM(uap)) {
580 if (mctrl & TIOCM_RTS)
581 set_bits |= RTS;
582 else
583 clear_bits |= RTS;
584 }
585 if (mctrl & TIOCM_DTR)
586 set_bits |= DTR;
587 else
588 clear_bits |= DTR;
589
590 /* NOTE: Not subject to 'transmitter active' rule. */
591 uap->curregs[R5] |= set_bits;
592 uap->curregs[R5] &= ~clear_bits;
593 if (ZS_IS_ASLEEP(uap))
594 return;
595 write_zsreg(uap, R5, uap->curregs[R5]);
596 pmz_debug("pmz_set_mctrl: set bits: %x, clear bits: %x -> %x\n",
597 set_bits, clear_bits, uap->curregs[R5]);
598 zssync(uap);
599}
600
601/*
602 * Get Modem Control bits (only the input ones, the core will
603 * or that with a cached value of the control ones)
604 * The port lock is held and interrupts are disabled.
605 */
606static unsigned int pmz_get_mctrl(struct uart_port *port)
607{
608 struct uart_pmac_port *uap = to_pmz(port);
609 unsigned char status;
610 unsigned int ret;
611
612 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
613 return 0;
614
615 status = read_zsreg(uap, R0);
616
617 ret = 0;
618 if (status & DCD)
619 ret |= TIOCM_CAR;
620 if (status & SYNC_HUNT)
621 ret |= TIOCM_DSR;
622 if (!(status & CTS))
623 ret |= TIOCM_CTS;
624
625 return ret;
626}
627
628/*
629 * Stop TX side. Dealt like sunzilog at next Tx interrupt,
630 * though for DMA, we will have to do a bit more.
631 * The port lock is held and interrupts are disabled.
632 */
633static void pmz_stop_tx(struct uart_port *port)
634{
635 to_pmz(port)->flags |= PMACZILOG_FLAG_TX_STOPPED;
636}
637
638/*
639 * Kick the Tx side.
640 * The port lock is held and interrupts are disabled.
641 */
642static void pmz_start_tx(struct uart_port *port)
643{
644 struct uart_pmac_port *uap = to_pmz(port);
645 unsigned char status;
646
647 pmz_debug("pmz: start_tx()\n");
648
649 uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
650 uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
651
652 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
653 return;
654
655 status = read_zsreg(uap, R0);
656
657 /* TX busy? Just wait for the TX done interrupt. */
658 if (!(status & Tx_BUF_EMP))
659 return;
660
661 /* Send the first character to jump-start the TX done
662 * IRQ sending engine.
663 */
664 if (port->x_char) {
665 write_zsdata(uap, port->x_char);
666 zssync(uap);
667 port->icount.tx++;
668 port->x_char = 0;
669 } else {
670 struct circ_buf *xmit = &port->state->xmit;
671
672 write_zsdata(uap, xmit->buf[xmit->tail]);
673 zssync(uap);
674 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
675 port->icount.tx++;
676
677 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
678 uart_write_wakeup(&uap->port);
679 }
680 pmz_debug("pmz: start_tx() done.\n");
681}
682
683/*
684 * Stop Rx side, basically disable emitting of
685 * Rx interrupts on the port. We don't disable the rx
686 * side of the chip proper though
687 * The port lock is held.
688 */
689static void pmz_stop_rx(struct uart_port *port)
690{
691 struct uart_pmac_port *uap = to_pmz(port);
692
693 if (ZS_IS_ASLEEP(uap) || uap->node == NULL)
694 return;
695
696 pmz_debug("pmz: stop_rx()()\n");
697
698 /* Disable all RX interrupts. */
699 uap->curregs[R1] &= ~RxINT_MASK;
700 pmz_maybe_update_regs(uap);
701
702 pmz_debug("pmz: stop_rx() done.\n");
703}
704
705/*
706 * Enable modem status change interrupts
707 * The port lock is held.
708 */
709static void pmz_enable_ms(struct uart_port *port)
710{
711 struct uart_pmac_port *uap = to_pmz(port);
712 unsigned char new_reg;
713
714 if (ZS_IS_IRDA(uap) || uap->node == NULL)
715 return;
716 new_reg = uap->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
717 if (new_reg != uap->curregs[R15]) {
718 uap->curregs[R15] = new_reg;
719
720 if (ZS_IS_ASLEEP(uap))
721 return;
722 /* NOTE: Not subject to 'transmitter active' rule. */
723 write_zsreg(uap, R15, uap->curregs[R15]);
724 }
725}
726
727/*
728 * Control break state emission
729 * The port lock is not held.
730 */
731static void pmz_break_ctl(struct uart_port *port, int break_state)
732{
733 struct uart_pmac_port *uap = to_pmz(port);
734 unsigned char set_bits, clear_bits, new_reg;
735 unsigned long flags;
736
737 if (uap->node == NULL)
738 return;
739 set_bits = clear_bits = 0;
740
741 if (break_state)
742 set_bits |= SND_BRK;
743 else
744 clear_bits |= SND_BRK;
745
746 spin_lock_irqsave(&port->lock, flags);
747
748 new_reg = (uap->curregs[R5] | set_bits) & ~clear_bits;
749 if (new_reg != uap->curregs[R5]) {
750 uap->curregs[R5] = new_reg;
751
752 /* NOTE: Not subject to 'transmitter active' rule. */
753 if (ZS_IS_ASLEEP(uap)) {
754 spin_unlock_irqrestore(&port->lock, flags);
755 return;
756 }
757 write_zsreg(uap, R5, uap->curregs[R5]);
758 }
759
760 spin_unlock_irqrestore(&port->lock, flags);
761}
762
763#ifdef CONFIG_PPC_PMAC
764
765/*
766 * Turn power on or off to the SCC and associated stuff
767 * (port drivers, modem, IR port, etc.)
768 * Returns the number of milliseconds we should wait before
769 * trying to use the port.
770 */
771static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
772{
773 int delay = 0;
774 int rc;
775
776 if (state) {
777 rc = pmac_call_feature(
778 PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 1);
779 pmz_debug("port power on result: %d\n", rc);
780 if (ZS_IS_INTMODEM(uap)) {
781 rc = pmac_call_feature(
782 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 1);
783 delay = 2500; /* wait for 2.5s before using */
784 pmz_debug("modem power result: %d\n", rc);
785 }
786 } else {
787 /* TODO: Make that depend on a timer, don't power down
788 * immediately
789 */
790 if (ZS_IS_INTMODEM(uap)) {
791 rc = pmac_call_feature(
792 PMAC_FTR_MODEM_ENABLE, uap->node, 0, 0);
793 pmz_debug("port power off result: %d\n", rc);
794 }
795 pmac_call_feature(PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 0);
796 }
797 return delay;
798}
799
800#else
801
802static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
803{
804 return 0;
805}
806
807#endif /* !CONFIG_PPC_PMAC */
808
809/*
810 * FixZeroBug....Works around a bug in the SCC receiving channel.
811 * Inspired from Darwin code, 15 Sept. 2000 -DanM
812 *
813 * The following sequence prevents a problem that is seen with O'Hare ASICs
814 * (most versions -- also with some Heathrow and Hydra ASICs) where a zero
815 * at the input to the receiver becomes 'stuck' and locks up the receiver.
816 * This problem can occur as a result of a zero bit at the receiver input
817 * coincident with any of the following events:
818 *
819 * The SCC is initialized (hardware or software).
820 * A framing error is detected.
821 * The clocking option changes from synchronous or X1 asynchronous
822 * clocking to X16, X32, or X64 asynchronous clocking.
823 * The decoding mode is changed among NRZ, NRZI, FM0, or FM1.
824 *
825 * This workaround attempts to recover from the lockup condition by placing
826 * the SCC in synchronous loopback mode with a fast clock before programming
827 * any of the asynchronous modes.
828 */
829static void pmz_fix_zero_bug_scc(struct uart_pmac_port *uap)
830{
831 write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
832 zssync(uap);
833 udelay(10);
834 write_zsreg(uap, 9, (ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB) | NV);
835 zssync(uap);
836
837 write_zsreg(uap, 4, X1CLK | MONSYNC);
838 write_zsreg(uap, 3, Rx8);
839 write_zsreg(uap, 5, Tx8 | RTS);
840 write_zsreg(uap, 9, NV); /* Didn't we already do this? */
841 write_zsreg(uap, 11, RCBR | TCBR);
842 write_zsreg(uap, 12, 0);
843 write_zsreg(uap, 13, 0);
844 write_zsreg(uap, 14, (LOOPBAK | BRSRC));
845 write_zsreg(uap, 14, (LOOPBAK | BRSRC | BRENAB));
846 write_zsreg(uap, 3, Rx8 | RxENABLE);
847 write_zsreg(uap, 0, RES_EXT_INT);
848 write_zsreg(uap, 0, RES_EXT_INT);
849 write_zsreg(uap, 0, RES_EXT_INT); /* to kill some time */
850
851 /* The channel should be OK now, but it is probably receiving
852 * loopback garbage.
853 * Switch to asynchronous mode, disable the receiver,
854 * and discard everything in the receive buffer.
855 */
856 write_zsreg(uap, 9, NV);
857 write_zsreg(uap, 4, X16CLK | SB_MASK);
858 write_zsreg(uap, 3, Rx8);
859
860 while (read_zsreg(uap, 0) & Rx_CH_AV) {
861 (void)read_zsreg(uap, 8);
862 write_zsreg(uap, 0, RES_EXT_INT);
863 write_zsreg(uap, 0, ERR_RES);
864 }
865}
866
867/*
868 * Real startup routine, powers up the hardware and sets up
869 * the SCC. Returns a delay in ms where you need to wait before
870 * actually using the port, this is typically the internal modem
871 * powerup delay. This routine expect the lock to be taken.
872 */
873static int __pmz_startup(struct uart_pmac_port *uap)
874{
875 int pwr_delay = 0;
876
877 memset(&uap->curregs, 0, sizeof(uap->curregs));
878
879 /* Power up the SCC & underlying hardware (modem/irda) */
880 pwr_delay = pmz_set_scc_power(uap, 1);
881
882 /* Nice buggy HW ... */
883 pmz_fix_zero_bug_scc(uap);
884
885 /* Reset the channel */
886 uap->curregs[R9] = 0;
887 write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
888 zssync(uap);
889 udelay(10);
890 write_zsreg(uap, 9, 0);
891 zssync(uap);
892
893 /* Clear the interrupt registers */
894 write_zsreg(uap, R1, 0);
895 write_zsreg(uap, R0, ERR_RES);
896 write_zsreg(uap, R0, ERR_RES);
897 write_zsreg(uap, R0, RES_H_IUS);
898 write_zsreg(uap, R0, RES_H_IUS);
899
900 /* Setup some valid baud rate */
901 uap->curregs[R4] = X16CLK | SB1;
902 uap->curregs[R3] = Rx8;
903 uap->curregs[R5] = Tx8 | RTS;
904 if (!ZS_IS_IRDA(uap))
905 uap->curregs[R5] |= DTR;
906 uap->curregs[R12] = 0;
907 uap->curregs[R13] = 0;
908 uap->curregs[R14] = BRENAB;
909
910 /* Clear handshaking, enable BREAK interrupts */
911 uap->curregs[R15] = BRKIE;
912
913 /* Master interrupt enable */
914 uap->curregs[R9] |= NV | MIE;
915
916 pmz_load_zsregs(uap, uap->curregs);
917
918 /* Enable receiver and transmitter. */
919 write_zsreg(uap, R3, uap->curregs[R3] |= RxENABLE);
920 write_zsreg(uap, R5, uap->curregs[R5] |= TxENABLE);
921
922 /* Remember status for DCD/CTS changes */
923 uap->prev_status = read_zsreg(uap, R0);
924
925 return pwr_delay;
926}
927
928static void pmz_irda_reset(struct uart_pmac_port *uap)
929{
930 uap->curregs[R5] |= DTR;
931 write_zsreg(uap, R5, uap->curregs[R5]);
932 zssync(uap);
933 mdelay(110);
934 uap->curregs[R5] &= ~DTR;
935 write_zsreg(uap, R5, uap->curregs[R5]);
936 zssync(uap);
937 mdelay(10);
938}
939
940/*
941 * This is the "normal" startup routine, using the above one
942 * wrapped with the lock and doing a schedule delay
943 */
944static int pmz_startup(struct uart_port *port)
945{
946 struct uart_pmac_port *uap = to_pmz(port);
947 unsigned long flags;
948 int pwr_delay = 0;
949
950 pmz_debug("pmz: startup()\n");
951
952 if (ZS_IS_ASLEEP(uap))
953 return -EAGAIN;
954 if (uap->node == NULL)
955 return -ENODEV;
956
957 mutex_lock(&pmz_irq_mutex);
958
959 uap->flags |= PMACZILOG_FLAG_IS_OPEN;
960
961 /* A console is never powered down. Else, power up and
962 * initialize the chip
963 */
964 if (!ZS_IS_CONS(uap)) {
965 spin_lock_irqsave(&port->lock, flags);
966 pwr_delay = __pmz_startup(uap);
967 spin_unlock_irqrestore(&port->lock, flags);
968 }
969
970 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
971 if (request_irq(uap->port.irq, pmz_interrupt, IRQF_SHARED,
972 "SCC", uap)) {
973 pmz_error("Unable to register zs interrupt handler.\n");
974 pmz_set_scc_power(uap, 0);
975 mutex_unlock(&pmz_irq_mutex);
976 return -ENXIO;
977 }
978
979 mutex_unlock(&pmz_irq_mutex);
980
981 /* Right now, we deal with delay by blocking here, I'll be
982 * smarter later on
983 */
984 if (pwr_delay != 0) {
985 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
986 msleep(pwr_delay);
987 }
988
989 /* IrDA reset is done now */
990 if (ZS_IS_IRDA(uap))
991 pmz_irda_reset(uap);
992
993 /* Enable interrupts emission from the chip */
994 spin_lock_irqsave(&port->lock, flags);
995 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
996 if (!ZS_IS_EXTCLK(uap))
997 uap->curregs[R1] |= EXT_INT_ENAB;
998 write_zsreg(uap, R1, uap->curregs[R1]);
999 spin_unlock_irqrestore(&port->lock, flags);
1000
1001 pmz_debug("pmz: startup() done.\n");
1002
1003 return 0;
1004}
1005
1006static void pmz_shutdown(struct uart_port *port)
1007{
1008 struct uart_pmac_port *uap = to_pmz(port);
1009 unsigned long flags;
1010
1011 pmz_debug("pmz: shutdown()\n");
1012
1013 if (uap->node == NULL)
1014 return;
1015
1016 mutex_lock(&pmz_irq_mutex);
1017
1018 /* Release interrupt handler */
1019 free_irq(uap->port.irq, uap);
1020
1021 spin_lock_irqsave(&port->lock, flags);
1022
1023 uap->flags &= ~PMACZILOG_FLAG_IS_OPEN;
1024
1025 if (!ZS_IS_OPEN(uap->mate))
1026 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1027
1028 /* Disable interrupts */
1029 if (!ZS_IS_ASLEEP(uap)) {
1030 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1031 write_zsreg(uap, R1, uap->curregs[R1]);
1032 zssync(uap);
1033 }
1034
1035 if (ZS_IS_CONS(uap) || ZS_IS_ASLEEP(uap)) {
1036 spin_unlock_irqrestore(&port->lock, flags);
1037 mutex_unlock(&pmz_irq_mutex);
1038 return;
1039 }
1040
1041 /* Disable receiver and transmitter. */
1042 uap->curregs[R3] &= ~RxENABLE;
1043 uap->curregs[R5] &= ~TxENABLE;
1044
1045 /* Disable all interrupts and BRK assertion. */
1046 uap->curregs[R5] &= ~SND_BRK;
1047 pmz_maybe_update_regs(uap);
1048
1049 /* Shut the chip down */
1050 pmz_set_scc_power(uap, 0);
1051
1052 spin_unlock_irqrestore(&port->lock, flags);
1053
1054 mutex_unlock(&pmz_irq_mutex);
1055
1056 pmz_debug("pmz: shutdown() done.\n");
1057}
1058
1059/* Shared by TTY driver and serial console setup. The port lock is held
1060 * and local interrupts are disabled.
1061 */
1062static void pmz_convert_to_zs(struct uart_pmac_port *uap, unsigned int cflag,
1063 unsigned int iflag, unsigned long baud)
1064{
1065 int brg;
1066
1067 /* Switch to external clocking for IrDA high clock rates. That
1068 * code could be re-used for Midi interfaces with different
1069 * multipliers
1070 */
1071 if (baud >= 115200 && ZS_IS_IRDA(uap)) {
1072 uap->curregs[R4] = X1CLK;
1073 uap->curregs[R11] = RCTRxCP | TCTRxCP;
1074 uap->curregs[R14] = 0; /* BRG off */
1075 uap->curregs[R12] = 0;
1076 uap->curregs[R13] = 0;
1077 uap->flags |= PMACZILOG_FLAG_IS_EXTCLK;
1078 } else {
1079 switch (baud) {
1080 case ZS_CLOCK/16: /* 230400 */
1081 uap->curregs[R4] = X16CLK;
1082 uap->curregs[R11] = 0;
1083 uap->curregs[R14] = 0;
1084 break;
1085 case ZS_CLOCK/32: /* 115200 */
1086 uap->curregs[R4] = X32CLK;
1087 uap->curregs[R11] = 0;
1088 uap->curregs[R14] = 0;
1089 break;
1090 default:
1091 uap->curregs[R4] = X16CLK;
1092 uap->curregs[R11] = TCBR | RCBR;
1093 brg = BPS_TO_BRG(baud, ZS_CLOCK / 16);
1094 uap->curregs[R12] = (brg & 255);
1095 uap->curregs[R13] = ((brg >> 8) & 255);
1096 uap->curregs[R14] = BRENAB;
1097 }
1098 uap->flags &= ~PMACZILOG_FLAG_IS_EXTCLK;
1099 }
1100
1101 /* Character size, stop bits, and parity. */
1102 uap->curregs[3] &= ~RxN_MASK;
1103 uap->curregs[5] &= ~TxN_MASK;
1104
1105 switch (cflag & CSIZE) {
1106 case CS5:
1107 uap->curregs[3] |= Rx5;
1108 uap->curregs[5] |= Tx5;
1109 uap->parity_mask = 0x1f;
1110 break;
1111 case CS6:
1112 uap->curregs[3] |= Rx6;
1113 uap->curregs[5] |= Tx6;
1114 uap->parity_mask = 0x3f;
1115 break;
1116 case CS7:
1117 uap->curregs[3] |= Rx7;
1118 uap->curregs[5] |= Tx7;
1119 uap->parity_mask = 0x7f;
1120 break;
1121 case CS8:
1122 default:
1123 uap->curregs[3] |= Rx8;
1124 uap->curregs[5] |= Tx8;
1125 uap->parity_mask = 0xff;
1126 break;
1127 };
1128 uap->curregs[4] &= ~(SB_MASK);
1129 if (cflag & CSTOPB)
1130 uap->curregs[4] |= SB2;
1131 else
1132 uap->curregs[4] |= SB1;
1133 if (cflag & PARENB)
1134 uap->curregs[4] |= PAR_ENAB;
1135 else
1136 uap->curregs[4] &= ~PAR_ENAB;
1137 if (!(cflag & PARODD))
1138 uap->curregs[4] |= PAR_EVEN;
1139 else
1140 uap->curregs[4] &= ~PAR_EVEN;
1141
1142 uap->port.read_status_mask = Rx_OVR;
1143 if (iflag & INPCK)
1144 uap->port.read_status_mask |= CRC_ERR | PAR_ERR;
1145 if (iflag & (BRKINT | PARMRK))
1146 uap->port.read_status_mask |= BRK_ABRT;
1147
1148 uap->port.ignore_status_mask = 0;
1149 if (iflag & IGNPAR)
1150 uap->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
1151 if (iflag & IGNBRK) {
1152 uap->port.ignore_status_mask |= BRK_ABRT;
1153 if (iflag & IGNPAR)
1154 uap->port.ignore_status_mask |= Rx_OVR;
1155 }
1156
1157 if ((cflag & CREAD) == 0)
1158 uap->port.ignore_status_mask = 0xff;
1159}
1160
1161
1162/*
1163 * Set the irda codec on the imac to the specified baud rate.
1164 */
1165static void pmz_irda_setup(struct uart_pmac_port *uap, unsigned long *baud)
1166{
1167 u8 cmdbyte;
1168 int t, version;
1169
1170 switch (*baud) {
1171 /* SIR modes */
1172 case 2400:
1173 cmdbyte = 0x53;
1174 break;
1175 case 4800:
1176 cmdbyte = 0x52;
1177 break;
1178 case 9600:
1179 cmdbyte = 0x51;
1180 break;
1181 case 19200:
1182 cmdbyte = 0x50;
1183 break;
1184 case 38400:
1185 cmdbyte = 0x4f;
1186 break;
1187 case 57600:
1188 cmdbyte = 0x4e;
1189 break;
1190 case 115200:
1191 cmdbyte = 0x4d;
1192 break;
1193 /* The FIR modes aren't really supported at this point, how
1194 * do we select the speed ? via the FCR on KeyLargo ?
1195 */
1196 case 1152000:
1197 cmdbyte = 0;
1198 break;
1199 case 4000000:
1200 cmdbyte = 0;
1201 break;
1202 default: /* 9600 */
1203 cmdbyte = 0x51;
1204 *baud = 9600;
1205 break;
1206 }
1207
1208 /* Wait for transmitter to drain */
1209 t = 10000;
1210 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0
1211 || (read_zsreg(uap, R1) & ALL_SNT) == 0) {
1212 if (--t <= 0) {
1213 pmz_error("transmitter didn't drain\n");
1214 return;
1215 }
1216 udelay(10);
1217 }
1218
1219 /* Drain the receiver too */
1220 t = 100;
1221 (void)read_zsdata(uap);
1222 (void)read_zsdata(uap);
1223 (void)read_zsdata(uap);
1224 mdelay(10);
1225 while (read_zsreg(uap, R0) & Rx_CH_AV) {
1226 read_zsdata(uap);
1227 mdelay(10);
1228 if (--t <= 0) {
1229 pmz_error("receiver didn't drain\n");
1230 return;
1231 }
1232 }
1233
1234 /* Switch to command mode */
1235 uap->curregs[R5] |= DTR;
1236 write_zsreg(uap, R5, uap->curregs[R5]);
1237 zssync(uap);
1238 mdelay(1);
1239
1240 /* Switch SCC to 19200 */
1241 pmz_convert_to_zs(uap, CS8, 0, 19200);
1242 pmz_load_zsregs(uap, uap->curregs);
1243 mdelay(1);
1244
1245 /* Write get_version command byte */
1246 write_zsdata(uap, 1);
1247 t = 5000;
1248 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1249 if (--t <= 0) {
1250 pmz_error("irda_setup timed out on get_version byte\n");
1251 goto out;
1252 }
1253 udelay(10);
1254 }
1255 version = read_zsdata(uap);
1256
1257 if (version < 4) {
1258 pmz_info("IrDA: dongle version %d not supported\n", version);
1259 goto out;
1260 }
1261
1262 /* Send speed mode */
1263 write_zsdata(uap, cmdbyte);
1264 t = 5000;
1265 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
1266 if (--t <= 0) {
1267 pmz_error("irda_setup timed out on speed mode byte\n");
1268 goto out;
1269 }
1270 udelay(10);
1271 }
1272 t = read_zsdata(uap);
1273 if (t != cmdbyte)
1274 pmz_error("irda_setup speed mode byte = %x (%x)\n", t, cmdbyte);
1275
1276 pmz_info("IrDA setup for %ld bps, dongle version: %d\n",
1277 *baud, version);
1278
1279 (void)read_zsdata(uap);
1280 (void)read_zsdata(uap);
1281 (void)read_zsdata(uap);
1282
1283 out:
1284 /* Switch back to data mode */
1285 uap->curregs[R5] &= ~DTR;
1286 write_zsreg(uap, R5, uap->curregs[R5]);
1287 zssync(uap);
1288
1289 (void)read_zsdata(uap);
1290 (void)read_zsdata(uap);
1291 (void)read_zsdata(uap);
1292}
1293
1294
1295static void __pmz_set_termios(struct uart_port *port, struct ktermios *termios,
1296 struct ktermios *old)
1297{
1298 struct uart_pmac_port *uap = to_pmz(port);
1299 unsigned long baud;
1300
1301 pmz_debug("pmz: set_termios()\n");
1302
1303 if (ZS_IS_ASLEEP(uap))
1304 return;
1305
1306 memcpy(&uap->termios_cache, termios, sizeof(struct ktermios));
1307
1308 /* XXX Check which revs of machines actually allow 1 and 4Mb speeds
1309 * on the IR dongle. Note that the IRTTY driver currently doesn't know
1310 * about the FIR mode and high speed modes. So these are unused. For
1311 * implementing proper support for these, we should probably add some
1312 * DMA as well, at least on the Rx side, which isn't a simple thing
1313 * at this point.
1314 */
1315 if (ZS_IS_IRDA(uap)) {
1316 /* Calc baud rate */
1317 baud = uart_get_baud_rate(port, termios, old, 1200, 4000000);
1318 pmz_debug("pmz: switch IRDA to %ld bauds\n", baud);
1319 /* Cet the irda codec to the right rate */
1320 pmz_irda_setup(uap, &baud);
1321 /* Set final baud rate */
1322 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1323 pmz_load_zsregs(uap, uap->curregs);
1324 zssync(uap);
1325 } else {
1326 baud = uart_get_baud_rate(port, termios, old, 1200, 230400);
1327 pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
1328 /* Make sure modem status interrupts are correctly configured */
1329 if (UART_ENABLE_MS(&uap->port, termios->c_cflag)) {
1330 uap->curregs[R15] |= DCDIE | SYNCIE | CTSIE;
1331 uap->flags |= PMACZILOG_FLAG_MODEM_STATUS;
1332 } else {
1333 uap->curregs[R15] &= ~(DCDIE | SYNCIE | CTSIE);
1334 uap->flags &= ~PMACZILOG_FLAG_MODEM_STATUS;
1335 }
1336
1337 /* Load registers to the chip */
1338 pmz_maybe_update_regs(uap);
1339 }
1340 uart_update_timeout(port, termios->c_cflag, baud);
1341
1342 pmz_debug("pmz: set_termios() done.\n");
1343}
1344
1345/* The port lock is not held. */
1346static void pmz_set_termios(struct uart_port *port, struct ktermios *termios,
1347 struct ktermios *old)
1348{
1349 struct uart_pmac_port *uap = to_pmz(port);
1350 unsigned long flags;
1351
1352 spin_lock_irqsave(&port->lock, flags);
1353
1354 /* Disable IRQs on the port */
1355 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1356 write_zsreg(uap, R1, uap->curregs[R1]);
1357
1358 /* Setup new port configuration */
1359 __pmz_set_termios(port, termios, old);
1360
1361 /* Re-enable IRQs on the port */
1362 if (ZS_IS_OPEN(uap)) {
1363 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1364 if (!ZS_IS_EXTCLK(uap))
1365 uap->curregs[R1] |= EXT_INT_ENAB;
1366 write_zsreg(uap, R1, uap->curregs[R1]);
1367 }
1368 spin_unlock_irqrestore(&port->lock, flags);
1369}
1370
1371static const char *pmz_type(struct uart_port *port)
1372{
1373 struct uart_pmac_port *uap = to_pmz(port);
1374
1375 if (ZS_IS_IRDA(uap))
1376 return "Z85c30 ESCC - Infrared port";
1377 else if (ZS_IS_INTMODEM(uap))
1378 return "Z85c30 ESCC - Internal modem";
1379 return "Z85c30 ESCC - Serial port";
1380}
1381
1382/* We do not request/release mappings of the registers here, this
1383 * happens at early serial probe time.
1384 */
1385static void pmz_release_port(struct uart_port *port)
1386{
1387}
1388
1389static int pmz_request_port(struct uart_port *port)
1390{
1391 return 0;
1392}
1393
1394/* These do not need to do anything interesting either. */
1395static void pmz_config_port(struct uart_port *port, int flags)
1396{
1397}
1398
1399/* We do not support letting the user mess with the divisor, IRQ, etc. */
1400static int pmz_verify_port(struct uart_port *port, struct serial_struct *ser)
1401{
1402 return -EINVAL;
1403}
1404
1405#ifdef CONFIG_CONSOLE_POLL
1406
1407static int pmz_poll_get_char(struct uart_port *port)
1408{
1409 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1410
1411 while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0)
1412 udelay(5);
1413 return read_zsdata(uap);
1414}
1415
1416static void pmz_poll_put_char(struct uart_port *port, unsigned char c)
1417{
1418 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
1419
1420 /* Wait for the transmit buffer to empty. */
1421 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
1422 udelay(5);
1423 write_zsdata(uap, c);
1424}
1425
1426#endif /* CONFIG_CONSOLE_POLL */
1427
1428static struct uart_ops pmz_pops = {
1429 .tx_empty = pmz_tx_empty,
1430 .set_mctrl = pmz_set_mctrl,
1431 .get_mctrl = pmz_get_mctrl,
1432 .stop_tx = pmz_stop_tx,
1433 .start_tx = pmz_start_tx,
1434 .stop_rx = pmz_stop_rx,
1435 .enable_ms = pmz_enable_ms,
1436 .break_ctl = pmz_break_ctl,
1437 .startup = pmz_startup,
1438 .shutdown = pmz_shutdown,
1439 .set_termios = pmz_set_termios,
1440 .type = pmz_type,
1441 .release_port = pmz_release_port,
1442 .request_port = pmz_request_port,
1443 .config_port = pmz_config_port,
1444 .verify_port = pmz_verify_port,
1445#ifdef CONFIG_CONSOLE_POLL
1446 .poll_get_char = pmz_poll_get_char,
1447 .poll_put_char = pmz_poll_put_char,
1448#endif
1449};
1450
1451#ifdef CONFIG_PPC_PMAC
1452
1453/*
1454 * Setup one port structure after probing, HW is down at this point,
1455 * Unlike sunzilog, we don't need to pre-init the spinlock as we don't
1456 * register our console before uart_add_one_port() is called
1457 */
1458static int __init pmz_init_port(struct uart_pmac_port *uap)
1459{
1460 struct device_node *np = uap->node;
1461 const char *conn;
1462 const struct slot_names_prop {
1463 int count;
1464 char name[1];
1465 } *slots;
1466 int len;
1467 struct resource r_ports, r_rxdma, r_txdma;
1468
1469 /*
1470 * Request & map chip registers
1471 */
1472 if (of_address_to_resource(np, 0, &r_ports))
1473 return -ENODEV;
1474 uap->port.mapbase = r_ports.start;
1475 uap->port.membase = ioremap(uap->port.mapbase, 0x1000);
1476
1477 uap->control_reg = uap->port.membase;
1478 uap->data_reg = uap->control_reg + 0x10;
1479
1480 /*
1481 * Request & map DBDMA registers
1482 */
1483#ifdef HAS_DBDMA
1484 if (of_address_to_resource(np, 1, &r_txdma) == 0 &&
1485 of_address_to_resource(np, 2, &r_rxdma) == 0)
1486 uap->flags |= PMACZILOG_FLAG_HAS_DMA;
1487#else
1488 memset(&r_txdma, 0, sizeof(struct resource));
1489 memset(&r_rxdma, 0, sizeof(struct resource));
1490#endif
1491 if (ZS_HAS_DMA(uap)) {
1492 uap->tx_dma_regs = ioremap(r_txdma.start, 0x100);
1493 if (uap->tx_dma_regs == NULL) {
1494 uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1495 goto no_dma;
1496 }
1497 uap->rx_dma_regs = ioremap(r_rxdma.start, 0x100);
1498 if (uap->rx_dma_regs == NULL) {
1499 iounmap(uap->tx_dma_regs);
1500 uap->tx_dma_regs = NULL;
1501 uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
1502 goto no_dma;
1503 }
1504 uap->tx_dma_irq = irq_of_parse_and_map(np, 1);
1505 uap->rx_dma_irq = irq_of_parse_and_map(np, 2);
1506 }
1507no_dma:
1508
1509 /*
1510 * Detect port type
1511 */
1512 if (of_device_is_compatible(np, "cobalt"))
1513 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
1514 conn = of_get_property(np, "AAPL,connector", &len);
1515 if (conn && (strcmp(conn, "infrared") == 0))
1516 uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1517 uap->port_type = PMAC_SCC_ASYNC;
1518 /* 1999 Powerbook G3 has slot-names property instead */
1519 slots = of_get_property(np, "slot-names", &len);
1520 if (slots && slots->count > 0) {
1521 if (strcmp(slots->name, "IrDA") == 0)
1522 uap->flags |= PMACZILOG_FLAG_IS_IRDA;
1523 else if (strcmp(slots->name, "Modem") == 0)
1524 uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
1525 }
1526 if (ZS_IS_IRDA(uap))
1527 uap->port_type = PMAC_SCC_IRDA;
1528 if (ZS_IS_INTMODEM(uap)) {
1529 struct device_node* i2c_modem =
1530 of_find_node_by_name(NULL, "i2c-modem");
1531 if (i2c_modem) {
1532 const char* mid =
1533 of_get_property(i2c_modem, "modem-id", NULL);
1534 if (mid) switch(*mid) {
1535 case 0x04 :
1536 case 0x05 :
1537 case 0x07 :
1538 case 0x08 :
1539 case 0x0b :
1540 case 0x0c :
1541 uap->port_type = PMAC_SCC_I2S1;
1542 }
1543 printk(KERN_INFO "pmac_zilog: i2c-modem detected, id: %d\n",
1544 mid ? (*mid) : 0);
1545 of_node_put(i2c_modem);
1546 } else {
1547 printk(KERN_INFO "pmac_zilog: serial modem detected\n");
1548 }
1549 }
1550
1551 /*
1552 * Init remaining bits of "port" structure
1553 */
1554 uap->port.iotype = UPIO_MEM;
1555 uap->port.irq = irq_of_parse_and_map(np, 0);
1556 uap->port.uartclk = ZS_CLOCK;
1557 uap->port.fifosize = 1;
1558 uap->port.ops = &pmz_pops;
1559 uap->port.type = PORT_PMAC_ZILOG;
1560 uap->port.flags = 0;
1561
1562 /*
1563 * Fixup for the port on Gatwick for which the device-tree has
1564 * missing interrupts. Normally, the macio_dev would contain
1565 * fixed up interrupt info, but we use the device-tree directly
1566 * here due to early probing so we need the fixup too.
1567 */
1568 if (uap->port.irq == NO_IRQ &&
1569 np->parent && np->parent->parent &&
1570 of_device_is_compatible(np->parent->parent, "gatwick")) {
1571 /* IRQs on gatwick are offset by 64 */
1572 uap->port.irq = irq_create_mapping(NULL, 64 + 15);
1573 uap->tx_dma_irq = irq_create_mapping(NULL, 64 + 4);
1574 uap->rx_dma_irq = irq_create_mapping(NULL, 64 + 5);
1575 }
1576
1577 /* Setup some valid baud rate information in the register
1578 * shadows so we don't write crap there before baud rate is
1579 * first initialized.
1580 */
1581 pmz_convert_to_zs(uap, CS8, 0, 9600);
1582
1583 return 0;
1584}
1585
1586/*
1587 * Get rid of a port on module removal
1588 */
1589static void pmz_dispose_port(struct uart_pmac_port *uap)
1590{
1591 struct device_node *np;
1592
1593 np = uap->node;
1594 iounmap(uap->rx_dma_regs);
1595 iounmap(uap->tx_dma_regs);
1596 iounmap(uap->control_reg);
1597 uap->node = NULL;
1598 of_node_put(np);
1599 memset(uap, 0, sizeof(struct uart_pmac_port));
1600}
1601
1602/*
1603 * Called upon match with an escc node in the device-tree.
1604 */
1605static int pmz_attach(struct macio_dev *mdev, const struct of_device_id *match)
1606{
1607 int i;
1608
1609 /* Iterate the pmz_ports array to find a matching entry
1610 */
1611 for (i = 0; i < MAX_ZS_PORTS; i++)
1612 if (pmz_ports[i].node == mdev->ofdev.dev.of_node) {
1613 struct uart_pmac_port *uap = &pmz_ports[i];
1614
1615 uap->dev = mdev;
1616 dev_set_drvdata(&mdev->ofdev.dev, uap);
1617 if (macio_request_resources(uap->dev, "pmac_zilog"))
1618 printk(KERN_WARNING "%s: Failed to request resource"
1619 ", port still active\n",
1620 uap->node->name);
1621 else
1622 uap->flags |= PMACZILOG_FLAG_RSRC_REQUESTED;
1623 return 0;
1624 }
1625 return -ENODEV;
1626}
1627
1628/*
1629 * That one should not be called, macio isn't really a hotswap device,
1630 * we don't expect one of those serial ports to go away...
1631 */
1632static int pmz_detach(struct macio_dev *mdev)
1633{
1634 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1635
1636 if (!uap)
1637 return -ENODEV;
1638
1639 if (uap->flags & PMACZILOG_FLAG_RSRC_REQUESTED) {
1640 macio_release_resources(uap->dev);
1641 uap->flags &= ~PMACZILOG_FLAG_RSRC_REQUESTED;
1642 }
1643 dev_set_drvdata(&mdev->ofdev.dev, NULL);
1644 uap->dev = NULL;
1645
1646 return 0;
1647}
1648
1649
1650static int pmz_suspend(struct macio_dev *mdev, pm_message_t pm_state)
1651{
1652 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1653 struct uart_state *state;
1654 unsigned long flags;
1655
1656 if (uap == NULL) {
1657 printk("HRM... pmz_suspend with NULL uap\n");
1658 return 0;
1659 }
1660
1661 if (pm_state.event == mdev->ofdev.dev.power.power_state.event)
1662 return 0;
1663
1664 pmz_debug("suspend, switching to state %d\n", pm_state.event);
1665
1666 state = pmz_uart_reg.state + uap->port.line;
1667
1668 mutex_lock(&pmz_irq_mutex);
1669 mutex_lock(&state->port.mutex);
1670
1671 spin_lock_irqsave(&uap->port.lock, flags);
1672
1673 if (ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)) {
1674 /* Disable receiver and transmitter. */
1675 uap->curregs[R3] &= ~RxENABLE;
1676 uap->curregs[R5] &= ~TxENABLE;
1677
1678 /* Disable all interrupts and BRK assertion. */
1679 uap->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
1680 uap->curregs[R5] &= ~SND_BRK;
1681 pmz_load_zsregs(uap, uap->curregs);
1682 uap->flags |= PMACZILOG_FLAG_IS_ASLEEP;
1683 mb();
1684 }
1685
1686 spin_unlock_irqrestore(&uap->port.lock, flags);
1687
1688 if (ZS_IS_OPEN(uap) || ZS_IS_OPEN(uap->mate))
1689 if (ZS_IS_ASLEEP(uap->mate) && ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1690 pmz_get_port_A(uap)->flags &= ~PMACZILOG_FLAG_IS_IRQ_ON;
1691 disable_irq(uap->port.irq);
1692 }
1693
1694 if (ZS_IS_CONS(uap))
1695 uap->port.cons->flags &= ~CON_ENABLED;
1696
1697 /* Shut the chip down */
1698 pmz_set_scc_power(uap, 0);
1699
1700 mutex_unlock(&state->port.mutex);
1701 mutex_unlock(&pmz_irq_mutex);
1702
1703 pmz_debug("suspend, switching complete\n");
1704
1705 mdev->ofdev.dev.power.power_state = pm_state;
1706
1707 return 0;
1708}
1709
1710
1711static int pmz_resume(struct macio_dev *mdev)
1712{
1713 struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
1714 struct uart_state *state;
1715 unsigned long flags;
1716 int pwr_delay = 0;
1717
1718 if (uap == NULL)
1719 return 0;
1720
1721 if (mdev->ofdev.dev.power.power_state.event == PM_EVENT_ON)
1722 return 0;
1723
1724 pmz_debug("resume, switching to state 0\n");
1725
1726 state = pmz_uart_reg.state + uap->port.line;
1727
1728 mutex_lock(&pmz_irq_mutex);
1729 mutex_lock(&state->port.mutex);
1730
1731 spin_lock_irqsave(&uap->port.lock, flags);
1732 if (!ZS_IS_OPEN(uap) && !ZS_IS_CONS(uap)) {
1733 spin_unlock_irqrestore(&uap->port.lock, flags);
1734 goto bail;
1735 }
1736 pwr_delay = __pmz_startup(uap);
1737
1738 /* Take care of config that may have changed while asleep */
1739 __pmz_set_termios(&uap->port, &uap->termios_cache, NULL);
1740
1741 if (ZS_IS_OPEN(uap)) {
1742 /* Enable interrupts */
1743 uap->curregs[R1] |= INT_ALL_Rx | TxINT_ENAB;
1744 if (!ZS_IS_EXTCLK(uap))
1745 uap->curregs[R1] |= EXT_INT_ENAB;
1746 write_zsreg(uap, R1, uap->curregs[R1]);
1747 }
1748
1749 spin_unlock_irqrestore(&uap->port.lock, flags);
1750
1751 if (ZS_IS_CONS(uap))
1752 uap->port.cons->flags |= CON_ENABLED;
1753
1754 /* Re-enable IRQ on the controller */
1755 if (ZS_IS_OPEN(uap) && !ZS_IS_IRQ_ON(pmz_get_port_A(uap))) {
1756 pmz_get_port_A(uap)->flags |= PMACZILOG_FLAG_IS_IRQ_ON;
1757 enable_irq(uap->port.irq);
1758 }
1759
1760 bail:
1761 mutex_unlock(&state->port.mutex);
1762 mutex_unlock(&pmz_irq_mutex);
1763
1764 /* Right now, we deal with delay by blocking here, I'll be
1765 * smarter later on
1766 */
1767 if (pwr_delay != 0) {
1768 pmz_debug("pmz: delaying %d ms\n", pwr_delay);
1769 msleep(pwr_delay);
1770 }
1771
1772 pmz_debug("resume, switching complete\n");
1773
1774 mdev->ofdev.dev.power.power_state.event = PM_EVENT_ON;
1775
1776 return 0;
1777}
1778
1779/*
1780 * Probe all ports in the system and build the ports array, we register
1781 * with the serial layer at this point, the macio-type probing is only
1782 * used later to "attach" to the sysfs tree so we get power management
1783 * events
1784 */
1785static int __init pmz_probe(void)
1786{
1787 struct device_node *node_p, *node_a, *node_b, *np;
1788 int count = 0;
1789 int rc;
1790
1791 /*
1792 * Find all escc chips in the system
1793 */
1794 node_p = of_find_node_by_name(NULL, "escc");
1795 while (node_p) {
1796 /*
1797 * First get channel A/B node pointers
1798 *
1799 * TODO: Add routines with proper locking to do that...
1800 */
1801 node_a = node_b = NULL;
1802 for (np = NULL; (np = of_get_next_child(node_p, np)) != NULL;) {
1803 if (strncmp(np->name, "ch-a", 4) == 0)
1804 node_a = of_node_get(np);
1805 else if (strncmp(np->name, "ch-b", 4) == 0)
1806 node_b = of_node_get(np);
1807 }
1808 if (!node_a && !node_b) {
1809 of_node_put(node_a);
1810 of_node_put(node_b);
1811 printk(KERN_ERR "pmac_zilog: missing node %c for escc %s\n",
1812 (!node_a) ? 'a' : 'b', node_p->full_name);
1813 goto next;
1814 }
1815
1816 /*
1817 * Fill basic fields in the port structures
1818 */
1819 pmz_ports[count].mate = &pmz_ports[count+1];
1820 pmz_ports[count+1].mate = &pmz_ports[count];
1821 pmz_ports[count].flags = PMACZILOG_FLAG_IS_CHANNEL_A;
1822 pmz_ports[count].node = node_a;
1823 pmz_ports[count+1].node = node_b;
1824 pmz_ports[count].port.line = count;
1825 pmz_ports[count+1].port.line = count+1;
1826
1827 /*
1828 * Setup the ports for real
1829 */
1830 rc = pmz_init_port(&pmz_ports[count]);
1831 if (rc == 0 && node_b != NULL)
1832 rc = pmz_init_port(&pmz_ports[count+1]);
1833 if (rc != 0) {
1834 of_node_put(node_a);
1835 of_node_put(node_b);
1836 memset(&pmz_ports[count], 0, sizeof(struct uart_pmac_port));
1837 memset(&pmz_ports[count+1], 0, sizeof(struct uart_pmac_port));
1838 goto next;
1839 }
1840 count += 2;
1841next:
1842 node_p = of_find_node_by_name(node_p, "escc");
1843 }
1844 pmz_ports_count = count;
1845
1846 return 0;
1847}
1848
1849#else
1850
1851extern struct platform_device scc_a_pdev, scc_b_pdev;
1852
1853static int __init pmz_init_port(struct uart_pmac_port *uap)
1854{
1855 struct resource *r_ports;
1856 int irq;
1857
1858 r_ports = platform_get_resource(uap->node, IORESOURCE_MEM, 0);
1859 irq = platform_get_irq(uap->node, 0);
1860 if (!r_ports || !irq)
1861 return -ENODEV;
1862
1863 uap->port.mapbase = r_ports->start;
1864 uap->port.membase = (unsigned char __iomem *) r_ports->start;
1865 uap->port.iotype = UPIO_MEM;
1866 uap->port.irq = irq;
1867 uap->port.uartclk = ZS_CLOCK;
1868 uap->port.fifosize = 1;
1869 uap->port.ops = &pmz_pops;
1870 uap->port.type = PORT_PMAC_ZILOG;
1871 uap->port.flags = 0;
1872
1873 uap->control_reg = uap->port.membase;
1874 uap->data_reg = uap->control_reg + 4;
1875 uap->port_type = 0;
1876
1877 pmz_convert_to_zs(uap, CS8, 0, 9600);
1878
1879 return 0;
1880}
1881
1882static int __init pmz_probe(void)
1883{
1884 int err;
1885
1886 pmz_ports_count = 0;
1887
1888 pmz_ports[0].mate = &pmz_ports[1];
1889 pmz_ports[0].port.line = 0;
1890 pmz_ports[0].flags = PMACZILOG_FLAG_IS_CHANNEL_A;
1891 pmz_ports[0].node = &scc_a_pdev;
1892 err = pmz_init_port(&pmz_ports[0]);
1893 if (err)
1894 return err;
1895 pmz_ports_count++;
1896
1897 pmz_ports[1].mate = &pmz_ports[0];
1898 pmz_ports[1].port.line = 1;
1899 pmz_ports[1].flags = 0;
1900 pmz_ports[1].node = &scc_b_pdev;
1901 err = pmz_init_port(&pmz_ports[1]);
1902 if (err)
1903 return err;
1904 pmz_ports_count++;
1905
1906 return 0;
1907}
1908
1909static void pmz_dispose_port(struct uart_pmac_port *uap)
1910{
1911 memset(uap, 0, sizeof(struct uart_pmac_port));
1912}
1913
1914static int __init pmz_attach(struct platform_device *pdev)
1915{
1916 int i;
1917
1918 for (i = 0; i < pmz_ports_count; i++)
1919 if (pmz_ports[i].node == pdev)
1920 return 0;
1921 return -ENODEV;
1922}
1923
1924static int __exit pmz_detach(struct platform_device *pdev)
1925{
1926 return 0;
1927}
1928
1929#endif /* !CONFIG_PPC_PMAC */
1930
1931#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
1932
1933static void pmz_console_write(struct console *con, const char *s, unsigned int count);
1934static int __init pmz_console_setup(struct console *co, char *options);
1935
1936static struct console pmz_console = {
1937 .name = PMACZILOG_NAME,
1938 .write = pmz_console_write,
1939 .device = uart_console_device,
1940 .setup = pmz_console_setup,
1941 .flags = CON_PRINTBUFFER,
1942 .index = -1,
1943 .data = &pmz_uart_reg,
1944};
1945
1946#define PMACZILOG_CONSOLE &pmz_console
1947#else /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1948#define PMACZILOG_CONSOLE (NULL)
1949#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
1950
1951/*
1952 * Register the driver, console driver and ports with the serial
1953 * core
1954 */
1955static int __init pmz_register(void)
1956{
1957 int i, rc;
1958
1959 pmz_uart_reg.nr = pmz_ports_count;
1960 pmz_uart_reg.cons = PMACZILOG_CONSOLE;
1961
1962 /*
1963 * Register this driver with the serial core
1964 */
1965 rc = uart_register_driver(&pmz_uart_reg);
1966 if (rc)
1967 return rc;
1968
1969 /*
1970 * Register each port with the serial core
1971 */
1972 for (i = 0; i < pmz_ports_count; i++) {
1973 struct uart_pmac_port *uport = &pmz_ports[i];
1974 /* NULL node may happen on wallstreet */
1975 if (uport->node != NULL)
1976 rc = uart_add_one_port(&pmz_uart_reg, &uport->port);
1977 if (rc)
1978 goto err_out;
1979 }
1980
1981 return 0;
1982err_out:
1983 while (i-- > 0) {
1984 struct uart_pmac_port *uport = &pmz_ports[i];
1985 uart_remove_one_port(&pmz_uart_reg, &uport->port);
1986 }
1987 uart_unregister_driver(&pmz_uart_reg);
1988 return rc;
1989}
1990
1991#ifdef CONFIG_PPC_PMAC
1992
1993static struct of_device_id pmz_match[] =
1994{
1995 {
1996 .name = "ch-a",
1997 },
1998 {
1999 .name = "ch-b",
2000 },
2001 {},
2002};
2003MODULE_DEVICE_TABLE (of, pmz_match);
2004
2005static struct macio_driver pmz_driver = {
2006 .driver = {
2007 .name = "pmac_zilog",
2008 .owner = THIS_MODULE,
2009 .of_match_table = pmz_match,
2010 },
2011 .probe = pmz_attach,
2012 .remove = pmz_detach,
2013 .suspend = pmz_suspend,
2014 .resume = pmz_resume,
2015};
2016
2017#else
2018
2019static struct platform_driver pmz_driver = {
2020 .remove = __exit_p(pmz_detach),
2021 .driver = {
2022 .name = "scc",
2023 .owner = THIS_MODULE,
2024 },
2025};
2026
2027#endif /* !CONFIG_PPC_PMAC */
2028
2029static int __init init_pmz(void)
2030{
2031 int rc, i;
2032 printk(KERN_INFO "%s\n", version);
2033
2034 /*
2035 * First, we need to do a direct OF-based probe pass. We
2036 * do that because we want serial console up before the
2037 * macio stuffs calls us back, and since that makes it
2038 * easier to pass the proper number of channels to
2039 * uart_register_driver()
2040 */
2041 if (pmz_ports_count == 0)
2042 pmz_probe();
2043
2044 /*
2045 * Bail early if no port found
2046 */
2047 if (pmz_ports_count == 0)
2048 return -ENODEV;
2049
2050 /*
2051 * Now we register with the serial layer
2052 */
2053 rc = pmz_register();
2054 if (rc) {
2055 printk(KERN_ERR
2056 "pmac_zilog: Error registering serial device, disabling pmac_zilog.\n"
2057 "pmac_zilog: Did another serial driver already claim the minors?\n");
2058 /* effectively "pmz_unprobe()" */
2059 for (i=0; i < pmz_ports_count; i++)
2060 pmz_dispose_port(&pmz_ports[i]);
2061 return rc;
2062 }
2063
2064 /*
2065 * Then we register the macio driver itself
2066 */
2067#ifdef CONFIG_PPC_PMAC
2068 return macio_register_driver(&pmz_driver);
2069#else
2070 return platform_driver_probe(&pmz_driver, pmz_attach);
2071#endif
2072}
2073
2074static void __exit exit_pmz(void)
2075{
2076 int i;
2077
2078#ifdef CONFIG_PPC_PMAC
2079 /* Get rid of macio-driver (detach from macio) */
2080 macio_unregister_driver(&pmz_driver);
2081#else
2082 platform_driver_unregister(&pmz_driver);
2083#endif
2084
2085 for (i = 0; i < pmz_ports_count; i++) {
2086 struct uart_pmac_port *uport = &pmz_ports[i];
2087 if (uport->node != NULL) {
2088 uart_remove_one_port(&pmz_uart_reg, &uport->port);
2089 pmz_dispose_port(uport);
2090 }
2091 }
2092 /* Unregister UART driver */
2093 uart_unregister_driver(&pmz_uart_reg);
2094}
2095
2096#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
2097
2098static void pmz_console_putchar(struct uart_port *port, int ch)
2099{
2100 struct uart_pmac_port *uap = (struct uart_pmac_port *)port;
2101
2102 /* Wait for the transmit buffer to empty. */
2103 while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
2104 udelay(5);
2105 write_zsdata(uap, ch);
2106}
2107
2108/*
2109 * Print a string to the serial port trying not to disturb
2110 * any possible real use of the port...
2111 */
2112static void pmz_console_write(struct console *con, const char *s, unsigned int count)
2113{
2114 struct uart_pmac_port *uap = &pmz_ports[con->index];
2115 unsigned long flags;
2116
2117 if (ZS_IS_ASLEEP(uap))
2118 return;
2119 spin_lock_irqsave(&uap->port.lock, flags);
2120
2121 /* Turn of interrupts and enable the transmitter. */
2122 write_zsreg(uap, R1, uap->curregs[1] & ~TxINT_ENAB);
2123 write_zsreg(uap, R5, uap->curregs[5] | TxENABLE | RTS | DTR);
2124
2125 uart_console_write(&uap->port, s, count, pmz_console_putchar);
2126
2127 /* Restore the values in the registers. */
2128 write_zsreg(uap, R1, uap->curregs[1]);
2129 /* Don't disable the transmitter. */
2130
2131 spin_unlock_irqrestore(&uap->port.lock, flags);
2132}
2133
2134/*
2135 * Setup the serial console
2136 */
2137static int __init pmz_console_setup(struct console *co, char *options)
2138{
2139 struct uart_pmac_port *uap;
2140 struct uart_port *port;
2141 int baud = 38400;
2142 int bits = 8;
2143 int parity = 'n';
2144 int flow = 'n';
2145 unsigned long pwr_delay;
2146
2147 /*
2148 * XServe's default to 57600 bps
2149 */
2150 if (of_machine_is_compatible("RackMac1,1")
2151 || of_machine_is_compatible("RackMac1,2")
2152 || of_machine_is_compatible("MacRISC4"))
2153 baud = 57600;
2154
2155 /*
2156 * Check whether an invalid uart number has been specified, and
2157 * if so, search for the first available port that does have
2158 * console support.
2159 */
2160 if (co->index >= pmz_ports_count)
2161 co->index = 0;
2162 uap = &pmz_ports[co->index];
2163 if (uap->node == NULL)
2164 return -ENODEV;
2165 port = &uap->port;
2166
2167 /*
2168 * Mark port as beeing a console
2169 */
2170 uap->flags |= PMACZILOG_FLAG_IS_CONS;
2171
2172 /*
2173 * Temporary fix for uart layer who didn't setup the spinlock yet
2174 */
2175 spin_lock_init(&port->lock);
2176
2177 /*
2178 * Enable the hardware
2179 */
2180 pwr_delay = __pmz_startup(uap);
2181 if (pwr_delay)
2182 mdelay(pwr_delay);
2183
2184 if (options)
2185 uart_parse_options(options, &baud, &parity, &bits, &flow);
2186
2187 return uart_set_options(port, co, baud, parity, bits, flow);
2188}
2189
2190static int __init pmz_console_init(void)
2191{
2192 /* Probe ports */
2193 pmz_probe();
2194
2195 /* TODO: Autoprobe console based on OF */
2196 /* pmz_console.index = i; */
2197 register_console(&pmz_console);
2198
2199 return 0;
2200
2201}
2202console_initcall(pmz_console_init);
2203#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
2204
2205module_init(init_pmz);
2206module_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..4302e6e3768e
--- /dev/null
+++ b/drivers/tty/serial/pxa.c
@@ -0,0 +1,877 @@
1/*
2 * Based on drivers/serial/8250.c by Russell King.
3 *
4 * Author: Nicolas Pitre
5 * Created: Feb 20, 2003
6 * Copyright: (C) 2003 Monta Vista Software, Inc.
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 * Note 1: This driver is made separate from the already too overloaded
14 * 8250.c because it needs some kirks of its own and that'll make it
15 * easier to add DMA support.
16 *
17 * Note 2: I'm too sick of device allocation policies for serial ports.
18 * If someone else wants to request an "official" allocation of major/minor
19 * for this driver please be my guest. And don't forget that new hardware
20 * to come from Intel might have more than 3 or 4 of those UARTs. Let's
21 * hope for a better port registration and dynamic device allocation scheme
22 * with the serial core maintainer satisfaction to appear soon.
23 */
24
25
26#if defined(CONFIG_SERIAL_PXA_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/serial_reg.h>
36#include <linux/circ_buf.h>
37#include <linux/delay.h>
38#include <linux/interrupt.h>
39#include <linux/platform_device.h>
40#include <linux/tty.h>
41#include <linux/tty_flip.h>
42#include <linux/serial_core.h>
43#include <linux/clk.h>
44#include <linux/io.h>
45#include <linux/slab.h>
46
47struct uart_pxa_port {
48 struct uart_port port;
49 unsigned char ier;
50 unsigned char lcr;
51 unsigned char mcr;
52 unsigned int lsr_break_flag;
53 struct clk *clk;
54 char *name;
55};
56
57static inline unsigned int serial_in(struct uart_pxa_port *up, int offset)
58{
59 offset <<= 2;
60 return readl(up->port.membase + offset);
61}
62
63static inline void serial_out(struct uart_pxa_port *up, int offset, int value)
64{
65 offset <<= 2;
66 writel(value, up->port.membase + offset);
67}
68
69static void serial_pxa_enable_ms(struct uart_port *port)
70{
71 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
72
73 up->ier |= UART_IER_MSI;
74 serial_out(up, UART_IER, up->ier);
75}
76
77static void serial_pxa_stop_tx(struct uart_port *port)
78{
79 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
80
81 if (up->ier & UART_IER_THRI) {
82 up->ier &= ~UART_IER_THRI;
83 serial_out(up, UART_IER, up->ier);
84 }
85}
86
87static void serial_pxa_stop_rx(struct uart_port *port)
88{
89 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
90
91 up->ier &= ~UART_IER_RLSI;
92 up->port.read_status_mask &= ~UART_LSR_DR;
93 serial_out(up, UART_IER, up->ier);
94}
95
96static inline void receive_chars(struct uart_pxa_port *up, int *status)
97{
98 struct tty_struct *tty = up->port.state->port.tty;
99 unsigned int ch, flag;
100 int max_count = 256;
101
102 do {
103 ch = serial_in(up, UART_RX);
104 flag = TTY_NORMAL;
105 up->port.icount.rx++;
106
107 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
108 UART_LSR_FE | UART_LSR_OE))) {
109 /*
110 * For statistics only
111 */
112 if (*status & UART_LSR_BI) {
113 *status &= ~(UART_LSR_FE | UART_LSR_PE);
114 up->port.icount.brk++;
115 /*
116 * We do the SysRQ and SAK checking
117 * here because otherwise the break
118 * may get masked by ignore_status_mask
119 * or read_status_mask.
120 */
121 if (uart_handle_break(&up->port))
122 goto ignore_char;
123 } else if (*status & UART_LSR_PE)
124 up->port.icount.parity++;
125 else if (*status & UART_LSR_FE)
126 up->port.icount.frame++;
127 if (*status & UART_LSR_OE)
128 up->port.icount.overrun++;
129
130 /*
131 * Mask off conditions which should be ignored.
132 */
133 *status &= up->port.read_status_mask;
134
135#ifdef CONFIG_SERIAL_PXA_CONSOLE
136 if (up->port.line == up->port.cons->index) {
137 /* Recover the break flag from console xmit */
138 *status |= up->lsr_break_flag;
139 up->lsr_break_flag = 0;
140 }
141#endif
142 if (*status & UART_LSR_BI) {
143 flag = TTY_BREAK;
144 } else if (*status & UART_LSR_PE)
145 flag = TTY_PARITY;
146 else if (*status & UART_LSR_FE)
147 flag = TTY_FRAME;
148 }
149
150 if (uart_handle_sysrq_char(&up->port, ch))
151 goto ignore_char;
152
153 uart_insert_char(&up->port, *status, UART_LSR_OE, ch, flag);
154
155 ignore_char:
156 *status = serial_in(up, UART_LSR);
157 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
158 tty_flip_buffer_push(tty);
159}
160
161static void transmit_chars(struct uart_pxa_port *up)
162{
163 struct circ_buf *xmit = &up->port.state->xmit;
164 int count;
165
166 if (up->port.x_char) {
167 serial_out(up, UART_TX, up->port.x_char);
168 up->port.icount.tx++;
169 up->port.x_char = 0;
170 return;
171 }
172 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
173 serial_pxa_stop_tx(&up->port);
174 return;
175 }
176
177 count = up->port.fifosize / 2;
178 do {
179 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
180 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
181 up->port.icount.tx++;
182 if (uart_circ_empty(xmit))
183 break;
184 } while (--count > 0);
185
186 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
187 uart_write_wakeup(&up->port);
188
189
190 if (uart_circ_empty(xmit))
191 serial_pxa_stop_tx(&up->port);
192}
193
194static void serial_pxa_start_tx(struct uart_port *port)
195{
196 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
197
198 if (!(up->ier & UART_IER_THRI)) {
199 up->ier |= UART_IER_THRI;
200 serial_out(up, UART_IER, up->ier);
201 }
202}
203
204static inline void check_modem_status(struct uart_pxa_port *up)
205{
206 int status;
207
208 status = serial_in(up, UART_MSR);
209
210 if ((status & UART_MSR_ANY_DELTA) == 0)
211 return;
212
213 if (status & UART_MSR_TERI)
214 up->port.icount.rng++;
215 if (status & UART_MSR_DDSR)
216 up->port.icount.dsr++;
217 if (status & UART_MSR_DDCD)
218 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
219 if (status & UART_MSR_DCTS)
220 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
221
222 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
223}
224
225/*
226 * This handles the interrupt from one port.
227 */
228static inline irqreturn_t serial_pxa_irq(int irq, void *dev_id)
229{
230 struct uart_pxa_port *up = dev_id;
231 unsigned int iir, lsr;
232
233 iir = serial_in(up, UART_IIR);
234 if (iir & UART_IIR_NO_INT)
235 return IRQ_NONE;
236 lsr = serial_in(up, UART_LSR);
237 if (lsr & UART_LSR_DR)
238 receive_chars(up, &lsr);
239 check_modem_status(up);
240 if (lsr & UART_LSR_THRE)
241 transmit_chars(up);
242 return IRQ_HANDLED;
243}
244
245static unsigned int serial_pxa_tx_empty(struct uart_port *port)
246{
247 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
248 unsigned long flags;
249 unsigned int ret;
250
251 spin_lock_irqsave(&up->port.lock, flags);
252 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
253 spin_unlock_irqrestore(&up->port.lock, flags);
254
255 return ret;
256}
257
258static unsigned int serial_pxa_get_mctrl(struct uart_port *port)
259{
260 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
261 unsigned char status;
262 unsigned int ret;
263
264 status = serial_in(up, UART_MSR);
265
266 ret = 0;
267 if (status & UART_MSR_DCD)
268 ret |= TIOCM_CAR;
269 if (status & UART_MSR_RI)
270 ret |= TIOCM_RNG;
271 if (status & UART_MSR_DSR)
272 ret |= TIOCM_DSR;
273 if (status & UART_MSR_CTS)
274 ret |= TIOCM_CTS;
275 return ret;
276}
277
278static void serial_pxa_set_mctrl(struct uart_port *port, unsigned int mctrl)
279{
280 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
281 unsigned char mcr = 0;
282
283 if (mctrl & TIOCM_RTS)
284 mcr |= UART_MCR_RTS;
285 if (mctrl & TIOCM_DTR)
286 mcr |= UART_MCR_DTR;
287 if (mctrl & TIOCM_OUT1)
288 mcr |= UART_MCR_OUT1;
289 if (mctrl & TIOCM_OUT2)
290 mcr |= UART_MCR_OUT2;
291 if (mctrl & TIOCM_LOOP)
292 mcr |= UART_MCR_LOOP;
293
294 mcr |= up->mcr;
295
296 serial_out(up, UART_MCR, mcr);
297}
298
299static void serial_pxa_break_ctl(struct uart_port *port, int break_state)
300{
301 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
302 unsigned long flags;
303
304 spin_lock_irqsave(&up->port.lock, flags);
305 if (break_state == -1)
306 up->lcr |= UART_LCR_SBC;
307 else
308 up->lcr &= ~UART_LCR_SBC;
309 serial_out(up, UART_LCR, up->lcr);
310 spin_unlock_irqrestore(&up->port.lock, flags);
311}
312
313#if 0
314static void serial_pxa_dma_init(struct pxa_uart *up)
315{
316 up->rxdma =
317 pxa_request_dma(up->name, DMA_PRIO_LOW, pxa_receive_dma, up);
318 if (up->rxdma < 0)
319 goto out;
320 up->txdma =
321 pxa_request_dma(up->name, DMA_PRIO_LOW, pxa_transmit_dma, up);
322 if (up->txdma < 0)
323 goto err_txdma;
324 up->dmadesc = kmalloc(4 * sizeof(pxa_dma_desc), GFP_KERNEL);
325 if (!up->dmadesc)
326 goto err_alloc;
327
328 /* ... */
329err_alloc:
330 pxa_free_dma(up->txdma);
331err_rxdma:
332 pxa_free_dma(up->rxdma);
333out:
334 return;
335}
336#endif
337
338static int serial_pxa_startup(struct uart_port *port)
339{
340 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
341 unsigned long flags;
342 int retval;
343
344 if (port->line == 3) /* HWUART */
345 up->mcr |= UART_MCR_AFE;
346 else
347 up->mcr = 0;
348
349 up->port.uartclk = clk_get_rate(up->clk);
350
351 /*
352 * Allocate the IRQ
353 */
354 retval = request_irq(up->port.irq, serial_pxa_irq, 0, up->name, up);
355 if (retval)
356 return retval;
357
358 /*
359 * Clear the FIFO buffers and disable them.
360 * (they will be reenabled in set_termios())
361 */
362 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
363 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
364 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
365 serial_out(up, UART_FCR, 0);
366
367 /*
368 * Clear the interrupt registers.
369 */
370 (void) serial_in(up, UART_LSR);
371 (void) serial_in(up, UART_RX);
372 (void) serial_in(up, UART_IIR);
373 (void) serial_in(up, UART_MSR);
374
375 /*
376 * Now, initialize the UART
377 */
378 serial_out(up, UART_LCR, UART_LCR_WLEN8);
379
380 spin_lock_irqsave(&up->port.lock, flags);
381 up->port.mctrl |= TIOCM_OUT2;
382 serial_pxa_set_mctrl(&up->port, up->port.mctrl);
383 spin_unlock_irqrestore(&up->port.lock, flags);
384
385 /*
386 * Finally, enable interrupts. Note: Modem status interrupts
387 * are set via set_termios(), which will be occurring imminently
388 * anyway, so we don't enable them here.
389 */
390 up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
391 serial_out(up, UART_IER, up->ier);
392
393 /*
394 * And clear the interrupt registers again for luck.
395 */
396 (void) serial_in(up, UART_LSR);
397 (void) serial_in(up, UART_RX);
398 (void) serial_in(up, UART_IIR);
399 (void) serial_in(up, UART_MSR);
400
401 return 0;
402}
403
404static void serial_pxa_shutdown(struct uart_port *port)
405{
406 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
407 unsigned long flags;
408
409 free_irq(up->port.irq, up);
410
411 /*
412 * Disable interrupts from this port
413 */
414 up->ier = 0;
415 serial_out(up, UART_IER, 0);
416
417 spin_lock_irqsave(&up->port.lock, flags);
418 up->port.mctrl &= ~TIOCM_OUT2;
419 serial_pxa_set_mctrl(&up->port, up->port.mctrl);
420 spin_unlock_irqrestore(&up->port.lock, flags);
421
422 /*
423 * Disable break condition and FIFOs
424 */
425 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
426 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
427 UART_FCR_CLEAR_RCVR |
428 UART_FCR_CLEAR_XMIT);
429 serial_out(up, UART_FCR, 0);
430}
431
432static void
433serial_pxa_set_termios(struct uart_port *port, struct ktermios *termios,
434 struct ktermios *old)
435{
436 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
437 unsigned char cval, fcr = 0;
438 unsigned long flags;
439 unsigned int baud, quot;
440 unsigned int dll;
441
442 switch (termios->c_cflag & CSIZE) {
443 case CS5:
444 cval = UART_LCR_WLEN5;
445 break;
446 case CS6:
447 cval = UART_LCR_WLEN6;
448 break;
449 case CS7:
450 cval = UART_LCR_WLEN7;
451 break;
452 default:
453 case CS8:
454 cval = UART_LCR_WLEN8;
455 break;
456 }
457
458 if (termios->c_cflag & CSTOPB)
459 cval |= UART_LCR_STOP;
460 if (termios->c_cflag & PARENB)
461 cval |= UART_LCR_PARITY;
462 if (!(termios->c_cflag & PARODD))
463 cval |= UART_LCR_EPAR;
464
465 /*
466 * Ask the core to calculate the divisor for us.
467 */
468 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
469 quot = uart_get_divisor(port, baud);
470
471 if ((up->port.uartclk / quot) < (2400 * 16))
472 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR1;
473 else if ((up->port.uartclk / quot) < (230400 * 16))
474 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR8;
475 else
476 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR32;
477
478 /*
479 * Ok, we're now changing the port state. Do it with
480 * interrupts disabled.
481 */
482 spin_lock_irqsave(&up->port.lock, flags);
483
484 /*
485 * Ensure the port will be enabled.
486 * This is required especially for serial console.
487 */
488 up->ier |= UART_IER_UUE;
489
490 /*
491 * Update the per-port timeout.
492 */
493 uart_update_timeout(port, termios->c_cflag, baud);
494
495 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
496 if (termios->c_iflag & INPCK)
497 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
498 if (termios->c_iflag & (BRKINT | PARMRK))
499 up->port.read_status_mask |= UART_LSR_BI;
500
501 /*
502 * Characters to ignore
503 */
504 up->port.ignore_status_mask = 0;
505 if (termios->c_iflag & IGNPAR)
506 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
507 if (termios->c_iflag & IGNBRK) {
508 up->port.ignore_status_mask |= UART_LSR_BI;
509 /*
510 * If we're ignoring parity and break indicators,
511 * ignore overruns too (for real raw support).
512 */
513 if (termios->c_iflag & IGNPAR)
514 up->port.ignore_status_mask |= UART_LSR_OE;
515 }
516
517 /*
518 * ignore all characters if CREAD is not set
519 */
520 if ((termios->c_cflag & CREAD) == 0)
521 up->port.ignore_status_mask |= UART_LSR_DR;
522
523 /*
524 * CTS flow control flag and modem status interrupts
525 */
526 up->ier &= ~UART_IER_MSI;
527 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
528 up->ier |= UART_IER_MSI;
529
530 serial_out(up, UART_IER, up->ier);
531
532 if (termios->c_cflag & CRTSCTS)
533 up->mcr |= UART_MCR_AFE;
534 else
535 up->mcr &= ~UART_MCR_AFE;
536
537 serial_out(up, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
538 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
539
540 /*
541 * work around Errata #75 according to Intel(R) PXA27x Processor Family
542 * Specification Update (Nov 2005)
543 */
544 dll = serial_in(up, UART_DLL);
545 WARN_ON(dll != (quot & 0xff));
546
547 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
548 serial_out(up, UART_LCR, cval); /* reset DLAB */
549 up->lcr = cval; /* Save LCR */
550 serial_pxa_set_mctrl(&up->port, up->port.mctrl);
551 serial_out(up, UART_FCR, fcr);
552 spin_unlock_irqrestore(&up->port.lock, flags);
553}
554
555static void
556serial_pxa_pm(struct uart_port *port, unsigned int state,
557 unsigned int oldstate)
558{
559 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
560
561 if (!state)
562 clk_enable(up->clk);
563 else
564 clk_disable(up->clk);
565}
566
567static void serial_pxa_release_port(struct uart_port *port)
568{
569}
570
571static int serial_pxa_request_port(struct uart_port *port)
572{
573 return 0;
574}
575
576static void serial_pxa_config_port(struct uart_port *port, int flags)
577{
578 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
579 up->port.type = PORT_PXA;
580}
581
582static int
583serial_pxa_verify_port(struct uart_port *port, struct serial_struct *ser)
584{
585 /* we don't want the core code to modify any port params */
586 return -EINVAL;
587}
588
589static const char *
590serial_pxa_type(struct uart_port *port)
591{
592 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
593 return up->name;
594}
595
596static struct uart_pxa_port *serial_pxa_ports[4];
597static struct uart_driver serial_pxa_reg;
598
599#ifdef CONFIG_SERIAL_PXA_CONSOLE
600
601#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
602
603/*
604 * Wait for transmitter & holding register to empty
605 */
606static inline void wait_for_xmitr(struct uart_pxa_port *up)
607{
608 unsigned int status, tmout = 10000;
609
610 /* Wait up to 10ms for the character(s) to be sent. */
611 do {
612 status = serial_in(up, UART_LSR);
613
614 if (status & UART_LSR_BI)
615 up->lsr_break_flag = UART_LSR_BI;
616
617 if (--tmout == 0)
618 break;
619 udelay(1);
620 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
621
622 /* Wait up to 1s for flow control if necessary */
623 if (up->port.flags & UPF_CONS_FLOW) {
624 tmout = 1000000;
625 while (--tmout &&
626 ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
627 udelay(1);
628 }
629}
630
631static void serial_pxa_console_putchar(struct uart_port *port, int ch)
632{
633 struct uart_pxa_port *up = (struct uart_pxa_port *)port;
634
635 wait_for_xmitr(up);
636 serial_out(up, UART_TX, ch);
637}
638
639/*
640 * Print a string to the serial port trying not to disturb
641 * any possible real use of the port...
642 *
643 * The console_lock must be held when we get here.
644 */
645static void
646serial_pxa_console_write(struct console *co, const char *s, unsigned int count)
647{
648 struct uart_pxa_port *up = serial_pxa_ports[co->index];
649 unsigned int ier;
650
651 clk_enable(up->clk);
652
653 /*
654 * First save the IER then disable the interrupts
655 */
656 ier = serial_in(up, UART_IER);
657 serial_out(up, UART_IER, UART_IER_UUE);
658
659 uart_console_write(&up->port, s, count, serial_pxa_console_putchar);
660
661 /*
662 * Finally, wait for transmitter to become empty
663 * and restore the IER
664 */
665 wait_for_xmitr(up);
666 serial_out(up, UART_IER, ier);
667
668 clk_disable(up->clk);
669}
670
671static int __init
672serial_pxa_console_setup(struct console *co, char *options)
673{
674 struct uart_pxa_port *up;
675 int baud = 9600;
676 int bits = 8;
677 int parity = 'n';
678 int flow = 'n';
679
680 if (co->index == -1 || co->index >= serial_pxa_reg.nr)
681 co->index = 0;
682 up = serial_pxa_ports[co->index];
683 if (!up)
684 return -ENODEV;
685
686 if (options)
687 uart_parse_options(options, &baud, &parity, &bits, &flow);
688
689 return uart_set_options(&up->port, co, baud, parity, bits, flow);
690}
691
692static struct console serial_pxa_console = {
693 .name = "ttyS",
694 .write = serial_pxa_console_write,
695 .device = uart_console_device,
696 .setup = serial_pxa_console_setup,
697 .flags = CON_PRINTBUFFER,
698 .index = -1,
699 .data = &serial_pxa_reg,
700};
701
702#define PXA_CONSOLE &serial_pxa_console
703#else
704#define PXA_CONSOLE NULL
705#endif
706
707struct uart_ops serial_pxa_pops = {
708 .tx_empty = serial_pxa_tx_empty,
709 .set_mctrl = serial_pxa_set_mctrl,
710 .get_mctrl = serial_pxa_get_mctrl,
711 .stop_tx = serial_pxa_stop_tx,
712 .start_tx = serial_pxa_start_tx,
713 .stop_rx = serial_pxa_stop_rx,
714 .enable_ms = serial_pxa_enable_ms,
715 .break_ctl = serial_pxa_break_ctl,
716 .startup = serial_pxa_startup,
717 .shutdown = serial_pxa_shutdown,
718 .set_termios = serial_pxa_set_termios,
719 .pm = serial_pxa_pm,
720 .type = serial_pxa_type,
721 .release_port = serial_pxa_release_port,
722 .request_port = serial_pxa_request_port,
723 .config_port = serial_pxa_config_port,
724 .verify_port = serial_pxa_verify_port,
725};
726
727static struct uart_driver serial_pxa_reg = {
728 .owner = THIS_MODULE,
729 .driver_name = "PXA serial",
730 .dev_name = "ttyS",
731 .major = TTY_MAJOR,
732 .minor = 64,
733 .nr = 4,
734 .cons = PXA_CONSOLE,
735};
736
737#ifdef CONFIG_PM
738static int serial_pxa_suspend(struct device *dev)
739{
740 struct uart_pxa_port *sport = dev_get_drvdata(dev);
741
742 if (sport)
743 uart_suspend_port(&serial_pxa_reg, &sport->port);
744
745 return 0;
746}
747
748static int serial_pxa_resume(struct device *dev)
749{
750 struct uart_pxa_port *sport = dev_get_drvdata(dev);
751
752 if (sport)
753 uart_resume_port(&serial_pxa_reg, &sport->port);
754
755 return 0;
756}
757
758static const struct dev_pm_ops serial_pxa_pm_ops = {
759 .suspend = serial_pxa_suspend,
760 .resume = serial_pxa_resume,
761};
762#endif
763
764static int serial_pxa_probe(struct platform_device *dev)
765{
766 struct uart_pxa_port *sport;
767 struct resource *mmres, *irqres;
768 int ret;
769
770 mmres = platform_get_resource(dev, IORESOURCE_MEM, 0);
771 irqres = platform_get_resource(dev, IORESOURCE_IRQ, 0);
772 if (!mmres || !irqres)
773 return -ENODEV;
774
775 sport = kzalloc(sizeof(struct uart_pxa_port), GFP_KERNEL);
776 if (!sport)
777 return -ENOMEM;
778
779 sport->clk = clk_get(&dev->dev, NULL);
780 if (IS_ERR(sport->clk)) {
781 ret = PTR_ERR(sport->clk);
782 goto err_free;
783 }
784
785 sport->port.type = PORT_PXA;
786 sport->port.iotype = UPIO_MEM;
787 sport->port.mapbase = mmres->start;
788 sport->port.irq = irqres->start;
789 sport->port.fifosize = 64;
790 sport->port.ops = &serial_pxa_pops;
791 sport->port.line = dev->id;
792 sport->port.dev = &dev->dev;
793 sport->port.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
794 sport->port.uartclk = clk_get_rate(sport->clk);
795
796 switch (dev->id) {
797 case 0: sport->name = "FFUART"; break;
798 case 1: sport->name = "BTUART"; break;
799 case 2: sport->name = "STUART"; break;
800 case 3: sport->name = "HWUART"; break;
801 default:
802 sport->name = "???";
803 break;
804 }
805
806 sport->port.membase = ioremap(mmres->start, mmres->end - mmres->start + 1);
807 if (!sport->port.membase) {
808 ret = -ENOMEM;
809 goto err_clk;
810 }
811
812 serial_pxa_ports[dev->id] = sport;
813
814 uart_add_one_port(&serial_pxa_reg, &sport->port);
815 platform_set_drvdata(dev, sport);
816
817 return 0;
818
819 err_clk:
820 clk_put(sport->clk);
821 err_free:
822 kfree(sport);
823 return ret;
824}
825
826static int serial_pxa_remove(struct platform_device *dev)
827{
828 struct uart_pxa_port *sport = platform_get_drvdata(dev);
829
830 platform_set_drvdata(dev, NULL);
831
832 uart_remove_one_port(&serial_pxa_reg, &sport->port);
833 clk_put(sport->clk);
834 kfree(sport);
835
836 return 0;
837}
838
839static struct platform_driver serial_pxa_driver = {
840 .probe = serial_pxa_probe,
841 .remove = serial_pxa_remove,
842
843 .driver = {
844 .name = "pxa2xx-uart",
845 .owner = THIS_MODULE,
846#ifdef CONFIG_PM
847 .pm = &serial_pxa_pm_ops,
848#endif
849 },
850};
851
852int __init serial_pxa_init(void)
853{
854 int ret;
855
856 ret = uart_register_driver(&serial_pxa_reg);
857 if (ret != 0)
858 return ret;
859
860 ret = platform_driver_register(&serial_pxa_driver);
861 if (ret != 0)
862 uart_unregister_driver(&serial_pxa_reg);
863
864 return ret;
865}
866
867void __exit serial_pxa_exit(void)
868{
869 platform_driver_unregister(&serial_pxa_driver);
870 uart_unregister_driver(&serial_pxa_reg);
871}
872
873module_init(serial_pxa_init);
874module_exit(serial_pxa_exit);
875
876MODULE_LICENSE("GPL");
877MODULE_ALIAS("platform:pxa2xx-uart");
diff --git a/drivers/tty/serial/s3c2400.c b/drivers/tty/serial/s3c2400.c
new file mode 100644
index 000000000000..d13051b3df87
--- /dev/null
+++ b/drivers/tty/serial/s3c2400.c
@@ -0,0 +1,105 @@
1/*
2 * Driver for Samsung SoC onboard UARTs.
3 *
4 * Ben Dooks, Copyright (c) 2003-2005 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
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
12#include <linux/module.h>
13#include <linux/ioport.h>
14#include <linux/io.h>
15#include <linux/platform_device.h>
16
17#include <asm/irq.h>
18
19#include <mach/hardware.h>
20
21#include <plat/regs-serial.h>
22#include <mach/regs-gpio.h>
23
24#include "samsung.h"
25
26static int s3c2400_serial_getsource(struct uart_port *port,
27 struct s3c24xx_uart_clksrc *clk)
28{
29 clk->divisor = 1;
30 clk->name = "pclk";
31
32 return 0;
33}
34
35static int s3c2400_serial_setsource(struct uart_port *port,
36 struct s3c24xx_uart_clksrc *clk)
37{
38 return 0;
39}
40
41static int s3c2400_serial_resetport(struct uart_port *port,
42 struct s3c2410_uartcfg *cfg)
43{
44 dbg("s3c2400_serial_resetport: port=%p (%08lx), cfg=%p\n",
45 port, port->mapbase, cfg);
46
47 wr_regl(port, S3C2410_UCON, cfg->ucon);
48 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
49
50 /* reset both fifos */
51
52 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
53 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
54
55 return 0;
56}
57
58static struct s3c24xx_uart_info s3c2400_uart_inf = {
59 .name = "Samsung S3C2400 UART",
60 .type = PORT_S3C2400,
61 .fifosize = 16,
62 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
63 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
64 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
65 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
66 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
67 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
68 .get_clksrc = s3c2400_serial_getsource,
69 .set_clksrc = s3c2400_serial_setsource,
70 .reset_port = s3c2400_serial_resetport,
71};
72
73static int s3c2400_serial_probe(struct platform_device *dev)
74{
75 return s3c24xx_serial_probe(dev, &s3c2400_uart_inf);
76}
77
78static struct platform_driver s3c2400_serial_driver = {
79 .probe = s3c2400_serial_probe,
80 .remove = __devexit_p(s3c24xx_serial_remove),
81 .driver = {
82 .name = "s3c2400-uart",
83 .owner = THIS_MODULE,
84 },
85};
86
87s3c24xx_console_init(&s3c2400_serial_driver, &s3c2400_uart_inf);
88
89static inline int s3c2400_serial_init(void)
90{
91 return s3c24xx_serial_init(&s3c2400_serial_driver, &s3c2400_uart_inf);
92}
93
94static inline void s3c2400_serial_exit(void)
95{
96 platform_driver_unregister(&s3c2400_serial_driver);
97}
98
99module_init(s3c2400_serial_init);
100module_exit(s3c2400_serial_exit);
101
102MODULE_LICENSE("GPL v2");
103MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
104MODULE_DESCRIPTION("Samsung S3C2400 SoC Serial port driver");
105MODULE_ALIAS("platform:s3c2400-uart");
diff --git a/drivers/tty/serial/s3c2410.c b/drivers/tty/serial/s3c2410.c
new file mode 100644
index 000000000000..bffe6ff9b158
--- /dev/null
+++ b/drivers/tty/serial/s3c2410.c
@@ -0,0 +1,117 @@
1/*
2 * Driver for Samsung S3C2410 SoC onboard UARTs.
3 *
4 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
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
12#include <linux/module.h>
13#include <linux/ioport.h>
14#include <linux/io.h>
15#include <linux/platform_device.h>
16#include <linux/init.h>
17#include <linux/serial_core.h>
18#include <linux/serial.h>
19
20#include <asm/irq.h>
21#include <mach/hardware.h>
22
23#include <plat/regs-serial.h>
24#include <mach/regs-gpio.h>
25
26#include "samsung.h"
27
28static int s3c2410_serial_setsource(struct uart_port *port,
29 struct s3c24xx_uart_clksrc *clk)
30{
31 unsigned long ucon = rd_regl(port, S3C2410_UCON);
32
33 if (strcmp(clk->name, "uclk") == 0)
34 ucon |= S3C2410_UCON_UCLK;
35 else
36 ucon &= ~S3C2410_UCON_UCLK;
37
38 wr_regl(port, S3C2410_UCON, ucon);
39 return 0;
40}
41
42static int s3c2410_serial_getsource(struct uart_port *port,
43 struct s3c24xx_uart_clksrc *clk)
44{
45 unsigned long ucon = rd_regl(port, S3C2410_UCON);
46
47 clk->divisor = 1;
48 clk->name = (ucon & S3C2410_UCON_UCLK) ? "uclk" : "pclk";
49
50 return 0;
51}
52
53static int s3c2410_serial_resetport(struct uart_port *port,
54 struct s3c2410_uartcfg *cfg)
55{
56 dbg("s3c2410_serial_resetport: port=%p (%08lx), cfg=%p\n",
57 port, port->mapbase, cfg);
58
59 wr_regl(port, S3C2410_UCON, cfg->ucon);
60 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
61
62 /* reset both fifos */
63
64 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
65 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
66
67 return 0;
68}
69
70static struct s3c24xx_uart_info s3c2410_uart_inf = {
71 .name = "Samsung S3C2410 UART",
72 .type = PORT_S3C2410,
73 .fifosize = 16,
74 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
75 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
76 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
77 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
78 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
79 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
80 .get_clksrc = s3c2410_serial_getsource,
81 .set_clksrc = s3c2410_serial_setsource,
82 .reset_port = s3c2410_serial_resetport,
83};
84
85static int s3c2410_serial_probe(struct platform_device *dev)
86{
87 return s3c24xx_serial_probe(dev, &s3c2410_uart_inf);
88}
89
90static struct platform_driver s3c2410_serial_driver = {
91 .probe = s3c2410_serial_probe,
92 .remove = __devexit_p(s3c24xx_serial_remove),
93 .driver = {
94 .name = "s3c2410-uart",
95 .owner = THIS_MODULE,
96 },
97};
98
99s3c24xx_console_init(&s3c2410_serial_driver, &s3c2410_uart_inf);
100
101static int __init s3c2410_serial_init(void)
102{
103 return s3c24xx_serial_init(&s3c2410_serial_driver, &s3c2410_uart_inf);
104}
105
106static void __exit s3c2410_serial_exit(void)
107{
108 platform_driver_unregister(&s3c2410_serial_driver);
109}
110
111module_init(s3c2410_serial_init);
112module_exit(s3c2410_serial_exit);
113
114MODULE_LICENSE("GPL v2");
115MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
116MODULE_DESCRIPTION("Samsung S3C2410 SoC Serial port driver");
117MODULE_ALIAS("platform:s3c2410-uart");
diff --git a/drivers/tty/serial/s3c2412.c b/drivers/tty/serial/s3c2412.c
new file mode 100644
index 000000000000..7e2b9504a687
--- /dev/null
+++ b/drivers/tty/serial/s3c2412.c
@@ -0,0 +1,151 @@
1/*
2 * Driver for Samsung S3C2412 and S3C2413 SoC onboard UARTs.
3 *
4 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
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
12#include <linux/module.h>
13#include <linux/ioport.h>
14#include <linux/io.h>
15#include <linux/platform_device.h>
16#include <linux/init.h>
17#include <linux/serial_core.h>
18#include <linux/serial.h>
19
20#include <asm/irq.h>
21#include <mach/hardware.h>
22
23#include <plat/regs-serial.h>
24#include <mach/regs-gpio.h>
25
26#include "samsung.h"
27
28static int s3c2412_serial_setsource(struct uart_port *port,
29 struct s3c24xx_uart_clksrc *clk)
30{
31 unsigned long ucon = rd_regl(port, S3C2410_UCON);
32
33 ucon &= ~S3C2412_UCON_CLKMASK;
34
35 if (strcmp(clk->name, "uclk") == 0)
36 ucon |= S3C2440_UCON_UCLK;
37 else if (strcmp(clk->name, "pclk") == 0)
38 ucon |= S3C2440_UCON_PCLK;
39 else if (strcmp(clk->name, "usysclk") == 0)
40 ucon |= S3C2412_UCON_USYSCLK;
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 s3c2412_serial_getsource(struct uart_port *port,
52 struct s3c24xx_uart_clksrc *clk)
53{
54 unsigned long ucon = rd_regl(port, S3C2410_UCON);
55
56 switch (ucon & S3C2412_UCON_CLKMASK) {
57 case S3C2412_UCON_UCLK:
58 clk->divisor = 1;
59 clk->name = "uclk";
60 break;
61
62 case S3C2412_UCON_PCLK:
63 case S3C2412_UCON_PCLK2:
64 clk->divisor = 1;
65 clk->name = "pclk";
66 break;
67
68 case S3C2412_UCON_USYSCLK:
69 clk->divisor = 1;
70 clk->name = "usysclk";
71 break;
72 }
73
74 return 0;
75}
76
77static int s3c2412_serial_resetport(struct uart_port *port,
78 struct s3c2410_uartcfg *cfg)
79{
80 unsigned long ucon = rd_regl(port, S3C2410_UCON);
81
82 dbg("%s: port=%p (%08lx), cfg=%p\n",
83 __func__, port, port->mapbase, cfg);
84
85 /* ensure we don't change the clock settings... */
86
87 ucon &= S3C2412_UCON_CLKMASK;
88
89 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
90 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
91
92 /* reset both fifos */
93
94 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
95 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
96
97 return 0;
98}
99
100static struct s3c24xx_uart_info s3c2412_uart_inf = {
101 .name = "Samsung S3C2412 UART",
102 .type = PORT_S3C2412,
103 .fifosize = 64,
104 .has_divslot = 1,
105 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
106 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
107 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
108 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
109 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
110 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
111 .get_clksrc = s3c2412_serial_getsource,
112 .set_clksrc = s3c2412_serial_setsource,
113 .reset_port = s3c2412_serial_resetport,
114};
115
116/* device management */
117
118static int s3c2412_serial_probe(struct platform_device *dev)
119{
120 dbg("s3c2440_serial_probe: dev=%p\n", dev);
121 return s3c24xx_serial_probe(dev, &s3c2412_uart_inf);
122}
123
124static struct platform_driver s3c2412_serial_driver = {
125 .probe = s3c2412_serial_probe,
126 .remove = __devexit_p(s3c24xx_serial_remove),
127 .driver = {
128 .name = "s3c2412-uart",
129 .owner = THIS_MODULE,
130 },
131};
132
133s3c24xx_console_init(&s3c2412_serial_driver, &s3c2412_uart_inf);
134
135static inline int s3c2412_serial_init(void)
136{
137 return s3c24xx_serial_init(&s3c2412_serial_driver, &s3c2412_uart_inf);
138}
139
140static inline void s3c2412_serial_exit(void)
141{
142 platform_driver_unregister(&s3c2412_serial_driver);
143}
144
145module_init(s3c2412_serial_init);
146module_exit(s3c2412_serial_exit);
147
148MODULE_DESCRIPTION("Samsung S3C2412,S3C2413 SoC Serial port driver");
149MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
150MODULE_LICENSE("GPL v2");
151MODULE_ALIAS("platform:s3c2412-uart");
diff --git a/drivers/tty/serial/s3c2440.c b/drivers/tty/serial/s3c2440.c
new file mode 100644
index 000000000000..9e10d415d5fd
--- /dev/null
+++ b/drivers/tty/serial/s3c2440.c
@@ -0,0 +1,180 @@
1/*
2 * Driver for Samsung S3C2440 and S3C2442 SoC onboard UARTs.
3 *
4 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
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
12#include <linux/module.h>
13#include <linux/ioport.h>
14#include <linux/io.h>
15#include <linux/platform_device.h>
16#include <linux/init.h>
17#include <linux/serial_core.h>
18#include <linux/serial.h>
19
20#include <asm/irq.h>
21#include <mach/hardware.h>
22
23#include <plat/regs-serial.h>
24#include <mach/regs-gpio.h>
25
26#include "samsung.h"
27
28
29static int s3c2440_serial_setsource(struct uart_port *port,
30 struct s3c24xx_uart_clksrc *clk)
31{
32 unsigned long ucon = rd_regl(port, S3C2410_UCON);
33
34 /* todo - proper fclk<>nonfclk switch. */
35
36 ucon &= ~S3C2440_UCON_CLKMASK;
37
38 if (strcmp(clk->name, "uclk") == 0)
39 ucon |= S3C2440_UCON_UCLK;
40 else if (strcmp(clk->name, "pclk") == 0)
41 ucon |= S3C2440_UCON_PCLK;
42 else if (strcmp(clk->name, "fclk") == 0)
43 ucon |= S3C2440_UCON_FCLK;
44 else {
45 printk(KERN_ERR "unknown clock source %s\n", clk->name);
46 return -EINVAL;
47 }
48
49 wr_regl(port, S3C2410_UCON, ucon);
50 return 0;
51}
52
53
54static int s3c2440_serial_getsource(struct uart_port *port,
55 struct s3c24xx_uart_clksrc *clk)
56{
57 unsigned long ucon = rd_regl(port, S3C2410_UCON);
58 unsigned long ucon0, ucon1, ucon2;
59
60 switch (ucon & S3C2440_UCON_CLKMASK) {
61 case S3C2440_UCON_UCLK:
62 clk->divisor = 1;
63 clk->name = "uclk";
64 break;
65
66 case S3C2440_UCON_PCLK:
67 case S3C2440_UCON_PCLK2:
68 clk->divisor = 1;
69 clk->name = "pclk";
70 break;
71
72 case S3C2440_UCON_FCLK:
73 /* the fun of calculating the uart divisors on
74 * the s3c2440 */
75
76 ucon0 = __raw_readl(S3C24XX_VA_UART0 + S3C2410_UCON);
77 ucon1 = __raw_readl(S3C24XX_VA_UART1 + S3C2410_UCON);
78 ucon2 = __raw_readl(S3C24XX_VA_UART2 + S3C2410_UCON);
79
80 printk("ucons: %08lx, %08lx, %08lx\n", ucon0, ucon1, ucon2);
81
82 ucon0 &= S3C2440_UCON0_DIVMASK;
83 ucon1 &= S3C2440_UCON1_DIVMASK;
84 ucon2 &= S3C2440_UCON2_DIVMASK;
85
86 if (ucon0 != 0) {
87 clk->divisor = ucon0 >> S3C2440_UCON_DIVSHIFT;
88 clk->divisor += 6;
89 } else if (ucon1 != 0) {
90 clk->divisor = ucon1 >> S3C2440_UCON_DIVSHIFT;
91 clk->divisor += 21;
92 } else if (ucon2 != 0) {
93 clk->divisor = ucon2 >> S3C2440_UCON_DIVSHIFT;
94 clk->divisor += 36;
95 } else {
96 /* manual calims 44, seems to be 9 */
97 clk->divisor = 9;
98 }
99
100 clk->name = "fclk";
101 break;
102 }
103
104 return 0;
105}
106
107static int s3c2440_serial_resetport(struct uart_port *port,
108 struct s3c2410_uartcfg *cfg)
109{
110 unsigned long ucon = rd_regl(port, S3C2410_UCON);
111
112 dbg("s3c2440_serial_resetport: port=%p (%08lx), cfg=%p\n",
113 port, port->mapbase, cfg);
114
115 /* ensure we don't change the clock settings... */
116
117 ucon &= (S3C2440_UCON0_DIVMASK | (3<<10));
118
119 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
120 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
121
122 /* reset both fifos */
123
124 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
125 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
126
127 return 0;
128}
129
130static struct s3c24xx_uart_info s3c2440_uart_inf = {
131 .name = "Samsung S3C2440 UART",
132 .type = PORT_S3C2440,
133 .fifosize = 64,
134 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
135 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
136 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
137 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
138 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
139 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
140 .get_clksrc = s3c2440_serial_getsource,
141 .set_clksrc = s3c2440_serial_setsource,
142 .reset_port = s3c2440_serial_resetport,
143};
144
145/* device management */
146
147static int s3c2440_serial_probe(struct platform_device *dev)
148{
149 dbg("s3c2440_serial_probe: dev=%p\n", dev);
150 return s3c24xx_serial_probe(dev, &s3c2440_uart_inf);
151}
152
153static struct platform_driver s3c2440_serial_driver = {
154 .probe = s3c2440_serial_probe,
155 .remove = __devexit_p(s3c24xx_serial_remove),
156 .driver = {
157 .name = "s3c2440-uart",
158 .owner = THIS_MODULE,
159 },
160};
161
162s3c24xx_console_init(&s3c2440_serial_driver, &s3c2440_uart_inf);
163
164static int __init s3c2440_serial_init(void)
165{
166 return s3c24xx_serial_init(&s3c2440_serial_driver, &s3c2440_uart_inf);
167}
168
169static void __exit s3c2440_serial_exit(void)
170{
171 platform_driver_unregister(&s3c2440_serial_driver);
172}
173
174module_init(s3c2440_serial_init);
175module_exit(s3c2440_serial_exit);
176
177MODULE_DESCRIPTION("Samsung S3C2440,S3C2442 SoC Serial port driver");
178MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
179MODULE_LICENSE("GPL v2");
180MODULE_ALIAS("platform:s3c2440-uart");
diff --git a/drivers/tty/serial/s3c24a0.c b/drivers/tty/serial/s3c24a0.c
new file mode 100644
index 000000000000..914eff22e499
--- /dev/null
+++ b/drivers/tty/serial/s3c24a0.c
@@ -0,0 +1,117 @@
1/*
2 * Driver for Samsung S3C24A0 SoC onboard UARTs.
3 *
4 * Based on drivers/serial/s3c2410.c
5 *
6 * Author: Sandeep Patil <sandeep.patil@azingo.com>
7 *
8 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
9 * http://armlinux.simtec.co.uk/
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 version 2 as
13 * published by the Free Software Foundation.
14*/
15
16#include <linux/module.h>
17#include <linux/ioport.h>
18#include <linux/platform_device.h>
19#include <linux/init.h>
20#include <linux/serial_core.h>
21#include <linux/serial.h>
22#include <linux/io.h>
23#include <linux/irq.h>
24
25#include <mach/hardware.h>
26
27#include <plat/regs-serial.h>
28#include <mach/regs-gpio.h>
29
30#include "samsung.h"
31
32static int s3c24a0_serial_setsource(struct uart_port *port,
33 struct s3c24xx_uart_clksrc *clk)
34{
35 unsigned long ucon = rd_regl(port, S3C2410_UCON);
36
37 if (strcmp(clk->name, "uclk") == 0)
38 ucon |= S3C2410_UCON_UCLK;
39 else
40 ucon &= ~S3C2410_UCON_UCLK;
41
42 wr_regl(port, S3C2410_UCON, ucon);
43 return 0;
44}
45
46static int s3c24a0_serial_getsource(struct uart_port *port,
47 struct s3c24xx_uart_clksrc *clk)
48{
49 unsigned long ucon = rd_regl(port, S3C2410_UCON);
50
51 clk->divisor = 1;
52 clk->name = (ucon & S3C2410_UCON_UCLK) ? "uclk" : "pclk";
53
54 return 0;
55}
56
57static int s3c24a0_serial_resetport(struct uart_port *port,
58 struct s3c2410_uartcfg *cfg)
59{
60 dbg("s3c24a0_serial_resetport: port=%p (%08lx), cfg=%p\n",
61 port, port->mapbase, cfg);
62
63 wr_regl(port, S3C2410_UCON, cfg->ucon);
64 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
65
66 /* reset both fifos */
67
68 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
69 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
70
71 return 0;
72}
73
74static struct s3c24xx_uart_info s3c24a0_uart_inf = {
75 .name = "Samsung S3C24A0 UART",
76 .type = PORT_S3C2410,
77 .fifosize = 16,
78 .rx_fifomask = S3C24A0_UFSTAT_RXMASK,
79 .rx_fifoshift = S3C24A0_UFSTAT_RXSHIFT,
80 .rx_fifofull = S3C24A0_UFSTAT_RXFULL,
81 .tx_fifofull = S3C24A0_UFSTAT_TXFULL,
82 .tx_fifomask = S3C24A0_UFSTAT_TXMASK,
83 .tx_fifoshift = S3C24A0_UFSTAT_TXSHIFT,
84 .get_clksrc = s3c24a0_serial_getsource,
85 .set_clksrc = s3c24a0_serial_setsource,
86 .reset_port = s3c24a0_serial_resetport,
87};
88
89static int s3c24a0_serial_probe(struct platform_device *dev)
90{
91 return s3c24xx_serial_probe(dev, &s3c24a0_uart_inf);
92}
93
94static struct platform_driver s3c24a0_serial_driver = {
95 .probe = s3c24a0_serial_probe,
96 .remove = __devexit_p(s3c24xx_serial_remove),
97 .driver = {
98 .name = "s3c24a0-uart",
99 .owner = THIS_MODULE,
100 },
101};
102
103s3c24xx_console_init(&s3c24a0_serial_driver, &s3c24a0_uart_inf);
104
105static int __init s3c24a0_serial_init(void)
106{
107 return s3c24xx_serial_init(&s3c24a0_serial_driver, &s3c24a0_uart_inf);
108}
109
110static void __exit s3c24a0_serial_exit(void)
111{
112 platform_driver_unregister(&s3c24a0_serial_driver);
113}
114
115module_init(s3c24a0_serial_init);
116module_exit(s3c24a0_serial_exit);
117
diff --git a/drivers/tty/serial/s3c6400.c b/drivers/tty/serial/s3c6400.c
new file mode 100644
index 000000000000..ded26c42ff37
--- /dev/null
+++ b/drivers/tty/serial/s3c6400.c
@@ -0,0 +1,151 @@
1/*
2 * Driver for Samsung S3C6400 and S3C6410 SoC onboard UARTs.
3 *
4 * Copyright 2008 Openmoko, Inc.
5 * Copyright 2008 Simtec Electronics
6 * Ben Dooks <ben@simtec.co.uk>
7 * http://armlinux.simtec.co.uk/
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
14#include <linux/module.h>
15#include <linux/ioport.h>
16#include <linux/io.h>
17#include <linux/platform_device.h>
18#include <linux/init.h>
19#include <linux/serial_core.h>
20#include <linux/serial.h>
21
22#include <asm/irq.h>
23#include <mach/hardware.h>
24
25#include <plat/regs-serial.h>
26
27#include "samsung.h"
28
29static int s3c6400_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, "uclk0") == 0) {
35 ucon &= ~S3C6400_UCON_CLKMASK;
36 ucon |= S3C6400_UCON_UCLK0;
37 } else if (strcmp(clk->name, "uclk1") == 0)
38 ucon |= S3C6400_UCON_UCLK1;
39 else if (strcmp(clk->name, "pclk") == 0) {
40 /* See notes about transitioning from UCLK to PCLK */
41 ucon &= ~S3C6400_UCON_UCLK0;
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 s3c6400_serial_getsource(struct uart_port *port,
53 struct s3c24xx_uart_clksrc *clk)
54{
55 u32 ucon = rd_regl(port, S3C2410_UCON);
56
57 clk->divisor = 1;
58
59 switch (ucon & S3C6400_UCON_CLKMASK) {
60 case S3C6400_UCON_UCLK0:
61 clk->name = "uclk0";
62 break;
63
64 case S3C6400_UCON_UCLK1:
65 clk->name = "uclk1";
66 break;
67
68 case S3C6400_UCON_PCLK:
69 case S3C6400_UCON_PCLK2:
70 clk->name = "pclk";
71 break;
72 }
73
74 return 0;
75}
76
77static int s3c6400_serial_resetport(struct uart_port *port,
78 struct s3c2410_uartcfg *cfg)
79{
80 unsigned long ucon = rd_regl(port, S3C2410_UCON);
81
82 dbg("s3c6400_serial_resetport: port=%p (%08lx), cfg=%p\n",
83 port, port->mapbase, cfg);
84
85 /* ensure we don't change the clock settings... */
86
87 ucon &= S3C6400_UCON_CLKMASK;
88
89 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
90 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
91
92 /* reset both fifos */
93
94 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
95 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
96
97 return 0;
98}
99
100static struct s3c24xx_uart_info s3c6400_uart_inf = {
101 .name = "Samsung S3C6400 UART",
102 .type = PORT_S3C6400,
103 .fifosize = 64,
104 .has_divslot = 1,
105 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
106 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
107 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
108 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
109 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
110 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
111 .get_clksrc = s3c6400_serial_getsource,
112 .set_clksrc = s3c6400_serial_setsource,
113 .reset_port = s3c6400_serial_resetport,
114};
115
116/* device management */
117
118static int s3c6400_serial_probe(struct platform_device *dev)
119{
120 dbg("s3c6400_serial_probe: dev=%p\n", dev);
121 return s3c24xx_serial_probe(dev, &s3c6400_uart_inf);
122}
123
124static struct platform_driver s3c6400_serial_driver = {
125 .probe = s3c6400_serial_probe,
126 .remove = __devexit_p(s3c24xx_serial_remove),
127 .driver = {
128 .name = "s3c6400-uart",
129 .owner = THIS_MODULE,
130 },
131};
132
133s3c24xx_console_init(&s3c6400_serial_driver, &s3c6400_uart_inf);
134
135static int __init s3c6400_serial_init(void)
136{
137 return s3c24xx_serial_init(&s3c6400_serial_driver, &s3c6400_uart_inf);
138}
139
140static void __exit s3c6400_serial_exit(void)
141{
142 platform_driver_unregister(&s3c6400_serial_driver);
143}
144
145module_init(s3c6400_serial_init);
146module_exit(s3c6400_serial_exit);
147
148MODULE_DESCRIPTION("Samsung S3C6400,S3C6410 SoC Serial port driver");
149MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
150MODULE_LICENSE("GPL v2");
151MODULE_ALIAS("platform:s3c6400-uart");
diff --git a/drivers/tty/serial/s5pv210.c b/drivers/tty/serial/s5pv210.c
new file mode 100644
index 000000000000..dd194dc80ee9
--- /dev/null
+++ b/drivers/tty/serial/s5pv210.c
@@ -0,0 +1,161 @@
1/*
2 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com/
4 *
5 * Based on drivers/serial/s3c6400.c
6 *
7 * Driver for Samsung S5PV210 SoC UARTs.
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
14#include <linux/module.h>
15#include <linux/ioport.h>
16#include <linux/io.h>
17#include <linux/platform_device.h>
18#include <linux/init.h>
19#include <linux/serial_core.h>
20#include <linux/serial.h>
21
22#include <asm/irq.h>
23#include <mach/hardware.h>
24#include <plat/regs-serial.h>
25#include "samsung.h"
26
27static int s5pv210_serial_setsource(struct uart_port *port,
28 struct s3c24xx_uart_clksrc *clk)
29{
30 struct s3c2410_uartcfg *cfg = port->dev->platform_data;
31 unsigned long ucon = rd_regl(port, S3C2410_UCON);
32
33 if (cfg->flags & NO_NEED_CHECK_CLKSRC)
34 return 0;
35
36 if (strcmp(clk->name, "pclk") == 0)
37 ucon &= ~S5PV210_UCON_CLKMASK;
38 else if (strcmp(clk->name, "uclk1") == 0)
39 ucon |= S5PV210_UCON_CLKMASK;
40 else {
41 printk(KERN_ERR "unknown clock source %s\n", clk->name);
42 return -EINVAL;
43 }
44
45 wr_regl(port, S3C2410_UCON, ucon);
46 return 0;
47}
48
49
50static int s5pv210_serial_getsource(struct uart_port *port,
51 struct s3c24xx_uart_clksrc *clk)
52{
53 struct s3c2410_uartcfg *cfg = port->dev->platform_data;
54 u32 ucon = rd_regl(port, S3C2410_UCON);
55
56 clk->divisor = 1;
57
58 if (cfg->flags & NO_NEED_CHECK_CLKSRC)
59 return 0;
60
61 switch (ucon & S5PV210_UCON_CLKMASK) {
62 case S5PV210_UCON_PCLK:
63 clk->name = "pclk";
64 break;
65 case S5PV210_UCON_UCLK:
66 clk->name = "uclk1";
67 break;
68 }
69
70 return 0;
71}
72
73static int s5pv210_serial_resetport(struct uart_port *port,
74 struct s3c2410_uartcfg *cfg)
75{
76 unsigned long ucon = rd_regl(port, S3C2410_UCON);
77
78 ucon &= S5PV210_UCON_CLKMASK;
79 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
80 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
81
82 /* reset both fifos */
83 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
84 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
85
86 return 0;
87}
88
89#define S5PV210_UART_DEFAULT_INFO(fifo_size) \
90 .name = "Samsung S5PV210 UART0", \
91 .type = PORT_S3C6400, \
92 .fifosize = fifo_size, \
93 .has_divslot = 1, \
94 .rx_fifomask = S5PV210_UFSTAT_RXMASK, \
95 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT, \
96 .rx_fifofull = S5PV210_UFSTAT_RXFULL, \
97 .tx_fifofull = S5PV210_UFSTAT_TXFULL, \
98 .tx_fifomask = S5PV210_UFSTAT_TXMASK, \
99 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT, \
100 .get_clksrc = s5pv210_serial_getsource, \
101 .set_clksrc = s5pv210_serial_setsource, \
102 .reset_port = s5pv210_serial_resetport
103
104static struct s3c24xx_uart_info s5p_port_fifo256 = {
105 S5PV210_UART_DEFAULT_INFO(256),
106};
107
108static struct s3c24xx_uart_info s5p_port_fifo64 = {
109 S5PV210_UART_DEFAULT_INFO(64),
110};
111
112static struct s3c24xx_uart_info s5p_port_fifo16 = {
113 S5PV210_UART_DEFAULT_INFO(16),
114};
115
116static struct s3c24xx_uart_info *s5p_uart_inf[] = {
117 [0] = &s5p_port_fifo256,
118 [1] = &s5p_port_fifo64,
119 [2] = &s5p_port_fifo16,
120 [3] = &s5p_port_fifo16,
121};
122
123/* device management */
124static int s5p_serial_probe(struct platform_device *pdev)
125{
126 return s3c24xx_serial_probe(pdev, s5p_uart_inf[pdev->id]);
127}
128
129static struct platform_driver s5p_serial_driver = {
130 .probe = s5p_serial_probe,
131 .remove = __devexit_p(s3c24xx_serial_remove),
132 .driver = {
133 .name = "s5pv210-uart",
134 .owner = THIS_MODULE,
135 },
136};
137
138static int __init s5pv210_serial_console_init(void)
139{
140 return s3c24xx_serial_initconsole(&s5p_serial_driver, s5p_uart_inf);
141}
142
143console_initcall(s5pv210_serial_console_init);
144
145static int __init s5p_serial_init(void)
146{
147 return s3c24xx_serial_init(&s5p_serial_driver, *s5p_uart_inf);
148}
149
150static void __exit s5p_serial_exit(void)
151{
152 platform_driver_unregister(&s5p_serial_driver);
153}
154
155module_init(s5p_serial_init);
156module_exit(s5p_serial_exit);
157
158MODULE_LICENSE("GPL");
159MODULE_ALIAS("platform:s5pv210-uart");
160MODULE_DESCRIPTION("Samsung S5PV210 UART Driver support");
161MODULE_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..ef7a21a6a01b
--- /dev/null
+++ b/drivers/tty/serial/sa1100.c
@@ -0,0 +1,916 @@
1/*
2 * Driver for SA11x0 serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2000 Deep Blue Solutions Ltd.
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_SA1100_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
24#define SUPPORT_SYSRQ
25#endif
26
27#include <linux/module.h>
28#include <linux/ioport.h>
29#include <linux/init.h>
30#include <linux/console.h>
31#include <linux/sysrq.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#include <linux/serial.h>
37#include <linux/io.h>
38
39#include <asm/irq.h>
40#include <mach/hardware.h>
41#include <asm/mach/serial_sa1100.h>
42
43/* We've been assigned a range on the "Low-density serial ports" major */
44#define SERIAL_SA1100_MAJOR 204
45#define MINOR_START 5
46
47#define NR_PORTS 3
48
49#define SA1100_ISR_PASS_LIMIT 256
50
51/*
52 * Convert from ignore_status_mask or read_status_mask to UTSR[01]
53 */
54#define SM_TO_UTSR0(x) ((x) & 0xff)
55#define SM_TO_UTSR1(x) ((x) >> 8)
56#define UTSR0_TO_SM(x) ((x))
57#define UTSR1_TO_SM(x) ((x) << 8)
58
59#define UART_GET_UTCR0(sport) __raw_readl((sport)->port.membase + UTCR0)
60#define UART_GET_UTCR1(sport) __raw_readl((sport)->port.membase + UTCR1)
61#define UART_GET_UTCR2(sport) __raw_readl((sport)->port.membase + UTCR2)
62#define UART_GET_UTCR3(sport) __raw_readl((sport)->port.membase + UTCR3)
63#define UART_GET_UTSR0(sport) __raw_readl((sport)->port.membase + UTSR0)
64#define UART_GET_UTSR1(sport) __raw_readl((sport)->port.membase + UTSR1)
65#define UART_GET_CHAR(sport) __raw_readl((sport)->port.membase + UTDR)
66
67#define UART_PUT_UTCR0(sport,v) __raw_writel((v),(sport)->port.membase + UTCR0)
68#define UART_PUT_UTCR1(sport,v) __raw_writel((v),(sport)->port.membase + UTCR1)
69#define UART_PUT_UTCR2(sport,v) __raw_writel((v),(sport)->port.membase + UTCR2)
70#define UART_PUT_UTCR3(sport,v) __raw_writel((v),(sport)->port.membase + UTCR3)
71#define UART_PUT_UTSR0(sport,v) __raw_writel((v),(sport)->port.membase + UTSR0)
72#define UART_PUT_UTSR1(sport,v) __raw_writel((v),(sport)->port.membase + UTSR1)
73#define UART_PUT_CHAR(sport,v) __raw_writel((v),(sport)->port.membase + UTDR)
74
75/*
76 * This is the size of our serial port register set.
77 */
78#define UART_PORT_SIZE 0x24
79
80/*
81 * This determines how often we check the modem status signals
82 * for any change. They generally aren't connected to an IRQ
83 * so we have to poll them. We also check immediately before
84 * filling the TX fifo incase CTS has been dropped.
85 */
86#define MCTRL_TIMEOUT (250*HZ/1000)
87
88struct sa1100_port {
89 struct uart_port port;
90 struct timer_list timer;
91 unsigned int old_status;
92};
93
94/*
95 * Handle any change of modem status signal since we were last called.
96 */
97static void sa1100_mctrl_check(struct sa1100_port *sport)
98{
99 unsigned int status, changed;
100
101 status = sport->port.ops->get_mctrl(&sport->port);
102 changed = status ^ sport->old_status;
103
104 if (changed == 0)
105 return;
106
107 sport->old_status = status;
108
109 if (changed & TIOCM_RI)
110 sport->port.icount.rng++;
111 if (changed & TIOCM_DSR)
112 sport->port.icount.dsr++;
113 if (changed & TIOCM_CAR)
114 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
115 if (changed & TIOCM_CTS)
116 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
117
118 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
119}
120
121/*
122 * This is our per-port timeout handler, for checking the
123 * modem status signals.
124 */
125static void sa1100_timeout(unsigned long data)
126{
127 struct sa1100_port *sport = (struct sa1100_port *)data;
128 unsigned long flags;
129
130 if (sport->port.state) {
131 spin_lock_irqsave(&sport->port.lock, flags);
132 sa1100_mctrl_check(sport);
133 spin_unlock_irqrestore(&sport->port.lock, flags);
134
135 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
136 }
137}
138
139/*
140 * interrupts disabled on entry
141 */
142static void sa1100_stop_tx(struct uart_port *port)
143{
144 struct sa1100_port *sport = (struct sa1100_port *)port;
145 u32 utcr3;
146
147 utcr3 = UART_GET_UTCR3(sport);
148 UART_PUT_UTCR3(sport, utcr3 & ~UTCR3_TIE);
149 sport->port.read_status_mask &= ~UTSR0_TO_SM(UTSR0_TFS);
150}
151
152/*
153 * port locked and interrupts disabled
154 */
155static void sa1100_start_tx(struct uart_port *port)
156{
157 struct sa1100_port *sport = (struct sa1100_port *)port;
158 u32 utcr3;
159
160 utcr3 = UART_GET_UTCR3(sport);
161 sport->port.read_status_mask |= UTSR0_TO_SM(UTSR0_TFS);
162 UART_PUT_UTCR3(sport, utcr3 | UTCR3_TIE);
163}
164
165/*
166 * Interrupts enabled
167 */
168static void sa1100_stop_rx(struct uart_port *port)
169{
170 struct sa1100_port *sport = (struct sa1100_port *)port;
171 u32 utcr3;
172
173 utcr3 = UART_GET_UTCR3(sport);
174 UART_PUT_UTCR3(sport, utcr3 & ~UTCR3_RIE);
175}
176
177/*
178 * Set the modem control timer to fire immediately.
179 */
180static void sa1100_enable_ms(struct uart_port *port)
181{
182 struct sa1100_port *sport = (struct sa1100_port *)port;
183
184 mod_timer(&sport->timer, jiffies);
185}
186
187static void
188sa1100_rx_chars(struct sa1100_port *sport)
189{
190 struct tty_struct *tty = sport->port.state->port.tty;
191 unsigned int status, ch, flg;
192
193 status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) |
194 UTSR0_TO_SM(UART_GET_UTSR0(sport));
195 while (status & UTSR1_TO_SM(UTSR1_RNE)) {
196 ch = UART_GET_CHAR(sport);
197
198 sport->port.icount.rx++;
199
200 flg = TTY_NORMAL;
201
202 /*
203 * note that the error handling code is
204 * out of the main execution path
205 */
206 if (status & UTSR1_TO_SM(UTSR1_PRE | UTSR1_FRE | UTSR1_ROR)) {
207 if (status & UTSR1_TO_SM(UTSR1_PRE))
208 sport->port.icount.parity++;
209 else if (status & UTSR1_TO_SM(UTSR1_FRE))
210 sport->port.icount.frame++;
211 if (status & UTSR1_TO_SM(UTSR1_ROR))
212 sport->port.icount.overrun++;
213
214 status &= sport->port.read_status_mask;
215
216 if (status & UTSR1_TO_SM(UTSR1_PRE))
217 flg = TTY_PARITY;
218 else if (status & UTSR1_TO_SM(UTSR1_FRE))
219 flg = TTY_FRAME;
220
221#ifdef SUPPORT_SYSRQ
222 sport->port.sysrq = 0;
223#endif
224 }
225
226 if (uart_handle_sysrq_char(&sport->port, ch))
227 goto ignore_char;
228
229 uart_insert_char(&sport->port, status, UTSR1_TO_SM(UTSR1_ROR), ch, flg);
230
231 ignore_char:
232 status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) |
233 UTSR0_TO_SM(UART_GET_UTSR0(sport));
234 }
235 tty_flip_buffer_push(tty);
236}
237
238static void sa1100_tx_chars(struct sa1100_port *sport)
239{
240 struct circ_buf *xmit = &sport->port.state->xmit;
241
242 if (sport->port.x_char) {
243 UART_PUT_CHAR(sport, sport->port.x_char);
244 sport->port.icount.tx++;
245 sport->port.x_char = 0;
246 return;
247 }
248
249 /*
250 * Check the modem control lines before
251 * transmitting anything.
252 */
253 sa1100_mctrl_check(sport);
254
255 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
256 sa1100_stop_tx(&sport->port);
257 return;
258 }
259
260 /*
261 * Tried using FIFO (not checking TNF) for fifo fill:
262 * still had the '4 bytes repeated' problem.
263 */
264 while (UART_GET_UTSR1(sport) & UTSR1_TNF) {
265 UART_PUT_CHAR(sport, xmit->buf[xmit->tail]);
266 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
267 sport->port.icount.tx++;
268 if (uart_circ_empty(xmit))
269 break;
270 }
271
272 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
273 uart_write_wakeup(&sport->port);
274
275 if (uart_circ_empty(xmit))
276 sa1100_stop_tx(&sport->port);
277}
278
279static irqreturn_t sa1100_int(int irq, void *dev_id)
280{
281 struct sa1100_port *sport = dev_id;
282 unsigned int status, pass_counter = 0;
283
284 spin_lock(&sport->port.lock);
285 status = UART_GET_UTSR0(sport);
286 status &= SM_TO_UTSR0(sport->port.read_status_mask) | ~UTSR0_TFS;
287 do {
288 if (status & (UTSR0_RFS | UTSR0_RID)) {
289 /* Clear the receiver idle bit, if set */
290 if (status & UTSR0_RID)
291 UART_PUT_UTSR0(sport, UTSR0_RID);
292 sa1100_rx_chars(sport);
293 }
294
295 /* Clear the relevant break bits */
296 if (status & (UTSR0_RBB | UTSR0_REB))
297 UART_PUT_UTSR0(sport, status & (UTSR0_RBB | UTSR0_REB));
298
299 if (status & UTSR0_RBB)
300 sport->port.icount.brk++;
301
302 if (status & UTSR0_REB)
303 uart_handle_break(&sport->port);
304
305 if (status & UTSR0_TFS)
306 sa1100_tx_chars(sport);
307 if (pass_counter++ > SA1100_ISR_PASS_LIMIT)
308 break;
309 status = UART_GET_UTSR0(sport);
310 status &= SM_TO_UTSR0(sport->port.read_status_mask) |
311 ~UTSR0_TFS;
312 } while (status & (UTSR0_TFS | UTSR0_RFS | UTSR0_RID));
313 spin_unlock(&sport->port.lock);
314
315 return IRQ_HANDLED;
316}
317
318/*
319 * Return TIOCSER_TEMT when transmitter is not busy.
320 */
321static unsigned int sa1100_tx_empty(struct uart_port *port)
322{
323 struct sa1100_port *sport = (struct sa1100_port *)port;
324
325 return UART_GET_UTSR1(sport) & UTSR1_TBY ? 0 : TIOCSER_TEMT;
326}
327
328static unsigned int sa1100_get_mctrl(struct uart_port *port)
329{
330 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
331}
332
333static void sa1100_set_mctrl(struct uart_port *port, unsigned int mctrl)
334{
335}
336
337/*
338 * Interrupts always disabled.
339 */
340static void sa1100_break_ctl(struct uart_port *port, int break_state)
341{
342 struct sa1100_port *sport = (struct sa1100_port *)port;
343 unsigned long flags;
344 unsigned int utcr3;
345
346 spin_lock_irqsave(&sport->port.lock, flags);
347 utcr3 = UART_GET_UTCR3(sport);
348 if (break_state == -1)
349 utcr3 |= UTCR3_BRK;
350 else
351 utcr3 &= ~UTCR3_BRK;
352 UART_PUT_UTCR3(sport, utcr3);
353 spin_unlock_irqrestore(&sport->port.lock, flags);
354}
355
356static int sa1100_startup(struct uart_port *port)
357{
358 struct sa1100_port *sport = (struct sa1100_port *)port;
359 int retval;
360
361 /*
362 * Allocate the IRQ
363 */
364 retval = request_irq(sport->port.irq, sa1100_int, 0,
365 "sa11x0-uart", sport);
366 if (retval)
367 return retval;
368
369 /*
370 * Finally, clear and enable interrupts
371 */
372 UART_PUT_UTSR0(sport, -1);
373 UART_PUT_UTCR3(sport, UTCR3_RXE | UTCR3_TXE | UTCR3_RIE);
374
375 /*
376 * Enable modem status interrupts
377 */
378 spin_lock_irq(&sport->port.lock);
379 sa1100_enable_ms(&sport->port);
380 spin_unlock_irq(&sport->port.lock);
381
382 return 0;
383}
384
385static void sa1100_shutdown(struct uart_port *port)
386{
387 struct sa1100_port *sport = (struct sa1100_port *)port;
388
389 /*
390 * Stop our timer.
391 */
392 del_timer_sync(&sport->timer);
393
394 /*
395 * Free the interrupt
396 */
397 free_irq(sport->port.irq, sport);
398
399 /*
400 * Disable all interrupts, port and break condition.
401 */
402 UART_PUT_UTCR3(sport, 0);
403}
404
405static void
406sa1100_set_termios(struct uart_port *port, struct ktermios *termios,
407 struct ktermios *old)
408{
409 struct sa1100_port *sport = (struct sa1100_port *)port;
410 unsigned long flags;
411 unsigned int utcr0, old_utcr3, baud, quot;
412 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
413
414 /*
415 * We only support CS7 and CS8.
416 */
417 while ((termios->c_cflag & CSIZE) != CS7 &&
418 (termios->c_cflag & CSIZE) != CS8) {
419 termios->c_cflag &= ~CSIZE;
420 termios->c_cflag |= old_csize;
421 old_csize = CS8;
422 }
423
424 if ((termios->c_cflag & CSIZE) == CS8)
425 utcr0 = UTCR0_DSS;
426 else
427 utcr0 = 0;
428
429 if (termios->c_cflag & CSTOPB)
430 utcr0 |= UTCR0_SBS;
431 if (termios->c_cflag & PARENB) {
432 utcr0 |= UTCR0_PE;
433 if (!(termios->c_cflag & PARODD))
434 utcr0 |= UTCR0_OES;
435 }
436
437 /*
438 * Ask the core to calculate the divisor for us.
439 */
440 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
441 quot = uart_get_divisor(port, baud);
442
443 spin_lock_irqsave(&sport->port.lock, flags);
444
445 sport->port.read_status_mask &= UTSR0_TO_SM(UTSR0_TFS);
446 sport->port.read_status_mask |= UTSR1_TO_SM(UTSR1_ROR);
447 if (termios->c_iflag & INPCK)
448 sport->port.read_status_mask |=
449 UTSR1_TO_SM(UTSR1_FRE | UTSR1_PRE);
450 if (termios->c_iflag & (BRKINT | PARMRK))
451 sport->port.read_status_mask |=
452 UTSR0_TO_SM(UTSR0_RBB | UTSR0_REB);
453
454 /*
455 * Characters to ignore
456 */
457 sport->port.ignore_status_mask = 0;
458 if (termios->c_iflag & IGNPAR)
459 sport->port.ignore_status_mask |=
460 UTSR1_TO_SM(UTSR1_FRE | UTSR1_PRE);
461 if (termios->c_iflag & IGNBRK) {
462 sport->port.ignore_status_mask |=
463 UTSR0_TO_SM(UTSR0_RBB | UTSR0_REB);
464 /*
465 * If we're ignoring parity and break indicators,
466 * ignore overruns too (for real raw support).
467 */
468 if (termios->c_iflag & IGNPAR)
469 sport->port.ignore_status_mask |=
470 UTSR1_TO_SM(UTSR1_ROR);
471 }
472
473 del_timer_sync(&sport->timer);
474
475 /*
476 * Update the per-port timeout.
477 */
478 uart_update_timeout(port, termios->c_cflag, baud);
479
480 /*
481 * disable interrupts and drain transmitter
482 */
483 old_utcr3 = UART_GET_UTCR3(sport);
484 UART_PUT_UTCR3(sport, old_utcr3 & ~(UTCR3_RIE | UTCR3_TIE));
485
486 while (UART_GET_UTSR1(sport) & UTSR1_TBY)
487 barrier();
488
489 /* then, disable everything */
490 UART_PUT_UTCR3(sport, 0);
491
492 /* set the parity, stop bits and data size */
493 UART_PUT_UTCR0(sport, utcr0);
494
495 /* set the baud rate */
496 quot -= 1;
497 UART_PUT_UTCR1(sport, ((quot & 0xf00) >> 8));
498 UART_PUT_UTCR2(sport, (quot & 0xff));
499
500 UART_PUT_UTSR0(sport, -1);
501
502 UART_PUT_UTCR3(sport, old_utcr3);
503
504 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
505 sa1100_enable_ms(&sport->port);
506
507 spin_unlock_irqrestore(&sport->port.lock, flags);
508}
509
510static const char *sa1100_type(struct uart_port *port)
511{
512 struct sa1100_port *sport = (struct sa1100_port *)port;
513
514 return sport->port.type == PORT_SA1100 ? "SA1100" : NULL;
515}
516
517/*
518 * Release the memory region(s) being used by 'port'.
519 */
520static void sa1100_release_port(struct uart_port *port)
521{
522 struct sa1100_port *sport = (struct sa1100_port *)port;
523
524 release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
525}
526
527/*
528 * Request the memory region(s) being used by 'port'.
529 */
530static int sa1100_request_port(struct uart_port *port)
531{
532 struct sa1100_port *sport = (struct sa1100_port *)port;
533
534 return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
535 "sa11x0-uart") != NULL ? 0 : -EBUSY;
536}
537
538/*
539 * Configure/autoconfigure the port.
540 */
541static void sa1100_config_port(struct uart_port *port, int flags)
542{
543 struct sa1100_port *sport = (struct sa1100_port *)port;
544
545 if (flags & UART_CONFIG_TYPE &&
546 sa1100_request_port(&sport->port) == 0)
547 sport->port.type = PORT_SA1100;
548}
549
550/*
551 * Verify the new serial_struct (for TIOCSSERIAL).
552 * The only change we allow are to the flags and type, and
553 * even then only between PORT_SA1100 and PORT_UNKNOWN
554 */
555static int
556sa1100_verify_port(struct uart_port *port, struct serial_struct *ser)
557{
558 struct sa1100_port *sport = (struct sa1100_port *)port;
559 int ret = 0;
560
561 if (ser->type != PORT_UNKNOWN && ser->type != PORT_SA1100)
562 ret = -EINVAL;
563 if (sport->port.irq != ser->irq)
564 ret = -EINVAL;
565 if (ser->io_type != SERIAL_IO_MEM)
566 ret = -EINVAL;
567 if (sport->port.uartclk / 16 != ser->baud_base)
568 ret = -EINVAL;
569 if ((void *)sport->port.mapbase != ser->iomem_base)
570 ret = -EINVAL;
571 if (sport->port.iobase != ser->port)
572 ret = -EINVAL;
573 if (ser->hub6 != 0)
574 ret = -EINVAL;
575 return ret;
576}
577
578static struct uart_ops sa1100_pops = {
579 .tx_empty = sa1100_tx_empty,
580 .set_mctrl = sa1100_set_mctrl,
581 .get_mctrl = sa1100_get_mctrl,
582 .stop_tx = sa1100_stop_tx,
583 .start_tx = sa1100_start_tx,
584 .stop_rx = sa1100_stop_rx,
585 .enable_ms = sa1100_enable_ms,
586 .break_ctl = sa1100_break_ctl,
587 .startup = sa1100_startup,
588 .shutdown = sa1100_shutdown,
589 .set_termios = sa1100_set_termios,
590 .type = sa1100_type,
591 .release_port = sa1100_release_port,
592 .request_port = sa1100_request_port,
593 .config_port = sa1100_config_port,
594 .verify_port = sa1100_verify_port,
595};
596
597static struct sa1100_port sa1100_ports[NR_PORTS];
598
599/*
600 * Setup the SA1100 serial ports. Note that we don't include the IrDA
601 * port here since we have our own SIR/FIR driver (see drivers/net/irda)
602 *
603 * Note also that we support "console=ttySAx" where "x" is either 0 or 1.
604 * Which serial port this ends up being depends on the machine you're
605 * running this kernel on. I'm not convinced that this is a good idea,
606 * but that's the way it traditionally works.
607 *
608 * Note that NanoEngine UART3 becomes UART2, and UART2 is no longer
609 * used here.
610 */
611static void __init sa1100_init_ports(void)
612{
613 static int first = 1;
614 int i;
615
616 if (!first)
617 return;
618 first = 0;
619
620 for (i = 0; i < NR_PORTS; i++) {
621 sa1100_ports[i].port.uartclk = 3686400;
622 sa1100_ports[i].port.ops = &sa1100_pops;
623 sa1100_ports[i].port.fifosize = 8;
624 sa1100_ports[i].port.line = i;
625 sa1100_ports[i].port.iotype = UPIO_MEM;
626 init_timer(&sa1100_ports[i].timer);
627 sa1100_ports[i].timer.function = sa1100_timeout;
628 sa1100_ports[i].timer.data = (unsigned long)&sa1100_ports[i];
629 }
630
631 /*
632 * make transmit lines outputs, so that when the port
633 * is closed, the output is in the MARK state.
634 */
635 PPDR |= PPC_TXD1 | PPC_TXD3;
636 PPSR |= PPC_TXD1 | PPC_TXD3;
637}
638
639void __devinit sa1100_register_uart_fns(struct sa1100_port_fns *fns)
640{
641 if (fns->get_mctrl)
642 sa1100_pops.get_mctrl = fns->get_mctrl;
643 if (fns->set_mctrl)
644 sa1100_pops.set_mctrl = fns->set_mctrl;
645
646 sa1100_pops.pm = fns->pm;
647 sa1100_pops.set_wake = fns->set_wake;
648}
649
650void __init sa1100_register_uart(int idx, int port)
651{
652 if (idx >= NR_PORTS) {
653 printk(KERN_ERR "%s: bad index number %d\n", __func__, idx);
654 return;
655 }
656
657 switch (port) {
658 case 1:
659 sa1100_ports[idx].port.membase = (void __iomem *)&Ser1UTCR0;
660 sa1100_ports[idx].port.mapbase = _Ser1UTCR0;
661 sa1100_ports[idx].port.irq = IRQ_Ser1UART;
662 sa1100_ports[idx].port.flags = UPF_BOOT_AUTOCONF;
663 break;
664
665 case 2:
666 sa1100_ports[idx].port.membase = (void __iomem *)&Ser2UTCR0;
667 sa1100_ports[idx].port.mapbase = _Ser2UTCR0;
668 sa1100_ports[idx].port.irq = IRQ_Ser2ICP;
669 sa1100_ports[idx].port.flags = UPF_BOOT_AUTOCONF;
670 break;
671
672 case 3:
673 sa1100_ports[idx].port.membase = (void __iomem *)&Ser3UTCR0;
674 sa1100_ports[idx].port.mapbase = _Ser3UTCR0;
675 sa1100_ports[idx].port.irq = IRQ_Ser3UART;
676 sa1100_ports[idx].port.flags = UPF_BOOT_AUTOCONF;
677 break;
678
679 default:
680 printk(KERN_ERR "%s: bad port number %d\n", __func__, port);
681 }
682}
683
684
685#ifdef CONFIG_SERIAL_SA1100_CONSOLE
686static void sa1100_console_putchar(struct uart_port *port, int ch)
687{
688 struct sa1100_port *sport = (struct sa1100_port *)port;
689
690 while (!(UART_GET_UTSR1(sport) & UTSR1_TNF))
691 barrier();
692 UART_PUT_CHAR(sport, ch);
693}
694
695/*
696 * Interrupts are disabled on entering
697 */
698static void
699sa1100_console_write(struct console *co, const char *s, unsigned int count)
700{
701 struct sa1100_port *sport = &sa1100_ports[co->index];
702 unsigned int old_utcr3, status;
703
704 /*
705 * First, save UTCR3 and then disable interrupts
706 */
707 old_utcr3 = UART_GET_UTCR3(sport);
708 UART_PUT_UTCR3(sport, (old_utcr3 & ~(UTCR3_RIE | UTCR3_TIE)) |
709 UTCR3_TXE);
710
711 uart_console_write(&sport->port, s, count, sa1100_console_putchar);
712
713 /*
714 * Finally, wait for transmitter to become empty
715 * and restore UTCR3
716 */
717 do {
718 status = UART_GET_UTSR1(sport);
719 } while (status & UTSR1_TBY);
720 UART_PUT_UTCR3(sport, old_utcr3);
721}
722
723/*
724 * If the port was already initialised (eg, by a boot loader),
725 * try to determine the current setup.
726 */
727static void __init
728sa1100_console_get_options(struct sa1100_port *sport, int *baud,
729 int *parity, int *bits)
730{
731 unsigned int utcr3;
732
733 utcr3 = UART_GET_UTCR3(sport) & (UTCR3_RXE | UTCR3_TXE);
734 if (utcr3 == (UTCR3_RXE | UTCR3_TXE)) {
735 /* ok, the port was enabled */
736 unsigned int utcr0, quot;
737
738 utcr0 = UART_GET_UTCR0(sport);
739
740 *parity = 'n';
741 if (utcr0 & UTCR0_PE) {
742 if (utcr0 & UTCR0_OES)
743 *parity = 'e';
744 else
745 *parity = 'o';
746 }
747
748 if (utcr0 & UTCR0_DSS)
749 *bits = 8;
750 else
751 *bits = 7;
752
753 quot = UART_GET_UTCR2(sport) | UART_GET_UTCR1(sport) << 8;
754 quot &= 0xfff;
755 *baud = sport->port.uartclk / (16 * (quot + 1));
756 }
757}
758
759static int __init
760sa1100_console_setup(struct console *co, char *options)
761{
762 struct sa1100_port *sport;
763 int baud = 9600;
764 int bits = 8;
765 int parity = 'n';
766 int flow = 'n';
767
768 /*
769 * Check whether an invalid uart number has been specified, and
770 * if so, search for the first available port that does have
771 * console support.
772 */
773 if (co->index == -1 || co->index >= NR_PORTS)
774 co->index = 0;
775 sport = &sa1100_ports[co->index];
776
777 if (options)
778 uart_parse_options(options, &baud, &parity, &bits, &flow);
779 else
780 sa1100_console_get_options(sport, &baud, &parity, &bits);
781
782 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
783}
784
785static struct uart_driver sa1100_reg;
786static struct console sa1100_console = {
787 .name = "ttySA",
788 .write = sa1100_console_write,
789 .device = uart_console_device,
790 .setup = sa1100_console_setup,
791 .flags = CON_PRINTBUFFER,
792 .index = -1,
793 .data = &sa1100_reg,
794};
795
796static int __init sa1100_rs_console_init(void)
797{
798 sa1100_init_ports();
799 register_console(&sa1100_console);
800 return 0;
801}
802console_initcall(sa1100_rs_console_init);
803
804#define SA1100_CONSOLE &sa1100_console
805#else
806#define SA1100_CONSOLE NULL
807#endif
808
809static struct uart_driver sa1100_reg = {
810 .owner = THIS_MODULE,
811 .driver_name = "ttySA",
812 .dev_name = "ttySA",
813 .major = SERIAL_SA1100_MAJOR,
814 .minor = MINOR_START,
815 .nr = NR_PORTS,
816 .cons = SA1100_CONSOLE,
817};
818
819static int sa1100_serial_suspend(struct platform_device *dev, pm_message_t state)
820{
821 struct sa1100_port *sport = platform_get_drvdata(dev);
822
823 if (sport)
824 uart_suspend_port(&sa1100_reg, &sport->port);
825
826 return 0;
827}
828
829static int sa1100_serial_resume(struct platform_device *dev)
830{
831 struct sa1100_port *sport = platform_get_drvdata(dev);
832
833 if (sport)
834 uart_resume_port(&sa1100_reg, &sport->port);
835
836 return 0;
837}
838
839static int sa1100_serial_probe(struct platform_device *dev)
840{
841 struct resource *res = dev->resource;
842 int i;
843
844 for (i = 0; i < dev->num_resources; i++, res++)
845 if (res->flags & IORESOURCE_MEM)
846 break;
847
848 if (i < dev->num_resources) {
849 for (i = 0; i < NR_PORTS; i++) {
850 if (sa1100_ports[i].port.mapbase != res->start)
851 continue;
852
853 sa1100_ports[i].port.dev = &dev->dev;
854 uart_add_one_port(&sa1100_reg, &sa1100_ports[i].port);
855 platform_set_drvdata(dev, &sa1100_ports[i]);
856 break;
857 }
858 }
859
860 return 0;
861}
862
863static int sa1100_serial_remove(struct platform_device *pdev)
864{
865 struct sa1100_port *sport = platform_get_drvdata(pdev);
866
867 platform_set_drvdata(pdev, NULL);
868
869 if (sport)
870 uart_remove_one_port(&sa1100_reg, &sport->port);
871
872 return 0;
873}
874
875static struct platform_driver sa11x0_serial_driver = {
876 .probe = sa1100_serial_probe,
877 .remove = sa1100_serial_remove,
878 .suspend = sa1100_serial_suspend,
879 .resume = sa1100_serial_resume,
880 .driver = {
881 .name = "sa11x0-uart",
882 .owner = THIS_MODULE,
883 },
884};
885
886static int __init sa1100_serial_init(void)
887{
888 int ret;
889
890 printk(KERN_INFO "Serial: SA11x0 driver\n");
891
892 sa1100_init_ports();
893
894 ret = uart_register_driver(&sa1100_reg);
895 if (ret == 0) {
896 ret = platform_driver_register(&sa11x0_serial_driver);
897 if (ret)
898 uart_unregister_driver(&sa1100_reg);
899 }
900 return ret;
901}
902
903static void __exit sa1100_serial_exit(void)
904{
905 platform_driver_unregister(&sa11x0_serial_driver);
906 uart_unregister_driver(&sa1100_reg);
907}
908
909module_init(sa1100_serial_init);
910module_exit(sa1100_serial_exit);
911
912MODULE_AUTHOR("Deep Blue Solutions Ltd");
913MODULE_DESCRIPTION("SA1100 generic serial port driver");
914MODULE_LICENSE("GPL");
915MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_SA1100_MAJOR);
916MODULE_ALIAS("platform:sa11x0-uart");
diff --git a/drivers/tty/serial/samsung.c b/drivers/tty/serial/samsung.c
new file mode 100644
index 000000000000..f66f64829303
--- /dev/null
+++ b/drivers/tty/serial/samsung.c
@@ -0,0 +1,1486 @@
1/*
2 * Driver core for Samsung SoC onboard UARTs.
3 *
4 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
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
12/* Hote on 2410 error handling
13 *
14 * The s3c2410 manual has a love/hate affair with the contents of the
15 * UERSTAT register in the UART blocks, and keeps marking some of the
16 * error bits as reserved. Having checked with the s3c2410x01,
17 * it copes with BREAKs properly, so I am happy to ignore the RESERVED
18 * feature from the latter versions of the manual.
19 *
20 * If it becomes aparrent that latter versions of the 2410 remove these
21 * bits, then action will have to be taken to differentiate the versions
22 * and change the policy on BREAK
23 *
24 * BJD, 04-Nov-2004
25*/
26
27#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
28#define SUPPORT_SYSRQ
29#endif
30
31#include <linux/module.h>
32#include <linux/ioport.h>
33#include <linux/io.h>
34#include <linux/platform_device.h>
35#include <linux/init.h>
36#include <linux/sysrq.h>
37#include <linux/console.h>
38#include <linux/tty.h>
39#include <linux/tty_flip.h>
40#include <linux/serial_core.h>
41#include <linux/serial.h>
42#include <linux/delay.h>
43#include <linux/clk.h>
44#include <linux/cpufreq.h>
45
46#include <asm/irq.h>
47
48#include <mach/hardware.h>
49#include <mach/map.h>
50
51#include <plat/regs-serial.h>
52
53#include "samsung.h"
54
55/* UART name and device definitions */
56
57#define S3C24XX_SERIAL_NAME "ttySAC"
58#define S3C24XX_SERIAL_MAJOR 204
59#define S3C24XX_SERIAL_MINOR 64
60
61/* macros to change one thing to another */
62
63#define tx_enabled(port) ((port)->unused[0])
64#define rx_enabled(port) ((port)->unused[1])
65
66/* flag to ignore all characters coming in */
67#define RXSTAT_DUMMY_READ (0x10000000)
68
69static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
70{
71 return container_of(port, struct s3c24xx_uart_port, port);
72}
73
74/* translate a port to the device name */
75
76static inline const char *s3c24xx_serial_portname(struct uart_port *port)
77{
78 return to_platform_device(port->dev)->name;
79}
80
81static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
82{
83 return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
84}
85
86static void s3c24xx_serial_rx_enable(struct uart_port *port)
87{
88 unsigned long flags;
89 unsigned int ucon, ufcon;
90 int count = 10000;
91
92 spin_lock_irqsave(&port->lock, flags);
93
94 while (--count && !s3c24xx_serial_txempty_nofifo(port))
95 udelay(100);
96
97 ufcon = rd_regl(port, S3C2410_UFCON);
98 ufcon |= S3C2410_UFCON_RESETRX;
99 wr_regl(port, S3C2410_UFCON, ufcon);
100
101 ucon = rd_regl(port, S3C2410_UCON);
102 ucon |= S3C2410_UCON_RXIRQMODE;
103 wr_regl(port, S3C2410_UCON, ucon);
104
105 rx_enabled(port) = 1;
106 spin_unlock_irqrestore(&port->lock, flags);
107}
108
109static void s3c24xx_serial_rx_disable(struct uart_port *port)
110{
111 unsigned long flags;
112 unsigned int ucon;
113
114 spin_lock_irqsave(&port->lock, flags);
115
116 ucon = rd_regl(port, S3C2410_UCON);
117 ucon &= ~S3C2410_UCON_RXIRQMODE;
118 wr_regl(port, S3C2410_UCON, ucon);
119
120 rx_enabled(port) = 0;
121 spin_unlock_irqrestore(&port->lock, flags);
122}
123
124static void s3c24xx_serial_stop_tx(struct uart_port *port)
125{
126 struct s3c24xx_uart_port *ourport = to_ourport(port);
127
128 if (tx_enabled(port)) {
129 disable_irq_nosync(ourport->tx_irq);
130 tx_enabled(port) = 0;
131 if (port->flags & UPF_CONS_FLOW)
132 s3c24xx_serial_rx_enable(port);
133 }
134}
135
136static void s3c24xx_serial_start_tx(struct uart_port *port)
137{
138 struct s3c24xx_uart_port *ourport = to_ourport(port);
139
140 if (!tx_enabled(port)) {
141 if (port->flags & UPF_CONS_FLOW)
142 s3c24xx_serial_rx_disable(port);
143
144 enable_irq(ourport->tx_irq);
145 tx_enabled(port) = 1;
146 }
147}
148
149
150static void s3c24xx_serial_stop_rx(struct uart_port *port)
151{
152 struct s3c24xx_uart_port *ourport = to_ourport(port);
153
154 if (rx_enabled(port)) {
155 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
156 disable_irq_nosync(ourport->rx_irq);
157 rx_enabled(port) = 0;
158 }
159}
160
161static void s3c24xx_serial_enable_ms(struct uart_port *port)
162{
163}
164
165static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
166{
167 return to_ourport(port)->info;
168}
169
170static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
171{
172 if (port->dev == NULL)
173 return NULL;
174
175 return (struct s3c2410_uartcfg *)port->dev->platform_data;
176}
177
178static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
179 unsigned long ufstat)
180{
181 struct s3c24xx_uart_info *info = ourport->info;
182
183 if (ufstat & info->rx_fifofull)
184 return info->fifosize;
185
186 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
187}
188
189
190/* ? - where has parity gone?? */
191#define S3C2410_UERSTAT_PARITY (0x1000)
192
193static irqreturn_t
194s3c24xx_serial_rx_chars(int irq, void *dev_id)
195{
196 struct s3c24xx_uart_port *ourport = dev_id;
197 struct uart_port *port = &ourport->port;
198 struct tty_struct *tty = port->state->port.tty;
199 unsigned int ufcon, ch, flag, ufstat, uerstat;
200 int max_count = 64;
201
202 while (max_count-- > 0) {
203 ufcon = rd_regl(port, S3C2410_UFCON);
204 ufstat = rd_regl(port, S3C2410_UFSTAT);
205
206 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
207 break;
208
209 uerstat = rd_regl(port, S3C2410_UERSTAT);
210 ch = rd_regb(port, S3C2410_URXH);
211
212 if (port->flags & UPF_CONS_FLOW) {
213 int txe = s3c24xx_serial_txempty_nofifo(port);
214
215 if (rx_enabled(port)) {
216 if (!txe) {
217 rx_enabled(port) = 0;
218 continue;
219 }
220 } else {
221 if (txe) {
222 ufcon |= S3C2410_UFCON_RESETRX;
223 wr_regl(port, S3C2410_UFCON, ufcon);
224 rx_enabled(port) = 1;
225 goto out;
226 }
227 continue;
228 }
229 }
230
231 /* insert the character into the buffer */
232
233 flag = TTY_NORMAL;
234 port->icount.rx++;
235
236 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
237 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
238 ch, uerstat);
239
240 /* check for break */
241 if (uerstat & S3C2410_UERSTAT_BREAK) {
242 dbg("break!\n");
243 port->icount.brk++;
244 if (uart_handle_break(port))
245 goto ignore_char;
246 }
247
248 if (uerstat & S3C2410_UERSTAT_FRAME)
249 port->icount.frame++;
250 if (uerstat & S3C2410_UERSTAT_OVERRUN)
251 port->icount.overrun++;
252
253 uerstat &= port->read_status_mask;
254
255 if (uerstat & S3C2410_UERSTAT_BREAK)
256 flag = TTY_BREAK;
257 else if (uerstat & S3C2410_UERSTAT_PARITY)
258 flag = TTY_PARITY;
259 else if (uerstat & (S3C2410_UERSTAT_FRAME |
260 S3C2410_UERSTAT_OVERRUN))
261 flag = TTY_FRAME;
262 }
263
264 if (uart_handle_sysrq_char(port, ch))
265 goto ignore_char;
266
267 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
268 ch, flag);
269
270 ignore_char:
271 continue;
272 }
273 tty_flip_buffer_push(tty);
274
275 out:
276 return IRQ_HANDLED;
277}
278
279static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
280{
281 struct s3c24xx_uart_port *ourport = id;
282 struct uart_port *port = &ourport->port;
283 struct circ_buf *xmit = &port->state->xmit;
284 int count = 256;
285
286 if (port->x_char) {
287 wr_regb(port, S3C2410_UTXH, port->x_char);
288 port->icount.tx++;
289 port->x_char = 0;
290 goto out;
291 }
292
293 /* if there isn't anything more to transmit, or the uart is now
294 * stopped, disable the uart and exit
295 */
296
297 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
298 s3c24xx_serial_stop_tx(port);
299 goto out;
300 }
301
302 /* try and drain the buffer... */
303
304 while (!uart_circ_empty(xmit) && count-- > 0) {
305 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
306 break;
307
308 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
309 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
310 port->icount.tx++;
311 }
312
313 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
314 uart_write_wakeup(port);
315
316 if (uart_circ_empty(xmit))
317 s3c24xx_serial_stop_tx(port);
318
319 out:
320 return IRQ_HANDLED;
321}
322
323static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
324{
325 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
326 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
327 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
328
329 if (ufcon & S3C2410_UFCON_FIFOMODE) {
330 if ((ufstat & info->tx_fifomask) != 0 ||
331 (ufstat & info->tx_fifofull))
332 return 0;
333
334 return 1;
335 }
336
337 return s3c24xx_serial_txempty_nofifo(port);
338}
339
340/* no modem control lines */
341static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
342{
343 unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
344
345 if (umstat & S3C2410_UMSTAT_CTS)
346 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
347 else
348 return TIOCM_CAR | TIOCM_DSR;
349}
350
351static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
352{
353 /* todo - possibly remove AFC and do manual CTS */
354}
355
356static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
357{
358 unsigned long flags;
359 unsigned int ucon;
360
361 spin_lock_irqsave(&port->lock, flags);
362
363 ucon = rd_regl(port, S3C2410_UCON);
364
365 if (break_state)
366 ucon |= S3C2410_UCON_SBREAK;
367 else
368 ucon &= ~S3C2410_UCON_SBREAK;
369
370 wr_regl(port, S3C2410_UCON, ucon);
371
372 spin_unlock_irqrestore(&port->lock, flags);
373}
374
375static void s3c24xx_serial_shutdown(struct uart_port *port)
376{
377 struct s3c24xx_uart_port *ourport = to_ourport(port);
378
379 if (ourport->tx_claimed) {
380 free_irq(ourport->tx_irq, ourport);
381 tx_enabled(port) = 0;
382 ourport->tx_claimed = 0;
383 }
384
385 if (ourport->rx_claimed) {
386 free_irq(ourport->rx_irq, ourport);
387 ourport->rx_claimed = 0;
388 rx_enabled(port) = 0;
389 }
390}
391
392
393static int s3c24xx_serial_startup(struct uart_port *port)
394{
395 struct s3c24xx_uart_port *ourport = to_ourport(port);
396 int ret;
397
398 dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
399 port->mapbase, port->membase);
400
401 rx_enabled(port) = 1;
402
403 ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
404 s3c24xx_serial_portname(port), ourport);
405
406 if (ret != 0) {
407 printk(KERN_ERR "cannot get irq %d\n", ourport->rx_irq);
408 return ret;
409 }
410
411 ourport->rx_claimed = 1;
412
413 dbg("requesting tx irq...\n");
414
415 tx_enabled(port) = 1;
416
417 ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
418 s3c24xx_serial_portname(port), ourport);
419
420 if (ret) {
421 printk(KERN_ERR "cannot get irq %d\n", ourport->tx_irq);
422 goto err;
423 }
424
425 ourport->tx_claimed = 1;
426
427 dbg("s3c24xx_serial_startup ok\n");
428
429 /* the port reset code should have done the correct
430 * register setup for the port controls */
431
432 return ret;
433
434 err:
435 s3c24xx_serial_shutdown(port);
436 return ret;
437}
438
439/* power power management control */
440
441static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
442 unsigned int old)
443{
444 struct s3c24xx_uart_port *ourport = to_ourport(port);
445
446 ourport->pm_level = level;
447
448 switch (level) {
449 case 3:
450 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
451 clk_disable(ourport->baudclk);
452
453 clk_disable(ourport->clk);
454 break;
455
456 case 0:
457 clk_enable(ourport->clk);
458
459 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
460 clk_enable(ourport->baudclk);
461
462 break;
463 default:
464 printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
465 }
466}
467
468/* baud rate calculation
469 *
470 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
471 * of different sources, including the peripheral clock ("pclk") and an
472 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
473 * with a programmable extra divisor.
474 *
475 * The following code goes through the clock sources, and calculates the
476 * baud clocks (and the resultant actual baud rates) and then tries to
477 * pick the closest one and select that.
478 *
479*/
480
481
482#define MAX_CLKS (8)
483
484static struct s3c24xx_uart_clksrc tmp_clksrc = {
485 .name = "pclk",
486 .min_baud = 0,
487 .max_baud = 0,
488 .divisor = 1,
489};
490
491static inline int
492s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
493{
494 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
495
496 return (info->get_clksrc)(port, c);
497}
498
499static inline int
500s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
501{
502 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
503
504 return (info->set_clksrc)(port, c);
505}
506
507struct baud_calc {
508 struct s3c24xx_uart_clksrc *clksrc;
509 unsigned int calc;
510 unsigned int divslot;
511 unsigned int quot;
512 struct clk *src;
513};
514
515static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
516 struct uart_port *port,
517 struct s3c24xx_uart_clksrc *clksrc,
518 unsigned int baud)
519{
520 struct s3c24xx_uart_port *ourport = to_ourport(port);
521 unsigned long rate;
522
523 calc->src = clk_get(port->dev, clksrc->name);
524 if (calc->src == NULL || IS_ERR(calc->src))
525 return 0;
526
527 rate = clk_get_rate(calc->src);
528 rate /= clksrc->divisor;
529
530 calc->clksrc = clksrc;
531
532 if (ourport->info->has_divslot) {
533 unsigned long div = rate / baud;
534
535 /* The UDIVSLOT register on the newer UARTs allows us to
536 * get a divisor adjustment of 1/16th on the baud clock.
537 *
538 * We don't keep the UDIVSLOT value (the 16ths we calculated
539 * by not multiplying the baud by 16) as it is easy enough
540 * to recalculate.
541 */
542
543 calc->quot = div / 16;
544 calc->calc = rate / div;
545 } else {
546 calc->quot = (rate + (8 * baud)) / (16 * baud);
547 calc->calc = (rate / (calc->quot * 16));
548 }
549
550 calc->quot--;
551 return 1;
552}
553
554static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
555 struct s3c24xx_uart_clksrc **clksrc,
556 struct clk **clk,
557 unsigned int baud)
558{
559 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
560 struct s3c24xx_uart_clksrc *clkp;
561 struct baud_calc res[MAX_CLKS];
562 struct baud_calc *resptr, *best, *sptr;
563 int i;
564
565 clkp = cfg->clocks;
566 best = NULL;
567
568 if (cfg->clocks_size < 2) {
569 if (cfg->clocks_size == 0)
570 clkp = &tmp_clksrc;
571
572 /* check to see if we're sourcing fclk, and if so we're
573 * going to have to update the clock source
574 */
575
576 if (strcmp(clkp->name, "fclk") == 0) {
577 struct s3c24xx_uart_clksrc src;
578
579 s3c24xx_serial_getsource(port, &src);
580
581 /* check that the port already using fclk, and if
582 * not, then re-select fclk
583 */
584
585 if (strcmp(src.name, clkp->name) == 0) {
586 s3c24xx_serial_setsource(port, clkp);
587 s3c24xx_serial_getsource(port, &src);
588 }
589
590 clkp->divisor = src.divisor;
591 }
592
593 s3c24xx_serial_calcbaud(res, port, clkp, baud);
594 best = res;
595 resptr = best + 1;
596 } else {
597 resptr = res;
598
599 for (i = 0; i < cfg->clocks_size; i++, clkp++) {
600 if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
601 resptr++;
602 }
603 }
604
605 /* ok, we now need to select the best clock we found */
606
607 if (!best) {
608 unsigned int deviation = (1<<30)|((1<<30)-1);
609 int calc_deviation;
610
611 for (sptr = res; sptr < resptr; sptr++) {
612 calc_deviation = baud - sptr->calc;
613 if (calc_deviation < 0)
614 calc_deviation = -calc_deviation;
615
616 if (calc_deviation < deviation) {
617 best = sptr;
618 deviation = calc_deviation;
619 }
620 }
621 }
622
623 /* store results to pass back */
624
625 *clksrc = best->clksrc;
626 *clk = best->src;
627
628 return best->quot;
629}
630
631/* udivslot_table[]
632 *
633 * This table takes the fractional value of the baud divisor and gives
634 * the recommended setting for the UDIVSLOT register.
635 */
636static u16 udivslot_table[16] = {
637 [0] = 0x0000,
638 [1] = 0x0080,
639 [2] = 0x0808,
640 [3] = 0x0888,
641 [4] = 0x2222,
642 [5] = 0x4924,
643 [6] = 0x4A52,
644 [7] = 0x54AA,
645 [8] = 0x5555,
646 [9] = 0xD555,
647 [10] = 0xD5D5,
648 [11] = 0xDDD5,
649 [12] = 0xDDDD,
650 [13] = 0xDFDD,
651 [14] = 0xDFDF,
652 [15] = 0xFFDF,
653};
654
655static void s3c24xx_serial_set_termios(struct uart_port *port,
656 struct ktermios *termios,
657 struct ktermios *old)
658{
659 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
660 struct s3c24xx_uart_port *ourport = to_ourport(port);
661 struct s3c24xx_uart_clksrc *clksrc = NULL;
662 struct clk *clk = NULL;
663 unsigned long flags;
664 unsigned int baud, quot;
665 unsigned int ulcon;
666 unsigned int umcon;
667 unsigned int udivslot = 0;
668
669 /*
670 * We don't support modem control lines.
671 */
672 termios->c_cflag &= ~(HUPCL | CMSPAR);
673 termios->c_cflag |= CLOCAL;
674
675 /*
676 * Ask the core to calculate the divisor for us.
677 */
678
679 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
680
681 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
682 quot = port->custom_divisor;
683 else
684 quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
685
686 /* check to see if we need to change clock source */
687
688 if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
689 dbg("selecting clock %p\n", clk);
690 s3c24xx_serial_setsource(port, clksrc);
691
692 if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
693 clk_disable(ourport->baudclk);
694 ourport->baudclk = NULL;
695 }
696
697 clk_enable(clk);
698
699 ourport->clksrc = clksrc;
700 ourport->baudclk = clk;
701 ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
702 }
703
704 if (ourport->info->has_divslot) {
705 unsigned int div = ourport->baudclk_rate / baud;
706
707 if (cfg->has_fracval) {
708 udivslot = (div & 15);
709 dbg("fracval = %04x\n", udivslot);
710 } else {
711 udivslot = udivslot_table[div & 15];
712 dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
713 }
714 }
715
716 switch (termios->c_cflag & CSIZE) {
717 case CS5:
718 dbg("config: 5bits/char\n");
719 ulcon = S3C2410_LCON_CS5;
720 break;
721 case CS6:
722 dbg("config: 6bits/char\n");
723 ulcon = S3C2410_LCON_CS6;
724 break;
725 case CS7:
726 dbg("config: 7bits/char\n");
727 ulcon = S3C2410_LCON_CS7;
728 break;
729 case CS8:
730 default:
731 dbg("config: 8bits/char\n");
732 ulcon = S3C2410_LCON_CS8;
733 break;
734 }
735
736 /* preserve original lcon IR settings */
737 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
738
739 if (termios->c_cflag & CSTOPB)
740 ulcon |= S3C2410_LCON_STOPB;
741
742 umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
743
744 if (termios->c_cflag & PARENB) {
745 if (termios->c_cflag & PARODD)
746 ulcon |= S3C2410_LCON_PODD;
747 else
748 ulcon |= S3C2410_LCON_PEVEN;
749 } else {
750 ulcon |= S3C2410_LCON_PNONE;
751 }
752
753 spin_lock_irqsave(&port->lock, flags);
754
755 dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
756 ulcon, quot, udivslot);
757
758 wr_regl(port, S3C2410_ULCON, ulcon);
759 wr_regl(port, S3C2410_UBRDIV, quot);
760 wr_regl(port, S3C2410_UMCON, umcon);
761
762 if (ourport->info->has_divslot)
763 wr_regl(port, S3C2443_DIVSLOT, udivslot);
764
765 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
766 rd_regl(port, S3C2410_ULCON),
767 rd_regl(port, S3C2410_UCON),
768 rd_regl(port, S3C2410_UFCON));
769
770 /*
771 * Update the per-port timeout.
772 */
773 uart_update_timeout(port, termios->c_cflag, baud);
774
775 /*
776 * Which character status flags are we interested in?
777 */
778 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
779 if (termios->c_iflag & INPCK)
780 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
781
782 /*
783 * Which character status flags should we ignore?
784 */
785 port->ignore_status_mask = 0;
786 if (termios->c_iflag & IGNPAR)
787 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
788 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
789 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
790
791 /*
792 * Ignore all characters if CREAD is not set.
793 */
794 if ((termios->c_cflag & CREAD) == 0)
795 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
796
797 spin_unlock_irqrestore(&port->lock, flags);
798}
799
800static const char *s3c24xx_serial_type(struct uart_port *port)
801{
802 switch (port->type) {
803 case PORT_S3C2410:
804 return "S3C2410";
805 case PORT_S3C2440:
806 return "S3C2440";
807 case PORT_S3C2412:
808 return "S3C2412";
809 case PORT_S3C6400:
810 return "S3C6400/10";
811 default:
812 return NULL;
813 }
814}
815
816#define MAP_SIZE (0x100)
817
818static void s3c24xx_serial_release_port(struct uart_port *port)
819{
820 release_mem_region(port->mapbase, MAP_SIZE);
821}
822
823static int s3c24xx_serial_request_port(struct uart_port *port)
824{
825 const char *name = s3c24xx_serial_portname(port);
826 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
827}
828
829static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
830{
831 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
832
833 if (flags & UART_CONFIG_TYPE &&
834 s3c24xx_serial_request_port(port) == 0)
835 port->type = info->type;
836}
837
838/*
839 * verify the new serial_struct (for TIOCSSERIAL).
840 */
841static int
842s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
843{
844 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
845
846 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
847 return -EINVAL;
848
849 return 0;
850}
851
852
853#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
854
855static struct console s3c24xx_serial_console;
856
857#define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
858#else
859#define S3C24XX_SERIAL_CONSOLE NULL
860#endif
861
862static struct uart_ops s3c24xx_serial_ops = {
863 .pm = s3c24xx_serial_pm,
864 .tx_empty = s3c24xx_serial_tx_empty,
865 .get_mctrl = s3c24xx_serial_get_mctrl,
866 .set_mctrl = s3c24xx_serial_set_mctrl,
867 .stop_tx = s3c24xx_serial_stop_tx,
868 .start_tx = s3c24xx_serial_start_tx,
869 .stop_rx = s3c24xx_serial_stop_rx,
870 .enable_ms = s3c24xx_serial_enable_ms,
871 .break_ctl = s3c24xx_serial_break_ctl,
872 .startup = s3c24xx_serial_startup,
873 .shutdown = s3c24xx_serial_shutdown,
874 .set_termios = s3c24xx_serial_set_termios,
875 .type = s3c24xx_serial_type,
876 .release_port = s3c24xx_serial_release_port,
877 .request_port = s3c24xx_serial_request_port,
878 .config_port = s3c24xx_serial_config_port,
879 .verify_port = s3c24xx_serial_verify_port,
880};
881
882
883static struct uart_driver s3c24xx_uart_drv = {
884 .owner = THIS_MODULE,
885 .driver_name = "s3c2410_serial",
886 .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
887 .cons = S3C24XX_SERIAL_CONSOLE,
888 .dev_name = S3C24XX_SERIAL_NAME,
889 .major = S3C24XX_SERIAL_MAJOR,
890 .minor = S3C24XX_SERIAL_MINOR,
891};
892
893static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
894 [0] = {
895 .port = {
896 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
897 .iotype = UPIO_MEM,
898 .irq = IRQ_S3CUART_RX0,
899 .uartclk = 0,
900 .fifosize = 16,
901 .ops = &s3c24xx_serial_ops,
902 .flags = UPF_BOOT_AUTOCONF,
903 .line = 0,
904 }
905 },
906 [1] = {
907 .port = {
908 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
909 .iotype = UPIO_MEM,
910 .irq = IRQ_S3CUART_RX1,
911 .uartclk = 0,
912 .fifosize = 16,
913 .ops = &s3c24xx_serial_ops,
914 .flags = UPF_BOOT_AUTOCONF,
915 .line = 1,
916 }
917 },
918#if CONFIG_SERIAL_SAMSUNG_UARTS > 2
919
920 [2] = {
921 .port = {
922 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
923 .iotype = UPIO_MEM,
924 .irq = IRQ_S3CUART_RX2,
925 .uartclk = 0,
926 .fifosize = 16,
927 .ops = &s3c24xx_serial_ops,
928 .flags = UPF_BOOT_AUTOCONF,
929 .line = 2,
930 }
931 },
932#endif
933#if CONFIG_SERIAL_SAMSUNG_UARTS > 3
934 [3] = {
935 .port = {
936 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
937 .iotype = UPIO_MEM,
938 .irq = IRQ_S3CUART_RX3,
939 .uartclk = 0,
940 .fifosize = 16,
941 .ops = &s3c24xx_serial_ops,
942 .flags = UPF_BOOT_AUTOCONF,
943 .line = 3,
944 }
945 }
946#endif
947};
948
949/* s3c24xx_serial_resetport
950 *
951 * wrapper to call the specific reset for this port (reset the fifos
952 * and the settings)
953*/
954
955static inline int s3c24xx_serial_resetport(struct uart_port *port,
956 struct s3c2410_uartcfg *cfg)
957{
958 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
959
960 return (info->reset_port)(port, cfg);
961}
962
963
964#ifdef CONFIG_CPU_FREQ
965
966static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
967 unsigned long val, void *data)
968{
969 struct s3c24xx_uart_port *port;
970 struct uart_port *uport;
971
972 port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
973 uport = &port->port;
974
975 /* check to see if port is enabled */
976
977 if (port->pm_level != 0)
978 return 0;
979
980 /* try and work out if the baudrate is changing, we can detect
981 * a change in rate, but we do not have support for detecting
982 * a disturbance in the clock-rate over the change.
983 */
984
985 if (IS_ERR(port->clk))
986 goto exit;
987
988 if (port->baudclk_rate == clk_get_rate(port->clk))
989 goto exit;
990
991 if (val == CPUFREQ_PRECHANGE) {
992 /* we should really shut the port down whilst the
993 * frequency change is in progress. */
994
995 } else if (val == CPUFREQ_POSTCHANGE) {
996 struct ktermios *termios;
997 struct tty_struct *tty;
998
999 if (uport->state == NULL)
1000 goto exit;
1001
1002 tty = uport->state->port.tty;
1003
1004 if (tty == NULL)
1005 goto exit;
1006
1007 termios = tty->termios;
1008
1009 if (termios == NULL) {
1010 printk(KERN_WARNING "%s: no termios?\n", __func__);
1011 goto exit;
1012 }
1013
1014 s3c24xx_serial_set_termios(uport, termios, NULL);
1015 }
1016
1017 exit:
1018 return 0;
1019}
1020
1021static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1022{
1023 port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
1024
1025 return cpufreq_register_notifier(&port->freq_transition,
1026 CPUFREQ_TRANSITION_NOTIFIER);
1027}
1028
1029static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1030{
1031 cpufreq_unregister_notifier(&port->freq_transition,
1032 CPUFREQ_TRANSITION_NOTIFIER);
1033}
1034
1035#else
1036static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1037{
1038 return 0;
1039}
1040
1041static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1042{
1043}
1044#endif
1045
1046/* s3c24xx_serial_init_port
1047 *
1048 * initialise a single serial port from the platform device given
1049 */
1050
1051static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
1052 struct s3c24xx_uart_info *info,
1053 struct platform_device *platdev)
1054{
1055 struct uart_port *port = &ourport->port;
1056 struct s3c2410_uartcfg *cfg;
1057 struct resource *res;
1058 int ret;
1059
1060 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1061
1062 if (platdev == NULL)
1063 return -ENODEV;
1064
1065 cfg = s3c24xx_dev_to_cfg(&platdev->dev);
1066
1067 if (port->mapbase != 0)
1068 return 0;
1069
1070 if (cfg->hwport > CONFIG_SERIAL_SAMSUNG_UARTS) {
1071 printk(KERN_ERR "%s: port %d bigger than %d\n", __func__,
1072 cfg->hwport, CONFIG_SERIAL_SAMSUNG_UARTS);
1073 return -ERANGE;
1074 }
1075
1076 /* setup info for port */
1077 port->dev = &platdev->dev;
1078 ourport->info = info;
1079
1080 /* copy the info in from provided structure */
1081 ourport->port.fifosize = info->fifosize;
1082
1083 dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
1084
1085 port->uartclk = 1;
1086
1087 if (cfg->uart_flags & UPF_CONS_FLOW) {
1088 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1089 port->flags |= UPF_CONS_FLOW;
1090 }
1091
1092 /* sort our the physical and virtual addresses for each UART */
1093
1094 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1095 if (res == NULL) {
1096 printk(KERN_ERR "failed to find memory resource for uart\n");
1097 return -EINVAL;
1098 }
1099
1100 dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
1101
1102 port->mapbase = res->start;
1103 port->membase = S3C_VA_UART + (res->start & 0xfffff);
1104 ret = platform_get_irq(platdev, 0);
1105 if (ret < 0)
1106 port->irq = 0;
1107 else {
1108 port->irq = ret;
1109 ourport->rx_irq = ret;
1110 ourport->tx_irq = ret + 1;
1111 }
1112
1113 ret = platform_get_irq(platdev, 1);
1114 if (ret > 0)
1115 ourport->tx_irq = ret;
1116
1117 ourport->clk = clk_get(&platdev->dev, "uart");
1118
1119 dbg("port: map=%08x, mem=%08x, irq=%d (%d,%d), clock=%ld\n",
1120 port->mapbase, port->membase, port->irq,
1121 ourport->rx_irq, ourport->tx_irq, port->uartclk);
1122
1123 /* reset the fifos (and setup the uart) */
1124 s3c24xx_serial_resetport(port, cfg);
1125 return 0;
1126}
1127
1128static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
1129 struct device_attribute *attr,
1130 char *buf)
1131{
1132 struct uart_port *port = s3c24xx_dev_to_port(dev);
1133 struct s3c24xx_uart_port *ourport = to_ourport(port);
1134
1135 return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->clksrc->name);
1136}
1137
1138static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
1139
1140/* Device driver serial port probe */
1141
1142static int probe_index;
1143
1144int s3c24xx_serial_probe(struct platform_device *dev,
1145 struct s3c24xx_uart_info *info)
1146{
1147 struct s3c24xx_uart_port *ourport;
1148 int ret;
1149
1150 dbg("s3c24xx_serial_probe(%p, %p) %d\n", dev, info, probe_index);
1151
1152 ourport = &s3c24xx_serial_ports[probe_index];
1153 probe_index++;
1154
1155 dbg("%s: initialising port %p...\n", __func__, ourport);
1156
1157 ret = s3c24xx_serial_init_port(ourport, info, dev);
1158 if (ret < 0)
1159 goto probe_err;
1160
1161 dbg("%s: adding port\n", __func__);
1162 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
1163 platform_set_drvdata(dev, &ourport->port);
1164
1165 ret = device_create_file(&dev->dev, &dev_attr_clock_source);
1166 if (ret < 0)
1167 printk(KERN_ERR "%s: failed to add clksrc attr.\n", __func__);
1168
1169 ret = s3c24xx_serial_cpufreq_register(ourport);
1170 if (ret < 0)
1171 dev_err(&dev->dev, "failed to add cpufreq notifier\n");
1172
1173 return 0;
1174
1175 probe_err:
1176 return ret;
1177}
1178
1179EXPORT_SYMBOL_GPL(s3c24xx_serial_probe);
1180
1181int __devexit s3c24xx_serial_remove(struct platform_device *dev)
1182{
1183 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1184
1185 if (port) {
1186 s3c24xx_serial_cpufreq_deregister(to_ourport(port));
1187 device_remove_file(&dev->dev, &dev_attr_clock_source);
1188 uart_remove_one_port(&s3c24xx_uart_drv, port);
1189 }
1190
1191 return 0;
1192}
1193
1194EXPORT_SYMBOL_GPL(s3c24xx_serial_remove);
1195
1196/* UART power management code */
1197
1198#ifdef CONFIG_PM
1199
1200static int s3c24xx_serial_suspend(struct platform_device *dev, pm_message_t state)
1201{
1202 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1203
1204 if (port)
1205 uart_suspend_port(&s3c24xx_uart_drv, port);
1206
1207 return 0;
1208}
1209
1210static int s3c24xx_serial_resume(struct platform_device *dev)
1211{
1212 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1213 struct s3c24xx_uart_port *ourport = to_ourport(port);
1214
1215 if (port) {
1216 clk_enable(ourport->clk);
1217 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
1218 clk_disable(ourport->clk);
1219
1220 uart_resume_port(&s3c24xx_uart_drv, port);
1221 }
1222
1223 return 0;
1224}
1225#endif
1226
1227int s3c24xx_serial_init(struct platform_driver *drv,
1228 struct s3c24xx_uart_info *info)
1229{
1230 dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
1231
1232#ifdef CONFIG_PM
1233 drv->suspend = s3c24xx_serial_suspend;
1234 drv->resume = s3c24xx_serial_resume;
1235#endif
1236
1237 return platform_driver_register(drv);
1238}
1239
1240EXPORT_SYMBOL_GPL(s3c24xx_serial_init);
1241
1242/* module initialisation code */
1243
1244static int __init s3c24xx_serial_modinit(void)
1245{
1246 int ret;
1247
1248 ret = uart_register_driver(&s3c24xx_uart_drv);
1249 if (ret < 0) {
1250 printk(KERN_ERR "failed to register UART driver\n");
1251 return -1;
1252 }
1253
1254 return 0;
1255}
1256
1257static void __exit s3c24xx_serial_modexit(void)
1258{
1259 uart_unregister_driver(&s3c24xx_uart_drv);
1260}
1261
1262module_init(s3c24xx_serial_modinit);
1263module_exit(s3c24xx_serial_modexit);
1264
1265/* Console code */
1266
1267#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1268
1269static struct uart_port *cons_uart;
1270
1271static int
1272s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1273{
1274 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1275 unsigned long ufstat, utrstat;
1276
1277 if (ufcon & S3C2410_UFCON_FIFOMODE) {
1278 /* fifo mode - check amount of data in fifo registers... */
1279
1280 ufstat = rd_regl(port, S3C2410_UFSTAT);
1281 return (ufstat & info->tx_fifofull) ? 0 : 1;
1282 }
1283
1284 /* in non-fifo mode, we go and use the tx buffer empty */
1285
1286 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1287 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1288}
1289
1290static void
1291s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
1292{
1293 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
1294 while (!s3c24xx_serial_console_txrdy(port, ufcon))
1295 barrier();
1296 wr_regb(cons_uart, S3C2410_UTXH, ch);
1297}
1298
1299static void
1300s3c24xx_serial_console_write(struct console *co, const char *s,
1301 unsigned int count)
1302{
1303 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
1304}
1305
1306static void __init
1307s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1308 int *parity, int *bits)
1309{
1310 struct s3c24xx_uart_clksrc clksrc;
1311 struct clk *clk;
1312 unsigned int ulcon;
1313 unsigned int ucon;
1314 unsigned int ubrdiv;
1315 unsigned long rate;
1316
1317 ulcon = rd_regl(port, S3C2410_ULCON);
1318 ucon = rd_regl(port, S3C2410_UCON);
1319 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1320
1321 dbg("s3c24xx_serial_get_options: port=%p\n"
1322 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1323 port, ulcon, ucon, ubrdiv);
1324
1325 if ((ucon & 0xf) != 0) {
1326 /* consider the serial port configured if the tx/rx mode set */
1327
1328 switch (ulcon & S3C2410_LCON_CSMASK) {
1329 case S3C2410_LCON_CS5:
1330 *bits = 5;
1331 break;
1332 case S3C2410_LCON_CS6:
1333 *bits = 6;
1334 break;
1335 case S3C2410_LCON_CS7:
1336 *bits = 7;
1337 break;
1338 default:
1339 case S3C2410_LCON_CS8:
1340 *bits = 8;
1341 break;
1342 }
1343
1344 switch (ulcon & S3C2410_LCON_PMASK) {
1345 case S3C2410_LCON_PEVEN:
1346 *parity = 'e';
1347 break;
1348
1349 case S3C2410_LCON_PODD:
1350 *parity = 'o';
1351 break;
1352
1353 case S3C2410_LCON_PNONE:
1354 default:
1355 *parity = 'n';
1356 }
1357
1358 /* now calculate the baud rate */
1359
1360 s3c24xx_serial_getsource(port, &clksrc);
1361
1362 clk = clk_get(port->dev, clksrc.name);
1363 if (!IS_ERR(clk) && clk != NULL)
1364 rate = clk_get_rate(clk) / clksrc.divisor;
1365 else
1366 rate = 1;
1367
1368
1369 *baud = rate / (16 * (ubrdiv + 1));
1370 dbg("calculated baud %d\n", *baud);
1371 }
1372
1373}
1374
1375/* s3c24xx_serial_init_ports
1376 *
1377 * initialise the serial ports from the machine provided initialisation
1378 * data.
1379*/
1380
1381static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info **info)
1382{
1383 struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
1384 struct platform_device **platdev_ptr;
1385 int i;
1386
1387 dbg("s3c24xx_serial_init_ports: initialising ports...\n");
1388
1389 platdev_ptr = s3c24xx_uart_devs;
1390
1391 for (i = 0; i < CONFIG_SERIAL_SAMSUNG_UARTS; i++, ptr++, platdev_ptr++) {
1392 s3c24xx_serial_init_port(ptr, info[i], *platdev_ptr);
1393 }
1394
1395 return 0;
1396}
1397
1398static int __init
1399s3c24xx_serial_console_setup(struct console *co, char *options)
1400{
1401 struct uart_port *port;
1402 int baud = 9600;
1403 int bits = 8;
1404 int parity = 'n';
1405 int flow = 'n';
1406
1407 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1408 co, co->index, options);
1409
1410 /* is this a valid port */
1411
1412 if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
1413 co->index = 0;
1414
1415 port = &s3c24xx_serial_ports[co->index].port;
1416
1417 /* is the port configured? */
1418
1419 if (port->mapbase == 0x0) {
1420 co->index = 0;
1421 port = &s3c24xx_serial_ports[co->index].port;
1422 }
1423
1424 cons_uart = port;
1425
1426 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1427
1428 /*
1429 * Check whether an invalid uart number has been specified, and
1430 * if so, search for the first available port that does have
1431 * console support.
1432 */
1433 if (options)
1434 uart_parse_options(options, &baud, &parity, &bits, &flow);
1435 else
1436 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1437
1438 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1439
1440 return uart_set_options(port, co, baud, parity, bits, flow);
1441}
1442
1443/* s3c24xx_serial_initconsole
1444 *
1445 * initialise the console from one of the uart drivers
1446*/
1447
1448static struct console s3c24xx_serial_console = {
1449 .name = S3C24XX_SERIAL_NAME,
1450 .device = uart_console_device,
1451 .flags = CON_PRINTBUFFER,
1452 .index = -1,
1453 .write = s3c24xx_serial_console_write,
1454 .setup = s3c24xx_serial_console_setup
1455};
1456
1457int s3c24xx_serial_initconsole(struct platform_driver *drv,
1458 struct s3c24xx_uart_info **info)
1459
1460{
1461 struct platform_device *dev = s3c24xx_uart_devs[0];
1462
1463 dbg("s3c24xx_serial_initconsole\n");
1464
1465 /* select driver based on the cpu */
1466
1467 if (dev == NULL) {
1468 printk(KERN_ERR "s3c24xx: no devices for console init\n");
1469 return 0;
1470 }
1471
1472 if (strcmp(dev->name, drv->driver.name) != 0)
1473 return 0;
1474
1475 s3c24xx_serial_console.data = &s3c24xx_uart_drv;
1476 s3c24xx_serial_init_ports(info);
1477
1478 register_console(&s3c24xx_serial_console);
1479 return 0;
1480}
1481
1482#endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
1483
1484MODULE_DESCRIPTION("Samsung SoC Serial port driver");
1485MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1486MODULE_LICENSE("GPL v2");
diff --git a/drivers/tty/serial/samsung.h b/drivers/tty/serial/samsung.h
new file mode 100644
index 000000000000..5b098cd76040
--- /dev/null
+++ b/drivers/tty/serial/samsung.h
@@ -0,0 +1,119 @@
1/*
2 * Driver for Samsung SoC onboard UARTs.
3 *
4 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
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
12struct s3c24xx_uart_info {
13 char *name;
14 unsigned int type;
15 unsigned int fifosize;
16 unsigned long rx_fifomask;
17 unsigned long rx_fifoshift;
18 unsigned long rx_fifofull;
19 unsigned long tx_fifomask;
20 unsigned long tx_fifoshift;
21 unsigned long tx_fifofull;
22
23 /* uart port features */
24
25 unsigned int has_divslot:1;
26
27 /* clock source control */
28
29 int (*get_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
30 int (*set_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
31
32 /* uart controls */
33 int (*reset_port)(struct uart_port *, struct s3c2410_uartcfg *);
34};
35
36struct s3c24xx_uart_port {
37 unsigned char rx_claimed;
38 unsigned char tx_claimed;
39 unsigned int pm_level;
40 unsigned long baudclk_rate;
41
42 unsigned int rx_irq;
43 unsigned int tx_irq;
44
45 struct s3c24xx_uart_info *info;
46 struct s3c24xx_uart_clksrc *clksrc;
47 struct clk *clk;
48 struct clk *baudclk;
49 struct uart_port port;
50
51#ifdef CONFIG_CPU_FREQ
52 struct notifier_block freq_transition;
53#endif
54};
55
56/* conversion functions */
57
58#define s3c24xx_dev_to_port(__dev) (struct uart_port *)dev_get_drvdata(__dev)
59#define s3c24xx_dev_to_cfg(__dev) (struct s3c2410_uartcfg *)((__dev)->platform_data)
60
61/* register access controls */
62
63#define portaddr(port, reg) ((port)->membase + (reg))
64
65#define rd_regb(port, reg) (__raw_readb(portaddr(port, reg)))
66#define rd_regl(port, reg) (__raw_readl(portaddr(port, reg)))
67
68#define wr_regb(port, reg, val) __raw_writeb(val, portaddr(port, reg))
69#define wr_regl(port, reg, val) __raw_writel(val, portaddr(port, reg))
70
71extern int s3c24xx_serial_probe(struct platform_device *dev,
72 struct s3c24xx_uart_info *uart);
73
74extern int __devexit s3c24xx_serial_remove(struct platform_device *dev);
75
76extern int s3c24xx_serial_initconsole(struct platform_driver *drv,
77 struct s3c24xx_uart_info **uart);
78
79extern int s3c24xx_serial_init(struct platform_driver *drv,
80 struct s3c24xx_uart_info *info);
81
82#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
83
84#define s3c24xx_console_init(__drv, __inf) \
85static int __init s3c_serial_console_init(void) \
86{ \
87 struct s3c24xx_uart_info *uinfo[CONFIG_SERIAL_SAMSUNG_UARTS]; \
88 int i; \
89 \
90 for (i = 0; i < CONFIG_SERIAL_SAMSUNG_UARTS; i++) \
91 uinfo[i] = __inf; \
92 return s3c24xx_serial_initconsole(__drv, uinfo); \
93} \
94 \
95console_initcall(s3c_serial_console_init)
96
97#else
98#define s3c24xx_console_init(drv, inf) extern void no_console(void)
99#endif
100
101#ifdef CONFIG_SERIAL_SAMSUNG_DEBUG
102
103extern void printascii(const char *);
104
105static void dbg(const char *fmt, ...)
106{
107 va_list va;
108 char buff[256];
109
110 va_start(va, fmt);
111 vsprintf(buff, fmt, va);
112 va_end(va);
113
114 printascii(buff);
115}
116
117#else
118#define dbg(x...) do { } while (0)
119#endif
diff --git a/drivers/tty/serial/sb1250-duart.c b/drivers/tty/serial/sb1250-duart.c
new file mode 100644
index 000000000000..ea2340b814e9
--- /dev/null
+++ b/drivers/tty/serial/sb1250-duart.c
@@ -0,0 +1,974 @@
1/*
2 * Support for the asynchronous serial interface (DUART) included
3 * in the BCM1250 and derived System-On-a-Chip (SOC) devices.
4 *
5 * Copyright (c) 2007 Maciej W. Rozycki
6 *
7 * Derived from drivers/char/sb1250_duart.c for which the following
8 * copyright applies:
9 *
10 * Copyright (c) 2000, 2001, 2002, 2003, 2004 Broadcom Corporation
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 * References:
18 *
19 * "BCM1250/BCM1125/BCM1125H User Manual", Broadcom Corporation
20 */
21
22#if defined(CONFIG_SERIAL_SB1250_DUART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
23#define SUPPORT_SYSRQ
24#endif
25
26#include <linux/compiler.h>
27#include <linux/console.h>
28#include <linux/delay.h>
29#include <linux/errno.h>
30#include <linux/init.h>
31#include <linux/interrupt.h>
32#include <linux/ioport.h>
33#include <linux/kernel.h>
34#include <linux/major.h>
35#include <linux/serial.h>
36#include <linux/serial_core.h>
37#include <linux/spinlock.h>
38#include <linux/sysrq.h>
39#include <linux/tty.h>
40#include <linux/types.h>
41
42#include <asm/atomic.h>
43#include <asm/io.h>
44#include <asm/war.h>
45
46#include <asm/sibyte/sb1250.h>
47#include <asm/sibyte/sb1250_uart.h>
48#include <asm/sibyte/swarm.h>
49
50
51#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
52#include <asm/sibyte/bcm1480_regs.h>
53#include <asm/sibyte/bcm1480_int.h>
54
55#define SBD_CHANREGS(line) A_BCM1480_DUART_CHANREG((line), 0)
56#define SBD_CTRLREGS(line) A_BCM1480_DUART_CTRLREG((line), 0)
57#define SBD_INT(line) (K_BCM1480_INT_UART_0 + (line))
58
59#define DUART_CHANREG_SPACING BCM1480_DUART_CHANREG_SPACING
60
61#define R_DUART_IMRREG(line) R_BCM1480_DUART_IMRREG(line)
62#define R_DUART_INCHREG(line) R_BCM1480_DUART_INCHREG(line)
63#define R_DUART_ISRREG(line) R_BCM1480_DUART_ISRREG(line)
64
65#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
66#include <asm/sibyte/sb1250_regs.h>
67#include <asm/sibyte/sb1250_int.h>
68
69#define SBD_CHANREGS(line) A_DUART_CHANREG((line), 0)
70#define SBD_CTRLREGS(line) A_DUART_CTRLREG(0)
71#define SBD_INT(line) (K_INT_UART_0 + (line))
72
73#else
74#error invalid SB1250 UART configuration
75
76#endif
77
78
79MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
80MODULE_DESCRIPTION("BCM1xxx on-chip DUART serial driver");
81MODULE_LICENSE("GPL");
82
83
84#define DUART_MAX_CHIP 2
85#define DUART_MAX_SIDE 2
86
87/*
88 * Per-port state.
89 */
90struct sbd_port {
91 struct sbd_duart *duart;
92 struct uart_port port;
93 unsigned char __iomem *memctrl;
94 int tx_stopped;
95 int initialised;
96};
97
98/*
99 * Per-DUART state for the shared register space.
100 */
101struct sbd_duart {
102 struct sbd_port sport[2];
103 unsigned long mapctrl;
104 atomic_t map_guard;
105};
106
107#define to_sport(uport) container_of(uport, struct sbd_port, port)
108
109static struct sbd_duart sbd_duarts[DUART_MAX_CHIP];
110
111
112/*
113 * Reading and writing SB1250 DUART registers.
114 *
115 * There are three register spaces: two per-channel ones and
116 * a shared one. We have to define accessors appropriately.
117 * All registers are 64-bit and all but the Baud Rate Clock
118 * registers only define 8 least significant bits. There is
119 * also a workaround to take into account. Raw accessors use
120 * the full register width, but cooked ones truncate it
121 * intentionally so that the rest of the driver does not care.
122 */
123static u64 __read_sbdchn(struct sbd_port *sport, int reg)
124{
125 void __iomem *csr = sport->port.membase + reg;
126
127 return __raw_readq(csr);
128}
129
130static u64 __read_sbdshr(struct sbd_port *sport, int reg)
131{
132 void __iomem *csr = sport->memctrl + reg;
133
134 return __raw_readq(csr);
135}
136
137static void __write_sbdchn(struct sbd_port *sport, int reg, u64 value)
138{
139 void __iomem *csr = sport->port.membase + reg;
140
141 __raw_writeq(value, csr);
142}
143
144static void __write_sbdshr(struct sbd_port *sport, int reg, u64 value)
145{
146 void __iomem *csr = sport->memctrl + reg;
147
148 __raw_writeq(value, csr);
149}
150
151/*
152 * In bug 1956, we get glitches that can mess up uart registers. This
153 * "read-mode-reg after any register access" is an accepted workaround.
154 */
155static void __war_sbd1956(struct sbd_port *sport)
156{
157 __read_sbdchn(sport, R_DUART_MODE_REG_1);
158 __read_sbdchn(sport, R_DUART_MODE_REG_2);
159}
160
161static unsigned char read_sbdchn(struct sbd_port *sport, int reg)
162{
163 unsigned char retval;
164
165 retval = __read_sbdchn(sport, reg);
166 if (SIBYTE_1956_WAR)
167 __war_sbd1956(sport);
168 return retval;
169}
170
171static unsigned char read_sbdshr(struct sbd_port *sport, int reg)
172{
173 unsigned char retval;
174
175 retval = __read_sbdshr(sport, reg);
176 if (SIBYTE_1956_WAR)
177 __war_sbd1956(sport);
178 return retval;
179}
180
181static void write_sbdchn(struct sbd_port *sport, int reg, unsigned int value)
182{
183 __write_sbdchn(sport, reg, value);
184 if (SIBYTE_1956_WAR)
185 __war_sbd1956(sport);
186}
187
188static void write_sbdshr(struct sbd_port *sport, int reg, unsigned int value)
189{
190 __write_sbdshr(sport, reg, value);
191 if (SIBYTE_1956_WAR)
192 __war_sbd1956(sport);
193}
194
195
196static int sbd_receive_ready(struct sbd_port *sport)
197{
198 return read_sbdchn(sport, R_DUART_STATUS) & M_DUART_RX_RDY;
199}
200
201static int sbd_receive_drain(struct sbd_port *sport)
202{
203 int loops = 10000;
204
205 while (sbd_receive_ready(sport) && --loops)
206 read_sbdchn(sport, R_DUART_RX_HOLD);
207 return loops;
208}
209
210static int __maybe_unused sbd_transmit_ready(struct sbd_port *sport)
211{
212 return read_sbdchn(sport, R_DUART_STATUS) & M_DUART_TX_RDY;
213}
214
215static int __maybe_unused sbd_transmit_drain(struct sbd_port *sport)
216{
217 int loops = 10000;
218
219 while (!sbd_transmit_ready(sport) && --loops)
220 udelay(2);
221 return loops;
222}
223
224static int sbd_transmit_empty(struct sbd_port *sport)
225{
226 return read_sbdchn(sport, R_DUART_STATUS) & M_DUART_TX_EMT;
227}
228
229static int sbd_line_drain(struct sbd_port *sport)
230{
231 int loops = 10000;
232
233 while (!sbd_transmit_empty(sport) && --loops)
234 udelay(2);
235 return loops;
236}
237
238
239static unsigned int sbd_tx_empty(struct uart_port *uport)
240{
241 struct sbd_port *sport = to_sport(uport);
242
243 return sbd_transmit_empty(sport) ? TIOCSER_TEMT : 0;
244}
245
246static unsigned int sbd_get_mctrl(struct uart_port *uport)
247{
248 struct sbd_port *sport = to_sport(uport);
249 unsigned int mctrl, status;
250
251 status = read_sbdshr(sport, R_DUART_IN_PORT);
252 status >>= (uport->line) % 2;
253 mctrl = (!(status & M_DUART_IN_PIN0_VAL) ? TIOCM_CTS : 0) |
254 (!(status & M_DUART_IN_PIN4_VAL) ? TIOCM_CAR : 0) |
255 (!(status & M_DUART_RIN0_PIN) ? TIOCM_RNG : 0) |
256 (!(status & M_DUART_IN_PIN2_VAL) ? TIOCM_DSR : 0);
257 return mctrl;
258}
259
260static void sbd_set_mctrl(struct uart_port *uport, unsigned int mctrl)
261{
262 struct sbd_port *sport = to_sport(uport);
263 unsigned int clr = 0, set = 0, mode2;
264
265 if (mctrl & TIOCM_DTR)
266 set |= M_DUART_SET_OPR2;
267 else
268 clr |= M_DUART_CLR_OPR2;
269 if (mctrl & TIOCM_RTS)
270 set |= M_DUART_SET_OPR0;
271 else
272 clr |= M_DUART_CLR_OPR0;
273 clr <<= (uport->line) % 2;
274 set <<= (uport->line) % 2;
275
276 mode2 = read_sbdchn(sport, R_DUART_MODE_REG_2);
277 mode2 &= ~M_DUART_CHAN_MODE;
278 if (mctrl & TIOCM_LOOP)
279 mode2 |= V_DUART_CHAN_MODE_LCL_LOOP;
280 else
281 mode2 |= V_DUART_CHAN_MODE_NORMAL;
282
283 write_sbdshr(sport, R_DUART_CLEAR_OPR, clr);
284 write_sbdshr(sport, R_DUART_SET_OPR, set);
285 write_sbdchn(sport, R_DUART_MODE_REG_2, mode2);
286}
287
288static void sbd_stop_tx(struct uart_port *uport)
289{
290 struct sbd_port *sport = to_sport(uport);
291
292 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS);
293 sport->tx_stopped = 1;
294};
295
296static void sbd_start_tx(struct uart_port *uport)
297{
298 struct sbd_port *sport = to_sport(uport);
299 unsigned int mask;
300
301 /* Enable tx interrupts. */
302 mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
303 mask |= M_DUART_IMR_TX;
304 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
305
306 /* Go!, go!, go!... */
307 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_EN);
308 sport->tx_stopped = 0;
309};
310
311static void sbd_stop_rx(struct uart_port *uport)
312{
313 struct sbd_port *sport = to_sport(uport);
314
315 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), 0);
316};
317
318static void sbd_enable_ms(struct uart_port *uport)
319{
320 struct sbd_port *sport = to_sport(uport);
321
322 write_sbdchn(sport, R_DUART_AUXCTL_X,
323 M_DUART_CIN_CHNG_ENA | M_DUART_CTS_CHNG_ENA);
324}
325
326static void sbd_break_ctl(struct uart_port *uport, int break_state)
327{
328 struct sbd_port *sport = to_sport(uport);
329
330 if (break_state == -1)
331 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_START_BREAK);
332 else
333 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_STOP_BREAK);
334}
335
336
337static void sbd_receive_chars(struct sbd_port *sport)
338{
339 struct uart_port *uport = &sport->port;
340 struct uart_icount *icount;
341 unsigned int status, ch, flag;
342 int count;
343
344 for (count = 16; count; count--) {
345 status = read_sbdchn(sport, R_DUART_STATUS);
346 if (!(status & M_DUART_RX_RDY))
347 break;
348
349 ch = read_sbdchn(sport, R_DUART_RX_HOLD);
350
351 flag = TTY_NORMAL;
352
353 icount = &uport->icount;
354 icount->rx++;
355
356 if (unlikely(status &
357 (M_DUART_RCVD_BRK | M_DUART_FRM_ERR |
358 M_DUART_PARITY_ERR | M_DUART_OVRUN_ERR))) {
359 if (status & M_DUART_RCVD_BRK) {
360 icount->brk++;
361 if (uart_handle_break(uport))
362 continue;
363 } else if (status & M_DUART_FRM_ERR)
364 icount->frame++;
365 else if (status & M_DUART_PARITY_ERR)
366 icount->parity++;
367 if (status & M_DUART_OVRUN_ERR)
368 icount->overrun++;
369
370 status &= uport->read_status_mask;
371 if (status & M_DUART_RCVD_BRK)
372 flag = TTY_BREAK;
373 else if (status & M_DUART_FRM_ERR)
374 flag = TTY_FRAME;
375 else if (status & M_DUART_PARITY_ERR)
376 flag = TTY_PARITY;
377 }
378
379 if (uart_handle_sysrq_char(uport, ch))
380 continue;
381
382 uart_insert_char(uport, status, M_DUART_OVRUN_ERR, ch, flag);
383 }
384
385 tty_flip_buffer_push(uport->state->port.tty);
386}
387
388static void sbd_transmit_chars(struct sbd_port *sport)
389{
390 struct uart_port *uport = &sport->port;
391 struct circ_buf *xmit = &sport->port.state->xmit;
392 unsigned int mask;
393 int stop_tx;
394
395 /* XON/XOFF chars. */
396 if (sport->port.x_char) {
397 write_sbdchn(sport, R_DUART_TX_HOLD, sport->port.x_char);
398 sport->port.icount.tx++;
399 sport->port.x_char = 0;
400 return;
401 }
402
403 /* If nothing to do or stopped or hardware stopped. */
404 stop_tx = (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port));
405
406 /* Send char. */
407 if (!stop_tx) {
408 write_sbdchn(sport, R_DUART_TX_HOLD, xmit->buf[xmit->tail]);
409 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
410 sport->port.icount.tx++;
411
412 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
413 uart_write_wakeup(&sport->port);
414 }
415
416 /* Are we are done? */
417 if (stop_tx || uart_circ_empty(xmit)) {
418 /* Disable tx interrupts. */
419 mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
420 mask &= ~M_DUART_IMR_TX;
421 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
422 }
423}
424
425static void sbd_status_handle(struct sbd_port *sport)
426{
427 struct uart_port *uport = &sport->port;
428 unsigned int delta;
429
430 delta = read_sbdshr(sport, R_DUART_INCHREG((uport->line) % 2));
431 delta >>= (uport->line) % 2;
432
433 if (delta & (M_DUART_IN_PIN0_VAL << S_DUART_IN_PIN_CHNG))
434 uart_handle_cts_change(uport, !(delta & M_DUART_IN_PIN0_VAL));
435
436 if (delta & (M_DUART_IN_PIN2_VAL << S_DUART_IN_PIN_CHNG))
437 uport->icount.dsr++;
438
439 if (delta & ((M_DUART_IN_PIN2_VAL | M_DUART_IN_PIN0_VAL) <<
440 S_DUART_IN_PIN_CHNG))
441 wake_up_interruptible(&uport->state->port.delta_msr_wait);
442}
443
444static irqreturn_t sbd_interrupt(int irq, void *dev_id)
445{
446 struct sbd_port *sport = dev_id;
447 struct uart_port *uport = &sport->port;
448 irqreturn_t status = IRQ_NONE;
449 unsigned int intstat;
450 int count;
451
452 for (count = 16; count; count--) {
453 intstat = read_sbdshr(sport,
454 R_DUART_ISRREG((uport->line) % 2));
455 intstat &= read_sbdshr(sport,
456 R_DUART_IMRREG((uport->line) % 2));
457 intstat &= M_DUART_ISR_ALL;
458 if (!intstat)
459 break;
460
461 if (intstat & M_DUART_ISR_RX)
462 sbd_receive_chars(sport);
463 if (intstat & M_DUART_ISR_IN)
464 sbd_status_handle(sport);
465 if (intstat & M_DUART_ISR_TX)
466 sbd_transmit_chars(sport);
467
468 status = IRQ_HANDLED;
469 }
470
471 return status;
472}
473
474
475static int sbd_startup(struct uart_port *uport)
476{
477 struct sbd_port *sport = to_sport(uport);
478 unsigned int mode1;
479 int ret;
480
481 ret = request_irq(sport->port.irq, sbd_interrupt,
482 IRQF_SHARED, "sb1250-duart", sport);
483 if (ret)
484 return ret;
485
486 /* Clear the receive FIFO. */
487 sbd_receive_drain(sport);
488
489 /* Clear the interrupt registers. */
490 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_BREAK_INT);
491 read_sbdshr(sport, R_DUART_INCHREG((uport->line) % 2));
492
493 /* Set rx/tx interrupt to FIFO available. */
494 mode1 = read_sbdchn(sport, R_DUART_MODE_REG_1);
495 mode1 &= ~(M_DUART_RX_IRQ_SEL_RXFULL | M_DUART_TX_IRQ_SEL_TXEMPT);
496 write_sbdchn(sport, R_DUART_MODE_REG_1, mode1);
497
498 /* Disable tx, enable rx. */
499 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_EN);
500 sport->tx_stopped = 1;
501
502 /* Enable interrupts. */
503 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2),
504 M_DUART_IMR_IN | M_DUART_IMR_RX);
505
506 return 0;
507}
508
509static void sbd_shutdown(struct uart_port *uport)
510{
511 struct sbd_port *sport = to_sport(uport);
512
513 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_DIS);
514 sport->tx_stopped = 1;
515 free_irq(sport->port.irq, sport);
516}
517
518
519static void sbd_init_port(struct sbd_port *sport)
520{
521 struct uart_port *uport = &sport->port;
522
523 if (sport->initialised)
524 return;
525
526 /* There is no DUART reset feature, so just set some sane defaults. */
527 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_TX);
528 write_sbdchn(sport, R_DUART_CMD, V_DUART_MISC_CMD_RESET_RX);
529 write_sbdchn(sport, R_DUART_MODE_REG_1, V_DUART_BITS_PER_CHAR_8);
530 write_sbdchn(sport, R_DUART_MODE_REG_2, 0);
531 write_sbdchn(sport, R_DUART_FULL_CTL,
532 V_DUART_INT_TIME(0) | V_DUART_SIG_FULL(15));
533 write_sbdchn(sport, R_DUART_OPCR_X, 0);
534 write_sbdchn(sport, R_DUART_AUXCTL_X, 0);
535 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), 0);
536
537 sport->initialised = 1;
538}
539
540static void sbd_set_termios(struct uart_port *uport, struct ktermios *termios,
541 struct ktermios *old_termios)
542{
543 struct sbd_port *sport = to_sport(uport);
544 unsigned int mode1 = 0, mode2 = 0, aux = 0;
545 unsigned int mode1mask = 0, mode2mask = 0, auxmask = 0;
546 unsigned int oldmode1, oldmode2, oldaux;
547 unsigned int baud, brg;
548 unsigned int command;
549
550 mode1mask |= ~(M_DUART_PARITY_MODE | M_DUART_PARITY_TYPE_ODD |
551 M_DUART_BITS_PER_CHAR);
552 mode2mask |= ~M_DUART_STOP_BIT_LEN_2;
553 auxmask |= ~M_DUART_CTS_CHNG_ENA;
554
555 /* Byte size. */
556 switch (termios->c_cflag & CSIZE) {
557 case CS5:
558 case CS6:
559 /* Unsupported, leave unchanged. */
560 mode1mask |= M_DUART_PARITY_MODE;
561 break;
562 case CS7:
563 mode1 |= V_DUART_BITS_PER_CHAR_7;
564 break;
565 case CS8:
566 default:
567 mode1 |= V_DUART_BITS_PER_CHAR_8;
568 break;
569 }
570
571 /* Parity and stop bits. */
572 if (termios->c_cflag & CSTOPB)
573 mode2 |= M_DUART_STOP_BIT_LEN_2;
574 else
575 mode2 |= M_DUART_STOP_BIT_LEN_1;
576 if (termios->c_cflag & PARENB)
577 mode1 |= V_DUART_PARITY_MODE_ADD;
578 else
579 mode1 |= V_DUART_PARITY_MODE_NONE;
580 if (termios->c_cflag & PARODD)
581 mode1 |= M_DUART_PARITY_TYPE_ODD;
582 else
583 mode1 |= M_DUART_PARITY_TYPE_EVEN;
584
585 baud = uart_get_baud_rate(uport, termios, old_termios, 1200, 5000000);
586 brg = V_DUART_BAUD_RATE(baud);
587 /* The actual lower bound is 1221bps, so compensate. */
588 if (brg > M_DUART_CLK_COUNTER)
589 brg = M_DUART_CLK_COUNTER;
590
591 uart_update_timeout(uport, termios->c_cflag, baud);
592
593 uport->read_status_mask = M_DUART_OVRUN_ERR;
594 if (termios->c_iflag & INPCK)
595 uport->read_status_mask |= M_DUART_FRM_ERR |
596 M_DUART_PARITY_ERR;
597 if (termios->c_iflag & (BRKINT | PARMRK))
598 uport->read_status_mask |= M_DUART_RCVD_BRK;
599
600 uport->ignore_status_mask = 0;
601 if (termios->c_iflag & IGNPAR)
602 uport->ignore_status_mask |= M_DUART_FRM_ERR |
603 M_DUART_PARITY_ERR;
604 if (termios->c_iflag & IGNBRK) {
605 uport->ignore_status_mask |= M_DUART_RCVD_BRK;
606 if (termios->c_iflag & IGNPAR)
607 uport->ignore_status_mask |= M_DUART_OVRUN_ERR;
608 }
609
610 if (termios->c_cflag & CREAD)
611 command = M_DUART_RX_EN;
612 else
613 command = M_DUART_RX_DIS;
614
615 if (termios->c_cflag & CRTSCTS)
616 aux |= M_DUART_CTS_CHNG_ENA;
617 else
618 aux &= ~M_DUART_CTS_CHNG_ENA;
619
620 spin_lock(&uport->lock);
621
622 if (sport->tx_stopped)
623 command |= M_DUART_TX_DIS;
624 else
625 command |= M_DUART_TX_EN;
626
627 oldmode1 = read_sbdchn(sport, R_DUART_MODE_REG_1) & mode1mask;
628 oldmode2 = read_sbdchn(sport, R_DUART_MODE_REG_2) & mode2mask;
629 oldaux = read_sbdchn(sport, R_DUART_AUXCTL_X) & auxmask;
630
631 if (!sport->tx_stopped)
632 sbd_line_drain(sport);
633 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS | M_DUART_RX_DIS);
634
635 write_sbdchn(sport, R_DUART_MODE_REG_1, mode1 | oldmode1);
636 write_sbdchn(sport, R_DUART_MODE_REG_2, mode2 | oldmode2);
637 write_sbdchn(sport, R_DUART_CLK_SEL, brg);
638 write_sbdchn(sport, R_DUART_AUXCTL_X, aux | oldaux);
639
640 write_sbdchn(sport, R_DUART_CMD, command);
641
642 spin_unlock(&uport->lock);
643}
644
645
646static const char *sbd_type(struct uart_port *uport)
647{
648 return "SB1250 DUART";
649}
650
651static void sbd_release_port(struct uart_port *uport)
652{
653 struct sbd_port *sport = to_sport(uport);
654 struct sbd_duart *duart = sport->duart;
655 int map_guard;
656
657 iounmap(sport->memctrl);
658 sport->memctrl = NULL;
659 iounmap(uport->membase);
660 uport->membase = NULL;
661
662 map_guard = atomic_add_return(-1, &duart->map_guard);
663 if (!map_guard)
664 release_mem_region(duart->mapctrl, DUART_CHANREG_SPACING);
665 release_mem_region(uport->mapbase, DUART_CHANREG_SPACING);
666}
667
668static int sbd_map_port(struct uart_port *uport)
669{
670 const char *err = KERN_ERR "sbd: Cannot map MMIO\n";
671 struct sbd_port *sport = to_sport(uport);
672 struct sbd_duart *duart = sport->duart;
673
674 if (!uport->membase)
675 uport->membase = ioremap_nocache(uport->mapbase,
676 DUART_CHANREG_SPACING);
677 if (!uport->membase) {
678 printk(err);
679 return -ENOMEM;
680 }
681
682 if (!sport->memctrl)
683 sport->memctrl = ioremap_nocache(duart->mapctrl,
684 DUART_CHANREG_SPACING);
685 if (!sport->memctrl) {
686 printk(err);
687 iounmap(uport->membase);
688 uport->membase = NULL;
689 return -ENOMEM;
690 }
691
692 return 0;
693}
694
695static int sbd_request_port(struct uart_port *uport)
696{
697 const char *err = KERN_ERR "sbd: Unable to reserve MMIO resource\n";
698 struct sbd_duart *duart = to_sport(uport)->duart;
699 int map_guard;
700 int ret = 0;
701
702 if (!request_mem_region(uport->mapbase, DUART_CHANREG_SPACING,
703 "sb1250-duart")) {
704 printk(err);
705 return -EBUSY;
706 }
707 map_guard = atomic_add_return(1, &duart->map_guard);
708 if (map_guard == 1) {
709 if (!request_mem_region(duart->mapctrl, DUART_CHANREG_SPACING,
710 "sb1250-duart")) {
711 atomic_add(-1, &duart->map_guard);
712 printk(err);
713 ret = -EBUSY;
714 }
715 }
716 if (!ret) {
717 ret = sbd_map_port(uport);
718 if (ret) {
719 map_guard = atomic_add_return(-1, &duart->map_guard);
720 if (!map_guard)
721 release_mem_region(duart->mapctrl,
722 DUART_CHANREG_SPACING);
723 }
724 }
725 if (ret) {
726 release_mem_region(uport->mapbase, DUART_CHANREG_SPACING);
727 return ret;
728 }
729 return 0;
730}
731
732static void sbd_config_port(struct uart_port *uport, int flags)
733{
734 struct sbd_port *sport = to_sport(uport);
735
736 if (flags & UART_CONFIG_TYPE) {
737 if (sbd_request_port(uport))
738 return;
739
740 uport->type = PORT_SB1250_DUART;
741
742 sbd_init_port(sport);
743 }
744}
745
746static int sbd_verify_port(struct uart_port *uport, struct serial_struct *ser)
747{
748 int ret = 0;
749
750 if (ser->type != PORT_UNKNOWN && ser->type != PORT_SB1250_DUART)
751 ret = -EINVAL;
752 if (ser->irq != uport->irq)
753 ret = -EINVAL;
754 if (ser->baud_base != uport->uartclk / 16)
755 ret = -EINVAL;
756 return ret;
757}
758
759
760static const struct uart_ops sbd_ops = {
761 .tx_empty = sbd_tx_empty,
762 .set_mctrl = sbd_set_mctrl,
763 .get_mctrl = sbd_get_mctrl,
764 .stop_tx = sbd_stop_tx,
765 .start_tx = sbd_start_tx,
766 .stop_rx = sbd_stop_rx,
767 .enable_ms = sbd_enable_ms,
768 .break_ctl = sbd_break_ctl,
769 .startup = sbd_startup,
770 .shutdown = sbd_shutdown,
771 .set_termios = sbd_set_termios,
772 .type = sbd_type,
773 .release_port = sbd_release_port,
774 .request_port = sbd_request_port,
775 .config_port = sbd_config_port,
776 .verify_port = sbd_verify_port,
777};
778
779/* Initialize SB1250 DUART port structures. */
780static void __init sbd_probe_duarts(void)
781{
782 static int probed;
783 int chip, side;
784 int max_lines, line;
785
786 if (probed)
787 return;
788
789 /* Set the number of available units based on the SOC type. */
790 switch (soc_type) {
791 case K_SYS_SOC_TYPE_BCM1x55:
792 case K_SYS_SOC_TYPE_BCM1x80:
793 max_lines = 4;
794 break;
795 default:
796 /* Assume at least two serial ports at the normal address. */
797 max_lines = 2;
798 break;
799 }
800
801 probed = 1;
802
803 for (chip = 0, line = 0; chip < DUART_MAX_CHIP && line < max_lines;
804 chip++) {
805 sbd_duarts[chip].mapctrl = SBD_CTRLREGS(line);
806
807 for (side = 0; side < DUART_MAX_SIDE && line < max_lines;
808 side++, line++) {
809 struct sbd_port *sport = &sbd_duarts[chip].sport[side];
810 struct uart_port *uport = &sport->port;
811
812 sport->duart = &sbd_duarts[chip];
813
814 uport->irq = SBD_INT(line);
815 uport->uartclk = 100000000 / 20 * 16;
816 uport->fifosize = 16;
817 uport->iotype = UPIO_MEM;
818 uport->flags = UPF_BOOT_AUTOCONF;
819 uport->ops = &sbd_ops;
820 uport->line = line;
821 uport->mapbase = SBD_CHANREGS(line);
822 }
823 }
824}
825
826
827#ifdef CONFIG_SERIAL_SB1250_DUART_CONSOLE
828/*
829 * Serial console stuff. Very basic, polling driver for doing serial
830 * console output. The console_lock is held by the caller, so we
831 * shouldn't be interrupted for more console activity.
832 */
833static void sbd_console_putchar(struct uart_port *uport, int ch)
834{
835 struct sbd_port *sport = to_sport(uport);
836
837 sbd_transmit_drain(sport);
838 write_sbdchn(sport, R_DUART_TX_HOLD, ch);
839}
840
841static void sbd_console_write(struct console *co, const char *s,
842 unsigned int count)
843{
844 int chip = co->index / DUART_MAX_SIDE;
845 int side = co->index % DUART_MAX_SIDE;
846 struct sbd_port *sport = &sbd_duarts[chip].sport[side];
847 struct uart_port *uport = &sport->port;
848 unsigned long flags;
849 unsigned int mask;
850
851 /* Disable transmit interrupts and enable the transmitter. */
852 spin_lock_irqsave(&uport->lock, flags);
853 mask = read_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2));
854 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2),
855 mask & ~M_DUART_IMR_TX);
856 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_EN);
857 spin_unlock_irqrestore(&uport->lock, flags);
858
859 uart_console_write(&sport->port, s, count, sbd_console_putchar);
860
861 /* Restore transmit interrupts and the transmitter enable. */
862 spin_lock_irqsave(&uport->lock, flags);
863 sbd_line_drain(sport);
864 if (sport->tx_stopped)
865 write_sbdchn(sport, R_DUART_CMD, M_DUART_TX_DIS);
866 write_sbdshr(sport, R_DUART_IMRREG((uport->line) % 2), mask);
867 spin_unlock_irqrestore(&uport->lock, flags);
868}
869
870static int __init sbd_console_setup(struct console *co, char *options)
871{
872 int chip = co->index / DUART_MAX_SIDE;
873 int side = co->index % DUART_MAX_SIDE;
874 struct sbd_port *sport = &sbd_duarts[chip].sport[side];
875 struct uart_port *uport = &sport->port;
876 int baud = 115200;
877 int bits = 8;
878 int parity = 'n';
879 int flow = 'n';
880 int ret;
881
882 if (!sport->duart)
883 return -ENXIO;
884
885 ret = sbd_map_port(uport);
886 if (ret)
887 return ret;
888
889 sbd_init_port(sport);
890
891 if (options)
892 uart_parse_options(options, &baud, &parity, &bits, &flow);
893 return uart_set_options(uport, co, baud, parity, bits, flow);
894}
895
896static struct uart_driver sbd_reg;
897static struct console sbd_console = {
898 .name = "duart",
899 .write = sbd_console_write,
900 .device = uart_console_device,
901 .setup = sbd_console_setup,
902 .flags = CON_PRINTBUFFER,
903 .index = -1,
904 .data = &sbd_reg
905};
906
907static int __init sbd_serial_console_init(void)
908{
909 sbd_probe_duarts();
910 register_console(&sbd_console);
911
912 return 0;
913}
914
915console_initcall(sbd_serial_console_init);
916
917#define SERIAL_SB1250_DUART_CONSOLE &sbd_console
918#else
919#define SERIAL_SB1250_DUART_CONSOLE NULL
920#endif /* CONFIG_SERIAL_SB1250_DUART_CONSOLE */
921
922
923static struct uart_driver sbd_reg = {
924 .owner = THIS_MODULE,
925 .driver_name = "sb1250_duart",
926 .dev_name = "duart",
927 .major = TTY_MAJOR,
928 .minor = SB1250_DUART_MINOR_BASE,
929 .nr = DUART_MAX_CHIP * DUART_MAX_SIDE,
930 .cons = SERIAL_SB1250_DUART_CONSOLE,
931};
932
933/* Set up the driver and register it. */
934static int __init sbd_init(void)
935{
936 int i, ret;
937
938 sbd_probe_duarts();
939
940 ret = uart_register_driver(&sbd_reg);
941 if (ret)
942 return ret;
943
944 for (i = 0; i < DUART_MAX_CHIP * DUART_MAX_SIDE; i++) {
945 struct sbd_duart *duart = &sbd_duarts[i / DUART_MAX_SIDE];
946 struct sbd_port *sport = &duart->sport[i % DUART_MAX_SIDE];
947 struct uart_port *uport = &sport->port;
948
949 if (sport->duart)
950 uart_add_one_port(&sbd_reg, uport);
951 }
952
953 return 0;
954}
955
956/* Unload the driver. Unregister stuff, get ready to go away. */
957static void __exit sbd_exit(void)
958{
959 int i;
960
961 for (i = DUART_MAX_CHIP * DUART_MAX_SIDE - 1; i >= 0; i--) {
962 struct sbd_duart *duart = &sbd_duarts[i / DUART_MAX_SIDE];
963 struct sbd_port *sport = &duart->sport[i % DUART_MAX_SIDE];
964 struct uart_port *uport = &sport->port;
965
966 if (sport->duart)
967 uart_remove_one_port(&sbd_reg, uport);
968 }
969
970 uart_unregister_driver(&sbd_reg);
971}
972
973module_init(sbd_init);
974module_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..db7912cb7ae0
--- /dev/null
+++ b/drivers/tty/serial/serial_core.c
@@ -0,0 +1,2504 @@
1/*
2 * Driver core for serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
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 as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
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 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23#include <linux/module.h>
24#include <linux/tty.h>
25#include <linux/slab.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/proc_fs.h>
29#include <linux/seq_file.h>
30#include <linux/device.h>
31#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
32#include <linux/serial_core.h>
33#include <linux/delay.h>
34#include <linux/mutex.h>
35
36#include <asm/irq.h>
37#include <asm/uaccess.h>
38
39/*
40 * This is used to lock changes in serial line configuration.
41 */
42static DEFINE_MUTEX(port_mutex);
43
44/*
45 * lockdep: port->lock is initialized in two places, but we
46 * want only one lock-class:
47 */
48static struct lock_class_key port_lock_key;
49
50#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
51
52#ifdef CONFIG_SERIAL_CORE_CONSOLE
53#define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
54#else
55#define uart_console(port) (0)
56#endif
57
58static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
59 struct ktermios *old_termios);
60static void __uart_wait_until_sent(struct uart_port *port, int timeout);
61static void uart_change_pm(struct uart_state *state, int pm_state);
62
63/*
64 * This routine is used by the interrupt handler to schedule processing in
65 * the software interrupt portion of the driver.
66 */
67void uart_write_wakeup(struct uart_port *port)
68{
69 struct uart_state *state = port->state;
70 /*
71 * This means you called this function _after_ the port was
72 * closed. No cookie for you.
73 */
74 BUG_ON(!state);
75 tasklet_schedule(&state->tlet);
76}
77
78static void uart_stop(struct tty_struct *tty)
79{
80 struct uart_state *state = tty->driver_data;
81 struct uart_port *port = state->uart_port;
82 unsigned long flags;
83
84 spin_lock_irqsave(&port->lock, flags);
85 port->ops->stop_tx(port);
86 spin_unlock_irqrestore(&port->lock, flags);
87}
88
89static void __uart_start(struct tty_struct *tty)
90{
91 struct uart_state *state = tty->driver_data;
92 struct uart_port *port = state->uart_port;
93
94 if (!uart_circ_empty(&state->xmit) && state->xmit.buf &&
95 !tty->stopped && !tty->hw_stopped)
96 port->ops->start_tx(port);
97}
98
99static void uart_start(struct tty_struct *tty)
100{
101 struct uart_state *state = tty->driver_data;
102 struct uart_port *port = state->uart_port;
103 unsigned long flags;
104
105 spin_lock_irqsave(&port->lock, flags);
106 __uart_start(tty);
107 spin_unlock_irqrestore(&port->lock, flags);
108}
109
110static void uart_tasklet_action(unsigned long data)
111{
112 struct uart_state *state = (struct uart_state *)data;
113 tty_wakeup(state->port.tty);
114}
115
116static inline void
117uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
118{
119 unsigned long flags;
120 unsigned int old;
121
122 spin_lock_irqsave(&port->lock, flags);
123 old = port->mctrl;
124 port->mctrl = (old & ~clear) | set;
125 if (old != port->mctrl)
126 port->ops->set_mctrl(port, port->mctrl);
127 spin_unlock_irqrestore(&port->lock, flags);
128}
129
130#define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
131#define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
132
133/*
134 * Startup the port. This will be called once per open. All calls
135 * will be serialised by the per-port mutex.
136 */
137static int uart_startup(struct tty_struct *tty, struct uart_state *state, int init_hw)
138{
139 struct uart_port *uport = state->uart_port;
140 struct tty_port *port = &state->port;
141 unsigned long page;
142 int retval = 0;
143
144 if (port->flags & ASYNC_INITIALIZED)
145 return 0;
146
147 /*
148 * Set the TTY IO error marker - we will only clear this
149 * once we have successfully opened the port. Also set
150 * up the tty->alt_speed kludge
151 */
152 set_bit(TTY_IO_ERROR, &tty->flags);
153
154 if (uport->type == PORT_UNKNOWN)
155 return 0;
156
157 /*
158 * Initialise and allocate the transmit and temporary
159 * buffer.
160 */
161 if (!state->xmit.buf) {
162 /* This is protected by the per port mutex */
163 page = get_zeroed_page(GFP_KERNEL);
164 if (!page)
165 return -ENOMEM;
166
167 state->xmit.buf = (unsigned char *) page;
168 uart_circ_clear(&state->xmit);
169 }
170
171 retval = uport->ops->startup(uport);
172 if (retval == 0) {
173 if (uart_console(uport) && uport->cons->cflag) {
174 tty->termios->c_cflag = uport->cons->cflag;
175 uport->cons->cflag = 0;
176 }
177 /*
178 * Initialise the hardware port settings.
179 */
180 uart_change_speed(tty, state, NULL);
181
182 if (init_hw) {
183 /*
184 * Setup the RTS and DTR signals once the
185 * port is open and ready to respond.
186 */
187 if (tty->termios->c_cflag & CBAUD)
188 uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
189 }
190
191 if (port->flags & ASYNC_CTS_FLOW) {
192 spin_lock_irq(&uport->lock);
193 if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
194 tty->hw_stopped = 1;
195 spin_unlock_irq(&uport->lock);
196 }
197
198 set_bit(ASYNCB_INITIALIZED, &port->flags);
199
200 clear_bit(TTY_IO_ERROR, &tty->flags);
201 }
202
203 if (retval && capable(CAP_SYS_ADMIN))
204 retval = 0;
205
206 return retval;
207}
208
209/*
210 * This routine will shutdown a serial port; interrupts are disabled, and
211 * DTR is dropped if the hangup on close termio flag is on. Calls to
212 * uart_shutdown are serialised by the per-port semaphore.
213 */
214static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
215{
216 struct uart_port *uport = state->uart_port;
217 struct tty_port *port = &state->port;
218
219 /*
220 * Set the TTY IO error marker
221 */
222 if (tty)
223 set_bit(TTY_IO_ERROR, &tty->flags);
224
225 if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
226 /*
227 * Turn off DTR and RTS early.
228 */
229 if (!tty || (tty->termios->c_cflag & HUPCL))
230 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
231
232 /*
233 * clear delta_msr_wait queue to avoid mem leaks: we may free
234 * the irq here so the queue might never be woken up. Note
235 * that we won't end up waiting on delta_msr_wait again since
236 * any outstanding file descriptors should be pointing at
237 * hung_up_tty_fops now.
238 */
239 wake_up_interruptible(&port->delta_msr_wait);
240
241 /*
242 * Free the IRQ and disable the port.
243 */
244 uport->ops->shutdown(uport);
245
246 /*
247 * Ensure that the IRQ handler isn't running on another CPU.
248 */
249 synchronize_irq(uport->irq);
250 }
251
252 /*
253 * kill off our tasklet
254 */
255 tasklet_kill(&state->tlet);
256
257 /*
258 * Free the transmit buffer page.
259 */
260 if (state->xmit.buf) {
261 free_page((unsigned long)state->xmit.buf);
262 state->xmit.buf = NULL;
263 }
264}
265
266/**
267 * uart_update_timeout - update per-port FIFO timeout.
268 * @port: uart_port structure describing the port
269 * @cflag: termios cflag value
270 * @baud: speed of the port
271 *
272 * Set the port FIFO timeout value. The @cflag value should
273 * reflect the actual hardware settings.
274 */
275void
276uart_update_timeout(struct uart_port *port, unsigned int cflag,
277 unsigned int baud)
278{
279 unsigned int bits;
280
281 /* byte size and parity */
282 switch (cflag & CSIZE) {
283 case CS5:
284 bits = 7;
285 break;
286 case CS6:
287 bits = 8;
288 break;
289 case CS7:
290 bits = 9;
291 break;
292 default:
293 bits = 10;
294 break; /* CS8 */
295 }
296
297 if (cflag & CSTOPB)
298 bits++;
299 if (cflag & PARENB)
300 bits++;
301
302 /*
303 * The total number of bits to be transmitted in the fifo.
304 */
305 bits = bits * port->fifosize;
306
307 /*
308 * Figure the timeout to send the above number of bits.
309 * Add .02 seconds of slop
310 */
311 port->timeout = (HZ * bits) / baud + HZ/50;
312}
313
314EXPORT_SYMBOL(uart_update_timeout);
315
316/**
317 * uart_get_baud_rate - return baud rate for a particular port
318 * @port: uart_port structure describing the port in question.
319 * @termios: desired termios settings.
320 * @old: old termios (or NULL)
321 * @min: minimum acceptable baud rate
322 * @max: maximum acceptable baud rate
323 *
324 * Decode the termios structure into a numeric baud rate,
325 * taking account of the magic 38400 baud rate (with spd_*
326 * flags), and mapping the %B0 rate to 9600 baud.
327 *
328 * If the new baud rate is invalid, try the old termios setting.
329 * If it's still invalid, we try 9600 baud.
330 *
331 * Update the @termios structure to reflect the baud rate
332 * we're actually going to be using. Don't do this for the case
333 * where B0 is requested ("hang up").
334 */
335unsigned int
336uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
337 struct ktermios *old, unsigned int min, unsigned int max)
338{
339 unsigned int try, baud, altbaud = 38400;
340 int hung_up = 0;
341 upf_t flags = port->flags & UPF_SPD_MASK;
342
343 if (flags == UPF_SPD_HI)
344 altbaud = 57600;
345 else if (flags == UPF_SPD_VHI)
346 altbaud = 115200;
347 else if (flags == UPF_SPD_SHI)
348 altbaud = 230400;
349 else if (flags == UPF_SPD_WARP)
350 altbaud = 460800;
351
352 for (try = 0; try < 2; try++) {
353 baud = tty_termios_baud_rate(termios);
354
355 /*
356 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
357 * Die! Die! Die!
358 */
359 if (baud == 38400)
360 baud = altbaud;
361
362 /*
363 * Special case: B0 rate.
364 */
365 if (baud == 0) {
366 hung_up = 1;
367 baud = 9600;
368 }
369
370 if (baud >= min && baud <= max)
371 return baud;
372
373 /*
374 * Oops, the quotient was zero. Try again with
375 * the old baud rate if possible.
376 */
377 termios->c_cflag &= ~CBAUD;
378 if (old) {
379 baud = tty_termios_baud_rate(old);
380 if (!hung_up)
381 tty_termios_encode_baud_rate(termios,
382 baud, baud);
383 old = NULL;
384 continue;
385 }
386
387 /*
388 * As a last resort, if the range cannot be met then clip to
389 * the nearest chip supported rate.
390 */
391 if (!hung_up) {
392 if (baud <= min)
393 tty_termios_encode_baud_rate(termios,
394 min + 1, min + 1);
395 else
396 tty_termios_encode_baud_rate(termios,
397 max - 1, max - 1);
398 }
399 }
400 /* Should never happen */
401 WARN_ON(1);
402 return 0;
403}
404
405EXPORT_SYMBOL(uart_get_baud_rate);
406
407/**
408 * uart_get_divisor - return uart clock divisor
409 * @port: uart_port structure describing the port.
410 * @baud: desired baud rate
411 *
412 * Calculate the uart clock divisor for the port.
413 */
414unsigned int
415uart_get_divisor(struct uart_port *port, unsigned int baud)
416{
417 unsigned int quot;
418
419 /*
420 * Old custom speed handling.
421 */
422 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
423 quot = port->custom_divisor;
424 else
425 quot = (port->uartclk + (8 * baud)) / (16 * baud);
426
427 return quot;
428}
429
430EXPORT_SYMBOL(uart_get_divisor);
431
432/* FIXME: Consistent locking policy */
433static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
434 struct ktermios *old_termios)
435{
436 struct tty_port *port = &state->port;
437 struct uart_port *uport = state->uart_port;
438 struct ktermios *termios;
439
440 /*
441 * If we have no tty, termios, or the port does not exist,
442 * then we can't set the parameters for this port.
443 */
444 if (!tty || !tty->termios || uport->type == PORT_UNKNOWN)
445 return;
446
447 termios = tty->termios;
448
449 /*
450 * Set flags based on termios cflag
451 */
452 if (termios->c_cflag & CRTSCTS)
453 set_bit(ASYNCB_CTS_FLOW, &port->flags);
454 else
455 clear_bit(ASYNCB_CTS_FLOW, &port->flags);
456
457 if (termios->c_cflag & CLOCAL)
458 clear_bit(ASYNCB_CHECK_CD, &port->flags);
459 else
460 set_bit(ASYNCB_CHECK_CD, &port->flags);
461
462 uport->ops->set_termios(uport, termios, old_termios);
463}
464
465static inline int __uart_put_char(struct uart_port *port,
466 struct circ_buf *circ, unsigned char c)
467{
468 unsigned long flags;
469 int ret = 0;
470
471 if (!circ->buf)
472 return 0;
473
474 spin_lock_irqsave(&port->lock, flags);
475 if (uart_circ_chars_free(circ) != 0) {
476 circ->buf[circ->head] = c;
477 circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
478 ret = 1;
479 }
480 spin_unlock_irqrestore(&port->lock, flags);
481 return ret;
482}
483
484static int uart_put_char(struct tty_struct *tty, unsigned char ch)
485{
486 struct uart_state *state = tty->driver_data;
487
488 return __uart_put_char(state->uart_port, &state->xmit, ch);
489}
490
491static void uart_flush_chars(struct tty_struct *tty)
492{
493 uart_start(tty);
494}
495
496static int uart_write(struct tty_struct *tty,
497 const unsigned char *buf, int count)
498{
499 struct uart_state *state = tty->driver_data;
500 struct uart_port *port;
501 struct circ_buf *circ;
502 unsigned long flags;
503 int c, ret = 0;
504
505 /*
506 * This means you called this function _after_ the port was
507 * closed. No cookie for you.
508 */
509 if (!state) {
510 WARN_ON(1);
511 return -EL3HLT;
512 }
513
514 port = state->uart_port;
515 circ = &state->xmit;
516
517 if (!circ->buf)
518 return 0;
519
520 spin_lock_irqsave(&port->lock, flags);
521 while (1) {
522 c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
523 if (count < c)
524 c = count;
525 if (c <= 0)
526 break;
527 memcpy(circ->buf + circ->head, buf, c);
528 circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
529 buf += c;
530 count -= c;
531 ret += c;
532 }
533 spin_unlock_irqrestore(&port->lock, flags);
534
535 uart_start(tty);
536 return ret;
537}
538
539static int uart_write_room(struct tty_struct *tty)
540{
541 struct uart_state *state = tty->driver_data;
542 unsigned long flags;
543 int ret;
544
545 spin_lock_irqsave(&state->uart_port->lock, flags);
546 ret = uart_circ_chars_free(&state->xmit);
547 spin_unlock_irqrestore(&state->uart_port->lock, flags);
548 return ret;
549}
550
551static int uart_chars_in_buffer(struct tty_struct *tty)
552{
553 struct uart_state *state = tty->driver_data;
554 unsigned long flags;
555 int ret;
556
557 spin_lock_irqsave(&state->uart_port->lock, flags);
558 ret = uart_circ_chars_pending(&state->xmit);
559 spin_unlock_irqrestore(&state->uart_port->lock, flags);
560 return ret;
561}
562
563static void uart_flush_buffer(struct tty_struct *tty)
564{
565 struct uart_state *state = tty->driver_data;
566 struct uart_port *port;
567 unsigned long flags;
568
569 /*
570 * This means you called this function _after_ the port was
571 * closed. No cookie for you.
572 */
573 if (!state) {
574 WARN_ON(1);
575 return;
576 }
577
578 port = state->uart_port;
579 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
580
581 spin_lock_irqsave(&port->lock, flags);
582 uart_circ_clear(&state->xmit);
583 if (port->ops->flush_buffer)
584 port->ops->flush_buffer(port);
585 spin_unlock_irqrestore(&port->lock, flags);
586 tty_wakeup(tty);
587}
588
589/*
590 * This function is used to send a high-priority XON/XOFF character to
591 * the device
592 */
593static void uart_send_xchar(struct tty_struct *tty, char ch)
594{
595 struct uart_state *state = tty->driver_data;
596 struct uart_port *port = state->uart_port;
597 unsigned long flags;
598
599 if (port->ops->send_xchar)
600 port->ops->send_xchar(port, ch);
601 else {
602 port->x_char = ch;
603 if (ch) {
604 spin_lock_irqsave(&port->lock, flags);
605 port->ops->start_tx(port);
606 spin_unlock_irqrestore(&port->lock, flags);
607 }
608 }
609}
610
611static void uart_throttle(struct tty_struct *tty)
612{
613 struct uart_state *state = tty->driver_data;
614
615 if (I_IXOFF(tty))
616 uart_send_xchar(tty, STOP_CHAR(tty));
617
618 if (tty->termios->c_cflag & CRTSCTS)
619 uart_clear_mctrl(state->uart_port, TIOCM_RTS);
620}
621
622static void uart_unthrottle(struct tty_struct *tty)
623{
624 struct uart_state *state = tty->driver_data;
625 struct uart_port *port = state->uart_port;
626
627 if (I_IXOFF(tty)) {
628 if (port->x_char)
629 port->x_char = 0;
630 else
631 uart_send_xchar(tty, START_CHAR(tty));
632 }
633
634 if (tty->termios->c_cflag & CRTSCTS)
635 uart_set_mctrl(port, TIOCM_RTS);
636}
637
638static int uart_get_info(struct uart_state *state,
639 struct serial_struct __user *retinfo)
640{
641 struct uart_port *uport = state->uart_port;
642 struct tty_port *port = &state->port;
643 struct serial_struct tmp;
644
645 memset(&tmp, 0, sizeof(tmp));
646
647 /* Ensure the state we copy is consistent and no hardware changes
648 occur as we go */
649 mutex_lock(&port->mutex);
650
651 tmp.type = uport->type;
652 tmp.line = uport->line;
653 tmp.port = uport->iobase;
654 if (HIGH_BITS_OFFSET)
655 tmp.port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
656 tmp.irq = uport->irq;
657 tmp.flags = uport->flags;
658 tmp.xmit_fifo_size = uport->fifosize;
659 tmp.baud_base = uport->uartclk / 16;
660 tmp.close_delay = port->close_delay / 10;
661 tmp.closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
662 ASYNC_CLOSING_WAIT_NONE :
663 port->closing_wait / 10;
664 tmp.custom_divisor = uport->custom_divisor;
665 tmp.hub6 = uport->hub6;
666 tmp.io_type = uport->iotype;
667 tmp.iomem_reg_shift = uport->regshift;
668 tmp.iomem_base = (void *)(unsigned long)uport->mapbase;
669
670 mutex_unlock(&port->mutex);
671
672 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
673 return -EFAULT;
674 return 0;
675}
676
677static int uart_set_info(struct tty_struct *tty, struct uart_state *state,
678 struct serial_struct __user *newinfo)
679{
680 struct serial_struct new_serial;
681 struct uart_port *uport = state->uart_port;
682 struct tty_port *port = &state->port;
683 unsigned long new_port;
684 unsigned int change_irq, change_port, closing_wait;
685 unsigned int old_custom_divisor, close_delay;
686 upf_t old_flags, new_flags;
687 int retval = 0;
688
689 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
690 return -EFAULT;
691
692 new_port = new_serial.port;
693 if (HIGH_BITS_OFFSET)
694 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
695
696 new_serial.irq = irq_canonicalize(new_serial.irq);
697 close_delay = new_serial.close_delay * 10;
698 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
699 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
700
701 /*
702 * This semaphore protects port->count. It is also
703 * very useful to prevent opens. Also, take the
704 * port configuration semaphore to make sure that a
705 * module insertion/removal doesn't change anything
706 * under us.
707 */
708 mutex_lock(&port->mutex);
709
710 change_irq = !(uport->flags & UPF_FIXED_PORT)
711 && new_serial.irq != uport->irq;
712
713 /*
714 * Since changing the 'type' of the port changes its resource
715 * allocations, we should treat type changes the same as
716 * IO port changes.
717 */
718 change_port = !(uport->flags & UPF_FIXED_PORT)
719 && (new_port != uport->iobase ||
720 (unsigned long)new_serial.iomem_base != uport->mapbase ||
721 new_serial.hub6 != uport->hub6 ||
722 new_serial.io_type != uport->iotype ||
723 new_serial.iomem_reg_shift != uport->regshift ||
724 new_serial.type != uport->type);
725
726 old_flags = uport->flags;
727 new_flags = new_serial.flags;
728 old_custom_divisor = uport->custom_divisor;
729
730 if (!capable(CAP_SYS_ADMIN)) {
731 retval = -EPERM;
732 if (change_irq || change_port ||
733 (new_serial.baud_base != uport->uartclk / 16) ||
734 (close_delay != port->close_delay) ||
735 (closing_wait != port->closing_wait) ||
736 (new_serial.xmit_fifo_size &&
737 new_serial.xmit_fifo_size != uport->fifosize) ||
738 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
739 goto exit;
740 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
741 (new_flags & UPF_USR_MASK));
742 uport->custom_divisor = new_serial.custom_divisor;
743 goto check_and_exit;
744 }
745
746 /*
747 * Ask the low level driver to verify the settings.
748 */
749 if (uport->ops->verify_port)
750 retval = uport->ops->verify_port(uport, &new_serial);
751
752 if ((new_serial.irq >= nr_irqs) || (new_serial.irq < 0) ||
753 (new_serial.baud_base < 9600))
754 retval = -EINVAL;
755
756 if (retval)
757 goto exit;
758
759 if (change_port || change_irq) {
760 retval = -EBUSY;
761
762 /*
763 * Make sure that we are the sole user of this port.
764 */
765 if (tty_port_users(port) > 1)
766 goto exit;
767
768 /*
769 * We need to shutdown the serial port at the old
770 * port/type/irq combination.
771 */
772 uart_shutdown(tty, state);
773 }
774
775 if (change_port) {
776 unsigned long old_iobase, old_mapbase;
777 unsigned int old_type, old_iotype, old_hub6, old_shift;
778
779 old_iobase = uport->iobase;
780 old_mapbase = uport->mapbase;
781 old_type = uport->type;
782 old_hub6 = uport->hub6;
783 old_iotype = uport->iotype;
784 old_shift = uport->regshift;
785
786 /*
787 * Free and release old regions
788 */
789 if (old_type != PORT_UNKNOWN)
790 uport->ops->release_port(uport);
791
792 uport->iobase = new_port;
793 uport->type = new_serial.type;
794 uport->hub6 = new_serial.hub6;
795 uport->iotype = new_serial.io_type;
796 uport->regshift = new_serial.iomem_reg_shift;
797 uport->mapbase = (unsigned long)new_serial.iomem_base;
798
799 /*
800 * Claim and map the new regions
801 */
802 if (uport->type != PORT_UNKNOWN) {
803 retval = uport->ops->request_port(uport);
804 } else {
805 /* Always success - Jean II */
806 retval = 0;
807 }
808
809 /*
810 * If we fail to request resources for the
811 * new port, try to restore the old settings.
812 */
813 if (retval && old_type != PORT_UNKNOWN) {
814 uport->iobase = old_iobase;
815 uport->type = old_type;
816 uport->hub6 = old_hub6;
817 uport->iotype = old_iotype;
818 uport->regshift = old_shift;
819 uport->mapbase = old_mapbase;
820 retval = uport->ops->request_port(uport);
821 /*
822 * If we failed to restore the old settings,
823 * we fail like this.
824 */
825 if (retval)
826 uport->type = PORT_UNKNOWN;
827
828 /*
829 * We failed anyway.
830 */
831 retval = -EBUSY;
832 /* Added to return the correct error -Ram Gupta */
833 goto exit;
834 }
835 }
836
837 if (change_irq)
838 uport->irq = new_serial.irq;
839 if (!(uport->flags & UPF_FIXED_PORT))
840 uport->uartclk = new_serial.baud_base * 16;
841 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
842 (new_flags & UPF_CHANGE_MASK);
843 uport->custom_divisor = new_serial.custom_divisor;
844 port->close_delay = close_delay;
845 port->closing_wait = closing_wait;
846 if (new_serial.xmit_fifo_size)
847 uport->fifosize = new_serial.xmit_fifo_size;
848 if (port->tty)
849 port->tty->low_latency =
850 (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
851
852 check_and_exit:
853 retval = 0;
854 if (uport->type == PORT_UNKNOWN)
855 goto exit;
856 if (port->flags & ASYNC_INITIALIZED) {
857 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
858 old_custom_divisor != uport->custom_divisor) {
859 /*
860 * If they're setting up a custom divisor or speed,
861 * instead of clearing it, then bitch about it. No
862 * need to rate-limit; it's CAP_SYS_ADMIN only.
863 */
864 if (uport->flags & UPF_SPD_MASK) {
865 char buf[64];
866 printk(KERN_NOTICE
867 "%s sets custom speed on %s. This "
868 "is deprecated.\n", current->comm,
869 tty_name(port->tty, buf));
870 }
871 uart_change_speed(tty, state, NULL);
872 }
873 } else
874 retval = uart_startup(tty, state, 1);
875 exit:
876 mutex_unlock(&port->mutex);
877 return retval;
878}
879
880/**
881 * uart_get_lsr_info - get line status register info
882 * @tty: tty associated with the UART
883 * @state: UART being queried
884 * @value: returned modem value
885 *
886 * Note: uart_ioctl protects us against hangups.
887 */
888static int uart_get_lsr_info(struct tty_struct *tty,
889 struct uart_state *state, unsigned int __user *value)
890{
891 struct uart_port *uport = state->uart_port;
892 unsigned int result;
893
894 result = uport->ops->tx_empty(uport);
895
896 /*
897 * If we're about to load something into the transmit
898 * register, we'll pretend the transmitter isn't empty to
899 * avoid a race condition (depending on when the transmit
900 * interrupt happens).
901 */
902 if (uport->x_char ||
903 ((uart_circ_chars_pending(&state->xmit) > 0) &&
904 !tty->stopped && !tty->hw_stopped))
905 result &= ~TIOCSER_TEMT;
906
907 return put_user(result, value);
908}
909
910static int uart_tiocmget(struct tty_struct *tty)
911{
912 struct uart_state *state = tty->driver_data;
913 struct tty_port *port = &state->port;
914 struct uart_port *uport = state->uart_port;
915 int result = -EIO;
916
917 mutex_lock(&port->mutex);
918 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
919 result = uport->mctrl;
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, unsigned int set, unsigned int clear)
931{
932 struct uart_state *state = tty->driver_data;
933 struct uart_port *uport = state->uart_port;
934 struct tty_port *port = &state->port;
935 int ret = -EIO;
936
937 mutex_lock(&port->mutex);
938 if (!(tty->flags & (1 << TTY_IO_ERROR))) {
939 uart_update_mctrl(uport, set, clear);
940 ret = 0;
941 }
942 mutex_unlock(&port->mutex);
943 return ret;
944}
945
946static int uart_break_ctl(struct tty_struct *tty, int break_state)
947{
948 struct uart_state *state = tty->driver_data;
949 struct tty_port *port = &state->port;
950 struct uart_port *uport = state->uart_port;
951
952 mutex_lock(&port->mutex);
953
954 if (uport->type != PORT_UNKNOWN)
955 uport->ops->break_ctl(uport, break_state);
956
957 mutex_unlock(&port->mutex);
958 return 0;
959}
960
961static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
962{
963 struct uart_port *uport = state->uart_port;
964 struct tty_port *port = &state->port;
965 int flags, ret;
966
967 if (!capable(CAP_SYS_ADMIN))
968 return -EPERM;
969
970 /*
971 * Take the per-port semaphore. This prevents count from
972 * changing, and hence any extra opens of the port while
973 * we're auto-configuring.
974 */
975 if (mutex_lock_interruptible(&port->mutex))
976 return -ERESTARTSYS;
977
978 ret = -EBUSY;
979 if (tty_port_users(port) == 1) {
980 uart_shutdown(tty, state);
981
982 /*
983 * If we already have a port type configured,
984 * we must release its resources.
985 */
986 if (uport->type != PORT_UNKNOWN)
987 uport->ops->release_port(uport);
988
989 flags = UART_CONFIG_TYPE;
990 if (uport->flags & UPF_AUTO_IRQ)
991 flags |= UART_CONFIG_IRQ;
992
993 /*
994 * This will claim the ports resources if
995 * a port is found.
996 */
997 uport->ops->config_port(uport, flags);
998
999 ret = uart_startup(tty, state, 1);
1000 }
1001 mutex_unlock(&port->mutex);
1002 return ret;
1003}
1004
1005/*
1006 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1007 * - mask passed in arg for lines of interest
1008 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1009 * Caller should use TIOCGICOUNT to see which one it was
1010 *
1011 * FIXME: This wants extracting into a common all driver implementation
1012 * of TIOCMWAIT using tty_port.
1013 */
1014static int
1015uart_wait_modem_status(struct uart_state *state, unsigned long arg)
1016{
1017 struct uart_port *uport = state->uart_port;
1018 struct tty_port *port = &state->port;
1019 DECLARE_WAITQUEUE(wait, current);
1020 struct uart_icount cprev, cnow;
1021 int ret;
1022
1023 /*
1024 * note the counters on entry
1025 */
1026 spin_lock_irq(&uport->lock);
1027 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1028
1029 /*
1030 * Force modem status interrupts on
1031 */
1032 uport->ops->enable_ms(uport);
1033 spin_unlock_irq(&uport->lock);
1034
1035 add_wait_queue(&port->delta_msr_wait, &wait);
1036 for (;;) {
1037 spin_lock_irq(&uport->lock);
1038 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1039 spin_unlock_irq(&uport->lock);
1040
1041 set_current_state(TASK_INTERRUPTIBLE);
1042
1043 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1044 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1045 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1046 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1047 ret = 0;
1048 break;
1049 }
1050
1051 schedule();
1052
1053 /* see if a signal did it */
1054 if (signal_pending(current)) {
1055 ret = -ERESTARTSYS;
1056 break;
1057 }
1058
1059 cprev = cnow;
1060 }
1061
1062 current->state = TASK_RUNNING;
1063 remove_wait_queue(&port->delta_msr_wait, &wait);
1064
1065 return ret;
1066}
1067
1068/*
1069 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1070 * Return: write counters to the user passed counter struct
1071 * NB: both 1->0 and 0->1 transitions are counted except for
1072 * RI where only 0->1 is counted.
1073 */
1074static int uart_get_icount(struct tty_struct *tty,
1075 struct serial_icounter_struct *icount)
1076{
1077 struct uart_state *state = tty->driver_data;
1078 struct uart_icount cnow;
1079 struct uart_port *uport = state->uart_port;
1080
1081 spin_lock_irq(&uport->lock);
1082 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1083 spin_unlock_irq(&uport->lock);
1084
1085 icount->cts = cnow.cts;
1086 icount->dsr = cnow.dsr;
1087 icount->rng = cnow.rng;
1088 icount->dcd = cnow.dcd;
1089 icount->rx = cnow.rx;
1090 icount->tx = cnow.tx;
1091 icount->frame = cnow.frame;
1092 icount->overrun = cnow.overrun;
1093 icount->parity = cnow.parity;
1094 icount->brk = cnow.brk;
1095 icount->buf_overrun = cnow.buf_overrun;
1096
1097 return 0;
1098}
1099
1100/*
1101 * Called via sys_ioctl. We can use spin_lock_irq() here.
1102 */
1103static int
1104uart_ioctl(struct tty_struct *tty, unsigned int cmd,
1105 unsigned long arg)
1106{
1107 struct uart_state *state = tty->driver_data;
1108 struct tty_port *port = &state->port;
1109 void __user *uarg = (void __user *)arg;
1110 int ret = -ENOIOCTLCMD;
1111
1112
1113 /*
1114 * These ioctls don't rely on the hardware to be present.
1115 */
1116 switch (cmd) {
1117 case TIOCGSERIAL:
1118 ret = uart_get_info(state, uarg);
1119 break;
1120
1121 case TIOCSSERIAL:
1122 ret = uart_set_info(tty, state, uarg);
1123 break;
1124
1125 case TIOCSERCONFIG:
1126 ret = uart_do_autoconfig(tty, state);
1127 break;
1128
1129 case TIOCSERGWILD: /* obsolete */
1130 case TIOCSERSWILD: /* obsolete */
1131 ret = 0;
1132 break;
1133 }
1134
1135 if (ret != -ENOIOCTLCMD)
1136 goto out;
1137
1138 if (tty->flags & (1 << TTY_IO_ERROR)) {
1139 ret = -EIO;
1140 goto out;
1141 }
1142
1143 /*
1144 * The following should only be used when hardware is present.
1145 */
1146 switch (cmd) {
1147 case TIOCMIWAIT:
1148 ret = uart_wait_modem_status(state, arg);
1149 break;
1150 }
1151
1152 if (ret != -ENOIOCTLCMD)
1153 goto out;
1154
1155 mutex_lock(&port->mutex);
1156
1157 if (tty->flags & (1 << TTY_IO_ERROR)) {
1158 ret = -EIO;
1159 goto out_up;
1160 }
1161
1162 /*
1163 * All these rely on hardware being present and need to be
1164 * protected against the tty being hung up.
1165 */
1166 switch (cmd) {
1167 case TIOCSERGETLSR: /* Get line status register */
1168 ret = uart_get_lsr_info(tty, state, uarg);
1169 break;
1170
1171 default: {
1172 struct uart_port *uport = state->uart_port;
1173 if (uport->ops->ioctl)
1174 ret = uport->ops->ioctl(uport, cmd, arg);
1175 break;
1176 }
1177 }
1178out_up:
1179 mutex_unlock(&port->mutex);
1180out:
1181 return ret;
1182}
1183
1184static void uart_set_ldisc(struct tty_struct *tty)
1185{
1186 struct uart_state *state = tty->driver_data;
1187 struct uart_port *uport = state->uart_port;
1188
1189 if (uport->ops->set_ldisc)
1190 uport->ops->set_ldisc(uport, tty->termios->c_line);
1191}
1192
1193static void uart_set_termios(struct tty_struct *tty,
1194 struct ktermios *old_termios)
1195{
1196 struct uart_state *state = tty->driver_data;
1197 unsigned long flags;
1198 unsigned int cflag = tty->termios->c_cflag;
1199
1200
1201 /*
1202 * These are the bits that are used to setup various
1203 * flags in the low level driver. We can ignore the Bfoo
1204 * bits in c_cflag; c_[io]speed will always be set
1205 * appropriately by set_termios() in tty_ioctl.c
1206 */
1207#define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1208 if ((cflag ^ old_termios->c_cflag) == 0 &&
1209 tty->termios->c_ospeed == old_termios->c_ospeed &&
1210 tty->termios->c_ispeed == old_termios->c_ispeed &&
1211 RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0) {
1212 return;
1213 }
1214
1215 uart_change_speed(tty, state, old_termios);
1216
1217 /* Handle transition to B0 status */
1218 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
1219 uart_clear_mctrl(state->uart_port, TIOCM_RTS | TIOCM_DTR);
1220 /* Handle transition away from B0 status */
1221 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
1222 unsigned int mask = TIOCM_DTR;
1223 if (!(cflag & CRTSCTS) ||
1224 !test_bit(TTY_THROTTLED, &tty->flags))
1225 mask |= TIOCM_RTS;
1226 uart_set_mctrl(state->uart_port, mask);
1227 }
1228
1229 /* Handle turning off CRTSCTS */
1230 if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
1231 spin_lock_irqsave(&state->uart_port->lock, flags);
1232 tty->hw_stopped = 0;
1233 __uart_start(tty);
1234 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1235 }
1236 /* Handle turning on CRTSCTS */
1237 else if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
1238 spin_lock_irqsave(&state->uart_port->lock, flags);
1239 if (!(state->uart_port->ops->get_mctrl(state->uart_port) & TIOCM_CTS)) {
1240 tty->hw_stopped = 1;
1241 state->uart_port->ops->stop_tx(state->uart_port);
1242 }
1243 spin_unlock_irqrestore(&state->uart_port->lock, flags);
1244 }
1245}
1246
1247/*
1248 * In 2.4.5, calls to this will be serialized via the BKL in
1249 * linux/drivers/char/tty_io.c:tty_release()
1250 * linux/drivers/char/tty_io.c:do_tty_handup()
1251 */
1252static void uart_close(struct tty_struct *tty, struct file *filp)
1253{
1254 struct uart_state *state = tty->driver_data;
1255 struct tty_port *port;
1256 struct uart_port *uport;
1257 unsigned long flags;
1258
1259 BUG_ON(!tty_locked());
1260
1261 if (!state)
1262 return;
1263
1264 uport = state->uart_port;
1265 port = &state->port;
1266
1267 pr_debug("uart_close(%d) called\n", uport->line);
1268
1269 mutex_lock(&port->mutex);
1270 spin_lock_irqsave(&port->lock, flags);
1271
1272 if (tty_hung_up_p(filp)) {
1273 spin_unlock_irqrestore(&port->lock, flags);
1274 goto done;
1275 }
1276
1277 if ((tty->count == 1) && (port->count != 1)) {
1278 /*
1279 * Uh, oh. tty->count is 1, which means that the tty
1280 * structure will be freed. port->count should always
1281 * be one in these conditions. If it's greater than
1282 * one, we've got real problems, since it means the
1283 * serial port won't be shutdown.
1284 */
1285 printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
1286 "port->count is %d\n", port->count);
1287 port->count = 1;
1288 }
1289 if (--port->count < 0) {
1290 printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
1291 tty->name, port->count);
1292 port->count = 0;
1293 }
1294 if (port->count) {
1295 spin_unlock_irqrestore(&port->lock, flags);
1296 goto done;
1297 }
1298
1299 /*
1300 * Now we wait for the transmit buffer to clear; and we notify
1301 * the line discipline to only process XON/XOFF characters by
1302 * setting tty->closing.
1303 */
1304 tty->closing = 1;
1305 spin_unlock_irqrestore(&port->lock, flags);
1306
1307 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
1308 /*
1309 * hack: open-coded tty_wait_until_sent to avoid
1310 * recursive tty_lock
1311 */
1312 long timeout = msecs_to_jiffies(port->closing_wait);
1313 if (wait_event_interruptible_timeout(tty->write_wait,
1314 !tty_chars_in_buffer(tty), timeout) >= 0)
1315 __uart_wait_until_sent(uport, timeout);
1316 }
1317
1318 /*
1319 * At this point, we stop accepting input. To do this, we
1320 * disable the receive line status interrupts.
1321 */
1322 if (port->flags & ASYNC_INITIALIZED) {
1323 unsigned long flags;
1324 spin_lock_irqsave(&uport->lock, flags);
1325 uport->ops->stop_rx(uport);
1326 spin_unlock_irqrestore(&uport->lock, flags);
1327 /*
1328 * Before we drop DTR, make sure the UART transmitter
1329 * has completely drained; this is especially
1330 * important if there is a transmit FIFO!
1331 */
1332 __uart_wait_until_sent(uport, uport->timeout);
1333 }
1334
1335 uart_shutdown(tty, state);
1336 uart_flush_buffer(tty);
1337
1338 tty_ldisc_flush(tty);
1339
1340 tty_port_tty_set(port, NULL);
1341 spin_lock_irqsave(&port->lock, flags);
1342 tty->closing = 0;
1343
1344 if (port->blocked_open) {
1345 spin_unlock_irqrestore(&port->lock, flags);
1346 if (port->close_delay)
1347 msleep_interruptible(port->close_delay);
1348 spin_lock_irqsave(&port->lock, flags);
1349 } else if (!uart_console(uport)) {
1350 spin_unlock_irqrestore(&port->lock, flags);
1351 uart_change_pm(state, 3);
1352 spin_lock_irqsave(&port->lock, flags);
1353 }
1354
1355 /*
1356 * Wake up anyone trying to open this port.
1357 */
1358 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1359 spin_unlock_irqrestore(&port->lock, flags);
1360 wake_up_interruptible(&port->open_wait);
1361
1362done:
1363 mutex_unlock(&port->mutex);
1364}
1365
1366static void __uart_wait_until_sent(struct uart_port *port, int timeout)
1367{
1368 unsigned long char_time, expire;
1369
1370 if (port->type == PORT_UNKNOWN || port->fifosize == 0)
1371 return;
1372
1373 /*
1374 * Set the check interval to be 1/5 of the estimated time to
1375 * send a single character, and make it at least 1. The check
1376 * interval should also be less than the timeout.
1377 *
1378 * Note: we have to use pretty tight timings here to satisfy
1379 * the NIST-PCTS.
1380 */
1381 char_time = (port->timeout - HZ/50) / port->fifosize;
1382 char_time = char_time / 5;
1383 if (char_time == 0)
1384 char_time = 1;
1385 if (timeout && timeout < char_time)
1386 char_time = timeout;
1387
1388 /*
1389 * If the transmitter hasn't cleared in twice the approximate
1390 * amount of time to send the entire FIFO, it probably won't
1391 * ever clear. This assumes the UART isn't doing flow
1392 * control, which is currently the case. Hence, if it ever
1393 * takes longer than port->timeout, this is probably due to a
1394 * UART bug of some kind. So, we clamp the timeout parameter at
1395 * 2*port->timeout.
1396 */
1397 if (timeout == 0 || timeout > 2 * port->timeout)
1398 timeout = 2 * port->timeout;
1399
1400 expire = jiffies + timeout;
1401
1402 pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
1403 port->line, jiffies, expire);
1404
1405 /*
1406 * Check whether the transmitter is empty every 'char_time'.
1407 * 'timeout' / 'expire' give us the maximum amount of time
1408 * we wait.
1409 */
1410 while (!port->ops->tx_empty(port)) {
1411 msleep_interruptible(jiffies_to_msecs(char_time));
1412 if (signal_pending(current))
1413 break;
1414 if (time_after(jiffies, expire))
1415 break;
1416 }
1417}
1418
1419static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1420{
1421 struct uart_state *state = tty->driver_data;
1422 struct uart_port *port = state->uart_port;
1423
1424 tty_lock();
1425 __uart_wait_until_sent(port, timeout);
1426 tty_unlock();
1427}
1428
1429/*
1430 * This is called with the BKL held in
1431 * linux/drivers/char/tty_io.c:do_tty_hangup()
1432 * We're called from the eventd thread, so we can sleep for
1433 * a _short_ time only.
1434 */
1435static void uart_hangup(struct tty_struct *tty)
1436{
1437 struct uart_state *state = tty->driver_data;
1438 struct tty_port *port = &state->port;
1439 unsigned long flags;
1440
1441 BUG_ON(!tty_locked());
1442 pr_debug("uart_hangup(%d)\n", state->uart_port->line);
1443
1444 mutex_lock(&port->mutex);
1445 if (port->flags & ASYNC_NORMAL_ACTIVE) {
1446 uart_flush_buffer(tty);
1447 uart_shutdown(tty, state);
1448 spin_lock_irqsave(&port->lock, flags);
1449 port->count = 0;
1450 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1451 spin_unlock_irqrestore(&port->lock, flags);
1452 tty_port_tty_set(port, NULL);
1453 wake_up_interruptible(&port->open_wait);
1454 wake_up_interruptible(&port->delta_msr_wait);
1455 }
1456 mutex_unlock(&port->mutex);
1457}
1458
1459static int uart_carrier_raised(struct tty_port *port)
1460{
1461 struct uart_state *state = container_of(port, struct uart_state, port);
1462 struct uart_port *uport = state->uart_port;
1463 int mctrl;
1464 spin_lock_irq(&uport->lock);
1465 uport->ops->enable_ms(uport);
1466 mctrl = uport->ops->get_mctrl(uport);
1467 spin_unlock_irq(&uport->lock);
1468 if (mctrl & TIOCM_CAR)
1469 return 1;
1470 return 0;
1471}
1472
1473static void uart_dtr_rts(struct tty_port *port, int onoff)
1474{
1475 struct uart_state *state = container_of(port, struct uart_state, port);
1476 struct uart_port *uport = state->uart_port;
1477
1478 if (onoff)
1479 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1480 else
1481 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
1482}
1483
1484static struct uart_state *uart_get(struct uart_driver *drv, int line)
1485{
1486 struct uart_state *state;
1487 struct tty_port *port;
1488 int ret = 0;
1489
1490 state = drv->state + line;
1491 port = &state->port;
1492 if (mutex_lock_interruptible(&port->mutex)) {
1493 ret = -ERESTARTSYS;
1494 goto err;
1495 }
1496
1497 port->count++;
1498 if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
1499 ret = -ENXIO;
1500 goto err_unlock;
1501 }
1502 return state;
1503
1504 err_unlock:
1505 port->count--;
1506 mutex_unlock(&port->mutex);
1507 err:
1508 return ERR_PTR(ret);
1509}
1510
1511/*
1512 * calls to uart_open are serialised by the BKL in
1513 * fs/char_dev.c:chrdev_open()
1514 * Note that if this fails, then uart_close() _will_ be called.
1515 *
1516 * In time, we want to scrap the "opening nonpresent ports"
1517 * behaviour and implement an alternative way for setserial
1518 * to set base addresses/ports/types. This will allow us to
1519 * get rid of a certain amount of extra tests.
1520 */
1521static int uart_open(struct tty_struct *tty, struct file *filp)
1522{
1523 struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
1524 struct uart_state *state;
1525 struct tty_port *port;
1526 int retval, line = tty->index;
1527
1528 BUG_ON(!tty_locked());
1529 pr_debug("uart_open(%d) called\n", line);
1530
1531 /*
1532 * We take the semaphore inside uart_get to guarantee that we won't
1533 * be re-entered while allocating the state structure, or while we
1534 * request any IRQs that the driver may need. This also has the nice
1535 * side-effect that it delays the action of uart_hangup, so we can
1536 * guarantee that state->port.tty will always contain something
1537 * reasonable.
1538 */
1539 state = uart_get(drv, line);
1540 if (IS_ERR(state)) {
1541 retval = PTR_ERR(state);
1542 goto fail;
1543 }
1544 port = &state->port;
1545
1546 /*
1547 * Once we set tty->driver_data here, we are guaranteed that
1548 * uart_close() will decrement the driver module use count.
1549 * Any failures from here onwards should not touch the count.
1550 */
1551 tty->driver_data = state;
1552 state->uart_port->state = state;
1553 tty->low_latency = (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
1554 tty->alt_speed = 0;
1555 tty_port_tty_set(port, tty);
1556
1557 /*
1558 * If the port is in the middle of closing, bail out now.
1559 */
1560 if (tty_hung_up_p(filp)) {
1561 retval = -EAGAIN;
1562 port->count--;
1563 mutex_unlock(&port->mutex);
1564 goto fail;
1565 }
1566
1567 /*
1568 * Make sure the device is in D0 state.
1569 */
1570 if (port->count == 1)
1571 uart_change_pm(state, 0);
1572
1573 /*
1574 * Start up the serial port.
1575 */
1576 retval = uart_startup(tty, state, 0);
1577
1578 /*
1579 * If we succeeded, wait until the port is ready.
1580 */
1581 mutex_unlock(&port->mutex);
1582 if (retval == 0)
1583 retval = tty_port_block_til_ready(port, tty, filp);
1584
1585fail:
1586 return retval;
1587}
1588
1589static const char *uart_type(struct uart_port *port)
1590{
1591 const char *str = NULL;
1592
1593 if (port->ops->type)
1594 str = port->ops->type(port);
1595
1596 if (!str)
1597 str = "unknown";
1598
1599 return str;
1600}
1601
1602#ifdef CONFIG_PROC_FS
1603
1604static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
1605{
1606 struct uart_state *state = drv->state + i;
1607 struct tty_port *port = &state->port;
1608 int pm_state;
1609 struct uart_port *uport = state->uart_port;
1610 char stat_buf[32];
1611 unsigned int status;
1612 int mmio;
1613
1614 if (!uport)
1615 return;
1616
1617 mmio = uport->iotype >= UPIO_MEM;
1618 seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
1619 uport->line, uart_type(uport),
1620 mmio ? "mmio:0x" : "port:",
1621 mmio ? (unsigned long long)uport->mapbase
1622 : (unsigned long long)uport->iobase,
1623 uport->irq);
1624
1625 if (uport->type == PORT_UNKNOWN) {
1626 seq_putc(m, '\n');
1627 return;
1628 }
1629
1630 if (capable(CAP_SYS_ADMIN)) {
1631 mutex_lock(&port->mutex);
1632 pm_state = state->pm_state;
1633 if (pm_state)
1634 uart_change_pm(state, 0);
1635 spin_lock_irq(&uport->lock);
1636 status = uport->ops->get_mctrl(uport);
1637 spin_unlock_irq(&uport->lock);
1638 if (pm_state)
1639 uart_change_pm(state, pm_state);
1640 mutex_unlock(&port->mutex);
1641
1642 seq_printf(m, " tx:%d rx:%d",
1643 uport->icount.tx, uport->icount.rx);
1644 if (uport->icount.frame)
1645 seq_printf(m, " fe:%d",
1646 uport->icount.frame);
1647 if (uport->icount.parity)
1648 seq_printf(m, " pe:%d",
1649 uport->icount.parity);
1650 if (uport->icount.brk)
1651 seq_printf(m, " brk:%d",
1652 uport->icount.brk);
1653 if (uport->icount.overrun)
1654 seq_printf(m, " oe:%d",
1655 uport->icount.overrun);
1656
1657#define INFOBIT(bit, str) \
1658 if (uport->mctrl & (bit)) \
1659 strncat(stat_buf, (str), sizeof(stat_buf) - \
1660 strlen(stat_buf) - 2)
1661#define STATBIT(bit, str) \
1662 if (status & (bit)) \
1663 strncat(stat_buf, (str), sizeof(stat_buf) - \
1664 strlen(stat_buf) - 2)
1665
1666 stat_buf[0] = '\0';
1667 stat_buf[1] = '\0';
1668 INFOBIT(TIOCM_RTS, "|RTS");
1669 STATBIT(TIOCM_CTS, "|CTS");
1670 INFOBIT(TIOCM_DTR, "|DTR");
1671 STATBIT(TIOCM_DSR, "|DSR");
1672 STATBIT(TIOCM_CAR, "|CD");
1673 STATBIT(TIOCM_RNG, "|RI");
1674 if (stat_buf[0])
1675 stat_buf[0] = ' ';
1676
1677 seq_puts(m, stat_buf);
1678 }
1679 seq_putc(m, '\n');
1680#undef STATBIT
1681#undef INFOBIT
1682}
1683
1684static int uart_proc_show(struct seq_file *m, void *v)
1685{
1686 struct tty_driver *ttydrv = m->private;
1687 struct uart_driver *drv = ttydrv->driver_state;
1688 int i;
1689
1690 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
1691 "", "", "");
1692 for (i = 0; i < drv->nr; i++)
1693 uart_line_info(m, drv, i);
1694 return 0;
1695}
1696
1697static int uart_proc_open(struct inode *inode, struct file *file)
1698{
1699 return single_open(file, uart_proc_show, PDE(inode)->data);
1700}
1701
1702static const struct file_operations uart_proc_fops = {
1703 .owner = THIS_MODULE,
1704 .open = uart_proc_open,
1705 .read = seq_read,
1706 .llseek = seq_lseek,
1707 .release = single_release,
1708};
1709#endif
1710
1711#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
1712/*
1713 * uart_console_write - write a console message to a serial port
1714 * @port: the port to write the message
1715 * @s: array of characters
1716 * @count: number of characters in string to write
1717 * @write: function to write character to port
1718 */
1719void uart_console_write(struct uart_port *port, const char *s,
1720 unsigned int count,
1721 void (*putchar)(struct uart_port *, int))
1722{
1723 unsigned int i;
1724
1725 for (i = 0; i < count; i++, s++) {
1726 if (*s == '\n')
1727 putchar(port, '\r');
1728 putchar(port, *s);
1729 }
1730}
1731EXPORT_SYMBOL_GPL(uart_console_write);
1732
1733/*
1734 * Check whether an invalid uart number has been specified, and
1735 * if so, search for the first available port that does have
1736 * console support.
1737 */
1738struct uart_port * __init
1739uart_get_console(struct uart_port *ports, int nr, struct console *co)
1740{
1741 int idx = co->index;
1742
1743 if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
1744 ports[idx].membase == NULL))
1745 for (idx = 0; idx < nr; idx++)
1746 if (ports[idx].iobase != 0 ||
1747 ports[idx].membase != NULL)
1748 break;
1749
1750 co->index = idx;
1751
1752 return ports + idx;
1753}
1754
1755/**
1756 * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
1757 * @options: pointer to option string
1758 * @baud: pointer to an 'int' variable for the baud rate.
1759 * @parity: pointer to an 'int' variable for the parity.
1760 * @bits: pointer to an 'int' variable for the number of data bits.
1761 * @flow: pointer to an 'int' variable for the flow control character.
1762 *
1763 * uart_parse_options decodes a string containing the serial console
1764 * options. The format of the string is <baud><parity><bits><flow>,
1765 * eg: 115200n8r
1766 */
1767void
1768uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
1769{
1770 char *s = options;
1771
1772 *baud = simple_strtoul(s, NULL, 10);
1773 while (*s >= '0' && *s <= '9')
1774 s++;
1775 if (*s)
1776 *parity = *s++;
1777 if (*s)
1778 *bits = *s++ - '0';
1779 if (*s)
1780 *flow = *s;
1781}
1782EXPORT_SYMBOL_GPL(uart_parse_options);
1783
1784struct baud_rates {
1785 unsigned int rate;
1786 unsigned int cflag;
1787};
1788
1789static const struct baud_rates baud_rates[] = {
1790 { 921600, B921600 },
1791 { 460800, B460800 },
1792 { 230400, B230400 },
1793 { 115200, B115200 },
1794 { 57600, B57600 },
1795 { 38400, B38400 },
1796 { 19200, B19200 },
1797 { 9600, B9600 },
1798 { 4800, B4800 },
1799 { 2400, B2400 },
1800 { 1200, B1200 },
1801 { 0, B38400 }
1802};
1803
1804/**
1805 * uart_set_options - setup the serial console parameters
1806 * @port: pointer to the serial ports uart_port structure
1807 * @co: console pointer
1808 * @baud: baud rate
1809 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
1810 * @bits: number of data bits
1811 * @flow: flow control character - 'r' (rts)
1812 */
1813int
1814uart_set_options(struct uart_port *port, struct console *co,
1815 int baud, int parity, int bits, int flow)
1816{
1817 struct ktermios termios;
1818 static struct ktermios dummy;
1819 int i;
1820
1821 /*
1822 * Ensure that the serial console lock is initialised
1823 * early.
1824 */
1825 spin_lock_init(&port->lock);
1826 lockdep_set_class(&port->lock, &port_lock_key);
1827
1828 memset(&termios, 0, sizeof(struct ktermios));
1829
1830 termios.c_cflag = CREAD | HUPCL | CLOCAL;
1831
1832 /*
1833 * Construct a cflag setting.
1834 */
1835 for (i = 0; baud_rates[i].rate; i++)
1836 if (baud_rates[i].rate <= baud)
1837 break;
1838
1839 termios.c_cflag |= baud_rates[i].cflag;
1840
1841 if (bits == 7)
1842 termios.c_cflag |= CS7;
1843 else
1844 termios.c_cflag |= CS8;
1845
1846 switch (parity) {
1847 case 'o': case 'O':
1848 termios.c_cflag |= PARODD;
1849 /*fall through*/
1850 case 'e': case 'E':
1851 termios.c_cflag |= PARENB;
1852 break;
1853 }
1854
1855 if (flow == 'r')
1856 termios.c_cflag |= CRTSCTS;
1857
1858 /*
1859 * some uarts on other side don't support no flow control.
1860 * So we set * DTR in host uart to make them happy
1861 */
1862 port->mctrl |= TIOCM_DTR;
1863
1864 port->ops->set_termios(port, &termios, &dummy);
1865 /*
1866 * Allow the setting of the UART parameters with a NULL console
1867 * too:
1868 */
1869 if (co)
1870 co->cflag = termios.c_cflag;
1871
1872 return 0;
1873}
1874EXPORT_SYMBOL_GPL(uart_set_options);
1875#endif /* CONFIG_SERIAL_CORE_CONSOLE */
1876
1877static void uart_change_pm(struct uart_state *state, int pm_state)
1878{
1879 struct uart_port *port = state->uart_port;
1880
1881 if (state->pm_state != pm_state) {
1882 if (port->ops->pm)
1883 port->ops->pm(port, pm_state, state->pm_state);
1884 state->pm_state = pm_state;
1885 }
1886}
1887
1888struct uart_match {
1889 struct uart_port *port;
1890 struct uart_driver *driver;
1891};
1892
1893static int serial_match_port(struct device *dev, void *data)
1894{
1895 struct uart_match *match = data;
1896 struct tty_driver *tty_drv = match->driver->tty_driver;
1897 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
1898 match->port->line;
1899
1900 return dev->devt == devt; /* Actually, only one tty per port */
1901}
1902
1903int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
1904{
1905 struct uart_state *state = drv->state + uport->line;
1906 struct tty_port *port = &state->port;
1907 struct device *tty_dev;
1908 struct uart_match match = {uport, drv};
1909
1910 mutex_lock(&port->mutex);
1911
1912 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1913 if (device_may_wakeup(tty_dev)) {
1914 if (!enable_irq_wake(uport->irq))
1915 uport->irq_wake = 1;
1916 put_device(tty_dev);
1917 mutex_unlock(&port->mutex);
1918 return 0;
1919 }
1920 if (console_suspend_enabled || !uart_console(uport))
1921 uport->suspended = 1;
1922
1923 if (port->flags & ASYNC_INITIALIZED) {
1924 const struct uart_ops *ops = uport->ops;
1925 int tries;
1926
1927 if (console_suspend_enabled || !uart_console(uport)) {
1928 set_bit(ASYNCB_SUSPENDED, &port->flags);
1929 clear_bit(ASYNCB_INITIALIZED, &port->flags);
1930
1931 spin_lock_irq(&uport->lock);
1932 ops->stop_tx(uport);
1933 ops->set_mctrl(uport, 0);
1934 ops->stop_rx(uport);
1935 spin_unlock_irq(&uport->lock);
1936 }
1937
1938 /*
1939 * Wait for the transmitter to empty.
1940 */
1941 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
1942 msleep(10);
1943 if (!tries)
1944 printk(KERN_ERR "%s%s%s%d: Unable to drain "
1945 "transmitter\n",
1946 uport->dev ? dev_name(uport->dev) : "",
1947 uport->dev ? ": " : "",
1948 drv->dev_name,
1949 drv->tty_driver->name_base + uport->line);
1950
1951 if (console_suspend_enabled || !uart_console(uport))
1952 ops->shutdown(uport);
1953 }
1954
1955 /*
1956 * Disable the console device before suspending.
1957 */
1958 if (console_suspend_enabled && uart_console(uport))
1959 console_stop(uport->cons);
1960
1961 if (console_suspend_enabled || !uart_console(uport))
1962 uart_change_pm(state, 3);
1963
1964 mutex_unlock(&port->mutex);
1965
1966 return 0;
1967}
1968
1969int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
1970{
1971 struct uart_state *state = drv->state + uport->line;
1972 struct tty_port *port = &state->port;
1973 struct device *tty_dev;
1974 struct uart_match match = {uport, drv};
1975 struct ktermios termios;
1976
1977 mutex_lock(&port->mutex);
1978
1979 tty_dev = device_find_child(uport->dev, &match, serial_match_port);
1980 if (!uport->suspended && device_may_wakeup(tty_dev)) {
1981 if (uport->irq_wake) {
1982 disable_irq_wake(uport->irq);
1983 uport->irq_wake = 0;
1984 }
1985 mutex_unlock(&port->mutex);
1986 return 0;
1987 }
1988 uport->suspended = 0;
1989
1990 /*
1991 * Re-enable the console device after suspending.
1992 */
1993 if (uart_console(uport)) {
1994 /*
1995 * First try to use the console cflag setting.
1996 */
1997 memset(&termios, 0, sizeof(struct ktermios));
1998 termios.c_cflag = uport->cons->cflag;
1999
2000 /*
2001 * If that's unset, use the tty termios setting.
2002 */
2003 if (port->tty && port->tty->termios && termios.c_cflag == 0)
2004 termios = *(port->tty->termios);
2005
2006 uport->ops->set_termios(uport, &termios, NULL);
2007 if (console_suspend_enabled)
2008 console_start(uport->cons);
2009 }
2010
2011 if (port->flags & ASYNC_SUSPENDED) {
2012 const struct uart_ops *ops = uport->ops;
2013 int ret;
2014
2015 uart_change_pm(state, 0);
2016 spin_lock_irq(&uport->lock);
2017 ops->set_mctrl(uport, 0);
2018 spin_unlock_irq(&uport->lock);
2019 if (console_suspend_enabled || !uart_console(uport)) {
2020 /* Protected by port mutex for now */
2021 struct tty_struct *tty = port->tty;
2022 ret = ops->startup(uport);
2023 if (ret == 0) {
2024 if (tty)
2025 uart_change_speed(tty, state, NULL);
2026 spin_lock_irq(&uport->lock);
2027 ops->set_mctrl(uport, uport->mctrl);
2028 ops->start_tx(uport);
2029 spin_unlock_irq(&uport->lock);
2030 set_bit(ASYNCB_INITIALIZED, &port->flags);
2031 } else {
2032 /*
2033 * Failed to resume - maybe hardware went away?
2034 * Clear the "initialized" flag so we won't try
2035 * to call the low level drivers shutdown method.
2036 */
2037 uart_shutdown(tty, state);
2038 }
2039 }
2040
2041 clear_bit(ASYNCB_SUSPENDED, &port->flags);
2042 }
2043
2044 mutex_unlock(&port->mutex);
2045
2046 return 0;
2047}
2048
2049static inline void
2050uart_report_port(struct uart_driver *drv, struct uart_port *port)
2051{
2052 char address[64];
2053
2054 switch (port->iotype) {
2055 case UPIO_PORT:
2056 snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
2057 break;
2058 case UPIO_HUB6:
2059 snprintf(address, sizeof(address),
2060 "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
2061 break;
2062 case UPIO_MEM:
2063 case UPIO_MEM32:
2064 case UPIO_AU:
2065 case UPIO_TSI:
2066 case UPIO_DWAPB:
2067 case UPIO_DWAPB32:
2068 snprintf(address, sizeof(address),
2069 "MMIO 0x%llx", (unsigned long long)port->mapbase);
2070 break;
2071 default:
2072 strlcpy(address, "*unknown*", sizeof(address));
2073 break;
2074 }
2075
2076 printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
2077 port->dev ? dev_name(port->dev) : "",
2078 port->dev ? ": " : "",
2079 drv->dev_name,
2080 drv->tty_driver->name_base + port->line,
2081 address, port->irq, uart_type(port));
2082}
2083
2084static void
2085uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2086 struct uart_port *port)
2087{
2088 unsigned int flags;
2089
2090 /*
2091 * If there isn't a port here, don't do anything further.
2092 */
2093 if (!port->iobase && !port->mapbase && !port->membase)
2094 return;
2095
2096 /*
2097 * Now do the auto configuration stuff. Note that config_port
2098 * is expected to claim the resources and map the port for us.
2099 */
2100 flags = 0;
2101 if (port->flags & UPF_AUTO_IRQ)
2102 flags |= UART_CONFIG_IRQ;
2103 if (port->flags & UPF_BOOT_AUTOCONF) {
2104 if (!(port->flags & UPF_FIXED_TYPE)) {
2105 port->type = PORT_UNKNOWN;
2106 flags |= UART_CONFIG_TYPE;
2107 }
2108 port->ops->config_port(port, flags);
2109 }
2110
2111 if (port->type != PORT_UNKNOWN) {
2112 unsigned long flags;
2113
2114 uart_report_port(drv, port);
2115
2116 /* Power up port for set_mctrl() */
2117 uart_change_pm(state, 0);
2118
2119 /*
2120 * Ensure that the modem control lines are de-activated.
2121 * keep the DTR setting that is set in uart_set_options()
2122 * We probably don't need a spinlock around this, but
2123 */
2124 spin_lock_irqsave(&port->lock, flags);
2125 port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
2126 spin_unlock_irqrestore(&port->lock, flags);
2127
2128 /*
2129 * If this driver supports console, and it hasn't been
2130 * successfully registered yet, try to re-register it.
2131 * It may be that the port was not available.
2132 */
2133 if (port->cons && !(port->cons->flags & CON_ENABLED))
2134 register_console(port->cons);
2135
2136 /*
2137 * Power down all ports by default, except the
2138 * console if we have one.
2139 */
2140 if (!uart_console(port))
2141 uart_change_pm(state, 3);
2142 }
2143}
2144
2145#ifdef CONFIG_CONSOLE_POLL
2146
2147static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2148{
2149 struct uart_driver *drv = driver->driver_state;
2150 struct uart_state *state = drv->state + line;
2151 struct uart_port *port;
2152 int baud = 9600;
2153 int bits = 8;
2154 int parity = 'n';
2155 int flow = 'n';
2156
2157 if (!state || !state->uart_port)
2158 return -1;
2159
2160 port = state->uart_port;
2161 if (!(port->ops->poll_get_char && port->ops->poll_put_char))
2162 return -1;
2163
2164 if (options) {
2165 uart_parse_options(options, &baud, &parity, &bits, &flow);
2166 return uart_set_options(port, NULL, baud, parity, bits, flow);
2167 }
2168
2169 return 0;
2170}
2171
2172static int uart_poll_get_char(struct tty_driver *driver, int line)
2173{
2174 struct uart_driver *drv = driver->driver_state;
2175 struct uart_state *state = drv->state + line;
2176 struct uart_port *port;
2177
2178 if (!state || !state->uart_port)
2179 return -1;
2180
2181 port = state->uart_port;
2182 return port->ops->poll_get_char(port);
2183}
2184
2185static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2186{
2187 struct uart_driver *drv = driver->driver_state;
2188 struct uart_state *state = drv->state + line;
2189 struct uart_port *port;
2190
2191 if (!state || !state->uart_port)
2192 return;
2193
2194 port = state->uart_port;
2195 port->ops->poll_put_char(port, ch);
2196}
2197#endif
2198
2199static const struct tty_operations uart_ops = {
2200 .open = uart_open,
2201 .close = uart_close,
2202 .write = uart_write,
2203 .put_char = uart_put_char,
2204 .flush_chars = uart_flush_chars,
2205 .write_room = uart_write_room,
2206 .chars_in_buffer= uart_chars_in_buffer,
2207 .flush_buffer = uart_flush_buffer,
2208 .ioctl = uart_ioctl,
2209 .throttle = uart_throttle,
2210 .unthrottle = uart_unthrottle,
2211 .send_xchar = uart_send_xchar,
2212 .set_termios = uart_set_termios,
2213 .set_ldisc = uart_set_ldisc,
2214 .stop = uart_stop,
2215 .start = uart_start,
2216 .hangup = uart_hangup,
2217 .break_ctl = uart_break_ctl,
2218 .wait_until_sent= uart_wait_until_sent,
2219#ifdef CONFIG_PROC_FS
2220 .proc_fops = &uart_proc_fops,
2221#endif
2222 .tiocmget = uart_tiocmget,
2223 .tiocmset = uart_tiocmset,
2224 .get_icount = uart_get_icount,
2225#ifdef CONFIG_CONSOLE_POLL
2226 .poll_init = uart_poll_init,
2227 .poll_get_char = uart_poll_get_char,
2228 .poll_put_char = uart_poll_put_char,
2229#endif
2230};
2231
2232static const struct tty_port_operations uart_port_ops = {
2233 .carrier_raised = uart_carrier_raised,
2234 .dtr_rts = uart_dtr_rts,
2235};
2236
2237/**
2238 * uart_register_driver - register a driver with the uart core layer
2239 * @drv: low level driver structure
2240 *
2241 * Register a uart driver with the core driver. We in turn register
2242 * with the tty layer, and initialise the core driver per-port state.
2243 *
2244 * We have a proc file in /proc/tty/driver which is named after the
2245 * normal driver.
2246 *
2247 * drv->port should be NULL, and the per-port structures should be
2248 * registered using uart_add_one_port after this call has succeeded.
2249 */
2250int uart_register_driver(struct uart_driver *drv)
2251{
2252 struct tty_driver *normal;
2253 int i, retval;
2254
2255 BUG_ON(drv->state);
2256
2257 /*
2258 * Maybe we should be using a slab cache for this, especially if
2259 * we have a large number of ports to handle.
2260 */
2261 drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
2262 if (!drv->state)
2263 goto out;
2264
2265 normal = alloc_tty_driver(drv->nr);
2266 if (!normal)
2267 goto out_kfree;
2268
2269 drv->tty_driver = normal;
2270
2271 normal->owner = drv->owner;
2272 normal->driver_name = drv->driver_name;
2273 normal->name = drv->dev_name;
2274 normal->major = drv->major;
2275 normal->minor_start = drv->minor;
2276 normal->type = TTY_DRIVER_TYPE_SERIAL;
2277 normal->subtype = SERIAL_TYPE_NORMAL;
2278 normal->init_termios = tty_std_termios;
2279 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2280 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2281 normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2282 normal->driver_state = drv;
2283 tty_set_operations(normal, &uart_ops);
2284
2285 /*
2286 * Initialise the UART state(s).
2287 */
2288 for (i = 0; i < drv->nr; i++) {
2289 struct uart_state *state = drv->state + i;
2290 struct tty_port *port = &state->port;
2291
2292 tty_port_init(port);
2293 port->ops = &uart_port_ops;
2294 port->close_delay = 500; /* .5 seconds */
2295 port->closing_wait = 30000; /* 30 seconds */
2296 tasklet_init(&state->tlet, uart_tasklet_action,
2297 (unsigned long)state);
2298 }
2299
2300 retval = tty_register_driver(normal);
2301 if (retval >= 0)
2302 return retval;
2303
2304 put_tty_driver(normal);
2305out_kfree:
2306 kfree(drv->state);
2307out:
2308 return -ENOMEM;
2309}
2310
2311/**
2312 * uart_unregister_driver - remove a driver from the uart core layer
2313 * @drv: low level driver structure
2314 *
2315 * Remove all references to a driver from the core driver. The low
2316 * level driver must have removed all its ports via the
2317 * uart_remove_one_port() if it registered them with uart_add_one_port().
2318 * (ie, drv->port == NULL)
2319 */
2320void uart_unregister_driver(struct uart_driver *drv)
2321{
2322 struct tty_driver *p = drv->tty_driver;
2323 tty_unregister_driver(p);
2324 put_tty_driver(p);
2325 kfree(drv->state);
2326 drv->tty_driver = NULL;
2327}
2328
2329struct tty_driver *uart_console_device(struct console *co, int *index)
2330{
2331 struct uart_driver *p = co->data;
2332 *index = co->index;
2333 return p->tty_driver;
2334}
2335
2336/**
2337 * uart_add_one_port - attach a driver-defined port structure
2338 * @drv: pointer to the uart low level driver structure for this port
2339 * @uport: uart port structure to use for this port.
2340 *
2341 * This allows the driver to register its own uart_port structure
2342 * with the core driver. The main purpose is to allow the low
2343 * level uart drivers to expand uart_port, rather than having yet
2344 * more levels of structures.
2345 */
2346int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
2347{
2348 struct uart_state *state;
2349 struct tty_port *port;
2350 int ret = 0;
2351 struct device *tty_dev;
2352
2353 BUG_ON(in_interrupt());
2354
2355 if (uport->line >= drv->nr)
2356 return -EINVAL;
2357
2358 state = drv->state + uport->line;
2359 port = &state->port;
2360
2361 mutex_lock(&port_mutex);
2362 mutex_lock(&port->mutex);
2363 if (state->uart_port) {
2364 ret = -EINVAL;
2365 goto out;
2366 }
2367
2368 state->uart_port = uport;
2369 state->pm_state = -1;
2370
2371 uport->cons = drv->cons;
2372 uport->state = state;
2373
2374 /*
2375 * If this port is a console, then the spinlock is already
2376 * initialised.
2377 */
2378 if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2379 spin_lock_init(&uport->lock);
2380 lockdep_set_class(&uport->lock, &port_lock_key);
2381 }
2382
2383 uart_configure_port(drv, state, uport);
2384
2385 /*
2386 * Register the port whether it's detected or not. This allows
2387 * setserial to be used to alter this ports parameters.
2388 */
2389 tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
2390 if (likely(!IS_ERR(tty_dev))) {
2391 device_init_wakeup(tty_dev, 1);
2392 device_set_wakeup_enable(tty_dev, 0);
2393 } else
2394 printk(KERN_ERR "Cannot register tty device on line %d\n",
2395 uport->line);
2396
2397 /*
2398 * Ensure UPF_DEAD is not set.
2399 */
2400 uport->flags &= ~UPF_DEAD;
2401
2402 out:
2403 mutex_unlock(&port->mutex);
2404 mutex_unlock(&port_mutex);
2405
2406 return ret;
2407}
2408
2409/**
2410 * uart_remove_one_port - detach a driver defined port structure
2411 * @drv: pointer to the uart low level driver structure for this port
2412 * @uport: uart port structure for this port
2413 *
2414 * This unhooks (and hangs up) the specified port structure from the
2415 * core driver. No further calls will be made to the low-level code
2416 * for this port.
2417 */
2418int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
2419{
2420 struct uart_state *state = drv->state + uport->line;
2421 struct tty_port *port = &state->port;
2422
2423 BUG_ON(in_interrupt());
2424
2425 if (state->uart_port != uport)
2426 printk(KERN_ALERT "Removing wrong port: %p != %p\n",
2427 state->uart_port, uport);
2428
2429 mutex_lock(&port_mutex);
2430
2431 /*
2432 * Mark the port "dead" - this prevents any opens from
2433 * succeeding while we shut down the port.
2434 */
2435 mutex_lock(&port->mutex);
2436 uport->flags |= UPF_DEAD;
2437 mutex_unlock(&port->mutex);
2438
2439 /*
2440 * Remove the devices from the tty layer
2441 */
2442 tty_unregister_device(drv->tty_driver, uport->line);
2443
2444 if (port->tty)
2445 tty_vhangup(port->tty);
2446
2447 /*
2448 * Free the port IO and memory resources, if any.
2449 */
2450 if (uport->type != PORT_UNKNOWN)
2451 uport->ops->release_port(uport);
2452
2453 /*
2454 * Indicate that there isn't a port here anymore.
2455 */
2456 uport->type = PORT_UNKNOWN;
2457
2458 /*
2459 * Kill the tasklet, and free resources.
2460 */
2461 tasklet_kill(&state->tlet);
2462
2463 state->uart_port = NULL;
2464 mutex_unlock(&port_mutex);
2465
2466 return 0;
2467}
2468
2469/*
2470 * Are the two ports equivalent?
2471 */
2472int uart_match_port(struct uart_port *port1, struct uart_port *port2)
2473{
2474 if (port1->iotype != port2->iotype)
2475 return 0;
2476
2477 switch (port1->iotype) {
2478 case UPIO_PORT:
2479 return (port1->iobase == port2->iobase);
2480 case UPIO_HUB6:
2481 return (port1->iobase == port2->iobase) &&
2482 (port1->hub6 == port2->hub6);
2483 case UPIO_MEM:
2484 case UPIO_MEM32:
2485 case UPIO_AU:
2486 case UPIO_TSI:
2487 case UPIO_DWAPB:
2488 case UPIO_DWAPB32:
2489 return (port1->mapbase == port2->mapbase);
2490 }
2491 return 0;
2492}
2493EXPORT_SYMBOL(uart_match_port);
2494
2495EXPORT_SYMBOL(uart_write_wakeup);
2496EXPORT_SYMBOL(uart_register_driver);
2497EXPORT_SYMBOL(uart_unregister_driver);
2498EXPORT_SYMBOL(uart_suspend_port);
2499EXPORT_SYMBOL(uart_resume_port);
2500EXPORT_SYMBOL(uart_add_one_port);
2501EXPORT_SYMBOL(uart_remove_one_port);
2502
2503MODULE_DESCRIPTION("Serial driver core");
2504MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/serial_cs.c b/drivers/tty/serial/serial_cs.c
new file mode 100644
index 000000000000..eef736ff810a
--- /dev/null
+++ b/drivers/tty/serial/serial_cs.c
@@ -0,0 +1,870 @@
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 const 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, 0x0b05),
716 PCMCIA_PFC_DEVICE_MANF_CARD(1, 0x0032, 0x1101),
717 PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x0104, 0x0070),
718 PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x0101, 0x0562),
719 PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x0104, 0x0070),
720 PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x016c, 0x0020),
721 PCMCIA_MFC_DEVICE_PROD_ID123(1, "APEX DATA", "MULTICARD", "ETHERNET-MODEM", 0x11c2da09, 0x7289dc5d, 0xaad95e1f),
722 PCMCIA_MFC_DEVICE_PROD_ID12(1, "IBM", "Home and Away 28.8 PC Card ", 0xb569a6e5, 0x5bd4ff2c),
723 PCMCIA_MFC_DEVICE_PROD_ID12(1, "IBM", "Home and Away Credit Card Adapter", 0xb569a6e5, 0x4bdf15c3),
724 PCMCIA_MFC_DEVICE_PROD_ID12(1, "IBM", "w95 Home and Away Credit Card ", 0xb569a6e5, 0xae911c15),
725 PCMCIA_MFC_DEVICE_PROD_ID1(1, "Motorola MARQUIS", 0xf03e4e77),
726 PCMCIA_MFC_DEVICE_PROD_ID2(1, "FAX/Modem/Ethernet Combo Card ", 0x1ed59302),
727 PCMCIA_DEVICE_MANF_CARD(0x0089, 0x0301),
728 PCMCIA_DEVICE_MANF_CARD(0x00a4, 0x0276),
729 PCMCIA_DEVICE_MANF_CARD(0x0101, 0x0039),
730 PCMCIA_DEVICE_MANF_CARD(0x0104, 0x0006),
731 PCMCIA_DEVICE_MANF_CARD(0x0105, 0x0101), /* TDK DF2814 */
732 PCMCIA_DEVICE_MANF_CARD(0x0105, 0x100a), /* Xircom CM-56G */
733 PCMCIA_DEVICE_MANF_CARD(0x0105, 0x3e0a), /* TDK DF5660 */
734 PCMCIA_DEVICE_MANF_CARD(0x0105, 0x410a),
735 PCMCIA_DEVICE_MANF_CARD(0x0107, 0x0002), /* USRobotics 14,400 */
736 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0d50),
737 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0d51),
738 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0d52),
739 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0d53),
740 PCMCIA_DEVICE_MANF_CARD(0x010b, 0xd180),
741 PCMCIA_DEVICE_MANF_CARD(0x0115, 0x3330), /* USRobotics/SUN 14,400 */
742 PCMCIA_DEVICE_MANF_CARD(0x0124, 0x0100), /* Nokia DTP-2 ver II */
743 PCMCIA_DEVICE_MANF_CARD(0x0134, 0x5600), /* LASAT COMMUNICATIONS A/S */
744 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x000e),
745 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x001b),
746 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0025),
747 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0045),
748 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0052),
749 PCMCIA_DEVICE_MANF_CARD(0x016c, 0x0006), /* Psion 56K+Fax */
750 PCMCIA_DEVICE_MANF_CARD(0x0200, 0x0001), /* MultiMobile */
751 PCMCIA_DEVICE_PROD_ID134("ADV", "TECH", "COMpad-32/85", 0x67459937, 0x916d02ba, 0x8fbe92ae),
752 PCMCIA_DEVICE_PROD_ID124("GATEWAY2000", "CC3144", "PCMCIA MODEM", 0x506bccae, 0xcb3685f1, 0xbd6c43ef),
753 PCMCIA_DEVICE_PROD_ID14("MEGAHERTZ", "PCMCIA MODEM", 0xf510db04, 0xbd6c43ef),
754 PCMCIA_DEVICE_PROD_ID124("TOSHIBA", "T144PF", "PCMCIA MODEM", 0xb4585a1a, 0x7271409c, 0xbd6c43ef),
755 PCMCIA_DEVICE_PROD_ID123("FUJITSU", "FC14F ", "MBH10213", 0x6ee5a3d8, 0x30ead12b, 0xb00f05a0),
756 PCMCIA_DEVICE_PROD_ID123("Novatel Wireless", "Merlin UMTS Modem", "U630", 0x32607776, 0xd9e73b13, 0xe87332e),
757 PCMCIA_DEVICE_PROD_ID13("MEGAHERTZ", "V.34 PCMCIA MODEM", 0xf510db04, 0xbb2cce4a),
758 PCMCIA_DEVICE_PROD_ID12("Brain Boxes", "Bluetooth PC Card", 0xee138382, 0xd4ce9b02),
759 PCMCIA_DEVICE_PROD_ID12("CIRRUS LOGIC", "FAX MODEM", 0xe625f451, 0xcecd6dfa),
760 PCMCIA_DEVICE_PROD_ID12("COMPAQ", "PCMCIA 28800 FAX/DATA MODEM", 0xa3a3062c, 0x8cbd7c76),
761 PCMCIA_DEVICE_PROD_ID12("COMPAQ", "PCMCIA 33600 FAX/DATA MODEM", 0xa3a3062c, 0x5a00ce95),
762 PCMCIA_DEVICE_PROD_ID12("Computerboards, Inc.", "PCM-COM422", 0xd0b78f51, 0x7e2d49ed),
763 PCMCIA_DEVICE_PROD_ID12("Dr. Neuhaus", "FURY CARD 14K4", 0x76942813, 0x8b96ce65),
764 PCMCIA_DEVICE_PROD_ID12("IBM", "ISDN/56K/GSM", 0xb569a6e5, 0xfee5297b),
765 PCMCIA_DEVICE_PROD_ID12("Intelligent", "ANGIA FAX/MODEM", 0xb496e65e, 0xf31602a6),
766 PCMCIA_DEVICE_PROD_ID12("Intel", "MODEM 2400+", 0x816cc815, 0x412729fb),
767 PCMCIA_DEVICE_PROD_ID12("Intertex", "IX34-PCMCIA", 0xf8a097e3, 0x97880447),
768 PCMCIA_DEVICE_PROD_ID12("IOTech Inc ", "PCMCIA Dual RS-232 Serial Port Card", 0x3bd2d898, 0x92abc92f),
769 PCMCIA_DEVICE_PROD_ID12("MACRONIX", "FAX/MODEM", 0x668388b3, 0x3f9bdf2f),
770 PCMCIA_DEVICE_PROD_ID12("Multi-Tech", "MT1432LT", 0x5f73be51, 0x0b3e2383),
771 PCMCIA_DEVICE_PROD_ID12("Multi-Tech", "MT2834LT", 0x5f73be51, 0x4cd7c09e),
772 PCMCIA_DEVICE_PROD_ID12("OEM ", "C288MX ", 0xb572d360, 0xd2385b7a),
773 PCMCIA_DEVICE_PROD_ID12("Option International", "V34bis GSM/PSTN Data/Fax Modem", 0x9d7cd6f5, 0x5cb8bf41),
774 PCMCIA_DEVICE_PROD_ID12("PCMCIA ", "C336MX ", 0x99bcafe9, 0xaa25bcab),
775 PCMCIA_DEVICE_PROD_ID12("Quatech Inc", "PCMCIA Dual RS-232 Serial Port Card", 0xc4420b35, 0x92abc92f),
776 PCMCIA_DEVICE_PROD_ID12("Quatech Inc", "Dual RS-232 Serial Port PC Card", 0xc4420b35, 0x031a380d),
777 PCMCIA_DEVICE_PROD_ID12("Telia", "SurfinBird 560P/A+", 0xe2cdd5e, 0xc9314b38),
778 PCMCIA_DEVICE_PROD_ID1("Smart Serial Port", 0x2d8ce292),
779 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "PCMCIA", "EN2218-LAN/MODEM", 0x281f1c5d, 0x570f348e, "cis/PCMLM28.cis"),
780 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "PCMCIA", "UE2218-LAN/MODEM", 0x281f1c5d, 0x6fdcacee, "cis/PCMLM28.cis"),
781 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "Psion Dacom", "Gold Card V34 Ethernet", 0xf5f025c2, 0x338e8155, "cis/PCMLM28.cis"),
782 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "Psion Dacom", "Gold Card V34 Ethernet GSM", 0xf5f025c2, 0x4ae85d35, "cis/PCMLM28.cis"),
783 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "LINKSYS", "PCMLM28", 0xf7cb0b07, 0x66881874, "cis/PCMLM28.cis"),
784 PCMCIA_PFC_DEVICE_CIS_PROD_ID12(1, "TOSHIBA", "Modem/LAN Card", 0xb4585a1a, 0x53f922f8, "cis/PCMLM28.cis"),
785 PCMCIA_MFC_DEVICE_CIS_PROD_ID12(1, "DAYNA COMMUNICATIONS", "LAN AND MODEM MULTIFUNCTION", 0x8fdf8f89, 0xdd5ed9e8, "cis/DP83903.cis"),
786 PCMCIA_MFC_DEVICE_CIS_PROD_ID4(1, "NSC MF LAN/Modem", 0x58fc6056, "cis/DP83903.cis"),
787 PCMCIA_MFC_DEVICE_CIS_MANF_CARD(1, 0x0101, 0x0556, "cis/3CCFEM556.cis"),
788 PCMCIA_MFC_DEVICE_CIS_MANF_CARD(1, 0x0175, 0x0000, "cis/DP83903.cis"),
789 PCMCIA_MFC_DEVICE_CIS_MANF_CARD(1, 0x0101, 0x0035, "cis/3CXEM556.cis"),
790 PCMCIA_MFC_DEVICE_CIS_MANF_CARD(1, 0x0101, 0x003d, "cis/3CXEM556.cis"),
791 PCMCIA_DEVICE_CIS_PROD_ID12("Sierra Wireless", "AC850", 0xd85f6206, 0x42a2c018, "cis/SW_8xx_SER.cis"), /* Sierra Wireless AC850 3G Network Adapter R1 */
792 PCMCIA_DEVICE_CIS_PROD_ID12("Sierra Wireless", "AC860", 0xd85f6206, 0x698f93db, "cis/SW_8xx_SER.cis"), /* Sierra Wireless AC860 3G Network Adapter R1 */
793 PCMCIA_DEVICE_CIS_PROD_ID12("Sierra Wireless", "AC710/AC750", 0xd85f6206, 0x761b11e0, "cis/SW_7xx_SER.cis"), /* Sierra Wireless AC710/AC750 GPRS Network Adapter R1 */
794 PCMCIA_DEVICE_CIS_MANF_CARD(0x0192, 0xa555, "cis/SW_555_SER.cis"), /* Sierra Aircard 555 CDMA 1xrtt Modem -- pre update */
795 PCMCIA_DEVICE_CIS_MANF_CARD(0x013f, 0xa555, "cis/SW_555_SER.cis"), /* Sierra Aircard 555 CDMA 1xrtt Modem -- post update */
796 PCMCIA_DEVICE_CIS_PROD_ID12("MultiTech", "PCMCIA 56K DataFax", 0x842047ee, 0xc2efcf03, "cis/MT5634ZLX.cis"),
797 PCMCIA_DEVICE_CIS_PROD_ID12("ADVANTECH", "COMpad-32/85B-2", 0x96913a85, 0x27ab5437, "cis/COMpad2.cis"),
798 PCMCIA_DEVICE_CIS_PROD_ID12("ADVANTECH", "COMpad-32/85B-4", 0x96913a85, 0xcec8f102, "cis/COMpad4.cis"),
799 PCMCIA_DEVICE_CIS_PROD_ID123("ADVANTECH", "COMpad-32/85", "1.0", 0x96913a85, 0x8fbe92ae, 0x0877b627, "cis/COMpad2.cis"),
800 PCMCIA_DEVICE_CIS_PROD_ID2("RS-COM 2P", 0xad20b156, "cis/RS-COM-2P.cis"),
801 PCMCIA_DEVICE_CIS_MANF_CARD(0x0013, 0x0000, "cis/GLOBETROTTER.cis"),
802 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c1997.","SERIAL CARD: SL100 1.00.",0x19ca78af,0xf964f42b),
803 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c1997.","SERIAL CARD: SL100",0x19ca78af,0x71d98e83),
804 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c1997.","SERIAL CARD: SL232 1.00.",0x19ca78af,0x69fb7490),
805 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c1997.","SERIAL CARD: SL232",0x19ca78af,0xb6bc0235),
806 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c2000.","SERIAL CARD: CF232",0x63f2e0bd,0xb9e175d3),
807 PCMCIA_DEVICE_PROD_ID12("ELAN DIGITAL SYSTEMS LTD, c2000.","SERIAL CARD: CF232-5",0x63f2e0bd,0xfce33442),
808 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: CF232",0x3beb8cf2,0x171e7190),
809 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: CF232-5",0x3beb8cf2,0x20da4262),
810 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: CF428",0x3beb8cf2,0xea5dd57d),
811 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: CF500",0x3beb8cf2,0xd77255fa),
812 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: IC232",0x3beb8cf2,0x6a709903),
813 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: SL232",0x3beb8cf2,0x18430676),
814 PCMCIA_DEVICE_PROD_ID12("Elan","Serial Port: XL232",0x3beb8cf2,0x6f933767),
815 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial Port: CF332",0x3beb8cf2,0x16dc1ba7),
816 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial Port: SL332",0x3beb8cf2,0x19816c41),
817 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial Port: SL385",0x3beb8cf2,0x64112029),
818 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial Port: SL432",0x3beb8cf2,0x1cce7ac4),
819 PCMCIA_MFC_DEVICE_PROD_ID12(0,"Elan","Serial+Parallel Port: SP230",0x3beb8cf2,0xdb9e58bc),
820 PCMCIA_MFC_DEVICE_PROD_ID12(1,"Elan","Serial Port: CF332",0x3beb8cf2,0x16dc1ba7),
821 PCMCIA_MFC_DEVICE_PROD_ID12(1,"Elan","Serial Port: SL332",0x3beb8cf2,0x19816c41),
822 PCMCIA_MFC_DEVICE_PROD_ID12(1,"Elan","Serial Port: SL385",0x3beb8cf2,0x64112029),
823 PCMCIA_MFC_DEVICE_PROD_ID12(1,"Elan","Serial Port: SL432",0x3beb8cf2,0x1cce7ac4),
824 PCMCIA_MFC_DEVICE_PROD_ID12(2,"Elan","Serial Port: SL432",0x3beb8cf2,0x1cce7ac4),
825 PCMCIA_MFC_DEVICE_PROD_ID12(3,"Elan","Serial Port: SL432",0x3beb8cf2,0x1cce7ac4),
826 PCMCIA_DEVICE_MANF_CARD(0x0279, 0x950b),
827 /* too generic */
828 /* PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x0160, 0x0002), */
829 /* PCMCIA_MFC_DEVICE_MANF_CARD(1, 0x0160, 0x0002), */
830 PCMCIA_DEVICE_FUNC_ID(2),
831 PCMCIA_DEVICE_NULL,
832};
833MODULE_DEVICE_TABLE(pcmcia, serial_ids);
834
835MODULE_FIRMWARE("cis/PCMLM28.cis");
836MODULE_FIRMWARE("cis/DP83903.cis");
837MODULE_FIRMWARE("cis/3CCFEM556.cis");
838MODULE_FIRMWARE("cis/3CXEM556.cis");
839MODULE_FIRMWARE("cis/SW_8xx_SER.cis");
840MODULE_FIRMWARE("cis/SW_7xx_SER.cis");
841MODULE_FIRMWARE("cis/SW_555_SER.cis");
842MODULE_FIRMWARE("cis/MT5634ZLX.cis");
843MODULE_FIRMWARE("cis/COMpad2.cis");
844MODULE_FIRMWARE("cis/COMpad4.cis");
845MODULE_FIRMWARE("cis/RS-COM-2P.cis");
846
847static struct pcmcia_driver serial_cs_driver = {
848 .owner = THIS_MODULE,
849 .name = "serial_cs",
850 .probe = serial_probe,
851 .remove = serial_detach,
852 .id_table = serial_ids,
853 .suspend = serial_suspend,
854 .resume = serial_resume,
855};
856
857static int __init init_serial_cs(void)
858{
859 return pcmcia_register_driver(&serial_cs_driver);
860}
861
862static void __exit exit_serial_cs(void)
863{
864 pcmcia_unregister_driver(&serial_cs_driver);
865}
866
867module_init(init_serial_cs);
868module_exit(exit_serial_cs);
869
870MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/serial_ks8695.c b/drivers/tty/serial/serial_ks8695.c
new file mode 100644
index 000000000000..2430319f2f52
--- /dev/null
+++ b/drivers/tty/serial/serial_ks8695.c
@@ -0,0 +1,703 @@
1/*
2 * Driver for KS8695 serial ports
3 *
4 * Based on drivers/serial/serial_amba.c, by Kam Lee.
5 *
6 * Copyright 2002-2005 Micrel Inc.
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#include <linux/module.h>
15#include <linux/tty.h>
16#include <linux/ioport.h>
17#include <linux/init.h>
18#include <linux/serial.h>
19#include <linux/console.h>
20#include <linux/sysrq.h>
21#include <linux/device.h>
22
23#include <asm/io.h>
24#include <asm/irq.h>
25#include <asm/mach/irq.h>
26
27#include <mach/regs-uart.h>
28#include <mach/regs-irq.h>
29
30#if defined(CONFIG_SERIAL_KS8695_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
31#define SUPPORT_SYSRQ
32#endif
33
34#include <linux/serial_core.h>
35
36
37#define SERIAL_KS8695_MAJOR 204
38#define SERIAL_KS8695_MINOR 16
39#define SERIAL_KS8695_DEVNAME "ttyAM"
40
41#define SERIAL_KS8695_NR 1
42
43/*
44 * Access macros for the KS8695 UART
45 */
46#define UART_GET_CHAR(p) (__raw_readl((p)->membase + KS8695_URRB) & 0xFF)
47#define UART_PUT_CHAR(p, c) __raw_writel((c), (p)->membase + KS8695_URTH)
48#define UART_GET_FCR(p) __raw_readl((p)->membase + KS8695_URFC)
49#define UART_PUT_FCR(p, c) __raw_writel((c), (p)->membase + KS8695_URFC)
50#define UART_GET_MSR(p) __raw_readl((p)->membase + KS8695_URMS)
51#define UART_GET_LSR(p) __raw_readl((p)->membase + KS8695_URLS)
52#define UART_GET_LCR(p) __raw_readl((p)->membase + KS8695_URLC)
53#define UART_PUT_LCR(p, c) __raw_writel((c), (p)->membase + KS8695_URLC)
54#define UART_GET_MCR(p) __raw_readl((p)->membase + KS8695_URMC)
55#define UART_PUT_MCR(p, c) __raw_writel((c), (p)->membase + KS8695_URMC)
56#define UART_GET_BRDR(p) __raw_readl((p)->membase + KS8695_URBD)
57#define UART_PUT_BRDR(p, c) __raw_writel((c), (p)->membase + KS8695_URBD)
58
59#define KS8695_CLR_TX_INT() __raw_writel(1 << KS8695_IRQ_UART_TX, KS8695_IRQ_VA + KS8695_INTST)
60
61#define UART_DUMMY_LSR_RX 0x100
62#define UART_PORT_SIZE (KS8695_USR - KS8695_URRB + 4)
63
64static inline int tx_enabled(struct uart_port *port)
65{
66 return port->unused[0] & 1;
67}
68
69static inline int rx_enabled(struct uart_port *port)
70{
71 return port->unused[0] & 2;
72}
73
74static inline int ms_enabled(struct uart_port *port)
75{
76 return port->unused[0] & 4;
77}
78
79static inline void ms_enable(struct uart_port *port, int enabled)
80{
81 if(enabled)
82 port->unused[0] |= 4;
83 else
84 port->unused[0] &= ~4;
85}
86
87static inline void rx_enable(struct uart_port *port, int enabled)
88{
89 if(enabled)
90 port->unused[0] |= 2;
91 else
92 port->unused[0] &= ~2;
93}
94
95static inline void tx_enable(struct uart_port *port, int enabled)
96{
97 if(enabled)
98 port->unused[0] |= 1;
99 else
100 port->unused[0] &= ~1;
101}
102
103
104#ifdef SUPPORT_SYSRQ
105static struct console ks8695_console;
106#endif
107
108static void ks8695uart_stop_tx(struct uart_port *port)
109{
110 if (tx_enabled(port)) {
111 /* use disable_irq_nosync() and not disable_irq() to avoid self
112 * imposed deadlock by not waiting for irq handler to end,
113 * since this ks8695uart_stop_tx() is called from interrupt context.
114 */
115 disable_irq_nosync(KS8695_IRQ_UART_TX);
116 tx_enable(port, 0);
117 }
118}
119
120static void ks8695uart_start_tx(struct uart_port *port)
121{
122 if (!tx_enabled(port)) {
123 enable_irq(KS8695_IRQ_UART_TX);
124 tx_enable(port, 1);
125 }
126}
127
128static void ks8695uart_stop_rx(struct uart_port *port)
129{
130 if (rx_enabled(port)) {
131 disable_irq(KS8695_IRQ_UART_RX);
132 rx_enable(port, 0);
133 }
134}
135
136static void ks8695uart_enable_ms(struct uart_port *port)
137{
138 if (!ms_enabled(port)) {
139 enable_irq(KS8695_IRQ_UART_MODEM_STATUS);
140 ms_enable(port,1);
141 }
142}
143
144static void ks8695uart_disable_ms(struct uart_port *port)
145{
146 if (ms_enabled(port)) {
147 disable_irq(KS8695_IRQ_UART_MODEM_STATUS);
148 ms_enable(port,0);
149 }
150}
151
152static irqreturn_t ks8695uart_rx_chars(int irq, void *dev_id)
153{
154 struct uart_port *port = dev_id;
155 struct tty_struct *tty = port->state->port.tty;
156 unsigned int status, ch, lsr, flg, max_count = 256;
157
158 status = UART_GET_LSR(port); /* clears pending LSR interrupts */
159 while ((status & URLS_URDR) && max_count--) {
160 ch = UART_GET_CHAR(port);
161 flg = TTY_NORMAL;
162
163 port->icount.rx++;
164
165 /*
166 * Note that the error handling code is
167 * out of the main execution path
168 */
169 lsr = UART_GET_LSR(port) | UART_DUMMY_LSR_RX;
170 if (unlikely(lsr & (URLS_URBI | URLS_URPE | URLS_URFE | URLS_URROE))) {
171 if (lsr & URLS_URBI) {
172 lsr &= ~(URLS_URFE | URLS_URPE);
173 port->icount.brk++;
174 if (uart_handle_break(port))
175 goto ignore_char;
176 }
177 if (lsr & URLS_URPE)
178 port->icount.parity++;
179 if (lsr & URLS_URFE)
180 port->icount.frame++;
181 if (lsr & URLS_URROE)
182 port->icount.overrun++;
183
184 lsr &= port->read_status_mask;
185
186 if (lsr & URLS_URBI)
187 flg = TTY_BREAK;
188 else if (lsr & URLS_URPE)
189 flg = TTY_PARITY;
190 else if (lsr & URLS_URFE)
191 flg = TTY_FRAME;
192 }
193
194 if (uart_handle_sysrq_char(port, ch))
195 goto ignore_char;
196
197 uart_insert_char(port, lsr, URLS_URROE, ch, flg);
198
199ignore_char:
200 status = UART_GET_LSR(port);
201 }
202 tty_flip_buffer_push(tty);
203
204 return IRQ_HANDLED;
205}
206
207
208static irqreturn_t ks8695uart_tx_chars(int irq, void *dev_id)
209{
210 struct uart_port *port = dev_id;
211 struct circ_buf *xmit = &port->state->xmit;
212 unsigned int count;
213
214 if (port->x_char) {
215 KS8695_CLR_TX_INT();
216 UART_PUT_CHAR(port, port->x_char);
217 port->icount.tx++;
218 port->x_char = 0;
219 return IRQ_HANDLED;
220 }
221
222 if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
223 ks8695uart_stop_tx(port);
224 return IRQ_HANDLED;
225 }
226
227 count = 16; /* fifo size */
228 while (!uart_circ_empty(xmit) && (count-- > 0)) {
229 KS8695_CLR_TX_INT();
230 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
231
232 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
233 port->icount.tx++;
234 }
235
236 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
237 uart_write_wakeup(port);
238
239 if (uart_circ_empty(xmit))
240 ks8695uart_stop_tx(port);
241
242 return IRQ_HANDLED;
243}
244
245static irqreturn_t ks8695uart_modem_status(int irq, void *dev_id)
246{
247 struct uart_port *port = dev_id;
248 unsigned int status;
249
250 /*
251 * clear modem interrupt by reading MSR
252 */
253 status = UART_GET_MSR(port);
254
255 if (status & URMS_URDDCD)
256 uart_handle_dcd_change(port, status & URMS_URDDCD);
257
258 if (status & URMS_URDDST)
259 port->icount.dsr++;
260
261 if (status & URMS_URDCTS)
262 uart_handle_cts_change(port, status & URMS_URDCTS);
263
264 if (status & URMS_URTERI)
265 port->icount.rng++;
266
267 wake_up_interruptible(&port->state->port.delta_msr_wait);
268
269 return IRQ_HANDLED;
270}
271
272static unsigned int ks8695uart_tx_empty(struct uart_port *port)
273{
274 return (UART_GET_LSR(port) & URLS_URTE) ? TIOCSER_TEMT : 0;
275}
276
277static unsigned int ks8695uart_get_mctrl(struct uart_port *port)
278{
279 unsigned int result = 0;
280 unsigned int status;
281
282 status = UART_GET_MSR(port);
283 if (status & URMS_URDCD)
284 result |= TIOCM_CAR;
285 if (status & URMS_URDSR)
286 result |= TIOCM_DSR;
287 if (status & URMS_URCTS)
288 result |= TIOCM_CTS;
289 if (status & URMS_URRI)
290 result |= TIOCM_RI;
291
292 return result;
293}
294
295static void ks8695uart_set_mctrl(struct uart_port *port, u_int mctrl)
296{
297 unsigned int mcr;
298
299 mcr = UART_GET_MCR(port);
300 if (mctrl & TIOCM_RTS)
301 mcr |= URMC_URRTS;
302 else
303 mcr &= ~URMC_URRTS;
304
305 if (mctrl & TIOCM_DTR)
306 mcr |= URMC_URDTR;
307 else
308 mcr &= ~URMC_URDTR;
309
310 UART_PUT_MCR(port, mcr);
311}
312
313static void ks8695uart_break_ctl(struct uart_port *port, int break_state)
314{
315 unsigned int lcr;
316
317 lcr = UART_GET_LCR(port);
318
319 if (break_state == -1)
320 lcr |= URLC_URSBC;
321 else
322 lcr &= ~URLC_URSBC;
323
324 UART_PUT_LCR(port, lcr);
325}
326
327static int ks8695uart_startup(struct uart_port *port)
328{
329 int retval;
330
331 set_irq_flags(KS8695_IRQ_UART_TX, IRQF_VALID | IRQF_NOAUTOEN);
332 tx_enable(port, 0);
333 rx_enable(port, 1);
334 ms_enable(port, 1);
335
336 /*
337 * Allocate the IRQ
338 */
339 retval = request_irq(KS8695_IRQ_UART_TX, ks8695uart_tx_chars, IRQF_DISABLED, "UART TX", port);
340 if (retval)
341 goto err_tx;
342
343 retval = request_irq(KS8695_IRQ_UART_RX, ks8695uart_rx_chars, IRQF_DISABLED, "UART RX", port);
344 if (retval)
345 goto err_rx;
346
347 retval = request_irq(KS8695_IRQ_UART_LINE_STATUS, ks8695uart_rx_chars, IRQF_DISABLED, "UART LineStatus", port);
348 if (retval)
349 goto err_ls;
350
351 retval = request_irq(KS8695_IRQ_UART_MODEM_STATUS, ks8695uart_modem_status, IRQF_DISABLED, "UART ModemStatus", port);
352 if (retval)
353 goto err_ms;
354
355 return 0;
356
357err_ms:
358 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
359err_ls:
360 free_irq(KS8695_IRQ_UART_RX, port);
361err_rx:
362 free_irq(KS8695_IRQ_UART_TX, port);
363err_tx:
364 return retval;
365}
366
367static void ks8695uart_shutdown(struct uart_port *port)
368{
369 /*
370 * Free the interrupt
371 */
372 free_irq(KS8695_IRQ_UART_RX, port);
373 free_irq(KS8695_IRQ_UART_TX, port);
374 free_irq(KS8695_IRQ_UART_MODEM_STATUS, port);
375 free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
376
377 /* disable break condition and fifos */
378 UART_PUT_LCR(port, UART_GET_LCR(port) & ~URLC_URSBC);
379 UART_PUT_FCR(port, UART_GET_FCR(port) & ~URFC_URFE);
380}
381
382static void ks8695uart_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old)
383{
384 unsigned int lcr, fcr = 0;
385 unsigned long flags;
386 unsigned int baud, quot;
387
388 /*
389 * Ask the core to calculate the divisor for us.
390 */
391 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
392 quot = uart_get_divisor(port, baud);
393
394 switch (termios->c_cflag & CSIZE) {
395 case CS5:
396 lcr = URCL_5;
397 break;
398 case CS6:
399 lcr = URCL_6;
400 break;
401 case CS7:
402 lcr = URCL_7;
403 break;
404 default:
405 lcr = URCL_8;
406 break;
407 }
408
409 /* stop bits */
410 if (termios->c_cflag & CSTOPB)
411 lcr |= URLC_URSB;
412
413 /* parity */
414 if (termios->c_cflag & PARENB) {
415 if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
416 if (termios->c_cflag & PARODD)
417 lcr |= URPE_MARK;
418 else
419 lcr |= URPE_SPACE;
420 }
421 else if (termios->c_cflag & PARODD)
422 lcr |= URPE_ODD;
423 else
424 lcr |= URPE_EVEN;
425 }
426
427 if (port->fifosize > 1)
428 fcr = URFC_URFRT_8 | URFC_URTFR | URFC_URRFR | URFC_URFE;
429
430 spin_lock_irqsave(&port->lock, flags);
431
432 /*
433 * Update the per-port timeout.
434 */
435 uart_update_timeout(port, termios->c_cflag, baud);
436
437 port->read_status_mask = URLS_URROE;
438 if (termios->c_iflag & INPCK)
439 port->read_status_mask |= (URLS_URFE | URLS_URPE);
440 if (termios->c_iflag & (BRKINT | PARMRK))
441 port->read_status_mask |= URLS_URBI;
442
443 /*
444 * Characters to ignore
445 */
446 port->ignore_status_mask = 0;
447 if (termios->c_iflag & IGNPAR)
448 port->ignore_status_mask |= (URLS_URFE | URLS_URPE);
449 if (termios->c_iflag & IGNBRK) {
450 port->ignore_status_mask |= URLS_URBI;
451 /*
452 * If we're ignoring parity and break indicators,
453 * ignore overruns too (for real raw support).
454 */
455 if (termios->c_iflag & IGNPAR)
456 port->ignore_status_mask |= URLS_URROE;
457 }
458
459 /*
460 * Ignore all characters if CREAD is not set.
461 */
462 if ((termios->c_cflag & CREAD) == 0)
463 port->ignore_status_mask |= UART_DUMMY_LSR_RX;
464
465 /* first, disable everything */
466 if (UART_ENABLE_MS(port, termios->c_cflag))
467 ks8695uart_enable_ms(port);
468 else
469 ks8695uart_disable_ms(port);
470
471 /* Set baud rate */
472 UART_PUT_BRDR(port, quot);
473
474 UART_PUT_LCR(port, lcr);
475 UART_PUT_FCR(port, fcr);
476
477 spin_unlock_irqrestore(&port->lock, flags);
478}
479
480static const char *ks8695uart_type(struct uart_port *port)
481{
482 return port->type == PORT_KS8695 ? "KS8695" : NULL;
483}
484
485/*
486 * Release the memory region(s) being used by 'port'
487 */
488static void ks8695uart_release_port(struct uart_port *port)
489{
490 release_mem_region(port->mapbase, UART_PORT_SIZE);
491}
492
493/*
494 * Request the memory region(s) being used by 'port'
495 */
496static int ks8695uart_request_port(struct uart_port *port)
497{
498 return request_mem_region(port->mapbase, UART_PORT_SIZE,
499 "serial_ks8695") != NULL ? 0 : -EBUSY;
500}
501
502/*
503 * Configure/autoconfigure the port.
504 */
505static void ks8695uart_config_port(struct uart_port *port, int flags)
506{
507 if (flags & UART_CONFIG_TYPE) {
508 port->type = PORT_KS8695;
509 ks8695uart_request_port(port);
510 }
511}
512
513/*
514 * verify the new serial_struct (for TIOCSSERIAL).
515 */
516static int ks8695uart_verify_port(struct uart_port *port, struct serial_struct *ser)
517{
518 int ret = 0;
519
520 if (ser->type != PORT_UNKNOWN && ser->type != PORT_KS8695)
521 ret = -EINVAL;
522 if (ser->irq != port->irq)
523 ret = -EINVAL;
524 if (ser->baud_base < 9600)
525 ret = -EINVAL;
526 return ret;
527}
528
529static struct uart_ops ks8695uart_pops = {
530 .tx_empty = ks8695uart_tx_empty,
531 .set_mctrl = ks8695uart_set_mctrl,
532 .get_mctrl = ks8695uart_get_mctrl,
533 .stop_tx = ks8695uart_stop_tx,
534 .start_tx = ks8695uart_start_tx,
535 .stop_rx = ks8695uart_stop_rx,
536 .enable_ms = ks8695uart_enable_ms,
537 .break_ctl = ks8695uart_break_ctl,
538 .startup = ks8695uart_startup,
539 .shutdown = ks8695uart_shutdown,
540 .set_termios = ks8695uart_set_termios,
541 .type = ks8695uart_type,
542 .release_port = ks8695uart_release_port,
543 .request_port = ks8695uart_request_port,
544 .config_port = ks8695uart_config_port,
545 .verify_port = ks8695uart_verify_port,
546};
547
548static struct uart_port ks8695uart_ports[SERIAL_KS8695_NR] = {
549 {
550 .membase = (void *) KS8695_UART_VA,
551 .mapbase = KS8695_UART_VA,
552 .iotype = SERIAL_IO_MEM,
553 .irq = KS8695_IRQ_UART_TX,
554 .uartclk = KS8695_CLOCK_RATE * 16,
555 .fifosize = 16,
556 .ops = &ks8695uart_pops,
557 .flags = ASYNC_BOOT_AUTOCONF,
558 .line = 0,
559 }
560};
561
562#ifdef CONFIG_SERIAL_KS8695_CONSOLE
563static void ks8695_console_putchar(struct uart_port *port, int ch)
564{
565 while (!(UART_GET_LSR(port) & URLS_URTHRE))
566 barrier();
567
568 UART_PUT_CHAR(port, ch);
569}
570
571static void ks8695_console_write(struct console *co, const char *s, u_int count)
572{
573 struct uart_port *port = ks8695uart_ports + co->index;
574
575 uart_console_write(port, s, count, ks8695_console_putchar);
576}
577
578static void __init ks8695_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
579{
580 unsigned int lcr;
581
582 lcr = UART_GET_LCR(port);
583
584 switch (lcr & URLC_PARITY) {
585 case URPE_ODD:
586 *parity = 'o';
587 break;
588 case URPE_EVEN:
589 *parity = 'e';
590 break;
591 default:
592 *parity = 'n';
593 }
594
595 switch (lcr & URLC_URCL) {
596 case URCL_5:
597 *bits = 5;
598 break;
599 case URCL_6:
600 *bits = 6;
601 break;
602 case URCL_7:
603 *bits = 7;
604 break;
605 default:
606 *bits = 8;
607 }
608
609 *baud = port->uartclk / (UART_GET_BRDR(port) & 0x0FFF);
610 *baud /= 16;
611 *baud &= 0xFFFFFFF0;
612}
613
614static int __init ks8695_console_setup(struct console *co, char *options)
615{
616 struct uart_port *port;
617 int baud = 115200;
618 int bits = 8;
619 int parity = 'n';
620 int flow = 'n';
621
622 /*
623 * Check whether an invalid uart number has been specified, and
624 * if so, search for the first available port that does have
625 * console support.
626 */
627 port = uart_get_console(ks8695uart_ports, SERIAL_KS8695_NR, co);
628
629 if (options)
630 uart_parse_options(options, &baud, &parity, &bits, &flow);
631 else
632 ks8695_console_get_options(port, &baud, &parity, &bits);
633
634 return uart_set_options(port, co, baud, parity, bits, flow);
635}
636
637static struct uart_driver ks8695_reg;
638
639static struct console ks8695_console = {
640 .name = SERIAL_KS8695_DEVNAME,
641 .write = ks8695_console_write,
642 .device = uart_console_device,
643 .setup = ks8695_console_setup,
644 .flags = CON_PRINTBUFFER,
645 .index = -1,
646 .data = &ks8695_reg,
647};
648
649static int __init ks8695_console_init(void)
650{
651 add_preferred_console(SERIAL_KS8695_DEVNAME, 0, NULL);
652 register_console(&ks8695_console);
653 return 0;
654}
655
656console_initcall(ks8695_console_init);
657
658#define KS8695_CONSOLE &ks8695_console
659#else
660#define KS8695_CONSOLE NULL
661#endif
662
663static struct uart_driver ks8695_reg = {
664 .owner = THIS_MODULE,
665 .driver_name = "serial_ks8695",
666 .dev_name = SERIAL_KS8695_DEVNAME,
667 .major = SERIAL_KS8695_MAJOR,
668 .minor = SERIAL_KS8695_MINOR,
669 .nr = SERIAL_KS8695_NR,
670 .cons = KS8695_CONSOLE,
671};
672
673static int __init ks8695uart_init(void)
674{
675 int i, ret;
676
677 printk(KERN_INFO "Serial: Micrel KS8695 UART driver\n");
678
679 ret = uart_register_driver(&ks8695_reg);
680 if (ret)
681 return ret;
682
683 for (i = 0; i < SERIAL_KS8695_NR; i++)
684 uart_add_one_port(&ks8695_reg, &ks8695uart_ports[0]);
685
686 return 0;
687}
688
689static void __exit ks8695uart_exit(void)
690{
691 int i;
692
693 for (i = 0; i < SERIAL_KS8695_NR; i++)
694 uart_remove_one_port(&ks8695_reg, &ks8695uart_ports[0]);
695 uart_unregister_driver(&ks8695_reg);
696}
697
698module_init(ks8695uart_init);
699module_exit(ks8695uart_exit);
700
701MODULE_DESCRIPTION("KS8695 serial port driver");
702MODULE_AUTHOR("Micrel Inc.");
703MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/serial_txx9.c b/drivers/tty/serial/serial_txx9.c
new file mode 100644
index 000000000000..8e3fc1944e6d
--- /dev/null
+++ b/drivers/tty/serial/serial_txx9.c
@@ -0,0 +1,1342 @@
1/*
2 * Derived from many drivers using generic_serial interface,
3 * especially serial_tx3912.c by Steven J. Hill and r39xx_serial.c
4 * (was in Linux/VR tree) by Jim Pick.
5 *
6 * Copyright (C) 1999 Harald Koerfgen
7 * Copyright (C) 2000 Jim Pick <jim@jimpick.com>
8 * Copyright (C) 2001 Steven J. Hill (sjhill@realitydiluted.com)
9 * Copyright (C) 2000-2002 Toshiba Corporation
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 version 2 as
13 * published by the Free Software Foundation.
14 *
15 * Serial driver for TX3927/TX4927/TX4925/TX4938 internal SIO controller
16 */
17
18#if defined(CONFIG_SERIAL_TXX9_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19#define SUPPORT_SYSRQ
20#endif
21
22#include <linux/module.h>
23#include <linux/ioport.h>
24#include <linux/init.h>
25#include <linux/console.h>
26#include <linux/delay.h>
27#include <linux/platform_device.h>
28#include <linux/pci.h>
29#include <linux/serial_core.h>
30#include <linux/serial.h>
31
32#include <asm/io.h>
33
34static char *serial_version = "1.11";
35static char *serial_name = "TX39/49 Serial driver";
36
37#define PASS_LIMIT 256
38
39#if !defined(CONFIG_SERIAL_TXX9_STDSERIAL)
40/* "ttyS" is used for standard serial driver */
41#define TXX9_TTY_NAME "ttyTX"
42#define TXX9_TTY_MINOR_START 196
43#define TXX9_TTY_MAJOR 204
44#else
45/* acts like standard serial driver */
46#define TXX9_TTY_NAME "ttyS"
47#define TXX9_TTY_MINOR_START 64
48#define TXX9_TTY_MAJOR TTY_MAJOR
49#endif
50
51/* flag aliases */
52#define UPF_TXX9_HAVE_CTS_LINE UPF_BUGGY_UART
53#define UPF_TXX9_USE_SCLK UPF_MAGIC_MULTIPLIER
54
55#ifdef CONFIG_PCI
56/* support for Toshiba TC86C001 SIO */
57#define ENABLE_SERIAL_TXX9_PCI
58#endif
59
60/*
61 * Number of serial ports
62 */
63#define UART_NR CONFIG_SERIAL_TXX9_NR_UARTS
64
65struct uart_txx9_port {
66 struct uart_port port;
67 /* No additional info for now */
68};
69
70#define TXX9_REGION_SIZE 0x24
71
72/* TXX9 Serial Registers */
73#define TXX9_SILCR 0x00
74#define TXX9_SIDICR 0x04
75#define TXX9_SIDISR 0x08
76#define TXX9_SICISR 0x0c
77#define TXX9_SIFCR 0x10
78#define TXX9_SIFLCR 0x14
79#define TXX9_SIBGR 0x18
80#define TXX9_SITFIFO 0x1c
81#define TXX9_SIRFIFO 0x20
82
83/* SILCR : Line Control */
84#define TXX9_SILCR_SCS_MASK 0x00000060
85#define TXX9_SILCR_SCS_IMCLK 0x00000000
86#define TXX9_SILCR_SCS_IMCLK_BG 0x00000020
87#define TXX9_SILCR_SCS_SCLK 0x00000040
88#define TXX9_SILCR_SCS_SCLK_BG 0x00000060
89#define TXX9_SILCR_UEPS 0x00000010
90#define TXX9_SILCR_UPEN 0x00000008
91#define TXX9_SILCR_USBL_MASK 0x00000004
92#define TXX9_SILCR_USBL_1BIT 0x00000000
93#define TXX9_SILCR_USBL_2BIT 0x00000004
94#define TXX9_SILCR_UMODE_MASK 0x00000003
95#define TXX9_SILCR_UMODE_8BIT 0x00000000
96#define TXX9_SILCR_UMODE_7BIT 0x00000001
97
98/* SIDICR : DMA/Int. Control */
99#define TXX9_SIDICR_TDE 0x00008000
100#define TXX9_SIDICR_RDE 0x00004000
101#define TXX9_SIDICR_TIE 0x00002000
102#define TXX9_SIDICR_RIE 0x00001000
103#define TXX9_SIDICR_SPIE 0x00000800
104#define TXX9_SIDICR_CTSAC 0x00000600
105#define TXX9_SIDICR_STIE_MASK 0x0000003f
106#define TXX9_SIDICR_STIE_OERS 0x00000020
107#define TXX9_SIDICR_STIE_CTSS 0x00000010
108#define TXX9_SIDICR_STIE_RBRKD 0x00000008
109#define TXX9_SIDICR_STIE_TRDY 0x00000004
110#define TXX9_SIDICR_STIE_TXALS 0x00000002
111#define TXX9_SIDICR_STIE_UBRKD 0x00000001
112
113/* SIDISR : DMA/Int. Status */
114#define TXX9_SIDISR_UBRK 0x00008000
115#define TXX9_SIDISR_UVALID 0x00004000
116#define TXX9_SIDISR_UFER 0x00002000
117#define TXX9_SIDISR_UPER 0x00001000
118#define TXX9_SIDISR_UOER 0x00000800
119#define TXX9_SIDISR_ERI 0x00000400
120#define TXX9_SIDISR_TOUT 0x00000200
121#define TXX9_SIDISR_TDIS 0x00000100
122#define TXX9_SIDISR_RDIS 0x00000080
123#define TXX9_SIDISR_STIS 0x00000040
124#define TXX9_SIDISR_RFDN_MASK 0x0000001f
125
126/* SICISR : Change Int. Status */
127#define TXX9_SICISR_OERS 0x00000020
128#define TXX9_SICISR_CTSS 0x00000010
129#define TXX9_SICISR_RBRKD 0x00000008
130#define TXX9_SICISR_TRDY 0x00000004
131#define TXX9_SICISR_TXALS 0x00000002
132#define TXX9_SICISR_UBRKD 0x00000001
133
134/* SIFCR : FIFO Control */
135#define TXX9_SIFCR_SWRST 0x00008000
136#define TXX9_SIFCR_RDIL_MASK 0x00000180
137#define TXX9_SIFCR_RDIL_1 0x00000000
138#define TXX9_SIFCR_RDIL_4 0x00000080
139#define TXX9_SIFCR_RDIL_8 0x00000100
140#define TXX9_SIFCR_RDIL_12 0x00000180
141#define TXX9_SIFCR_RDIL_MAX 0x00000180
142#define TXX9_SIFCR_TDIL_MASK 0x00000018
143#define TXX9_SIFCR_TDIL_MASK 0x00000018
144#define TXX9_SIFCR_TDIL_1 0x00000000
145#define TXX9_SIFCR_TDIL_4 0x00000001
146#define TXX9_SIFCR_TDIL_8 0x00000010
147#define TXX9_SIFCR_TDIL_MAX 0x00000010
148#define TXX9_SIFCR_TFRST 0x00000004
149#define TXX9_SIFCR_RFRST 0x00000002
150#define TXX9_SIFCR_FRSTE 0x00000001
151#define TXX9_SIO_TX_FIFO 8
152#define TXX9_SIO_RX_FIFO 16
153
154/* SIFLCR : Flow Control */
155#define TXX9_SIFLCR_RCS 0x00001000
156#define TXX9_SIFLCR_TES 0x00000800
157#define TXX9_SIFLCR_RTSSC 0x00000200
158#define TXX9_SIFLCR_RSDE 0x00000100
159#define TXX9_SIFLCR_TSDE 0x00000080
160#define TXX9_SIFLCR_RTSTL_MASK 0x0000001e
161#define TXX9_SIFLCR_RTSTL_MAX 0x0000001e
162#define TXX9_SIFLCR_TBRK 0x00000001
163
164/* SIBGR : Baudrate Control */
165#define TXX9_SIBGR_BCLK_MASK 0x00000300
166#define TXX9_SIBGR_BCLK_T0 0x00000000
167#define TXX9_SIBGR_BCLK_T2 0x00000100
168#define TXX9_SIBGR_BCLK_T4 0x00000200
169#define TXX9_SIBGR_BCLK_T6 0x00000300
170#define TXX9_SIBGR_BRD_MASK 0x000000ff
171
172static inline unsigned int sio_in(struct uart_txx9_port *up, int offset)
173{
174 switch (up->port.iotype) {
175 default:
176 return __raw_readl(up->port.membase + offset);
177 case UPIO_PORT:
178 return inl(up->port.iobase + offset);
179 }
180}
181
182static inline void
183sio_out(struct uart_txx9_port *up, int offset, int value)
184{
185 switch (up->port.iotype) {
186 default:
187 __raw_writel(value, up->port.membase + offset);
188 break;
189 case UPIO_PORT:
190 outl(value, up->port.iobase + offset);
191 break;
192 }
193}
194
195static inline void
196sio_mask(struct uart_txx9_port *up, int offset, unsigned int value)
197{
198 sio_out(up, offset, sio_in(up, offset) & ~value);
199}
200static inline void
201sio_set(struct uart_txx9_port *up, int offset, unsigned int value)
202{
203 sio_out(up, offset, sio_in(up, offset) | value);
204}
205
206static inline void
207sio_quot_set(struct uart_txx9_port *up, int quot)
208{
209 quot >>= 1;
210 if (quot < 256)
211 sio_out(up, TXX9_SIBGR, quot | TXX9_SIBGR_BCLK_T0);
212 else if (quot < (256 << 2))
213 sio_out(up, TXX9_SIBGR, (quot >> 2) | TXX9_SIBGR_BCLK_T2);
214 else if (quot < (256 << 4))
215 sio_out(up, TXX9_SIBGR, (quot >> 4) | TXX9_SIBGR_BCLK_T4);
216 else if (quot < (256 << 6))
217 sio_out(up, TXX9_SIBGR, (quot >> 6) | TXX9_SIBGR_BCLK_T6);
218 else
219 sio_out(up, TXX9_SIBGR, 0xff | TXX9_SIBGR_BCLK_T6);
220}
221
222static struct uart_txx9_port *to_uart_txx9_port(struct uart_port *port)
223{
224 return container_of(port, struct uart_txx9_port, port);
225}
226
227static void serial_txx9_stop_tx(struct uart_port *port)
228{
229 struct uart_txx9_port *up = to_uart_txx9_port(port);
230 sio_mask(up, TXX9_SIDICR, TXX9_SIDICR_TIE);
231}
232
233static void serial_txx9_start_tx(struct uart_port *port)
234{
235 struct uart_txx9_port *up = to_uart_txx9_port(port);
236 sio_set(up, TXX9_SIDICR, TXX9_SIDICR_TIE);
237}
238
239static void serial_txx9_stop_rx(struct uart_port *port)
240{
241 struct uart_txx9_port *up = to_uart_txx9_port(port);
242 up->port.read_status_mask &= ~TXX9_SIDISR_RDIS;
243}
244
245static void serial_txx9_enable_ms(struct uart_port *port)
246{
247 /* TXX9-SIO can not control DTR... */
248}
249
250static void serial_txx9_initialize(struct uart_port *port)
251{
252 struct uart_txx9_port *up = to_uart_txx9_port(port);
253 unsigned int tmout = 10000;
254
255 sio_out(up, TXX9_SIFCR, TXX9_SIFCR_SWRST);
256 /* TX4925 BUG WORKAROUND. Accessing SIOC register
257 * immediately after soft reset causes bus error. */
258 mmiowb();
259 udelay(1);
260 while ((sio_in(up, TXX9_SIFCR) & TXX9_SIFCR_SWRST) && --tmout)
261 udelay(1);
262 /* TX Int by FIFO Empty, RX Int by Receiving 1 char. */
263 sio_set(up, TXX9_SIFCR,
264 TXX9_SIFCR_TDIL_MAX | TXX9_SIFCR_RDIL_1);
265 /* initial settings */
266 sio_out(up, TXX9_SILCR,
267 TXX9_SILCR_UMODE_8BIT | TXX9_SILCR_USBL_1BIT |
268 ((up->port.flags & UPF_TXX9_USE_SCLK) ?
269 TXX9_SILCR_SCS_SCLK_BG : TXX9_SILCR_SCS_IMCLK_BG));
270 sio_quot_set(up, uart_get_divisor(port, 9600));
271 sio_out(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSTL_MAX /* 15 */);
272 sio_out(up, TXX9_SIDICR, 0);
273}
274
275static inline void
276receive_chars(struct uart_txx9_port *up, unsigned int *status)
277{
278 struct tty_struct *tty = up->port.state->port.tty;
279 unsigned char ch;
280 unsigned int disr = *status;
281 int max_count = 256;
282 char flag;
283 unsigned int next_ignore_status_mask;
284
285 do {
286 ch = sio_in(up, TXX9_SIRFIFO);
287 flag = TTY_NORMAL;
288 up->port.icount.rx++;
289
290 /* mask out RFDN_MASK bit added by previous overrun */
291 next_ignore_status_mask =
292 up->port.ignore_status_mask & ~TXX9_SIDISR_RFDN_MASK;
293 if (unlikely(disr & (TXX9_SIDISR_UBRK | TXX9_SIDISR_UPER |
294 TXX9_SIDISR_UFER | TXX9_SIDISR_UOER))) {
295 /*
296 * For statistics only
297 */
298 if (disr & TXX9_SIDISR_UBRK) {
299 disr &= ~(TXX9_SIDISR_UFER | TXX9_SIDISR_UPER);
300 up->port.icount.brk++;
301 /*
302 * We do the SysRQ and SAK checking
303 * here because otherwise the break
304 * may get masked by ignore_status_mask
305 * or read_status_mask.
306 */
307 if (uart_handle_break(&up->port))
308 goto ignore_char;
309 } else if (disr & TXX9_SIDISR_UPER)
310 up->port.icount.parity++;
311 else if (disr & TXX9_SIDISR_UFER)
312 up->port.icount.frame++;
313 if (disr & TXX9_SIDISR_UOER) {
314 up->port.icount.overrun++;
315 /*
316 * The receiver read buffer still hold
317 * a char which caused overrun.
318 * Ignore next char by adding RFDN_MASK
319 * to ignore_status_mask temporarily.
320 */
321 next_ignore_status_mask |=
322 TXX9_SIDISR_RFDN_MASK;
323 }
324
325 /*
326 * Mask off conditions which should be ingored.
327 */
328 disr &= up->port.read_status_mask;
329
330 if (disr & TXX9_SIDISR_UBRK) {
331 flag = TTY_BREAK;
332 } else if (disr & TXX9_SIDISR_UPER)
333 flag = TTY_PARITY;
334 else if (disr & TXX9_SIDISR_UFER)
335 flag = TTY_FRAME;
336 }
337 if (uart_handle_sysrq_char(&up->port, ch))
338 goto ignore_char;
339
340 uart_insert_char(&up->port, disr, TXX9_SIDISR_UOER, ch, flag);
341
342 ignore_char:
343 up->port.ignore_status_mask = next_ignore_status_mask;
344 disr = sio_in(up, TXX9_SIDISR);
345 } while (!(disr & TXX9_SIDISR_UVALID) && (max_count-- > 0));
346 spin_unlock(&up->port.lock);
347 tty_flip_buffer_push(tty);
348 spin_lock(&up->port.lock);
349 *status = disr;
350}
351
352static inline void transmit_chars(struct uart_txx9_port *up)
353{
354 struct circ_buf *xmit = &up->port.state->xmit;
355 int count;
356
357 if (up->port.x_char) {
358 sio_out(up, TXX9_SITFIFO, up->port.x_char);
359 up->port.icount.tx++;
360 up->port.x_char = 0;
361 return;
362 }
363 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
364 serial_txx9_stop_tx(&up->port);
365 return;
366 }
367
368 count = TXX9_SIO_TX_FIFO;
369 do {
370 sio_out(up, TXX9_SITFIFO, xmit->buf[xmit->tail]);
371 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
372 up->port.icount.tx++;
373 if (uart_circ_empty(xmit))
374 break;
375 } while (--count > 0);
376
377 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
378 uart_write_wakeup(&up->port);
379
380 if (uart_circ_empty(xmit))
381 serial_txx9_stop_tx(&up->port);
382}
383
384static irqreturn_t serial_txx9_interrupt(int irq, void *dev_id)
385{
386 int pass_counter = 0;
387 struct uart_txx9_port *up = dev_id;
388 unsigned int status;
389
390 while (1) {
391 spin_lock(&up->port.lock);
392 status = sio_in(up, TXX9_SIDISR);
393 if (!(sio_in(up, TXX9_SIDICR) & TXX9_SIDICR_TIE))
394 status &= ~TXX9_SIDISR_TDIS;
395 if (!(status & (TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS |
396 TXX9_SIDISR_TOUT))) {
397 spin_unlock(&up->port.lock);
398 break;
399 }
400
401 if (status & TXX9_SIDISR_RDIS)
402 receive_chars(up, &status);
403 if (status & TXX9_SIDISR_TDIS)
404 transmit_chars(up);
405 /* Clear TX/RX Int. Status */
406 sio_mask(up, TXX9_SIDISR,
407 TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS |
408 TXX9_SIDISR_TOUT);
409 spin_unlock(&up->port.lock);
410
411 if (pass_counter++ > PASS_LIMIT)
412 break;
413 }
414
415 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
416}
417
418static unsigned int serial_txx9_tx_empty(struct uart_port *port)
419{
420 struct uart_txx9_port *up = to_uart_txx9_port(port);
421 unsigned long flags;
422 unsigned int ret;
423
424 spin_lock_irqsave(&up->port.lock, flags);
425 ret = (sio_in(up, TXX9_SICISR) & TXX9_SICISR_TXALS) ? TIOCSER_TEMT : 0;
426 spin_unlock_irqrestore(&up->port.lock, flags);
427
428 return ret;
429}
430
431static unsigned int serial_txx9_get_mctrl(struct uart_port *port)
432{
433 struct uart_txx9_port *up = to_uart_txx9_port(port);
434 unsigned int ret;
435
436 /* no modem control lines */
437 ret = TIOCM_CAR | TIOCM_DSR;
438 ret |= (sio_in(up, TXX9_SIFLCR) & TXX9_SIFLCR_RTSSC) ? 0 : TIOCM_RTS;
439 ret |= (sio_in(up, TXX9_SICISR) & TXX9_SICISR_CTSS) ? 0 : TIOCM_CTS;
440
441 return ret;
442}
443
444static void serial_txx9_set_mctrl(struct uart_port *port, unsigned int mctrl)
445{
446 struct uart_txx9_port *up = to_uart_txx9_port(port);
447
448 if (mctrl & TIOCM_RTS)
449 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSSC);
450 else
451 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_RTSSC);
452}
453
454static void serial_txx9_break_ctl(struct uart_port *port, int break_state)
455{
456 struct uart_txx9_port *up = to_uart_txx9_port(port);
457 unsigned long flags;
458
459 spin_lock_irqsave(&up->port.lock, flags);
460 if (break_state == -1)
461 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
462 else
463 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
464 spin_unlock_irqrestore(&up->port.lock, flags);
465}
466
467#if defined(CONFIG_SERIAL_TXX9_CONSOLE) || (CONFIG_CONSOLE_POLL)
468/*
469 * Wait for transmitter & holding register to empty
470 */
471static void wait_for_xmitr(struct uart_txx9_port *up)
472{
473 unsigned int tmout = 10000;
474
475 /* Wait up to 10ms for the character(s) to be sent. */
476 while (--tmout &&
477 !(sio_in(up, TXX9_SICISR) & TXX9_SICISR_TXALS))
478 udelay(1);
479
480 /* Wait up to 1s for flow control if necessary */
481 if (up->port.flags & UPF_CONS_FLOW) {
482 tmout = 1000000;
483 while (--tmout &&
484 (sio_in(up, TXX9_SICISR) & TXX9_SICISR_CTSS))
485 udelay(1);
486 }
487}
488#endif
489
490#ifdef CONFIG_CONSOLE_POLL
491/*
492 * Console polling routines for writing and reading from the uart while
493 * in an interrupt or debug context.
494 */
495
496static int serial_txx9_get_poll_char(struct uart_port *port)
497{
498 unsigned int ier;
499 unsigned char c;
500 struct uart_txx9_port *up = to_uart_txx9_port(port);
501
502 /*
503 * First save the IER then disable the interrupts
504 */
505 ier = sio_in(up, TXX9_SIDICR);
506 sio_out(up, TXX9_SIDICR, 0);
507
508 while (sio_in(up, TXX9_SIDISR) & TXX9_SIDISR_UVALID)
509 ;
510
511 c = sio_in(up, TXX9_SIRFIFO);
512
513 /*
514 * Finally, clear RX interrupt status
515 * and restore the IER
516 */
517 sio_mask(up, TXX9_SIDISR, TXX9_SIDISR_RDIS);
518 sio_out(up, TXX9_SIDICR, ier);
519 return c;
520}
521
522
523static void serial_txx9_put_poll_char(struct uart_port *port, unsigned char c)
524{
525 unsigned int ier;
526 struct uart_txx9_port *up = to_uart_txx9_port(port);
527
528 /*
529 * First save the IER then disable the interrupts
530 */
531 ier = sio_in(up, TXX9_SIDICR);
532 sio_out(up, TXX9_SIDICR, 0);
533
534 wait_for_xmitr(up);
535 /*
536 * Send the character out.
537 * If a LF, also do CR...
538 */
539 sio_out(up, TXX9_SITFIFO, c);
540 if (c == 10) {
541 wait_for_xmitr(up);
542 sio_out(up, TXX9_SITFIFO, 13);
543 }
544
545 /*
546 * Finally, wait for transmitter to become empty
547 * and restore the IER
548 */
549 wait_for_xmitr(up);
550 sio_out(up, TXX9_SIDICR, ier);
551}
552
553#endif /* CONFIG_CONSOLE_POLL */
554
555static int serial_txx9_startup(struct uart_port *port)
556{
557 struct uart_txx9_port *up = to_uart_txx9_port(port);
558 unsigned long flags;
559 int retval;
560
561 /*
562 * Clear the FIFO buffers and disable them.
563 * (they will be reenabled in set_termios())
564 */
565 sio_set(up, TXX9_SIFCR,
566 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
567 /* clear reset */
568 sio_mask(up, TXX9_SIFCR,
569 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
570 sio_out(up, TXX9_SIDICR, 0);
571
572 /*
573 * Clear the interrupt registers.
574 */
575 sio_out(up, TXX9_SIDISR, 0);
576
577 retval = request_irq(up->port.irq, serial_txx9_interrupt,
578 IRQF_SHARED, "serial_txx9", up);
579 if (retval)
580 return retval;
581
582 /*
583 * Now, initialize the UART
584 */
585 spin_lock_irqsave(&up->port.lock, flags);
586 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
587 spin_unlock_irqrestore(&up->port.lock, flags);
588
589 /* Enable RX/TX */
590 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_RSDE | TXX9_SIFLCR_TSDE);
591
592 /*
593 * Finally, enable interrupts.
594 */
595 sio_set(up, TXX9_SIDICR, TXX9_SIDICR_RIE);
596
597 return 0;
598}
599
600static void serial_txx9_shutdown(struct uart_port *port)
601{
602 struct uart_txx9_port *up = to_uart_txx9_port(port);
603 unsigned long flags;
604
605 /*
606 * Disable interrupts from this port
607 */
608 sio_out(up, TXX9_SIDICR, 0); /* disable all intrs */
609
610 spin_lock_irqsave(&up->port.lock, flags);
611 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
612 spin_unlock_irqrestore(&up->port.lock, flags);
613
614 /*
615 * Disable break condition
616 */
617 sio_mask(up, TXX9_SIFLCR, TXX9_SIFLCR_TBRK);
618
619#ifdef CONFIG_SERIAL_TXX9_CONSOLE
620 if (up->port.cons && up->port.line == up->port.cons->index) {
621 free_irq(up->port.irq, up);
622 return;
623 }
624#endif
625 /* reset FIFOs */
626 sio_set(up, TXX9_SIFCR,
627 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
628 /* clear reset */
629 sio_mask(up, TXX9_SIFCR,
630 TXX9_SIFCR_TFRST | TXX9_SIFCR_RFRST | TXX9_SIFCR_FRSTE);
631
632 /* Disable RX/TX */
633 sio_set(up, TXX9_SIFLCR, TXX9_SIFLCR_RSDE | TXX9_SIFLCR_TSDE);
634
635 free_irq(up->port.irq, up);
636}
637
638static void
639serial_txx9_set_termios(struct uart_port *port, struct ktermios *termios,
640 struct ktermios *old)
641{
642 struct uart_txx9_port *up = to_uart_txx9_port(port);
643 unsigned int cval, fcr = 0;
644 unsigned long flags;
645 unsigned int baud, quot;
646
647 /*
648 * We don't support modem control lines.
649 */
650 termios->c_cflag &= ~(HUPCL | CMSPAR);
651 termios->c_cflag |= CLOCAL;
652
653 cval = sio_in(up, TXX9_SILCR);
654 /* byte size and parity */
655 cval &= ~TXX9_SILCR_UMODE_MASK;
656 switch (termios->c_cflag & CSIZE) {
657 case CS7:
658 cval |= TXX9_SILCR_UMODE_7BIT;
659 break;
660 default:
661 case CS5: /* not supported */
662 case CS6: /* not supported */
663 case CS8:
664 cval |= TXX9_SILCR_UMODE_8BIT;
665 break;
666 }
667
668 cval &= ~TXX9_SILCR_USBL_MASK;
669 if (termios->c_cflag & CSTOPB)
670 cval |= TXX9_SILCR_USBL_2BIT;
671 else
672 cval |= TXX9_SILCR_USBL_1BIT;
673 cval &= ~(TXX9_SILCR_UPEN | TXX9_SILCR_UEPS);
674 if (termios->c_cflag & PARENB)
675 cval |= TXX9_SILCR_UPEN;
676 if (!(termios->c_cflag & PARODD))
677 cval |= TXX9_SILCR_UEPS;
678
679 /*
680 * Ask the core to calculate the divisor for us.
681 */
682 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16/2);
683 quot = uart_get_divisor(port, baud);
684
685 /* Set up FIFOs */
686 /* TX Int by FIFO Empty, RX Int by Receiving 1 char. */
687 fcr = TXX9_SIFCR_TDIL_MAX | TXX9_SIFCR_RDIL_1;
688
689 /*
690 * Ok, we're now changing the port state. Do it with
691 * interrupts disabled.
692 */
693 spin_lock_irqsave(&up->port.lock, flags);
694
695 /*
696 * Update the per-port timeout.
697 */
698 uart_update_timeout(port, termios->c_cflag, baud);
699
700 up->port.read_status_mask = TXX9_SIDISR_UOER |
701 TXX9_SIDISR_TDIS | TXX9_SIDISR_RDIS;
702 if (termios->c_iflag & INPCK)
703 up->port.read_status_mask |= TXX9_SIDISR_UFER | TXX9_SIDISR_UPER;
704 if (termios->c_iflag & (BRKINT | PARMRK))
705 up->port.read_status_mask |= TXX9_SIDISR_UBRK;
706
707 /*
708 * Characteres to ignore
709 */
710 up->port.ignore_status_mask = 0;
711 if (termios->c_iflag & IGNPAR)
712 up->port.ignore_status_mask |= TXX9_SIDISR_UPER | TXX9_SIDISR_UFER;
713 if (termios->c_iflag & IGNBRK) {
714 up->port.ignore_status_mask |= TXX9_SIDISR_UBRK;
715 /*
716 * If we're ignoring parity and break indicators,
717 * ignore overruns too (for real raw support).
718 */
719 if (termios->c_iflag & IGNPAR)
720 up->port.ignore_status_mask |= TXX9_SIDISR_UOER;
721 }
722
723 /*
724 * ignore all characters if CREAD is not set
725 */
726 if ((termios->c_cflag & CREAD) == 0)
727 up->port.ignore_status_mask |= TXX9_SIDISR_RDIS;
728
729 /* CTS flow control flag */
730 if ((termios->c_cflag & CRTSCTS) &&
731 (up->port.flags & UPF_TXX9_HAVE_CTS_LINE)) {
732 sio_set(up, TXX9_SIFLCR,
733 TXX9_SIFLCR_RCS | TXX9_SIFLCR_TES);
734 } else {
735 sio_mask(up, TXX9_SIFLCR,
736 TXX9_SIFLCR_RCS | TXX9_SIFLCR_TES);
737 }
738
739 sio_out(up, TXX9_SILCR, cval);
740 sio_quot_set(up, quot);
741 sio_out(up, TXX9_SIFCR, fcr);
742
743 serial_txx9_set_mctrl(&up->port, up->port.mctrl);
744 spin_unlock_irqrestore(&up->port.lock, flags);
745}
746
747static void
748serial_txx9_pm(struct uart_port *port, unsigned int state,
749 unsigned int oldstate)
750{
751 /*
752 * If oldstate was -1 this is called from
753 * uart_configure_port(). In this case do not initialize the
754 * port now, because the port was already initialized (for
755 * non-console port) or should not be initialized here (for
756 * console port). If we initialized the port here we lose
757 * serial console settings.
758 */
759 if (state == 0 && oldstate != -1)
760 serial_txx9_initialize(port);
761}
762
763static int serial_txx9_request_resource(struct uart_txx9_port *up)
764{
765 unsigned int size = TXX9_REGION_SIZE;
766 int ret = 0;
767
768 switch (up->port.iotype) {
769 default:
770 if (!up->port.mapbase)
771 break;
772
773 if (!request_mem_region(up->port.mapbase, size, "serial_txx9")) {
774 ret = -EBUSY;
775 break;
776 }
777
778 if (up->port.flags & UPF_IOREMAP) {
779 up->port.membase = ioremap(up->port.mapbase, size);
780 if (!up->port.membase) {
781 release_mem_region(up->port.mapbase, size);
782 ret = -ENOMEM;
783 }
784 }
785 break;
786
787 case UPIO_PORT:
788 if (!request_region(up->port.iobase, size, "serial_txx9"))
789 ret = -EBUSY;
790 break;
791 }
792 return ret;
793}
794
795static void serial_txx9_release_resource(struct uart_txx9_port *up)
796{
797 unsigned int size = TXX9_REGION_SIZE;
798
799 switch (up->port.iotype) {
800 default:
801 if (!up->port.mapbase)
802 break;
803
804 if (up->port.flags & UPF_IOREMAP) {
805 iounmap(up->port.membase);
806 up->port.membase = NULL;
807 }
808
809 release_mem_region(up->port.mapbase, size);
810 break;
811
812 case UPIO_PORT:
813 release_region(up->port.iobase, size);
814 break;
815 }
816}
817
818static void serial_txx9_release_port(struct uart_port *port)
819{
820 struct uart_txx9_port *up = to_uart_txx9_port(port);
821 serial_txx9_release_resource(up);
822}
823
824static int serial_txx9_request_port(struct uart_port *port)
825{
826 struct uart_txx9_port *up = to_uart_txx9_port(port);
827 return serial_txx9_request_resource(up);
828}
829
830static void serial_txx9_config_port(struct uart_port *port, int uflags)
831{
832 struct uart_txx9_port *up = to_uart_txx9_port(port);
833 int ret;
834
835 /*
836 * Find the region that we can probe for. This in turn
837 * tells us whether we can probe for the type of port.
838 */
839 ret = serial_txx9_request_resource(up);
840 if (ret < 0)
841 return;
842 port->type = PORT_TXX9;
843 up->port.fifosize = TXX9_SIO_TX_FIFO;
844
845#ifdef CONFIG_SERIAL_TXX9_CONSOLE
846 if (up->port.line == up->port.cons->index)
847 return;
848#endif
849 serial_txx9_initialize(port);
850}
851
852static const char *
853serial_txx9_type(struct uart_port *port)
854{
855 return "txx9";
856}
857
858static struct uart_ops serial_txx9_pops = {
859 .tx_empty = serial_txx9_tx_empty,
860 .set_mctrl = serial_txx9_set_mctrl,
861 .get_mctrl = serial_txx9_get_mctrl,
862 .stop_tx = serial_txx9_stop_tx,
863 .start_tx = serial_txx9_start_tx,
864 .stop_rx = serial_txx9_stop_rx,
865 .enable_ms = serial_txx9_enable_ms,
866 .break_ctl = serial_txx9_break_ctl,
867 .startup = serial_txx9_startup,
868 .shutdown = serial_txx9_shutdown,
869 .set_termios = serial_txx9_set_termios,
870 .pm = serial_txx9_pm,
871 .type = serial_txx9_type,
872 .release_port = serial_txx9_release_port,
873 .request_port = serial_txx9_request_port,
874 .config_port = serial_txx9_config_port,
875#ifdef CONFIG_CONSOLE_POLL
876 .poll_get_char = serial_txx9_get_poll_char,
877 .poll_put_char = serial_txx9_put_poll_char,
878#endif
879};
880
881static struct uart_txx9_port serial_txx9_ports[UART_NR];
882
883static void __init serial_txx9_register_ports(struct uart_driver *drv,
884 struct device *dev)
885{
886 int i;
887
888 for (i = 0; i < UART_NR; i++) {
889 struct uart_txx9_port *up = &serial_txx9_ports[i];
890
891 up->port.line = i;
892 up->port.ops = &serial_txx9_pops;
893 up->port.dev = dev;
894 if (up->port.iobase || up->port.mapbase)
895 uart_add_one_port(drv, &up->port);
896 }
897}
898
899#ifdef CONFIG_SERIAL_TXX9_CONSOLE
900
901static void serial_txx9_console_putchar(struct uart_port *port, int ch)
902{
903 struct uart_txx9_port *up = to_uart_txx9_port(port);
904
905 wait_for_xmitr(up);
906 sio_out(up, TXX9_SITFIFO, ch);
907}
908
909/*
910 * Print a string to the serial port trying not to disturb
911 * any possible real use of the port...
912 *
913 * The console_lock must be held when we get here.
914 */
915static void
916serial_txx9_console_write(struct console *co, const char *s, unsigned int count)
917{
918 struct uart_txx9_port *up = &serial_txx9_ports[co->index];
919 unsigned int ier, flcr;
920
921 /*
922 * First save the UER then disable the interrupts
923 */
924 ier = sio_in(up, TXX9_SIDICR);
925 sio_out(up, TXX9_SIDICR, 0);
926 /*
927 * Disable flow-control if enabled (and unnecessary)
928 */
929 flcr = sio_in(up, TXX9_SIFLCR);
930 if (!(up->port.flags & UPF_CONS_FLOW) && (flcr & TXX9_SIFLCR_TES))
931 sio_out(up, TXX9_SIFLCR, flcr & ~TXX9_SIFLCR_TES);
932
933 uart_console_write(&up->port, s, count, serial_txx9_console_putchar);
934
935 /*
936 * Finally, wait for transmitter to become empty
937 * and restore the IER
938 */
939 wait_for_xmitr(up);
940 sio_out(up, TXX9_SIFLCR, flcr);
941 sio_out(up, TXX9_SIDICR, ier);
942}
943
944static int __init serial_txx9_console_setup(struct console *co, char *options)
945{
946 struct uart_port *port;
947 struct uart_txx9_port *up;
948 int baud = 9600;
949 int bits = 8;
950 int parity = 'n';
951 int flow = 'n';
952
953 /*
954 * Check whether an invalid uart number has been specified, and
955 * if so, search for the first available port that does have
956 * console support.
957 */
958 if (co->index >= UART_NR)
959 co->index = 0;
960 up = &serial_txx9_ports[co->index];
961 port = &up->port;
962 if (!port->ops)
963 return -ENODEV;
964
965 serial_txx9_initialize(&up->port);
966
967 if (options)
968 uart_parse_options(options, &baud, &parity, &bits, &flow);
969
970 return uart_set_options(port, co, baud, parity, bits, flow);
971}
972
973static struct uart_driver serial_txx9_reg;
974static struct console serial_txx9_console = {
975 .name = TXX9_TTY_NAME,
976 .write = serial_txx9_console_write,
977 .device = uart_console_device,
978 .setup = serial_txx9_console_setup,
979 .flags = CON_PRINTBUFFER,
980 .index = -1,
981 .data = &serial_txx9_reg,
982};
983
984static int __init serial_txx9_console_init(void)
985{
986 register_console(&serial_txx9_console);
987 return 0;
988}
989console_initcall(serial_txx9_console_init);
990
991#define SERIAL_TXX9_CONSOLE &serial_txx9_console
992#else
993#define SERIAL_TXX9_CONSOLE NULL
994#endif
995
996static struct uart_driver serial_txx9_reg = {
997 .owner = THIS_MODULE,
998 .driver_name = "serial_txx9",
999 .dev_name = TXX9_TTY_NAME,
1000 .major = TXX9_TTY_MAJOR,
1001 .minor = TXX9_TTY_MINOR_START,
1002 .nr = UART_NR,
1003 .cons = SERIAL_TXX9_CONSOLE,
1004};
1005
1006int __init early_serial_txx9_setup(struct uart_port *port)
1007{
1008 if (port->line >= ARRAY_SIZE(serial_txx9_ports))
1009 return -ENODEV;
1010
1011 serial_txx9_ports[port->line].port = *port;
1012 serial_txx9_ports[port->line].port.ops = &serial_txx9_pops;
1013 serial_txx9_ports[port->line].port.flags |=
1014 UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
1015 return 0;
1016}
1017
1018static DEFINE_MUTEX(serial_txx9_mutex);
1019
1020/**
1021 * serial_txx9_register_port - register a serial port
1022 * @port: serial port template
1023 *
1024 * Configure the serial port specified by the request.
1025 *
1026 * The port is then probed and if necessary the IRQ is autodetected
1027 * If this fails an error is returned.
1028 *
1029 * On success the port is ready to use and the line number is returned.
1030 */
1031static int __devinit serial_txx9_register_port(struct uart_port *port)
1032{
1033 int i;
1034 struct uart_txx9_port *uart;
1035 int ret = -ENOSPC;
1036
1037 mutex_lock(&serial_txx9_mutex);
1038 for (i = 0; i < UART_NR; i++) {
1039 uart = &serial_txx9_ports[i];
1040 if (uart_match_port(&uart->port, port)) {
1041 uart_remove_one_port(&serial_txx9_reg, &uart->port);
1042 break;
1043 }
1044 }
1045 if (i == UART_NR) {
1046 /* Find unused port */
1047 for (i = 0; i < UART_NR; i++) {
1048 uart = &serial_txx9_ports[i];
1049 if (!(uart->port.iobase || uart->port.mapbase))
1050 break;
1051 }
1052 }
1053 if (i < UART_NR) {
1054 uart->port.iobase = port->iobase;
1055 uart->port.membase = port->membase;
1056 uart->port.irq = port->irq;
1057 uart->port.uartclk = port->uartclk;
1058 uart->port.iotype = port->iotype;
1059 uart->port.flags = port->flags
1060 | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT;
1061 uart->port.mapbase = port->mapbase;
1062 if (port->dev)
1063 uart->port.dev = port->dev;
1064 ret = uart_add_one_port(&serial_txx9_reg, &uart->port);
1065 if (ret == 0)
1066 ret = uart->port.line;
1067 }
1068 mutex_unlock(&serial_txx9_mutex);
1069 return ret;
1070}
1071
1072/**
1073 * serial_txx9_unregister_port - remove a txx9 serial port at runtime
1074 * @line: serial line number
1075 *
1076 * Remove one serial port. This may not be called from interrupt
1077 * context. We hand the port back to the our control.
1078 */
1079static void __devexit serial_txx9_unregister_port(int line)
1080{
1081 struct uart_txx9_port *uart = &serial_txx9_ports[line];
1082
1083 mutex_lock(&serial_txx9_mutex);
1084 uart_remove_one_port(&serial_txx9_reg, &uart->port);
1085 uart->port.flags = 0;
1086 uart->port.type = PORT_UNKNOWN;
1087 uart->port.iobase = 0;
1088 uart->port.mapbase = 0;
1089 uart->port.membase = NULL;
1090 uart->port.dev = NULL;
1091 mutex_unlock(&serial_txx9_mutex);
1092}
1093
1094/*
1095 * Register a set of serial devices attached to a platform device.
1096 */
1097static int __devinit serial_txx9_probe(struct platform_device *dev)
1098{
1099 struct uart_port *p = dev->dev.platform_data;
1100 struct uart_port port;
1101 int ret, i;
1102
1103 memset(&port, 0, sizeof(struct uart_port));
1104 for (i = 0; p && p->uartclk != 0; p++, i++) {
1105 port.iobase = p->iobase;
1106 port.membase = p->membase;
1107 port.irq = p->irq;
1108 port.uartclk = p->uartclk;
1109 port.iotype = p->iotype;
1110 port.flags = p->flags;
1111 port.mapbase = p->mapbase;
1112 port.dev = &dev->dev;
1113 ret = serial_txx9_register_port(&port);
1114 if (ret < 0) {
1115 dev_err(&dev->dev, "unable to register port at index %d "
1116 "(IO%lx MEM%llx IRQ%d): %d\n", i,
1117 p->iobase, (unsigned long long)p->mapbase,
1118 p->irq, ret);
1119 }
1120 }
1121 return 0;
1122}
1123
1124/*
1125 * Remove serial ports registered against a platform device.
1126 */
1127static int __devexit serial_txx9_remove(struct platform_device *dev)
1128{
1129 int i;
1130
1131 for (i = 0; i < UART_NR; i++) {
1132 struct uart_txx9_port *up = &serial_txx9_ports[i];
1133
1134 if (up->port.dev == &dev->dev)
1135 serial_txx9_unregister_port(i);
1136 }
1137 return 0;
1138}
1139
1140#ifdef CONFIG_PM
1141static int serial_txx9_suspend(struct platform_device *dev, pm_message_t state)
1142{
1143 int i;
1144
1145 for (i = 0; i < UART_NR; i++) {
1146 struct uart_txx9_port *up = &serial_txx9_ports[i];
1147
1148 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
1149 uart_suspend_port(&serial_txx9_reg, &up->port);
1150 }
1151
1152 return 0;
1153}
1154
1155static int serial_txx9_resume(struct platform_device *dev)
1156{
1157 int i;
1158
1159 for (i = 0; i < UART_NR; i++) {
1160 struct uart_txx9_port *up = &serial_txx9_ports[i];
1161
1162 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
1163 uart_resume_port(&serial_txx9_reg, &up->port);
1164 }
1165
1166 return 0;
1167}
1168#endif
1169
1170static struct platform_driver serial_txx9_plat_driver = {
1171 .probe = serial_txx9_probe,
1172 .remove = __devexit_p(serial_txx9_remove),
1173#ifdef CONFIG_PM
1174 .suspend = serial_txx9_suspend,
1175 .resume = serial_txx9_resume,
1176#endif
1177 .driver = {
1178 .name = "serial_txx9",
1179 .owner = THIS_MODULE,
1180 },
1181};
1182
1183#ifdef ENABLE_SERIAL_TXX9_PCI
1184/*
1185 * Probe one serial board. Unfortunately, there is no rhyme nor reason
1186 * to the arrangement of serial ports on a PCI card.
1187 */
1188static int __devinit
1189pciserial_txx9_init_one(struct pci_dev *dev, const struct pci_device_id *ent)
1190{
1191 struct uart_port port;
1192 int line;
1193 int rc;
1194
1195 rc = pci_enable_device(dev);
1196 if (rc)
1197 return rc;
1198
1199 memset(&port, 0, sizeof(port));
1200 port.ops = &serial_txx9_pops;
1201 port.flags |= UPF_TXX9_HAVE_CTS_LINE;
1202 port.uartclk = 66670000;
1203 port.irq = dev->irq;
1204 port.iotype = UPIO_PORT;
1205 port.iobase = pci_resource_start(dev, 1);
1206 port.dev = &dev->dev;
1207 line = serial_txx9_register_port(&port);
1208 if (line < 0) {
1209 printk(KERN_WARNING "Couldn't register serial port %s: %d\n", pci_name(dev), line);
1210 pci_disable_device(dev);
1211 return line;
1212 }
1213 pci_set_drvdata(dev, &serial_txx9_ports[line]);
1214
1215 return 0;
1216}
1217
1218static void __devexit pciserial_txx9_remove_one(struct pci_dev *dev)
1219{
1220 struct uart_txx9_port *up = pci_get_drvdata(dev);
1221
1222 pci_set_drvdata(dev, NULL);
1223
1224 if (up) {
1225 serial_txx9_unregister_port(up->port.line);
1226 pci_disable_device(dev);
1227 }
1228}
1229
1230#ifdef CONFIG_PM
1231static int pciserial_txx9_suspend_one(struct pci_dev *dev, pm_message_t state)
1232{
1233 struct uart_txx9_port *up = pci_get_drvdata(dev);
1234
1235 if (up)
1236 uart_suspend_port(&serial_txx9_reg, &up->port);
1237 pci_save_state(dev);
1238 pci_set_power_state(dev, pci_choose_state(dev, state));
1239 return 0;
1240}
1241
1242static int pciserial_txx9_resume_one(struct pci_dev *dev)
1243{
1244 struct uart_txx9_port *up = pci_get_drvdata(dev);
1245
1246 pci_set_power_state(dev, PCI_D0);
1247 pci_restore_state(dev);
1248 if (up)
1249 uart_resume_port(&serial_txx9_reg, &up->port);
1250 return 0;
1251}
1252#endif
1253
1254static const struct pci_device_id serial_txx9_pci_tbl[] = {
1255 { PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC86C001_MISC) },
1256 { 0, }
1257};
1258
1259static struct pci_driver serial_txx9_pci_driver = {
1260 .name = "serial_txx9",
1261 .probe = pciserial_txx9_init_one,
1262 .remove = __devexit_p(pciserial_txx9_remove_one),
1263#ifdef CONFIG_PM
1264 .suspend = pciserial_txx9_suspend_one,
1265 .resume = pciserial_txx9_resume_one,
1266#endif
1267 .id_table = serial_txx9_pci_tbl,
1268};
1269
1270MODULE_DEVICE_TABLE(pci, serial_txx9_pci_tbl);
1271#endif /* ENABLE_SERIAL_TXX9_PCI */
1272
1273static struct platform_device *serial_txx9_plat_devs;
1274
1275static int __init serial_txx9_init(void)
1276{
1277 int ret;
1278
1279 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1280
1281 ret = uart_register_driver(&serial_txx9_reg);
1282 if (ret)
1283 goto out;
1284
1285 serial_txx9_plat_devs = platform_device_alloc("serial_txx9", -1);
1286 if (!serial_txx9_plat_devs) {
1287 ret = -ENOMEM;
1288 goto unreg_uart_drv;
1289 }
1290
1291 ret = platform_device_add(serial_txx9_plat_devs);
1292 if (ret)
1293 goto put_dev;
1294
1295 serial_txx9_register_ports(&serial_txx9_reg,
1296 &serial_txx9_plat_devs->dev);
1297
1298 ret = platform_driver_register(&serial_txx9_plat_driver);
1299 if (ret)
1300 goto del_dev;
1301
1302#ifdef ENABLE_SERIAL_TXX9_PCI
1303 ret = pci_register_driver(&serial_txx9_pci_driver);
1304#endif
1305 if (ret == 0)
1306 goto out;
1307
1308 del_dev:
1309 platform_device_del(serial_txx9_plat_devs);
1310 put_dev:
1311 platform_device_put(serial_txx9_plat_devs);
1312 unreg_uart_drv:
1313 uart_unregister_driver(&serial_txx9_reg);
1314 out:
1315 return ret;
1316}
1317
1318static void __exit serial_txx9_exit(void)
1319{
1320 int i;
1321
1322#ifdef ENABLE_SERIAL_TXX9_PCI
1323 pci_unregister_driver(&serial_txx9_pci_driver);
1324#endif
1325 platform_driver_unregister(&serial_txx9_plat_driver);
1326 platform_device_unregister(serial_txx9_plat_devs);
1327 for (i = 0; i < UART_NR; i++) {
1328 struct uart_txx9_port *up = &serial_txx9_ports[i];
1329 if (up->port.iobase || up->port.mapbase)
1330 uart_remove_one_port(&serial_txx9_reg, &up->port);
1331 }
1332
1333 uart_unregister_driver(&serial_txx9_reg);
1334}
1335
1336module_init(serial_txx9_init);
1337module_exit(serial_txx9_exit);
1338
1339MODULE_LICENSE("GPL");
1340MODULE_DESCRIPTION("TX39/49 serial driver");
1341
1342MODULE_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..ebd8629c108d
--- /dev/null
+++ b/drivers/tty/serial/sh-sci.c
@@ -0,0 +1,2083 @@
1/*
2 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
3 *
4 * Copyright (C) 2002 - 2011 Paul Mundt
5 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
6 *
7 * based off of the old drivers/char/sh-sci.c by:
8 *
9 * Copyright (C) 1999, 2000 Niibe Yutaka
10 * Copyright (C) 2000 Sugioka Toshinobu
11 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
12 * Modified to support SecureEdge. David McCullough (2002)
13 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
14 * Removed SH7300 support (Jul 2007).
15 *
16 * This file is subject to the terms and conditions of the GNU General Public
17 * License. See the file "COPYING" in the main directory of this archive
18 * for more details.
19 */
20#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
21#define SUPPORT_SYSRQ
22#endif
23
24#undef DEBUG
25
26#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/timer.h>
29#include <linux/interrupt.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial.h>
33#include <linux/major.h>
34#include <linux/string.h>
35#include <linux/sysrq.h>
36#include <linux/ioport.h>
37#include <linux/mm.h>
38#include <linux/init.h>
39#include <linux/delay.h>
40#include <linux/console.h>
41#include <linux/platform_device.h>
42#include <linux/serial_sci.h>
43#include <linux/notifier.h>
44#include <linux/pm_runtime.h>
45#include <linux/cpufreq.h>
46#include <linux/clk.h>
47#include <linux/ctype.h>
48#include <linux/err.h>
49#include <linux/dmaengine.h>
50#include <linux/scatterlist.h>
51#include <linux/slab.h>
52
53#ifdef CONFIG_SUPERH
54#include <asm/sh_bios.h>
55#endif
56
57#ifdef CONFIG_H8300
58#include <asm/gpio.h>
59#endif
60
61#include "sh-sci.h"
62
63struct sci_port {
64 struct uart_port port;
65
66 /* Platform configuration */
67 struct plat_sci_port *cfg;
68
69 /* Port enable callback */
70 void (*enable)(struct uart_port *port);
71
72 /* Port disable callback */
73 void (*disable)(struct uart_port *port);
74
75 /* Break timer */
76 struct timer_list break_timer;
77 int break_flag;
78
79 /* Interface clock */
80 struct clk *iclk;
81 /* Function clock */
82 struct clk *fclk;
83
84 struct dma_chan *chan_tx;
85 struct dma_chan *chan_rx;
86
87#ifdef CONFIG_SERIAL_SH_SCI_DMA
88 struct dma_async_tx_descriptor *desc_tx;
89 struct dma_async_tx_descriptor *desc_rx[2];
90 dma_cookie_t cookie_tx;
91 dma_cookie_t cookie_rx[2];
92 dma_cookie_t active_rx;
93 struct scatterlist sg_tx;
94 unsigned int sg_len_tx;
95 struct scatterlist sg_rx[2];
96 size_t buf_len_rx;
97 struct sh_dmae_slave param_tx;
98 struct sh_dmae_slave param_rx;
99 struct work_struct work_tx;
100 struct work_struct work_rx;
101 struct timer_list rx_timer;
102 unsigned int rx_timeout;
103#endif
104
105 struct notifier_block freq_transition;
106};
107
108/* Function prototypes */
109static void sci_start_tx(struct uart_port *port);
110static void sci_stop_tx(struct uart_port *port);
111static void sci_start_rx(struct uart_port *port);
112
113#define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
114
115static struct sci_port sci_ports[SCI_NPORTS];
116static struct uart_driver sci_uart_driver;
117
118static inline struct sci_port *
119to_sci_port(struct uart_port *uart)
120{
121 return container_of(uart, struct sci_port, port);
122}
123
124#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
125
126#ifdef CONFIG_CONSOLE_POLL
127static int sci_poll_get_char(struct uart_port *port)
128{
129 unsigned short status;
130 int c;
131
132 do {
133 status = sci_in(port, SCxSR);
134 if (status & SCxSR_ERRORS(port)) {
135 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
136 continue;
137 }
138 break;
139 } while (1);
140
141 if (!(status & SCxSR_RDxF(port)))
142 return NO_POLL_CHAR;
143
144 c = sci_in(port, SCxRDR);
145
146 /* Dummy read */
147 sci_in(port, SCxSR);
148 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
149
150 return c;
151}
152#endif
153
154static void sci_poll_put_char(struct uart_port *port, unsigned char c)
155{
156 unsigned short status;
157
158 do {
159 status = sci_in(port, SCxSR);
160 } while (!(status & SCxSR_TDxE(port)));
161
162 sci_out(port, SCxTDR, c);
163 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
164}
165#endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
166
167#if defined(__H8300H__) || defined(__H8300S__)
168static void sci_init_pins(struct uart_port *port, unsigned int cflag)
169{
170 int ch = (port->mapbase - SMR0) >> 3;
171
172 /* set DDR regs */
173 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
174 h8300_sci_pins[ch].rx,
175 H8300_GPIO_INPUT);
176 H8300_GPIO_DDR(h8300_sci_pins[ch].port,
177 h8300_sci_pins[ch].tx,
178 H8300_GPIO_OUTPUT);
179
180 /* tx mark output*/
181 H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;
182}
183#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
184static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
185{
186 if (port->mapbase == 0xA4400000) {
187 __raw_writew(__raw_readw(PACR) & 0xffc0, PACR);
188 __raw_writew(__raw_readw(PBCR) & 0x0fff, PBCR);
189 } else if (port->mapbase == 0xA4410000)
190 __raw_writew(__raw_readw(PBCR) & 0xf003, PBCR);
191}
192#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
193static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
194{
195 unsigned short data;
196
197 if (cflag & CRTSCTS) {
198 /* enable RTS/CTS */
199 if (port->mapbase == 0xa4430000) { /* SCIF0 */
200 /* Clear PTCR bit 9-2; enable all scif pins but sck */
201 data = __raw_readw(PORT_PTCR);
202 __raw_writew((data & 0xfc03), PORT_PTCR);
203 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
204 /* Clear PVCR bit 9-2 */
205 data = __raw_readw(PORT_PVCR);
206 __raw_writew((data & 0xfc03), PORT_PVCR);
207 }
208 } else {
209 if (port->mapbase == 0xa4430000) { /* SCIF0 */
210 /* Clear PTCR bit 5-2; enable only tx and rx */
211 data = __raw_readw(PORT_PTCR);
212 __raw_writew((data & 0xffc3), PORT_PTCR);
213 } else if (port->mapbase == 0xa4438000) { /* SCIF1 */
214 /* Clear PVCR bit 5-2 */
215 data = __raw_readw(PORT_PVCR);
216 __raw_writew((data & 0xffc3), PORT_PVCR);
217 }
218 }
219}
220#elif defined(CONFIG_CPU_SH3)
221/* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
222static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
223{
224 unsigned short data;
225
226 /* We need to set SCPCR to enable RTS/CTS */
227 data = __raw_readw(SCPCR);
228 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
229 __raw_writew(data & 0x0fcf, SCPCR);
230
231 if (!(cflag & CRTSCTS)) {
232 /* We need to set SCPCR to enable RTS/CTS */
233 data = __raw_readw(SCPCR);
234 /* Clear out SCP7MD1,0, SCP4MD1,0,
235 Set SCP6MD1,0 = {01} (output) */
236 __raw_writew((data & 0x0fcf) | 0x1000, SCPCR);
237
238 data = __raw_readb(SCPDR);
239 /* Set /RTS2 (bit6) = 0 */
240 __raw_writeb(data & 0xbf, SCPDR);
241 }
242}
243#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
244static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
245{
246 unsigned short data;
247
248 if (port->mapbase == 0xffe00000) {
249 data = __raw_readw(PSCR);
250 data &= ~0x03cf;
251 if (!(cflag & CRTSCTS))
252 data |= 0x0340;
253
254 __raw_writew(data, PSCR);
255 }
256}
257#elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
258 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
259 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
260 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
261 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
262 defined(CONFIG_CPU_SUBTYPE_SHX3)
263static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
264{
265 if (!(cflag & CRTSCTS))
266 __raw_writew(0x0080, SCSPTR0); /* Set RTS = 1 */
267}
268#elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
269static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
270{
271 if (!(cflag & CRTSCTS))
272 __raw_writew(0x0080, SCSPTR2); /* Set RTS = 1 */
273}
274#else
275static inline void sci_init_pins(struct uart_port *port, unsigned int cflag)
276{
277 /* Nothing to do */
278}
279#endif
280
281#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
282 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
283 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
284 defined(CONFIG_CPU_SUBTYPE_SH7786)
285static int scif_txfill(struct uart_port *port)
286{
287 return sci_in(port, SCTFDR) & 0xff;
288}
289
290static int scif_txroom(struct uart_port *port)
291{
292 return SCIF_TXROOM_MAX - scif_txfill(port);
293}
294
295static int scif_rxfill(struct uart_port *port)
296{
297 return sci_in(port, SCRFDR) & 0xff;
298}
299#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
300static int scif_txfill(struct uart_port *port)
301{
302 if (port->mapbase == 0xffe00000 ||
303 port->mapbase == 0xffe08000)
304 /* SCIF0/1*/
305 return sci_in(port, SCTFDR) & 0xff;
306 else
307 /* SCIF2 */
308 return sci_in(port, SCFDR) >> 8;
309}
310
311static int scif_txroom(struct uart_port *port)
312{
313 if (port->mapbase == 0xffe00000 ||
314 port->mapbase == 0xffe08000)
315 /* SCIF0/1*/
316 return SCIF_TXROOM_MAX - scif_txfill(port);
317 else
318 /* SCIF2 */
319 return SCIF2_TXROOM_MAX - scif_txfill(port);
320}
321
322static int scif_rxfill(struct uart_port *port)
323{
324 if ((port->mapbase == 0xffe00000) ||
325 (port->mapbase == 0xffe08000)) {
326 /* SCIF0/1*/
327 return sci_in(port, SCRFDR) & 0xff;
328 } else {
329 /* SCIF2 */
330 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
331 }
332}
333#elif defined(CONFIG_ARCH_SH7372)
334static int scif_txfill(struct uart_port *port)
335{
336 if (port->type == PORT_SCIFA)
337 return sci_in(port, SCFDR) >> 8;
338 else
339 return sci_in(port, SCTFDR);
340}
341
342static int scif_txroom(struct uart_port *port)
343{
344 return port->fifosize - scif_txfill(port);
345}
346
347static int scif_rxfill(struct uart_port *port)
348{
349 if (port->type == PORT_SCIFA)
350 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
351 else
352 return sci_in(port, SCRFDR);
353}
354#else
355static int scif_txfill(struct uart_port *port)
356{
357 return sci_in(port, SCFDR) >> 8;
358}
359
360static int scif_txroom(struct uart_port *port)
361{
362 return SCIF_TXROOM_MAX - scif_txfill(port);
363}
364
365static int scif_rxfill(struct uart_port *port)
366{
367 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
368}
369#endif
370
371static int sci_txfill(struct uart_port *port)
372{
373 return !(sci_in(port, SCxSR) & SCI_TDRE);
374}
375
376static int sci_txroom(struct uart_port *port)
377{
378 return !sci_txfill(port);
379}
380
381static int sci_rxfill(struct uart_port *port)
382{
383 return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
384}
385
386/* ********************************************************************** *
387 * the interrupt related routines *
388 * ********************************************************************** */
389
390static void sci_transmit_chars(struct uart_port *port)
391{
392 struct circ_buf *xmit = &port->state->xmit;
393 unsigned int stopped = uart_tx_stopped(port);
394 unsigned short status;
395 unsigned short ctrl;
396 int count;
397
398 status = sci_in(port, SCxSR);
399 if (!(status & SCxSR_TDxE(port))) {
400 ctrl = sci_in(port, SCSCR);
401 if (uart_circ_empty(xmit))
402 ctrl &= ~SCSCR_TIE;
403 else
404 ctrl |= SCSCR_TIE;
405 sci_out(port, SCSCR, ctrl);
406 return;
407 }
408
409 if (port->type == PORT_SCI)
410 count = sci_txroom(port);
411 else
412 count = scif_txroom(port);
413
414 do {
415 unsigned char c;
416
417 if (port->x_char) {
418 c = port->x_char;
419 port->x_char = 0;
420 } else if (!uart_circ_empty(xmit) && !stopped) {
421 c = xmit->buf[xmit->tail];
422 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
423 } else {
424 break;
425 }
426
427 sci_out(port, SCxTDR, c);
428
429 port->icount.tx++;
430 } while (--count > 0);
431
432 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
433
434 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
435 uart_write_wakeup(port);
436 if (uart_circ_empty(xmit)) {
437 sci_stop_tx(port);
438 } else {
439 ctrl = sci_in(port, SCSCR);
440
441 if (port->type != PORT_SCI) {
442 sci_in(port, SCxSR); /* Dummy read */
443 sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
444 }
445
446 ctrl |= SCSCR_TIE;
447 sci_out(port, SCSCR, ctrl);
448 }
449}
450
451/* On SH3, SCIF may read end-of-break as a space->mark char */
452#define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
453
454static void sci_receive_chars(struct uart_port *port)
455{
456 struct sci_port *sci_port = to_sci_port(port);
457 struct tty_struct *tty = port->state->port.tty;
458 int i, count, copied = 0;
459 unsigned short status;
460 unsigned char flag;
461
462 status = sci_in(port, SCxSR);
463 if (!(status & SCxSR_RDxF(port)))
464 return;
465
466 while (1) {
467 if (port->type == PORT_SCI)
468 count = sci_rxfill(port);
469 else
470 count = scif_rxfill(port);
471
472 /* Don't copy more bytes than there is room for in the buffer */
473 count = tty_buffer_request_room(tty, count);
474
475 /* If for any reason we can't copy more data, we're done! */
476 if (count == 0)
477 break;
478
479 if (port->type == PORT_SCI) {
480 char c = sci_in(port, SCxRDR);
481 if (uart_handle_sysrq_char(port, c) ||
482 sci_port->break_flag)
483 count = 0;
484 else
485 tty_insert_flip_char(tty, c, TTY_NORMAL);
486 } else {
487 for (i = 0; i < count; i++) {
488 char c = sci_in(port, SCxRDR);
489 status = sci_in(port, SCxSR);
490#if defined(CONFIG_CPU_SH3)
491 /* Skip "chars" during break */
492 if (sci_port->break_flag) {
493 if ((c == 0) &&
494 (status & SCxSR_FER(port))) {
495 count--; i--;
496 continue;
497 }
498
499 /* Nonzero => end-of-break */
500 dev_dbg(port->dev, "debounce<%02x>\n", c);
501 sci_port->break_flag = 0;
502
503 if (STEPFN(c)) {
504 count--; i--;
505 continue;
506 }
507 }
508#endif /* CONFIG_CPU_SH3 */
509 if (uart_handle_sysrq_char(port, c)) {
510 count--; i--;
511 continue;
512 }
513
514 /* Store data and status */
515 if (status & SCxSR_FER(port)) {
516 flag = TTY_FRAME;
517 dev_notice(port->dev, "frame error\n");
518 } else if (status & SCxSR_PER(port)) {
519 flag = TTY_PARITY;
520 dev_notice(port->dev, "parity error\n");
521 } else
522 flag = TTY_NORMAL;
523
524 tty_insert_flip_char(tty, c, flag);
525 }
526 }
527
528 sci_in(port, SCxSR); /* dummy read */
529 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
530
531 copied += count;
532 port->icount.rx += count;
533 }
534
535 if (copied) {
536 /* Tell the rest of the system the news. New characters! */
537 tty_flip_buffer_push(tty);
538 } else {
539 sci_in(port, SCxSR); /* dummy read */
540 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
541 }
542}
543
544#define SCI_BREAK_JIFFIES (HZ/20)
545
546/*
547 * The sci generates interrupts during the break,
548 * 1 per millisecond or so during the break period, for 9600 baud.
549 * So dont bother disabling interrupts.
550 * But dont want more than 1 break event.
551 * Use a kernel timer to periodically poll the rx line until
552 * the break is finished.
553 */
554static inline void sci_schedule_break_timer(struct sci_port *port)
555{
556 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
557}
558
559/* Ensure that two consecutive samples find the break over. */
560static void sci_break_timer(unsigned long data)
561{
562 struct sci_port *port = (struct sci_port *)data;
563
564 if (port->enable)
565 port->enable(&port->port);
566
567 if (sci_rxd_in(&port->port) == 0) {
568 port->break_flag = 1;
569 sci_schedule_break_timer(port);
570 } else if (port->break_flag == 1) {
571 /* break is over. */
572 port->break_flag = 2;
573 sci_schedule_break_timer(port);
574 } else
575 port->break_flag = 0;
576
577 if (port->disable)
578 port->disable(&port->port);
579}
580
581static int sci_handle_errors(struct uart_port *port)
582{
583 int copied = 0;
584 unsigned short status = sci_in(port, SCxSR);
585 struct tty_struct *tty = port->state->port.tty;
586
587 if (status & SCxSR_ORER(port)) {
588 /* overrun error */
589 if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
590 copied++;
591
592 dev_notice(port->dev, "overrun error");
593 }
594
595 if (status & SCxSR_FER(port)) {
596 if (sci_rxd_in(port) == 0) {
597 /* Notify of BREAK */
598 struct sci_port *sci_port = to_sci_port(port);
599
600 if (!sci_port->break_flag) {
601 sci_port->break_flag = 1;
602 sci_schedule_break_timer(sci_port);
603
604 /* Do sysrq handling. */
605 if (uart_handle_break(port))
606 return 0;
607
608 dev_dbg(port->dev, "BREAK detected\n");
609
610 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
611 copied++;
612 }
613
614 } else {
615 /* frame error */
616 if (tty_insert_flip_char(tty, 0, TTY_FRAME))
617 copied++;
618
619 dev_notice(port->dev, "frame error\n");
620 }
621 }
622
623 if (status & SCxSR_PER(port)) {
624 /* parity error */
625 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
626 copied++;
627
628 dev_notice(port->dev, "parity error");
629 }
630
631 if (copied)
632 tty_flip_buffer_push(tty);
633
634 return copied;
635}
636
637static int sci_handle_fifo_overrun(struct uart_port *port)
638{
639 struct tty_struct *tty = port->state->port.tty;
640 int copied = 0;
641
642 if (port->type != PORT_SCIF)
643 return 0;
644
645 if ((sci_in(port, SCLSR) & SCIF_ORER) != 0) {
646 sci_out(port, SCLSR, 0);
647
648 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
649 tty_flip_buffer_push(tty);
650
651 dev_notice(port->dev, "overrun error\n");
652 copied++;
653 }
654
655 return copied;
656}
657
658static int sci_handle_breaks(struct uart_port *port)
659{
660 int copied = 0;
661 unsigned short status = sci_in(port, SCxSR);
662 struct tty_struct *tty = port->state->port.tty;
663 struct sci_port *s = to_sci_port(port);
664
665 if (uart_handle_break(port))
666 return 0;
667
668 if (!s->break_flag && status & SCxSR_BRK(port)) {
669#if defined(CONFIG_CPU_SH3)
670 /* Debounce break */
671 s->break_flag = 1;
672#endif
673 /* Notify of BREAK */
674 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
675 copied++;
676
677 dev_dbg(port->dev, "BREAK detected\n");
678 }
679
680 if (copied)
681 tty_flip_buffer_push(tty);
682
683 copied += sci_handle_fifo_overrun(port);
684
685 return copied;
686}
687
688static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
689{
690#ifdef CONFIG_SERIAL_SH_SCI_DMA
691 struct uart_port *port = ptr;
692 struct sci_port *s = to_sci_port(port);
693
694 if (s->chan_rx) {
695 u16 scr = sci_in(port, SCSCR);
696 u16 ssr = sci_in(port, SCxSR);
697
698 /* Disable future Rx interrupts */
699 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
700 disable_irq_nosync(irq);
701 scr |= 0x4000;
702 } else {
703 scr &= ~SCSCR_RIE;
704 }
705 sci_out(port, SCSCR, scr);
706 /* Clear current interrupt */
707 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
708 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
709 jiffies, s->rx_timeout);
710 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
711
712 return IRQ_HANDLED;
713 }
714#endif
715
716 /* I think sci_receive_chars has to be called irrespective
717 * of whether the I_IXOFF is set, otherwise, how is the interrupt
718 * to be disabled?
719 */
720 sci_receive_chars(ptr);
721
722 return IRQ_HANDLED;
723}
724
725static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
726{
727 struct uart_port *port = ptr;
728 unsigned long flags;
729
730 spin_lock_irqsave(&port->lock, flags);
731 sci_transmit_chars(port);
732 spin_unlock_irqrestore(&port->lock, flags);
733
734 return IRQ_HANDLED;
735}
736
737static irqreturn_t sci_er_interrupt(int irq, void *ptr)
738{
739 struct uart_port *port = ptr;
740
741 /* Handle errors */
742 if (port->type == PORT_SCI) {
743 if (sci_handle_errors(port)) {
744 /* discard character in rx buffer */
745 sci_in(port, SCxSR);
746 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
747 }
748 } else {
749 sci_handle_fifo_overrun(port);
750 sci_rx_interrupt(irq, ptr);
751 }
752
753 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
754
755 /* Kick the transmission */
756 sci_tx_interrupt(irq, ptr);
757
758 return IRQ_HANDLED;
759}
760
761static irqreturn_t sci_br_interrupt(int irq, void *ptr)
762{
763 struct uart_port *port = ptr;
764
765 /* Handle BREAKs */
766 sci_handle_breaks(port);
767 sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
768
769 return IRQ_HANDLED;
770}
771
772static inline unsigned long port_rx_irq_mask(struct uart_port *port)
773{
774 /*
775 * Not all ports (such as SCIFA) will support REIE. Rather than
776 * special-casing the port type, we check the port initialization
777 * IRQ enable mask to see whether the IRQ is desired at all. If
778 * it's unset, it's logically inferred that there's no point in
779 * testing for it.
780 */
781 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
782}
783
784static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
785{
786 unsigned short ssr_status, scr_status, err_enabled;
787 struct uart_port *port = ptr;
788 struct sci_port *s = to_sci_port(port);
789 irqreturn_t ret = IRQ_NONE;
790
791 ssr_status = sci_in(port, SCxSR);
792 scr_status = sci_in(port, SCSCR);
793 err_enabled = scr_status & port_rx_irq_mask(port);
794
795 /* Tx Interrupt */
796 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
797 !s->chan_tx)
798 ret = sci_tx_interrupt(irq, ptr);
799
800 /*
801 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
802 * DR flags
803 */
804 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
805 (scr_status & SCSCR_RIE))
806 ret = sci_rx_interrupt(irq, ptr);
807
808 /* Error Interrupt */
809 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
810 ret = sci_er_interrupt(irq, ptr);
811
812 /* Break Interrupt */
813 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
814 ret = sci_br_interrupt(irq, ptr);
815
816 return ret;
817}
818
819/*
820 * Here we define a transition notifier so that we can update all of our
821 * ports' baud rate when the peripheral clock changes.
822 */
823static int sci_notifier(struct notifier_block *self,
824 unsigned long phase, void *p)
825{
826 struct sci_port *sci_port;
827 unsigned long flags;
828
829 sci_port = container_of(self, struct sci_port, freq_transition);
830
831 if ((phase == CPUFREQ_POSTCHANGE) ||
832 (phase == CPUFREQ_RESUMECHANGE)) {
833 struct uart_port *port = &sci_port->port;
834
835 spin_lock_irqsave(&port->lock, flags);
836 port->uartclk = clk_get_rate(sci_port->iclk);
837 spin_unlock_irqrestore(&port->lock, flags);
838 }
839
840 return NOTIFY_OK;
841}
842
843static void sci_clk_enable(struct uart_port *port)
844{
845 struct sci_port *sci_port = to_sci_port(port);
846
847 pm_runtime_get_sync(port->dev);
848
849 clk_enable(sci_port->iclk);
850 sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
851 clk_enable(sci_port->fclk);
852}
853
854static void sci_clk_disable(struct uart_port *port)
855{
856 struct sci_port *sci_port = to_sci_port(port);
857
858 clk_disable(sci_port->fclk);
859 clk_disable(sci_port->iclk);
860
861 pm_runtime_put_sync(port->dev);
862}
863
864static int sci_request_irq(struct sci_port *port)
865{
866 int i;
867 irqreturn_t (*handlers[4])(int irq, void *ptr) = {
868 sci_er_interrupt, sci_rx_interrupt, sci_tx_interrupt,
869 sci_br_interrupt,
870 };
871 const char *desc[] = { "SCI Receive Error", "SCI Receive Data Full",
872 "SCI Transmit Data Empty", "SCI Break" };
873
874 if (port->cfg->irqs[0] == port->cfg->irqs[1]) {
875 if (unlikely(!port->cfg->irqs[0]))
876 return -ENODEV;
877
878 if (request_irq(port->cfg->irqs[0], sci_mpxed_interrupt,
879 IRQF_DISABLED, "sci", port)) {
880 dev_err(port->port.dev, "Can't allocate IRQ\n");
881 return -ENODEV;
882 }
883 } else {
884 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
885 if (unlikely(!port->cfg->irqs[i]))
886 continue;
887
888 if (request_irq(port->cfg->irqs[i], handlers[i],
889 IRQF_DISABLED, desc[i], port)) {
890 dev_err(port->port.dev, "Can't allocate IRQ\n");
891 return -ENODEV;
892 }
893 }
894 }
895
896 return 0;
897}
898
899static void sci_free_irq(struct sci_port *port)
900{
901 int i;
902
903 if (port->cfg->irqs[0] == port->cfg->irqs[1])
904 free_irq(port->cfg->irqs[0], port);
905 else {
906 for (i = 0; i < ARRAY_SIZE(port->cfg->irqs); i++) {
907 if (!port->cfg->irqs[i])
908 continue;
909
910 free_irq(port->cfg->irqs[i], port);
911 }
912 }
913}
914
915static unsigned int sci_tx_empty(struct uart_port *port)
916{
917 unsigned short status = sci_in(port, SCxSR);
918 unsigned short in_tx_fifo = scif_txfill(port);
919
920 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
921}
922
923static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
924{
925 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
926 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
927 /* If you have signals for DTR and DCD, please implement here. */
928}
929
930static unsigned int sci_get_mctrl(struct uart_port *port)
931{
932 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
933 and CTS/RTS */
934
935 return TIOCM_DTR | TIOCM_RTS | TIOCM_DSR;
936}
937
938#ifdef CONFIG_SERIAL_SH_SCI_DMA
939static void sci_dma_tx_complete(void *arg)
940{
941 struct sci_port *s = arg;
942 struct uart_port *port = &s->port;
943 struct circ_buf *xmit = &port->state->xmit;
944 unsigned long flags;
945
946 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
947
948 spin_lock_irqsave(&port->lock, flags);
949
950 xmit->tail += sg_dma_len(&s->sg_tx);
951 xmit->tail &= UART_XMIT_SIZE - 1;
952
953 port->icount.tx += sg_dma_len(&s->sg_tx);
954
955 async_tx_ack(s->desc_tx);
956 s->cookie_tx = -EINVAL;
957 s->desc_tx = NULL;
958
959 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
960 uart_write_wakeup(port);
961
962 if (!uart_circ_empty(xmit)) {
963 schedule_work(&s->work_tx);
964 } else if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
965 u16 ctrl = sci_in(port, SCSCR);
966 sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
967 }
968
969 spin_unlock_irqrestore(&port->lock, flags);
970}
971
972/* Locking: called with port lock held */
973static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
974 size_t count)
975{
976 struct uart_port *port = &s->port;
977 int i, active, room;
978
979 room = tty_buffer_request_room(tty, count);
980
981 if (s->active_rx == s->cookie_rx[0]) {
982 active = 0;
983 } else if (s->active_rx == s->cookie_rx[1]) {
984 active = 1;
985 } else {
986 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
987 return 0;
988 }
989
990 if (room < count)
991 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
992 count - room);
993 if (!room)
994 return room;
995
996 for (i = 0; i < room; i++)
997 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
998 TTY_NORMAL);
999
1000 port->icount.rx += room;
1001
1002 return room;
1003}
1004
1005static void sci_dma_rx_complete(void *arg)
1006{
1007 struct sci_port *s = arg;
1008 struct uart_port *port = &s->port;
1009 struct tty_struct *tty = port->state->port.tty;
1010 unsigned long flags;
1011 int count;
1012
1013 dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
1014
1015 spin_lock_irqsave(&port->lock, flags);
1016
1017 count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1018
1019 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
1020
1021 spin_unlock_irqrestore(&port->lock, flags);
1022
1023 if (count)
1024 tty_flip_buffer_push(tty);
1025
1026 schedule_work(&s->work_rx);
1027}
1028
1029static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1030{
1031 struct dma_chan *chan = s->chan_rx;
1032 struct uart_port *port = &s->port;
1033
1034 s->chan_rx = NULL;
1035 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1036 dma_release_channel(chan);
1037 if (sg_dma_address(&s->sg_rx[0]))
1038 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1039 sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
1040 if (enable_pio)
1041 sci_start_rx(port);
1042}
1043
1044static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1045{
1046 struct dma_chan *chan = s->chan_tx;
1047 struct uart_port *port = &s->port;
1048
1049 s->chan_tx = NULL;
1050 s->cookie_tx = -EINVAL;
1051 dma_release_channel(chan);
1052 if (enable_pio)
1053 sci_start_tx(port);
1054}
1055
1056static void sci_submit_rx(struct sci_port *s)
1057{
1058 struct dma_chan *chan = s->chan_rx;
1059 int i;
1060
1061 for (i = 0; i < 2; i++) {
1062 struct scatterlist *sg = &s->sg_rx[i];
1063 struct dma_async_tx_descriptor *desc;
1064
1065 desc = chan->device->device_prep_slave_sg(chan,
1066 sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1067
1068 if (desc) {
1069 s->desc_rx[i] = desc;
1070 desc->callback = sci_dma_rx_complete;
1071 desc->callback_param = s;
1072 s->cookie_rx[i] = desc->tx_submit(desc);
1073 }
1074
1075 if (!desc || s->cookie_rx[i] < 0) {
1076 if (i) {
1077 async_tx_ack(s->desc_rx[0]);
1078 s->cookie_rx[0] = -EINVAL;
1079 }
1080 if (desc) {
1081 async_tx_ack(desc);
1082 s->cookie_rx[i] = -EINVAL;
1083 }
1084 dev_warn(s->port.dev,
1085 "failed to re-start DMA, using PIO\n");
1086 sci_rx_dma_release(s, true);
1087 return;
1088 }
1089 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1090 s->cookie_rx[i], i);
1091 }
1092
1093 s->active_rx = s->cookie_rx[0];
1094
1095 dma_async_issue_pending(chan);
1096}
1097
1098static void work_fn_rx(struct work_struct *work)
1099{
1100 struct sci_port *s = container_of(work, struct sci_port, work_rx);
1101 struct uart_port *port = &s->port;
1102 struct dma_async_tx_descriptor *desc;
1103 int new;
1104
1105 if (s->active_rx == s->cookie_rx[0]) {
1106 new = 0;
1107 } else if (s->active_rx == s->cookie_rx[1]) {
1108 new = 1;
1109 } else {
1110 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1111 return;
1112 }
1113 desc = s->desc_rx[new];
1114
1115 if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1116 DMA_SUCCESS) {
1117 /* Handle incomplete DMA receive */
1118 struct tty_struct *tty = port->state->port.tty;
1119 struct dma_chan *chan = s->chan_rx;
1120 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1121 async_tx);
1122 unsigned long flags;
1123 int count;
1124
1125 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
1126 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1127 sh_desc->partial, sh_desc->cookie);
1128
1129 spin_lock_irqsave(&port->lock, flags);
1130 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1131 spin_unlock_irqrestore(&port->lock, flags);
1132
1133 if (count)
1134 tty_flip_buffer_push(tty);
1135
1136 sci_submit_rx(s);
1137
1138 return;
1139 }
1140
1141 s->cookie_rx[new] = desc->tx_submit(desc);
1142 if (s->cookie_rx[new] < 0) {
1143 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1144 sci_rx_dma_release(s, true);
1145 return;
1146 }
1147
1148 s->active_rx = s->cookie_rx[!new];
1149
1150 dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1151 s->cookie_rx[new], new, s->active_rx);
1152}
1153
1154static void work_fn_tx(struct work_struct *work)
1155{
1156 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1157 struct dma_async_tx_descriptor *desc;
1158 struct dma_chan *chan = s->chan_tx;
1159 struct uart_port *port = &s->port;
1160 struct circ_buf *xmit = &port->state->xmit;
1161 struct scatterlist *sg = &s->sg_tx;
1162
1163 /*
1164 * DMA is idle now.
1165 * Port xmit buffer is already mapped, and it is one page... Just adjust
1166 * offsets and lengths. Since it is a circular buffer, we have to
1167 * transmit till the end, and then the rest. Take the port lock to get a
1168 * consistent xmit buffer state.
1169 */
1170 spin_lock_irq(&port->lock);
1171 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
1172 sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
1173 sg->offset;
1174 sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
1175 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
1176 spin_unlock_irq(&port->lock);
1177
1178 BUG_ON(!sg_dma_len(sg));
1179
1180 desc = chan->device->device_prep_slave_sg(chan,
1181 sg, s->sg_len_tx, DMA_TO_DEVICE,
1182 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1183 if (!desc) {
1184 /* switch to PIO */
1185 sci_tx_dma_release(s, true);
1186 return;
1187 }
1188
1189 dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1190
1191 spin_lock_irq(&port->lock);
1192 s->desc_tx = desc;
1193 desc->callback = sci_dma_tx_complete;
1194 desc->callback_param = s;
1195 spin_unlock_irq(&port->lock);
1196 s->cookie_tx = desc->tx_submit(desc);
1197 if (s->cookie_tx < 0) {
1198 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1199 /* switch to PIO */
1200 sci_tx_dma_release(s, true);
1201 return;
1202 }
1203
1204 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1205 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1206
1207 dma_async_issue_pending(chan);
1208}
1209#endif
1210
1211static void sci_start_tx(struct uart_port *port)
1212{
1213 struct sci_port *s = to_sci_port(port);
1214 unsigned short ctrl;
1215
1216#ifdef CONFIG_SERIAL_SH_SCI_DMA
1217 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1218 u16 new, scr = sci_in(port, SCSCR);
1219 if (s->chan_tx)
1220 new = scr | 0x8000;
1221 else
1222 new = scr & ~0x8000;
1223 if (new != scr)
1224 sci_out(port, SCSCR, new);
1225 }
1226
1227 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1228 s->cookie_tx < 0)
1229 schedule_work(&s->work_tx);
1230#endif
1231
1232 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1233 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1234 ctrl = sci_in(port, SCSCR);
1235 sci_out(port, SCSCR, ctrl | SCSCR_TIE);
1236 }
1237}
1238
1239static void sci_stop_tx(struct uart_port *port)
1240{
1241 unsigned short ctrl;
1242
1243 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
1244 ctrl = sci_in(port, SCSCR);
1245
1246 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1247 ctrl &= ~0x8000;
1248
1249 ctrl &= ~SCSCR_TIE;
1250
1251 sci_out(port, SCSCR, ctrl);
1252}
1253
1254static void sci_start_rx(struct uart_port *port)
1255{
1256 unsigned short ctrl;
1257
1258 ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
1259
1260 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1261 ctrl &= ~0x4000;
1262
1263 sci_out(port, SCSCR, ctrl);
1264}
1265
1266static void sci_stop_rx(struct uart_port *port)
1267{
1268 unsigned short ctrl;
1269
1270 ctrl = sci_in(port, SCSCR);
1271
1272 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1273 ctrl &= ~0x4000;
1274
1275 ctrl &= ~port_rx_irq_mask(port);
1276
1277 sci_out(port, SCSCR, ctrl);
1278}
1279
1280static void sci_enable_ms(struct uart_port *port)
1281{
1282 /* Nothing here yet .. */
1283}
1284
1285static void sci_break_ctl(struct uart_port *port, int break_state)
1286{
1287 /* Nothing here yet .. */
1288}
1289
1290#ifdef CONFIG_SERIAL_SH_SCI_DMA
1291static bool filter(struct dma_chan *chan, void *slave)
1292{
1293 struct sh_dmae_slave *param = slave;
1294
1295 dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1296 param->slave_id);
1297
1298 if (param->dma_dev == chan->device->dev) {
1299 chan->private = param;
1300 return true;
1301 } else {
1302 return false;
1303 }
1304}
1305
1306static void rx_timer_fn(unsigned long arg)
1307{
1308 struct sci_port *s = (struct sci_port *)arg;
1309 struct uart_port *port = &s->port;
1310 u16 scr = sci_in(port, SCSCR);
1311
1312 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1313 scr &= ~0x4000;
1314 enable_irq(s->cfg->irqs[1]);
1315 }
1316 sci_out(port, SCSCR, scr | SCSCR_RIE);
1317 dev_dbg(port->dev, "DMA Rx timed out\n");
1318 schedule_work(&s->work_rx);
1319}
1320
1321static void sci_request_dma(struct uart_port *port)
1322{
1323 struct sci_port *s = to_sci_port(port);
1324 struct sh_dmae_slave *param;
1325 struct dma_chan *chan;
1326 dma_cap_mask_t mask;
1327 int nent;
1328
1329 dev_dbg(port->dev, "%s: port %d DMA %p\n", __func__,
1330 port->line, s->cfg->dma_dev);
1331
1332 if (!s->cfg->dma_dev)
1333 return;
1334
1335 dma_cap_zero(mask);
1336 dma_cap_set(DMA_SLAVE, mask);
1337
1338 param = &s->param_tx;
1339
1340 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
1341 param->slave_id = s->cfg->dma_slave_tx;
1342 param->dma_dev = s->cfg->dma_dev;
1343
1344 s->cookie_tx = -EINVAL;
1345 chan = dma_request_channel(mask, filter, param);
1346 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1347 if (chan) {
1348 s->chan_tx = chan;
1349 sg_init_table(&s->sg_tx, 1);
1350 /* UART circular tx buffer is an aligned page. */
1351 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1352 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1353 UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1354 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1355 if (!nent)
1356 sci_tx_dma_release(s, false);
1357 else
1358 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1359 sg_dma_len(&s->sg_tx),
1360 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1361
1362 s->sg_len_tx = nent;
1363
1364 INIT_WORK(&s->work_tx, work_fn_tx);
1365 }
1366
1367 param = &s->param_rx;
1368
1369 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
1370 param->slave_id = s->cfg->dma_slave_rx;
1371 param->dma_dev = s->cfg->dma_dev;
1372
1373 chan = dma_request_channel(mask, filter, param);
1374 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1375 if (chan) {
1376 dma_addr_t dma[2];
1377 void *buf[2];
1378 int i;
1379
1380 s->chan_rx = chan;
1381
1382 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1383 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1384 &dma[0], GFP_KERNEL);
1385
1386 if (!buf[0]) {
1387 dev_warn(port->dev,
1388 "failed to allocate dma buffer, using PIO\n");
1389 sci_rx_dma_release(s, true);
1390 return;
1391 }
1392
1393 buf[1] = buf[0] + s->buf_len_rx;
1394 dma[1] = dma[0] + s->buf_len_rx;
1395
1396 for (i = 0; i < 2; i++) {
1397 struct scatterlist *sg = &s->sg_rx[i];
1398
1399 sg_init_table(sg, 1);
1400 sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1401 (int)buf[i] & ~PAGE_MASK);
1402 sg_dma_address(sg) = dma[i];
1403 }
1404
1405 INIT_WORK(&s->work_rx, work_fn_rx);
1406 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1407
1408 sci_submit_rx(s);
1409 }
1410}
1411
1412static void sci_free_dma(struct uart_port *port)
1413{
1414 struct sci_port *s = to_sci_port(port);
1415
1416 if (!s->cfg->dma_dev)
1417 return;
1418
1419 if (s->chan_tx)
1420 sci_tx_dma_release(s, false);
1421 if (s->chan_rx)
1422 sci_rx_dma_release(s, false);
1423}
1424#else
1425static inline void sci_request_dma(struct uart_port *port)
1426{
1427}
1428
1429static inline void sci_free_dma(struct uart_port *port)
1430{
1431}
1432#endif
1433
1434static int sci_startup(struct uart_port *port)
1435{
1436 struct sci_port *s = to_sci_port(port);
1437 int ret;
1438
1439 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1440
1441 if (s->enable)
1442 s->enable(port);
1443
1444 ret = sci_request_irq(s);
1445 if (unlikely(ret < 0))
1446 return ret;
1447
1448 sci_request_dma(port);
1449
1450 sci_start_tx(port);
1451 sci_start_rx(port);
1452
1453 return 0;
1454}
1455
1456static void sci_shutdown(struct uart_port *port)
1457{
1458 struct sci_port *s = to_sci_port(port);
1459
1460 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1461
1462 sci_stop_rx(port);
1463 sci_stop_tx(port);
1464
1465 sci_free_dma(port);
1466 sci_free_irq(s);
1467
1468 if (s->disable)
1469 s->disable(port);
1470}
1471
1472static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1473 unsigned long freq)
1474{
1475 switch (algo_id) {
1476 case SCBRR_ALGO_1:
1477 return ((freq + 16 * bps) / (16 * bps) - 1);
1478 case SCBRR_ALGO_2:
1479 return ((freq + 16 * bps) / (32 * bps) - 1);
1480 case SCBRR_ALGO_3:
1481 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
1482 case SCBRR_ALGO_4:
1483 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
1484 case SCBRR_ALGO_5:
1485 return (((freq * 1000 / 32) / bps) - 1);
1486 }
1487
1488 /* Warn, but use a safe default */
1489 WARN_ON(1);
1490
1491 return ((freq + 16 * bps) / (32 * bps) - 1);
1492}
1493
1494static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1495 struct ktermios *old)
1496{
1497 struct sci_port *s = to_sci_port(port);
1498 unsigned int status, baud, smr_val, max_baud;
1499 int t = -1;
1500 u16 scfcr = 0;
1501
1502 /*
1503 * earlyprintk comes here early on with port->uartclk set to zero.
1504 * the clock framework is not up and running at this point so here
1505 * we assume that 115200 is the maximum baud rate. please note that
1506 * the baud rate is not programmed during earlyprintk - it is assumed
1507 * that the previous boot loader has enabled required clocks and
1508 * setup the baud rate generator hardware for us already.
1509 */
1510 max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1511
1512 baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1513 if (likely(baud && port->uartclk))
1514 t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud, port->uartclk);
1515
1516 if (s->enable)
1517 s->enable(port);
1518
1519 do {
1520 status = sci_in(port, SCxSR);
1521 } while (!(status & SCxSR_TEND(port)));
1522
1523 sci_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
1524
1525 if (port->type != PORT_SCI)
1526 sci_out(port, SCFCR, scfcr | SCFCR_RFRST | SCFCR_TFRST);
1527
1528 smr_val = sci_in(port, SCSMR) & 3;
1529
1530 if ((termios->c_cflag & CSIZE) == CS7)
1531 smr_val |= 0x40;
1532 if (termios->c_cflag & PARENB)
1533 smr_val |= 0x20;
1534 if (termios->c_cflag & PARODD)
1535 smr_val |= 0x30;
1536 if (termios->c_cflag & CSTOPB)
1537 smr_val |= 0x08;
1538
1539 uart_update_timeout(port, termios->c_cflag, baud);
1540
1541 sci_out(port, SCSMR, smr_val);
1542
1543 dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
1544 s->cfg->scscr);
1545
1546 if (t > 0) {
1547 if (t >= 256) {
1548 sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1549 t >>= 2;
1550 } else
1551 sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
1552
1553 sci_out(port, SCBRR, t);
1554 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1555 }
1556
1557 sci_init_pins(port, termios->c_cflag);
1558 sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
1559
1560 sci_out(port, SCSCR, s->cfg->scscr);
1561
1562#ifdef CONFIG_SERIAL_SH_SCI_DMA
1563 /*
1564 * Calculate delay for 1.5 DMA buffers: see
1565 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1566 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1567 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1568 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1569 * sizes), but it has been found out experimentally, that this is not
1570 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1571 * as a minimum seem to work perfectly.
1572 */
1573 if (s->chan_rx) {
1574 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1575 port->fifosize / 2;
1576 dev_dbg(port->dev,
1577 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1578 s->rx_timeout * 1000 / HZ, port->timeout);
1579 if (s->rx_timeout < msecs_to_jiffies(20))
1580 s->rx_timeout = msecs_to_jiffies(20);
1581 }
1582#endif
1583
1584 if ((termios->c_cflag & CREAD) != 0)
1585 sci_start_rx(port);
1586
1587 if (s->disable)
1588 s->disable(port);
1589}
1590
1591static const char *sci_type(struct uart_port *port)
1592{
1593 switch (port->type) {
1594 case PORT_IRDA:
1595 return "irda";
1596 case PORT_SCI:
1597 return "sci";
1598 case PORT_SCIF:
1599 return "scif";
1600 case PORT_SCIFA:
1601 return "scifa";
1602 case PORT_SCIFB:
1603 return "scifb";
1604 }
1605
1606 return NULL;
1607}
1608
1609static inline unsigned long sci_port_size(struct uart_port *port)
1610{
1611 /*
1612 * Pick an arbitrary size that encapsulates all of the base
1613 * registers by default. This can be optimized later, or derived
1614 * from platform resource data at such a time that ports begin to
1615 * behave more erratically.
1616 */
1617 return 64;
1618}
1619
1620static int sci_remap_port(struct uart_port *port)
1621{
1622 unsigned long size = sci_port_size(port);
1623
1624 /*
1625 * Nothing to do if there's already an established membase.
1626 */
1627 if (port->membase)
1628 return 0;
1629
1630 if (port->flags & UPF_IOREMAP) {
1631 port->membase = ioremap_nocache(port->mapbase, size);
1632 if (unlikely(!port->membase)) {
1633 dev_err(port->dev, "can't remap port#%d\n", port->line);
1634 return -ENXIO;
1635 }
1636 } else {
1637 /*
1638 * For the simple (and majority of) cases where we don't
1639 * need to do any remapping, just cast the cookie
1640 * directly.
1641 */
1642 port->membase = (void __iomem *)port->mapbase;
1643 }
1644
1645 return 0;
1646}
1647
1648static void sci_release_port(struct uart_port *port)
1649{
1650 if (port->flags & UPF_IOREMAP) {
1651 iounmap(port->membase);
1652 port->membase = NULL;
1653 }
1654
1655 release_mem_region(port->mapbase, sci_port_size(port));
1656}
1657
1658static int sci_request_port(struct uart_port *port)
1659{
1660 unsigned long size = sci_port_size(port);
1661 struct resource *res;
1662 int ret;
1663
1664 res = request_mem_region(port->mapbase, size, dev_name(port->dev));
1665 if (unlikely(res == NULL))
1666 return -EBUSY;
1667
1668 ret = sci_remap_port(port);
1669 if (unlikely(ret != 0)) {
1670 release_resource(res);
1671 return ret;
1672 }
1673
1674 return 0;
1675}
1676
1677static void sci_config_port(struct uart_port *port, int flags)
1678{
1679 if (flags & UART_CONFIG_TYPE) {
1680 struct sci_port *sport = to_sci_port(port);
1681
1682 port->type = sport->cfg->type;
1683 sci_request_port(port);
1684 }
1685}
1686
1687static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1688{
1689 struct sci_port *s = to_sci_port(port);
1690
1691 if (ser->irq != s->cfg->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
1692 return -EINVAL;
1693 if (ser->baud_base < 2400)
1694 /* No paper tape reader for Mitch.. */
1695 return -EINVAL;
1696
1697 return 0;
1698}
1699
1700static struct uart_ops sci_uart_ops = {
1701 .tx_empty = sci_tx_empty,
1702 .set_mctrl = sci_set_mctrl,
1703 .get_mctrl = sci_get_mctrl,
1704 .start_tx = sci_start_tx,
1705 .stop_tx = sci_stop_tx,
1706 .stop_rx = sci_stop_rx,
1707 .enable_ms = sci_enable_ms,
1708 .break_ctl = sci_break_ctl,
1709 .startup = sci_startup,
1710 .shutdown = sci_shutdown,
1711 .set_termios = sci_set_termios,
1712 .type = sci_type,
1713 .release_port = sci_release_port,
1714 .request_port = sci_request_port,
1715 .config_port = sci_config_port,
1716 .verify_port = sci_verify_port,
1717#ifdef CONFIG_CONSOLE_POLL
1718 .poll_get_char = sci_poll_get_char,
1719 .poll_put_char = sci_poll_put_char,
1720#endif
1721};
1722
1723static int __devinit sci_init_single(struct platform_device *dev,
1724 struct sci_port *sci_port,
1725 unsigned int index,
1726 struct plat_sci_port *p)
1727{
1728 struct uart_port *port = &sci_port->port;
1729
1730 port->ops = &sci_uart_ops;
1731 port->iotype = UPIO_MEM;
1732 port->line = index;
1733
1734 switch (p->type) {
1735 case PORT_SCIFB:
1736 port->fifosize = 256;
1737 break;
1738 case PORT_SCIFA:
1739 port->fifosize = 64;
1740 break;
1741 case PORT_SCIF:
1742 port->fifosize = 16;
1743 break;
1744 default:
1745 port->fifosize = 1;
1746 break;
1747 }
1748
1749 if (dev) {
1750 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1751 if (IS_ERR(sci_port->iclk)) {
1752 sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1753 if (IS_ERR(sci_port->iclk)) {
1754 dev_err(&dev->dev, "can't get iclk\n");
1755 return PTR_ERR(sci_port->iclk);
1756 }
1757 }
1758
1759 /*
1760 * The function clock is optional, ignore it if we can't
1761 * find it.
1762 */
1763 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1764 if (IS_ERR(sci_port->fclk))
1765 sci_port->fclk = NULL;
1766
1767 sci_port->enable = sci_clk_enable;
1768 sci_port->disable = sci_clk_disable;
1769 port->dev = &dev->dev;
1770
1771 pm_runtime_enable(&dev->dev);
1772 }
1773
1774 sci_port->break_timer.data = (unsigned long)sci_port;
1775 sci_port->break_timer.function = sci_break_timer;
1776 init_timer(&sci_port->break_timer);
1777
1778 sci_port->cfg = p;
1779
1780 port->mapbase = p->mapbase;
1781 port->type = p->type;
1782 port->flags = p->flags;
1783
1784 /*
1785 * The UART port needs an IRQ value, so we peg this to the TX IRQ
1786 * for the multi-IRQ ports, which is where we are primarily
1787 * concerned with the shutdown path synchronization.
1788 *
1789 * For the muxed case there's nothing more to do.
1790 */
1791 port->irq = p->irqs[SCIx_RXI_IRQ];
1792
1793 if (p->dma_dev)
1794 dev_dbg(port->dev, "DMA device %p, tx %d, rx %d\n",
1795 p->dma_dev, p->dma_slave_tx, p->dma_slave_rx);
1796
1797 return 0;
1798}
1799
1800#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1801static void serial_console_putchar(struct uart_port *port, int ch)
1802{
1803 sci_poll_put_char(port, ch);
1804}
1805
1806/*
1807 * Print a string to the serial port trying not to disturb
1808 * any possible real use of the port...
1809 */
1810static void serial_console_write(struct console *co, const char *s,
1811 unsigned count)
1812{
1813 struct sci_port *sci_port = &sci_ports[co->index];
1814 struct uart_port *port = &sci_port->port;
1815 unsigned short bits;
1816
1817 if (sci_port->enable)
1818 sci_port->enable(port);
1819
1820 uart_console_write(port, s, count, serial_console_putchar);
1821
1822 /* wait until fifo is empty and last bit has been transmitted */
1823 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
1824 while ((sci_in(port, SCxSR) & bits) != bits)
1825 cpu_relax();
1826
1827 if (sci_port->disable)
1828 sci_port->disable(port);
1829}
1830
1831static int __devinit serial_console_setup(struct console *co, char *options)
1832{
1833 struct sci_port *sci_port;
1834 struct uart_port *port;
1835 int baud = 115200;
1836 int bits = 8;
1837 int parity = 'n';
1838 int flow = 'n';
1839 int ret;
1840
1841 /*
1842 * Refuse to handle any bogus ports.
1843 */
1844 if (co->index < 0 || co->index >= SCI_NPORTS)
1845 return -ENODEV;
1846
1847 sci_port = &sci_ports[co->index];
1848 port = &sci_port->port;
1849
1850 /*
1851 * Refuse to handle uninitialized ports.
1852 */
1853 if (!port->ops)
1854 return -ENODEV;
1855
1856 ret = sci_remap_port(port);
1857 if (unlikely(ret != 0))
1858 return ret;
1859
1860 if (sci_port->enable)
1861 sci_port->enable(port);
1862
1863 if (options)
1864 uart_parse_options(options, &baud, &parity, &bits, &flow);
1865
1866 ret = uart_set_options(port, co, baud, parity, bits, flow);
1867#if defined(__H8300H__) || defined(__H8300S__)
1868 /* disable rx interrupt */
1869 if (ret == 0)
1870 sci_stop_rx(port);
1871#endif
1872 /* TODO: disable clock */
1873 return ret;
1874}
1875
1876static struct console serial_console = {
1877 .name = "ttySC",
1878 .device = uart_console_device,
1879 .write = serial_console_write,
1880 .setup = serial_console_setup,
1881 .flags = CON_PRINTBUFFER,
1882 .index = -1,
1883 .data = &sci_uart_driver,
1884};
1885
1886static struct console early_serial_console = {
1887 .name = "early_ttySC",
1888 .write = serial_console_write,
1889 .flags = CON_PRINTBUFFER,
1890 .index = -1,
1891};
1892
1893static char early_serial_buf[32];
1894
1895static int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
1896{
1897 struct plat_sci_port *cfg = pdev->dev.platform_data;
1898
1899 if (early_serial_console.data)
1900 return -EEXIST;
1901
1902 early_serial_console.index = pdev->id;
1903
1904 sci_init_single(NULL, &sci_ports[pdev->id], pdev->id, cfg);
1905
1906 serial_console_setup(&early_serial_console, early_serial_buf);
1907
1908 if (!strstr(early_serial_buf, "keep"))
1909 early_serial_console.flags |= CON_BOOT;
1910
1911 register_console(&early_serial_console);
1912 return 0;
1913}
1914
1915#define SCI_CONSOLE (&serial_console)
1916
1917#else
1918static inline int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
1919{
1920 return -EINVAL;
1921}
1922
1923#define SCI_CONSOLE NULL
1924
1925#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1926
1927static char banner[] __initdata =
1928 KERN_INFO "SuperH SCI(F) driver initialized\n";
1929
1930static struct uart_driver sci_uart_driver = {
1931 .owner = THIS_MODULE,
1932 .driver_name = "sci",
1933 .dev_name = "ttySC",
1934 .major = SCI_MAJOR,
1935 .minor = SCI_MINOR_START,
1936 .nr = SCI_NPORTS,
1937 .cons = SCI_CONSOLE,
1938};
1939
1940static int sci_remove(struct platform_device *dev)
1941{
1942 struct sci_port *port = platform_get_drvdata(dev);
1943
1944 cpufreq_unregister_notifier(&port->freq_transition,
1945 CPUFREQ_TRANSITION_NOTIFIER);
1946
1947 uart_remove_one_port(&sci_uart_driver, &port->port);
1948
1949 clk_put(port->iclk);
1950 clk_put(port->fclk);
1951
1952 pm_runtime_disable(&dev->dev);
1953 return 0;
1954}
1955
1956static int __devinit sci_probe_single(struct platform_device *dev,
1957 unsigned int index,
1958 struct plat_sci_port *p,
1959 struct sci_port *sciport)
1960{
1961 int ret;
1962
1963 /* Sanity check */
1964 if (unlikely(index >= SCI_NPORTS)) {
1965 dev_notice(&dev->dev, "Attempting to register port "
1966 "%d when only %d are available.\n",
1967 index+1, SCI_NPORTS);
1968 dev_notice(&dev->dev, "Consider bumping "
1969 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1970 return 0;
1971 }
1972
1973 ret = sci_init_single(dev, sciport, index, p);
1974 if (ret)
1975 return ret;
1976
1977 return uart_add_one_port(&sci_uart_driver, &sciport->port);
1978}
1979
1980static int __devinit sci_probe(struct platform_device *dev)
1981{
1982 struct plat_sci_port *p = dev->dev.platform_data;
1983 struct sci_port *sp = &sci_ports[dev->id];
1984 int ret;
1985
1986 /*
1987 * If we've come here via earlyprintk initialization, head off to
1988 * the special early probe. We don't have sufficient device state
1989 * to make it beyond this yet.
1990 */
1991 if (is_early_platform_device(dev))
1992 return sci_probe_earlyprintk(dev);
1993
1994 platform_set_drvdata(dev, sp);
1995
1996 ret = sci_probe_single(dev, dev->id, p, sp);
1997 if (ret)
1998 goto err_unreg;
1999
2000 sp->freq_transition.notifier_call = sci_notifier;
2001
2002 ret = cpufreq_register_notifier(&sp->freq_transition,
2003 CPUFREQ_TRANSITION_NOTIFIER);
2004 if (unlikely(ret < 0))
2005 goto err_unreg;
2006
2007#ifdef CONFIG_SH_STANDARD_BIOS
2008 sh_bios_gdb_detach();
2009#endif
2010
2011 return 0;
2012
2013err_unreg:
2014 sci_remove(dev);
2015 return ret;
2016}
2017
2018static int sci_suspend(struct device *dev)
2019{
2020 struct sci_port *sport = dev_get_drvdata(dev);
2021
2022 if (sport)
2023 uart_suspend_port(&sci_uart_driver, &sport->port);
2024
2025 return 0;
2026}
2027
2028static int sci_resume(struct device *dev)
2029{
2030 struct sci_port *sport = dev_get_drvdata(dev);
2031
2032 if (sport)
2033 uart_resume_port(&sci_uart_driver, &sport->port);
2034
2035 return 0;
2036}
2037
2038static const struct dev_pm_ops sci_dev_pm_ops = {
2039 .suspend = sci_suspend,
2040 .resume = sci_resume,
2041};
2042
2043static struct platform_driver sci_driver = {
2044 .probe = sci_probe,
2045 .remove = sci_remove,
2046 .driver = {
2047 .name = "sh-sci",
2048 .owner = THIS_MODULE,
2049 .pm = &sci_dev_pm_ops,
2050 },
2051};
2052
2053static int __init sci_init(void)
2054{
2055 int ret;
2056
2057 printk(banner);
2058
2059 ret = uart_register_driver(&sci_uart_driver);
2060 if (likely(ret == 0)) {
2061 ret = platform_driver_register(&sci_driver);
2062 if (unlikely(ret))
2063 uart_unregister_driver(&sci_uart_driver);
2064 }
2065
2066 return ret;
2067}
2068
2069static void __exit sci_exit(void)
2070{
2071 platform_driver_unregister(&sci_driver);
2072 uart_unregister_driver(&sci_uart_driver);
2073}
2074
2075#ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2076early_platform_init_buffer("earlyprintk", &sci_driver,
2077 early_serial_buf, ARRAY_SIZE(early_serial_buf));
2078#endif
2079module_init(sci_init);
2080module_exit(sci_exit);
2081
2082MODULE_LICENSE("GPL");
2083MODULE_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..b04d937c9110
--- /dev/null
+++ b/drivers/tty/serial/sh-sci.h
@@ -0,0 +1,468 @@
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#elif defined(CONFIG_CPU_SUBTYPE_SH7705)
19# define SCIF0 0xA4400000
20# define SCIF2 0xA4410000
21# define SCPCR 0xA4000116
22# define SCPDR 0xA4000136
23#elif defined(CONFIG_CPU_SUBTYPE_SH7720) || \
24 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
25 defined(CONFIG_ARCH_SH73A0) || \
26 defined(CONFIG_ARCH_SH7367) || \
27 defined(CONFIG_ARCH_SH7377) || \
28 defined(CONFIG_ARCH_SH7372)
29# define PORT_PTCR 0xA405011EUL
30# define PORT_PVCR 0xA4050122UL
31# define SCIF_ORER 0x0200 /* overrun error bit */
32#elif defined(CONFIG_SH_RTS7751R2D)
33# define SCSPTR1 0xFFE0001C /* 8 bit SCIF */
34# define SCSPTR2 0xFFE80020 /* 16 bit SCIF */
35# define SCIF_ORER 0x0001 /* overrun error bit */
36#elif defined(CONFIG_CPU_SUBTYPE_SH7750) || \
37 defined(CONFIG_CPU_SUBTYPE_SH7750R) || \
38 defined(CONFIG_CPU_SUBTYPE_SH7750S) || \
39 defined(CONFIG_CPU_SUBTYPE_SH7091) || \
40 defined(CONFIG_CPU_SUBTYPE_SH7751) || \
41 defined(CONFIG_CPU_SUBTYPE_SH7751R)
42# define SCSPTR1 0xffe0001c /* 8 bit SCI */
43# define SCSPTR2 0xFFE80020 /* 16 bit SCIF */
44# define SCIF_ORER 0x0001 /* overrun error bit */
45#elif defined(CONFIG_CPU_SUBTYPE_SH7760)
46# define SCSPTR0 0xfe600024 /* 16 bit SCIF */
47# define SCSPTR1 0xfe610024 /* 16 bit SCIF */
48# define SCSPTR2 0xfe620024 /* 16 bit SCIF */
49# define SCIF_ORER 0x0001 /* overrun error bit */
50#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
51# define SCSPTR0 0xA4400000 /* 16 bit SCIF */
52# define SCIF_ORER 0x0001 /* overrun error bit */
53# define PACR 0xa4050100
54# define PBCR 0xa4050102
55#elif defined(CONFIG_CPU_SUBTYPE_SH7343)
56# define SCSPTR0 0xffe00010 /* 16 bit SCIF */
57#elif defined(CONFIG_CPU_SUBTYPE_SH7722)
58# define PADR 0xA4050120
59# define PSDR 0xA405013e
60# define PWDR 0xA4050166
61# define PSCR 0xA405011E
62# define SCIF_ORER 0x0001 /* overrun error bit */
63#elif defined(CONFIG_CPU_SUBTYPE_SH7366)
64# define SCPDR0 0xA405013E /* 16 bit SCIF0 PSDR */
65# define SCSPTR0 SCPDR0
66# define SCIF_ORER 0x0001 /* overrun error bit */
67#elif defined(CONFIG_CPU_SUBTYPE_SH7723)
68# define SCSPTR0 0xa4050160
69# define SCIF_ORER 0x0001 /* overrun error bit */
70#elif defined(CONFIG_CPU_SUBTYPE_SH7724)
71# define SCIF_ORER 0x0001 /* overrun error bit */
72#elif defined(CONFIG_CPU_SUBTYPE_SH4_202)
73# define SCSPTR2 0xffe80020 /* 16 bit SCIF */
74# define SCIF_ORER 0x0001 /* overrun error bit */
75#elif defined(CONFIG_H83007) || defined(CONFIG_H83068)
76# define H8300_SCI_DR(ch) *(volatile char *)(P1DR + h8300_sci_pins[ch].port)
77#elif defined(CONFIG_H8S2678)
78# define H8300_SCI_DR(ch) *(volatile char *)(P1DR + h8300_sci_pins[ch].port)
79#elif defined(CONFIG_CPU_SUBTYPE_SH7757)
80# define SCSPTR0 0xfe4b0020
81# define SCIF_ORER 0x0001
82#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
83# define SCSPTR0 0xffe00024 /* 16 bit SCIF */
84# define SCIF_ORER 0x0001 /* overrun error bit */
85#elif defined(CONFIG_CPU_SUBTYPE_SH7770)
86# define SCSPTR0 0xff923020 /* 16 bit SCIF */
87# define SCIF_ORER 0x0001 /* overrun error bit */
88#elif defined(CONFIG_CPU_SUBTYPE_SH7780)
89# define SCSPTR0 0xffe00024 /* 16 bit SCIF */
90# define SCIF_ORER 0x0001 /* Overrun error bit */
91#elif defined(CONFIG_CPU_SUBTYPE_SH7785) || \
92 defined(CONFIG_CPU_SUBTYPE_SH7786)
93# define SCSPTR0 0xffea0024 /* 16 bit SCIF */
94# define SCIF_ORER 0x0001 /* Overrun error bit */
95#elif defined(CONFIG_CPU_SUBTYPE_SH7201) || \
96 defined(CONFIG_CPU_SUBTYPE_SH7203) || \
97 defined(CONFIG_CPU_SUBTYPE_SH7206) || \
98 defined(CONFIG_CPU_SUBTYPE_SH7263)
99# define SCSPTR0 0xfffe8020 /* 16 bit SCIF */
100#elif defined(CONFIG_CPU_SUBTYPE_SH7619)
101# define SCSPTR0 0xf8400020 /* 16 bit SCIF */
102# define SCIF_ORER 0x0001 /* overrun error bit */
103#elif defined(CONFIG_CPU_SUBTYPE_SHX3)
104# define SCSPTR0 0xffc30020 /* 16 bit SCIF */
105# define SCIF_ORER 0x0001 /* Overrun error bit */
106#else
107# error CPU subtype not defined
108#endif
109
110/* SCxSR SCI */
111#define SCI_TDRE 0x80 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
112#define SCI_RDRF 0x40 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
113#define SCI_ORER 0x20 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
114#define SCI_FER 0x10 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
115#define SCI_PER 0x08 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
116#define SCI_TEND 0x04 /* 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
117/* SCI_MPB 0x02 * 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
118/* SCI_MPBT 0x01 * 7707 SCI, 7708 SCI, 7709 SCI, 7750 SCI */
119
120#define SCI_ERRORS ( SCI_PER | SCI_FER | SCI_ORER)
121
122/* SCxSR SCIF */
123#define SCIF_ER 0x0080 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
124#define SCIF_TEND 0x0040 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
125#define SCIF_TDFE 0x0020 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
126#define SCIF_BRK 0x0010 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
127#define SCIF_FER 0x0008 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
128#define SCIF_PER 0x0004 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
129#define SCIF_RDF 0x0002 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
130#define SCIF_DR 0x0001 /* 7705 SCIF, 7707 SCIF, 7709 SCIF, 7750 SCIF */
131
132#if defined(CONFIG_CPU_SUBTYPE_SH7705) || \
133 defined(CONFIG_CPU_SUBTYPE_SH7720) || \
134 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
135 defined(CONFIG_ARCH_SH73A0) || \
136 defined(CONFIG_ARCH_SH7367) || \
137 defined(CONFIG_ARCH_SH7377) || \
138 defined(CONFIG_ARCH_SH7372)
139# define SCIF_ORER 0x0200
140# define SCIF_ERRORS ( SCIF_PER | SCIF_FER | SCIF_ER | SCIF_BRK | SCIF_ORER)
141# define SCIF_RFDC_MASK 0x007f
142# define SCIF_TXROOM_MAX 64
143#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
144# define SCIF_ERRORS ( SCIF_PER | SCIF_FER | SCIF_ER | SCIF_BRK )
145# define SCIF_RFDC_MASK 0x007f
146# define SCIF_TXROOM_MAX 64
147/* SH7763 SCIF2 support */
148# define SCIF2_RFDC_MASK 0x001f
149# define SCIF2_TXROOM_MAX 16
150#else
151# define SCIF_ERRORS ( SCIF_PER | SCIF_FER | SCIF_ER | SCIF_BRK)
152# define SCIF_RFDC_MASK 0x001f
153# define SCIF_TXROOM_MAX 16
154#endif
155
156#ifndef SCIF_ORER
157#define SCIF_ORER 0x0000
158#endif
159
160#define SCxSR_TEND(port) (((port)->type == PORT_SCI) ? SCI_TEND : SCIF_TEND)
161#define SCxSR_ERRORS(port) (((port)->type == PORT_SCI) ? SCI_ERRORS : SCIF_ERRORS)
162#define SCxSR_RDxF(port) (((port)->type == PORT_SCI) ? SCI_RDRF : SCIF_RDF)
163#define SCxSR_TDxE(port) (((port)->type == PORT_SCI) ? SCI_TDRE : SCIF_TDFE)
164#define SCxSR_FER(port) (((port)->type == PORT_SCI) ? SCI_FER : SCIF_FER)
165#define SCxSR_PER(port) (((port)->type == PORT_SCI) ? SCI_PER : SCIF_PER)
166#define SCxSR_BRK(port) (((port)->type == PORT_SCI) ? 0x00 : SCIF_BRK)
167#define SCxSR_ORER(port) (((port)->type == PORT_SCI) ? SCI_ORER : SCIF_ORER)
168
169#if defined(CONFIG_CPU_SUBTYPE_SH7705) || \
170 defined(CONFIG_CPU_SUBTYPE_SH7720) || \
171 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
172 defined(CONFIG_ARCH_SH73A0) || \
173 defined(CONFIG_ARCH_SH7367) || \
174 defined(CONFIG_ARCH_SH7377) || \
175 defined(CONFIG_ARCH_SH7372)
176# define SCxSR_RDxF_CLEAR(port) (sci_in(port, SCxSR) & 0xfffc)
177# define SCxSR_ERROR_CLEAR(port) (sci_in(port, SCxSR) & 0xfd73)
178# define SCxSR_TDxE_CLEAR(port) (sci_in(port, SCxSR) & 0xffdf)
179# define SCxSR_BREAK_CLEAR(port) (sci_in(port, SCxSR) & 0xffe3)
180#else
181# define SCxSR_RDxF_CLEAR(port) (((port)->type == PORT_SCI) ? 0xbc : 0x00fc)
182# define SCxSR_ERROR_CLEAR(port) (((port)->type == PORT_SCI) ? 0xc4 : 0x0073)
183# define SCxSR_TDxE_CLEAR(port) (((port)->type == PORT_SCI) ? 0x78 : 0x00df)
184# define SCxSR_BREAK_CLEAR(port) (((port)->type == PORT_SCI) ? 0xc4 : 0x00e3)
185#endif
186
187/* SCFCR */
188#define SCFCR_RFRST 0x0002
189#define SCFCR_TFRST 0x0004
190#define SCFCR_MCE 0x0008
191
192#define SCI_MAJOR 204
193#define SCI_MINOR_START 8
194
195#define SCI_IN(size, offset) \
196 if ((size) == 8) { \
197 return ioread8(port->membase + (offset)); \
198 } else { \
199 return ioread16(port->membase + (offset)); \
200 }
201#define SCI_OUT(size, offset, value) \
202 if ((size) == 8) { \
203 iowrite8(value, port->membase + (offset)); \
204 } else if ((size) == 16) { \
205 iowrite16(value, port->membase + (offset)); \
206 }
207
208#define CPU_SCIx_FNS(name, sci_offset, sci_size, scif_offset, scif_size)\
209 static inline unsigned int sci_##name##_in(struct uart_port *port) \
210 { \
211 if (port->type == PORT_SCIF || port->type == PORT_SCIFB) { \
212 SCI_IN(scif_size, scif_offset) \
213 } else { /* PORT_SCI or PORT_SCIFA */ \
214 SCI_IN(sci_size, sci_offset); \
215 } \
216 } \
217 static inline void sci_##name##_out(struct uart_port *port, unsigned int value) \
218 { \
219 if (port->type == PORT_SCIF || port->type == PORT_SCIFB) { \
220 SCI_OUT(scif_size, scif_offset, value) \
221 } else { /* PORT_SCI or PORT_SCIFA */ \
222 SCI_OUT(sci_size, sci_offset, value); \
223 } \
224 }
225
226#ifdef CONFIG_H8300
227/* h8300 don't have SCIF */
228#define CPU_SCIF_FNS(name) \
229 static inline unsigned int sci_##name##_in(struct uart_port *port) \
230 { \
231 return 0; \
232 } \
233 static inline void sci_##name##_out(struct uart_port *port, unsigned int value) \
234 { \
235 }
236#else
237#define CPU_SCIF_FNS(name, scif_offset, scif_size) \
238 static inline unsigned int sci_##name##_in(struct uart_port *port) \
239 { \
240 SCI_IN(scif_size, scif_offset); \
241 } \
242 static inline void sci_##name##_out(struct uart_port *port, unsigned int value) \
243 { \
244 SCI_OUT(scif_size, scif_offset, value); \
245 }
246#endif
247
248#define CPU_SCI_FNS(name, sci_offset, sci_size) \
249 static inline unsigned int sci_##name##_in(struct uart_port* port) \
250 { \
251 SCI_IN(sci_size, sci_offset); \
252 } \
253 static inline void sci_##name##_out(struct uart_port* port, unsigned int value) \
254 { \
255 SCI_OUT(sci_size, sci_offset, value); \
256 }
257
258#if defined(CONFIG_CPU_SH3) || \
259 defined(CONFIG_ARCH_SH73A0) || \
260 defined(CONFIG_ARCH_SH7367) || \
261 defined(CONFIG_ARCH_SH7377) || \
262 defined(CONFIG_ARCH_SH7372)
263#if defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
264#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
265 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
266 h8_sci_offset, h8_sci_size) \
267 CPU_SCIx_FNS(name, sh4_sci_offset, sh4_sci_size, sh4_scif_offset, sh4_scif_size)
268#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
269 CPU_SCIF_FNS(name, sh4_scif_offset, sh4_scif_size)
270#elif defined(CONFIG_CPU_SUBTYPE_SH7705) || \
271 defined(CONFIG_CPU_SUBTYPE_SH7720) || \
272 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
273 defined(CONFIG_ARCH_SH7367)
274#define SCIF_FNS(name, scif_offset, scif_size) \
275 CPU_SCIF_FNS(name, scif_offset, scif_size)
276#elif defined(CONFIG_ARCH_SH7377) || \
277 defined(CONFIG_ARCH_SH7372) || \
278 defined(CONFIG_ARCH_SH73A0)
279#define SCIx_FNS(name, sh4_scifa_offset, sh4_scifa_size, sh4_scifb_offset, sh4_scifb_size) \
280 CPU_SCIx_FNS(name, sh4_scifa_offset, sh4_scifa_size, sh4_scifb_offset, sh4_scifb_size)
281#define SCIF_FNS(name, scif_offset, scif_size) \
282 CPU_SCIF_FNS(name, scif_offset, scif_size)
283#else
284#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
285 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
286 h8_sci_offset, h8_sci_size) \
287 CPU_SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh3_scif_offset, sh3_scif_size)
288#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
289 CPU_SCIF_FNS(name, sh3_scif_offset, sh3_scif_size)
290#endif
291#elif defined(__H8300H__) || defined(__H8300S__)
292#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
293 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
294 h8_sci_offset, h8_sci_size) \
295 CPU_SCI_FNS(name, h8_sci_offset, h8_sci_size)
296#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
297 CPU_SCIF_FNS(name)
298#elif defined(CONFIG_CPU_SUBTYPE_SH7723) ||\
299 defined(CONFIG_CPU_SUBTYPE_SH7724)
300 #define SCIx_FNS(name, sh4_scifa_offset, sh4_scifa_size, sh4_scif_offset, sh4_scif_size) \
301 CPU_SCIx_FNS(name, sh4_scifa_offset, sh4_scifa_size, sh4_scif_offset, sh4_scif_size)
302 #define SCIF_FNS(name, sh4_scif_offset, sh4_scif_size) \
303 CPU_SCIF_FNS(name, sh4_scif_offset, sh4_scif_size)
304#else
305#define SCIx_FNS(name, sh3_sci_offset, sh3_sci_size, sh4_sci_offset, sh4_sci_size, \
306 sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size, \
307 h8_sci_offset, h8_sci_size) \
308 CPU_SCIx_FNS(name, sh4_sci_offset, sh4_sci_size, sh4_scif_offset, sh4_scif_size)
309#define SCIF_FNS(name, sh3_scif_offset, sh3_scif_size, sh4_scif_offset, sh4_scif_size) \
310 CPU_SCIF_FNS(name, sh4_scif_offset, sh4_scif_size)
311#endif
312
313#if defined(CONFIG_CPU_SUBTYPE_SH7705) || \
314 defined(CONFIG_CPU_SUBTYPE_SH7720) || \
315 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
316 defined(CONFIG_ARCH_SH7367)
317
318SCIF_FNS(SCSMR, 0x00, 16)
319SCIF_FNS(SCBRR, 0x04, 8)
320SCIF_FNS(SCSCR, 0x08, 16)
321SCIF_FNS(SCxSR, 0x14, 16)
322SCIF_FNS(SCFCR, 0x18, 16)
323SCIF_FNS(SCFDR, 0x1c, 16)
324SCIF_FNS(SCxTDR, 0x20, 8)
325SCIF_FNS(SCxRDR, 0x24, 8)
326SCIF_FNS(SCLSR, 0x00, 0)
327#elif defined(CONFIG_ARCH_SH7377) || \
328 defined(CONFIG_ARCH_SH7372) || \
329 defined(CONFIG_ARCH_SH73A0)
330SCIF_FNS(SCSMR, 0x00, 16)
331SCIF_FNS(SCBRR, 0x04, 8)
332SCIF_FNS(SCSCR, 0x08, 16)
333SCIF_FNS(SCTDSR, 0x0c, 16)
334SCIF_FNS(SCFER, 0x10, 16)
335SCIF_FNS(SCxSR, 0x14, 16)
336SCIF_FNS(SCFCR, 0x18, 16)
337SCIF_FNS(SCFDR, 0x1c, 16)
338SCIF_FNS(SCTFDR, 0x38, 16)
339SCIF_FNS(SCRFDR, 0x3c, 16)
340SCIx_FNS(SCxTDR, 0x20, 8, 0x40, 8)
341SCIx_FNS(SCxRDR, 0x24, 8, 0x60, 8)
342SCIF_FNS(SCLSR, 0x00, 0)
343#elif defined(CONFIG_CPU_SUBTYPE_SH7723) ||\
344 defined(CONFIG_CPU_SUBTYPE_SH7724)
345SCIx_FNS(SCSMR, 0x00, 16, 0x00, 16)
346SCIx_FNS(SCBRR, 0x04, 8, 0x04, 8)
347SCIx_FNS(SCSCR, 0x08, 16, 0x08, 16)
348SCIx_FNS(SCxTDR, 0x20, 8, 0x0c, 8)
349SCIx_FNS(SCxSR, 0x14, 16, 0x10, 16)
350SCIx_FNS(SCxRDR, 0x24, 8, 0x14, 8)
351SCIx_FNS(SCSPTR, 0, 0, 0, 0)
352SCIF_FNS(SCFCR, 0x18, 16)
353SCIF_FNS(SCFDR, 0x1c, 16)
354SCIF_FNS(SCLSR, 0x24, 16)
355#else
356/* reg SCI/SH3 SCI/SH4 SCIF/SH3 SCIF/SH4 SCI/H8*/
357/* name off sz off sz off sz off sz off sz*/
358SCIx_FNS(SCSMR, 0x00, 8, 0x00, 8, 0x00, 8, 0x00, 16, 0x00, 8)
359SCIx_FNS(SCBRR, 0x02, 8, 0x04, 8, 0x02, 8, 0x04, 8, 0x01, 8)
360SCIx_FNS(SCSCR, 0x04, 8, 0x08, 8, 0x04, 8, 0x08, 16, 0x02, 8)
361SCIx_FNS(SCxTDR, 0x06, 8, 0x0c, 8, 0x06, 8, 0x0C, 8, 0x03, 8)
362SCIx_FNS(SCxSR, 0x08, 8, 0x10, 8, 0x08, 16, 0x10, 16, 0x04, 8)
363SCIx_FNS(SCxRDR, 0x0a, 8, 0x14, 8, 0x0A, 8, 0x14, 8, 0x05, 8)
364SCIF_FNS(SCFCR, 0x0c, 8, 0x18, 16)
365#if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
366 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
367 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
368 defined(CONFIG_CPU_SUBTYPE_SH7786)
369SCIF_FNS(SCFDR, 0x0e, 16, 0x1C, 16)
370SCIF_FNS(SCTFDR, 0x0e, 16, 0x1C, 16)
371SCIF_FNS(SCRFDR, 0x0e, 16, 0x20, 16)
372SCIF_FNS(SCSPTR, 0, 0, 0x24, 16)
373SCIF_FNS(SCLSR, 0, 0, 0x28, 16)
374#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
375SCIF_FNS(SCFDR, 0, 0, 0x1C, 16)
376SCIF_FNS(SCTFDR, 0x0e, 16, 0x1C, 16)
377SCIF_FNS(SCRFDR, 0x0e, 16, 0x20, 16)
378SCIF_FNS(SCSPTR, 0, 0, 0x24, 16)
379SCIF_FNS(SCLSR, 0, 0, 0x28, 16)
380#else
381SCIF_FNS(SCFDR, 0x0e, 16, 0x1C, 16)
382#if defined(CONFIG_CPU_SUBTYPE_SH7722)
383SCIF_FNS(SCSPTR, 0, 0, 0, 0)
384#else
385SCIF_FNS(SCSPTR, 0, 0, 0x20, 16)
386#endif
387SCIF_FNS(SCLSR, 0, 0, 0x24, 16)
388#endif
389#endif
390#define sci_in(port, reg) sci_##reg##_in(port)
391#define sci_out(port, reg, value) sci_##reg##_out(port, value)
392
393/* H8/300 series SCI pins assignment */
394#if defined(__H8300H__) || defined(__H8300S__)
395static const struct __attribute__((packed)) {
396 int port; /* GPIO port no */
397 unsigned short rx,tx; /* GPIO bit no */
398} h8300_sci_pins[] = {
399#if defined(CONFIG_H83007) || defined(CONFIG_H83068)
400 { /* SCI0 */
401 .port = H8300_GPIO_P9,
402 .rx = H8300_GPIO_B2,
403 .tx = H8300_GPIO_B0,
404 },
405 { /* SCI1 */
406 .port = H8300_GPIO_P9,
407 .rx = H8300_GPIO_B3,
408 .tx = H8300_GPIO_B1,
409 },
410 { /* SCI2 */
411 .port = H8300_GPIO_PB,
412 .rx = H8300_GPIO_B7,
413 .tx = H8300_GPIO_B6,
414 }
415#elif defined(CONFIG_H8S2678)
416 { /* SCI0 */
417 .port = H8300_GPIO_P3,
418 .rx = H8300_GPIO_B2,
419 .tx = H8300_GPIO_B0,
420 },
421 { /* SCI1 */
422 .port = H8300_GPIO_P3,
423 .rx = H8300_GPIO_B3,
424 .tx = H8300_GPIO_B1,
425 },
426 { /* SCI2 */
427 .port = H8300_GPIO_P5,
428 .rx = H8300_GPIO_B1,
429 .tx = H8300_GPIO_B0,
430 }
431#endif
432};
433#endif
434
435#if defined(CONFIG_CPU_SUBTYPE_SH7706) || \
436 defined(CONFIG_CPU_SUBTYPE_SH7707) || \
437 defined(CONFIG_CPU_SUBTYPE_SH7708) || \
438 defined(CONFIG_CPU_SUBTYPE_SH7709)
439static inline int sci_rxd_in(struct uart_port *port)
440{
441 if (port->mapbase == 0xfffffe80)
442 return __raw_readb(SCPDR)&0x01 ? 1 : 0; /* SCI */
443 return 1;
444}
445#elif defined(CONFIG_CPU_SUBTYPE_SH7750) || \
446 defined(CONFIG_CPU_SUBTYPE_SH7751) || \
447 defined(CONFIG_CPU_SUBTYPE_SH7751R) || \
448 defined(CONFIG_CPU_SUBTYPE_SH7750R) || \
449 defined(CONFIG_CPU_SUBTYPE_SH7750S) || \
450 defined(CONFIG_CPU_SUBTYPE_SH7091)
451static inline int sci_rxd_in(struct uart_port *port)
452{
453 if (port->mapbase == 0xffe00000)
454 return __raw_readb(SCSPTR1)&0x01 ? 1 : 0; /* SCI */
455 return 1;
456}
457#elif defined(__H8300H__) || defined(__H8300S__)
458static inline int sci_rxd_in(struct uart_port *port)
459{
460 int ch = (port->mapbase - SMR0) >> 3;
461 return (H8300_SCI_DR(ch) & h8300_sci_pins[ch].rx) ? 1 : 0;
462}
463#else /* default case for non-SCI processors */
464static inline int sci_rxd_in(struct uart_port *port)
465{
466 return 1;
467}
468#endif
diff --git a/drivers/tty/serial/sn_console.c b/drivers/tty/serial/sn_console.c
new file mode 100644
index 000000000000..377ae74e7154
--- /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). initially, 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 occurred 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..c0b7246d7339
--- /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)
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 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 platform_driver_register(&hv_driver);
648}
649
650static void __exit sunhv_exit(void)
651{
652 platform_driver_unregister(&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..b5fa2a57b9da
--- /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)
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 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 platform_driver_register(&sab_driver);
1134}
1135
1136static void __exit sunsab_exit(void)
1137{
1138 platform_driver_unregister(&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..92aa54550e84
--- /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)
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 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 = platform_driver_register(&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..8e916e76b7b5
--- /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 becoming 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)
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 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 = platform_driver_register(&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 platform_driver_unregister(&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 platform_driver_unregister(&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..8af1ed83a4c0
--- /dev/null
+++ b/drivers/tty/serial/uartlite.c
@@ -0,0 +1,654 @@
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#include <linux/of.h>
23#include <linux/of_address.h>
24#include <linux/of_device.h>
25#include <linux/of_platform.h>
26
27#define ULITE_NAME "ttyUL"
28#define ULITE_MAJOR 204
29#define ULITE_MINOR 187
30#define ULITE_NR_UARTS 4
31
32/* ---------------------------------------------------------------------
33 * Register definitions
34 *
35 * For register details see datasheet:
36 * http://www.xilinx.com/support/documentation/ip_documentation/opb_uartlite.pdf
37 */
38
39#define ULITE_RX 0x00
40#define ULITE_TX 0x04
41#define ULITE_STATUS 0x08
42#define ULITE_CONTROL 0x0c
43
44#define ULITE_REGION 16
45
46#define ULITE_STATUS_RXVALID 0x01
47#define ULITE_STATUS_RXFULL 0x02
48#define ULITE_STATUS_TXEMPTY 0x04
49#define ULITE_STATUS_TXFULL 0x08
50#define ULITE_STATUS_IE 0x10
51#define ULITE_STATUS_OVERRUN 0x20
52#define ULITE_STATUS_FRAME 0x40
53#define ULITE_STATUS_PARITY 0x80
54
55#define ULITE_CONTROL_RST_TX 0x01
56#define ULITE_CONTROL_RST_RX 0x02
57#define ULITE_CONTROL_IE 0x10
58
59
60static struct uart_port ulite_ports[ULITE_NR_UARTS];
61
62/* ---------------------------------------------------------------------
63 * Core UART driver operations
64 */
65
66static int ulite_receive(struct uart_port *port, int stat)
67{
68 struct tty_struct *tty = port->state->port.tty;
69 unsigned char ch = 0;
70 char flag = TTY_NORMAL;
71
72 if ((stat & (ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
73 | ULITE_STATUS_FRAME)) == 0)
74 return 0;
75
76 /* stats */
77 if (stat & ULITE_STATUS_RXVALID) {
78 port->icount.rx++;
79 ch = ioread32be(port->membase + ULITE_RX);
80
81 if (stat & ULITE_STATUS_PARITY)
82 port->icount.parity++;
83 }
84
85 if (stat & ULITE_STATUS_OVERRUN)
86 port->icount.overrun++;
87
88 if (stat & ULITE_STATUS_FRAME)
89 port->icount.frame++;
90
91
92 /* drop byte with parity error if IGNPAR specificed */
93 if (stat & port->ignore_status_mask & ULITE_STATUS_PARITY)
94 stat &= ~ULITE_STATUS_RXVALID;
95
96 stat &= port->read_status_mask;
97
98 if (stat & ULITE_STATUS_PARITY)
99 flag = TTY_PARITY;
100
101
102 stat &= ~port->ignore_status_mask;
103
104 if (stat & ULITE_STATUS_RXVALID)
105 tty_insert_flip_char(tty, ch, flag);
106
107 if (stat & ULITE_STATUS_FRAME)
108 tty_insert_flip_char(tty, 0, TTY_FRAME);
109
110 if (stat & ULITE_STATUS_OVERRUN)
111 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
112
113 return 1;
114}
115
116static int ulite_transmit(struct uart_port *port, int stat)
117{
118 struct circ_buf *xmit = &port->state->xmit;
119
120 if (stat & ULITE_STATUS_TXFULL)
121 return 0;
122
123 if (port->x_char) {
124 iowrite32be(port->x_char, port->membase + ULITE_TX);
125 port->x_char = 0;
126 port->icount.tx++;
127 return 1;
128 }
129
130 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
131 return 0;
132
133 iowrite32be(xmit->buf[xmit->tail], port->membase + ULITE_TX);
134 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
135 port->icount.tx++;
136
137 /* wake up */
138 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
139 uart_write_wakeup(port);
140
141 return 1;
142}
143
144static irqreturn_t ulite_isr(int irq, void *dev_id)
145{
146 struct uart_port *port = dev_id;
147 int busy, n = 0;
148
149 do {
150 int stat = ioread32be(port->membase + ULITE_STATUS);
151 busy = ulite_receive(port, stat);
152 busy |= ulite_transmit(port, stat);
153 n++;
154 } while (busy);
155
156 /* work done? */
157 if (n > 1) {
158 tty_flip_buffer_push(port->state->port.tty);
159 return IRQ_HANDLED;
160 } else {
161 return IRQ_NONE;
162 }
163}
164
165static unsigned int ulite_tx_empty(struct uart_port *port)
166{
167 unsigned long flags;
168 unsigned int ret;
169
170 spin_lock_irqsave(&port->lock, flags);
171 ret = ioread32be(port->membase + ULITE_STATUS);
172 spin_unlock_irqrestore(&port->lock, flags);
173
174 return ret & ULITE_STATUS_TXEMPTY ? TIOCSER_TEMT : 0;
175}
176
177static unsigned int ulite_get_mctrl(struct uart_port *port)
178{
179 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
180}
181
182static void ulite_set_mctrl(struct uart_port *port, unsigned int mctrl)
183{
184 /* N/A */
185}
186
187static void ulite_stop_tx(struct uart_port *port)
188{
189 /* N/A */
190}
191
192static void ulite_start_tx(struct uart_port *port)
193{
194 ulite_transmit(port, ioread32be(port->membase + ULITE_STATUS));
195}
196
197static void ulite_stop_rx(struct uart_port *port)
198{
199 /* don't forward any more data (like !CREAD) */
200 port->ignore_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
201 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
202}
203
204static void ulite_enable_ms(struct uart_port *port)
205{
206 /* N/A */
207}
208
209static void ulite_break_ctl(struct uart_port *port, int ctl)
210{
211 /* N/A */
212}
213
214static int ulite_startup(struct uart_port *port)
215{
216 int ret;
217
218 ret = request_irq(port->irq, ulite_isr,
219 IRQF_SHARED | IRQF_SAMPLE_RANDOM, "uartlite", port);
220 if (ret)
221 return ret;
222
223 iowrite32be(ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX,
224 port->membase + ULITE_CONTROL);
225 iowrite32be(ULITE_CONTROL_IE, port->membase + ULITE_CONTROL);
226
227 return 0;
228}
229
230static void ulite_shutdown(struct uart_port *port)
231{
232 iowrite32be(0, port->membase + ULITE_CONTROL);
233 ioread32be(port->membase + ULITE_CONTROL); /* dummy */
234 free_irq(port->irq, port);
235}
236
237static void ulite_set_termios(struct uart_port *port, struct ktermios *termios,
238 struct ktermios *old)
239{
240 unsigned long flags;
241 unsigned int baud;
242
243 spin_lock_irqsave(&port->lock, flags);
244
245 port->read_status_mask = ULITE_STATUS_RXVALID | ULITE_STATUS_OVERRUN
246 | ULITE_STATUS_TXFULL;
247
248 if (termios->c_iflag & INPCK)
249 port->read_status_mask |=
250 ULITE_STATUS_PARITY | ULITE_STATUS_FRAME;
251
252 port->ignore_status_mask = 0;
253 if (termios->c_iflag & IGNPAR)
254 port->ignore_status_mask |= ULITE_STATUS_PARITY
255 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
256
257 /* ignore all characters if CREAD is not set */
258 if ((termios->c_cflag & CREAD) == 0)
259 port->ignore_status_mask |=
260 ULITE_STATUS_RXVALID | ULITE_STATUS_PARITY
261 | ULITE_STATUS_FRAME | ULITE_STATUS_OVERRUN;
262
263 /* update timeout */
264 baud = uart_get_baud_rate(port, termios, old, 0, 460800);
265 uart_update_timeout(port, termios->c_cflag, baud);
266
267 spin_unlock_irqrestore(&port->lock, flags);
268}
269
270static const char *ulite_type(struct uart_port *port)
271{
272 return port->type == PORT_UARTLITE ? "uartlite" : NULL;
273}
274
275static void ulite_release_port(struct uart_port *port)
276{
277 release_mem_region(port->mapbase, ULITE_REGION);
278 iounmap(port->membase);
279 port->membase = NULL;
280}
281
282static int ulite_request_port(struct uart_port *port)
283{
284 pr_debug("ulite console: port=%p; port->mapbase=%llx\n",
285 port, (unsigned long long) port->mapbase);
286
287 if (!request_mem_region(port->mapbase, ULITE_REGION, "uartlite")) {
288 dev_err(port->dev, "Memory region busy\n");
289 return -EBUSY;
290 }
291
292 port->membase = ioremap(port->mapbase, ULITE_REGION);
293 if (!port->membase) {
294 dev_err(port->dev, "Unable to map registers\n");
295 release_mem_region(port->mapbase, ULITE_REGION);
296 return -EBUSY;
297 }
298
299 return 0;
300}
301
302static void ulite_config_port(struct uart_port *port, int flags)
303{
304 if (!ulite_request_port(port))
305 port->type = PORT_UARTLITE;
306}
307
308static int ulite_verify_port(struct uart_port *port, struct serial_struct *ser)
309{
310 /* we don't want the core code to modify any port params */
311 return -EINVAL;
312}
313
314#ifdef CONFIG_CONSOLE_POLL
315static int ulite_get_poll_char(struct uart_port *port)
316{
317 if (!(ioread32be(port->membase + ULITE_STATUS)
318 & ULITE_STATUS_RXVALID))
319 return NO_POLL_CHAR;
320
321 return ioread32be(port->membase + ULITE_RX);
322}
323
324static void ulite_put_poll_char(struct uart_port *port, unsigned char ch)
325{
326 while (ioread32be(port->membase + ULITE_STATUS) & ULITE_STATUS_TXFULL)
327 cpu_relax();
328
329 /* write char to device */
330 iowrite32be(ch, port->membase + ULITE_TX);
331}
332#endif
333
334static struct uart_ops ulite_ops = {
335 .tx_empty = ulite_tx_empty,
336 .set_mctrl = ulite_set_mctrl,
337 .get_mctrl = ulite_get_mctrl,
338 .stop_tx = ulite_stop_tx,
339 .start_tx = ulite_start_tx,
340 .stop_rx = ulite_stop_rx,
341 .enable_ms = ulite_enable_ms,
342 .break_ctl = ulite_break_ctl,
343 .startup = ulite_startup,
344 .shutdown = ulite_shutdown,
345 .set_termios = ulite_set_termios,
346 .type = ulite_type,
347 .release_port = ulite_release_port,
348 .request_port = ulite_request_port,
349 .config_port = ulite_config_port,
350 .verify_port = ulite_verify_port,
351#ifdef CONFIG_CONSOLE_POLL
352 .poll_get_char = ulite_get_poll_char,
353 .poll_put_char = ulite_put_poll_char,
354#endif
355};
356
357/* ---------------------------------------------------------------------
358 * Console driver operations
359 */
360
361#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
362static void ulite_console_wait_tx(struct uart_port *port)
363{
364 int i;
365 u8 val;
366
367 /* Spin waiting for TX fifo to have space available */
368 for (i = 0; i < 100000; i++) {
369 val = ioread32be(port->membase + ULITE_STATUS);
370 if ((val & ULITE_STATUS_TXFULL) == 0)
371 break;
372 cpu_relax();
373 }
374}
375
376static void ulite_console_putchar(struct uart_port *port, int ch)
377{
378 ulite_console_wait_tx(port);
379 iowrite32be(ch, port->membase + ULITE_TX);
380}
381
382static void ulite_console_write(struct console *co, const char *s,
383 unsigned int count)
384{
385 struct uart_port *port = &ulite_ports[co->index];
386 unsigned long flags;
387 unsigned int ier;
388 int locked = 1;
389
390 if (oops_in_progress) {
391 locked = spin_trylock_irqsave(&port->lock, flags);
392 } else
393 spin_lock_irqsave(&port->lock, flags);
394
395 /* save and disable interrupt */
396 ier = ioread32be(port->membase + ULITE_STATUS) & ULITE_STATUS_IE;
397 iowrite32be(0, port->membase + ULITE_CONTROL);
398
399 uart_console_write(port, s, count, ulite_console_putchar);
400
401 ulite_console_wait_tx(port);
402
403 /* restore interrupt state */
404 if (ier)
405 iowrite32be(ULITE_CONTROL_IE, port->membase + ULITE_CONTROL);
406
407 if (locked)
408 spin_unlock_irqrestore(&port->lock, flags);
409}
410
411static int __devinit ulite_console_setup(struct console *co, char *options)
412{
413 struct uart_port *port;
414 int baud = 9600;
415 int bits = 8;
416 int parity = 'n';
417 int flow = 'n';
418
419 if (co->index < 0 || co->index >= ULITE_NR_UARTS)
420 return -EINVAL;
421
422 port = &ulite_ports[co->index];
423
424 /* Has the device been initialized yet? */
425 if (!port->mapbase) {
426 pr_debug("console on ttyUL%i not present\n", co->index);
427 return -ENODEV;
428 }
429
430 /* not initialized yet? */
431 if (!port->membase) {
432 if (ulite_request_port(port))
433 return -ENODEV;
434 }
435
436 if (options)
437 uart_parse_options(options, &baud, &parity, &bits, &flow);
438
439 return uart_set_options(port, co, baud, parity, bits, flow);
440}
441
442static struct uart_driver ulite_uart_driver;
443
444static struct console ulite_console = {
445 .name = ULITE_NAME,
446 .write = ulite_console_write,
447 .device = uart_console_device,
448 .setup = ulite_console_setup,
449 .flags = CON_PRINTBUFFER,
450 .index = -1, /* Specified on the cmdline (e.g. console=ttyUL0 ) */
451 .data = &ulite_uart_driver,
452};
453
454static int __init ulite_console_init(void)
455{
456 register_console(&ulite_console);
457 return 0;
458}
459
460console_initcall(ulite_console_init);
461
462#endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
463
464static struct uart_driver ulite_uart_driver = {
465 .owner = THIS_MODULE,
466 .driver_name = "uartlite",
467 .dev_name = ULITE_NAME,
468 .major = ULITE_MAJOR,
469 .minor = ULITE_MINOR,
470 .nr = ULITE_NR_UARTS,
471#ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
472 .cons = &ulite_console,
473#endif
474};
475
476/* ---------------------------------------------------------------------
477 * Port assignment functions (mapping devices to uart_port structures)
478 */
479
480/** ulite_assign: register a uartlite device with the driver
481 *
482 * @dev: pointer to device structure
483 * @id: requested id number. Pass -1 for automatic port assignment
484 * @base: base address of uartlite registers
485 * @irq: irq number for uartlite
486 *
487 * Returns: 0 on success, <0 otherwise
488 */
489static int __devinit ulite_assign(struct device *dev, int id, u32 base, int irq)
490{
491 struct uart_port *port;
492 int rc;
493
494 /* if id = -1; then scan for a free id and use that */
495 if (id < 0) {
496 for (id = 0; id < ULITE_NR_UARTS; id++)
497 if (ulite_ports[id].mapbase == 0)
498 break;
499 }
500 if (id < 0 || id >= ULITE_NR_UARTS) {
501 dev_err(dev, "%s%i too large\n", ULITE_NAME, id);
502 return -EINVAL;
503 }
504
505 if ((ulite_ports[id].mapbase) && (ulite_ports[id].mapbase != base)) {
506 dev_err(dev, "cannot assign to %s%i; it is already in use\n",
507 ULITE_NAME, id);
508 return -EBUSY;
509 }
510
511 port = &ulite_ports[id];
512
513 spin_lock_init(&port->lock);
514 port->fifosize = 16;
515 port->regshift = 2;
516 port->iotype = UPIO_MEM;
517 port->iobase = 1; /* mark port in use */
518 port->mapbase = base;
519 port->membase = NULL;
520 port->ops = &ulite_ops;
521 port->irq = irq;
522 port->flags = UPF_BOOT_AUTOCONF;
523 port->dev = dev;
524 port->type = PORT_UNKNOWN;
525 port->line = id;
526
527 dev_set_drvdata(dev, port);
528
529 /* Register the port */
530 rc = uart_add_one_port(&ulite_uart_driver, port);
531 if (rc) {
532 dev_err(dev, "uart_add_one_port() failed; err=%i\n", rc);
533 port->mapbase = 0;
534 dev_set_drvdata(dev, NULL);
535 return rc;
536 }
537
538 return 0;
539}
540
541/** ulite_release: register a uartlite device with the driver
542 *
543 * @dev: pointer to device structure
544 */
545static int __devexit ulite_release(struct device *dev)
546{
547 struct uart_port *port = dev_get_drvdata(dev);
548 int rc = 0;
549
550 if (port) {
551 rc = uart_remove_one_port(&ulite_uart_driver, port);
552 dev_set_drvdata(dev, NULL);
553 port->mapbase = 0;
554 }
555
556 return rc;
557}
558
559/* ---------------------------------------------------------------------
560 * Platform bus binding
561 */
562
563#if defined(CONFIG_OF)
564/* Match table for of_platform binding */
565static struct of_device_id ulite_of_match[] __devinitdata = {
566 { .compatible = "xlnx,opb-uartlite-1.00.b", },
567 { .compatible = "xlnx,xps-uartlite-1.00.a", },
568 {}
569};
570MODULE_DEVICE_TABLE(of, ulite_of_match);
571#else /* CONFIG_OF */
572#define ulite_of_match NULL
573#endif /* CONFIG_OF */
574
575static int __devinit ulite_probe(struct platform_device *pdev)
576{
577 struct resource *res, *res2;
578 int id = pdev->id;
579#ifdef CONFIG_OF
580 const __be32 *prop;
581
582 prop = of_get_property(pdev->dev.of_node, "port-number", NULL);
583 if (prop)
584 id = be32_to_cpup(prop);
585#endif
586
587 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
588 if (!res)
589 return -ENODEV;
590
591 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
592 if (!res2)
593 return -ENODEV;
594
595 return ulite_assign(&pdev->dev, id, res->start, res2->start);
596}
597
598static int __devexit ulite_remove(struct platform_device *pdev)
599{
600 return ulite_release(&pdev->dev);
601}
602
603/* work with hotplug and coldplug */
604MODULE_ALIAS("platform:uartlite");
605
606static struct platform_driver ulite_platform_driver = {
607 .probe = ulite_probe,
608 .remove = __devexit_p(ulite_remove),
609 .driver = {
610 .owner = THIS_MODULE,
611 .name = "uartlite",
612 .of_match_table = ulite_of_match,
613 },
614};
615
616/* ---------------------------------------------------------------------
617 * Module setup/teardown
618 */
619
620int __init ulite_init(void)
621{
622 int ret;
623
624 pr_debug("uartlite: calling uart_register_driver()\n");
625 ret = uart_register_driver(&ulite_uart_driver);
626 if (ret)
627 goto err_uart;
628
629 pr_debug("uartlite: calling platform_driver_register()\n");
630 ret = platform_driver_register(&ulite_platform_driver);
631 if (ret)
632 goto err_plat;
633
634 return 0;
635
636err_plat:
637 uart_unregister_driver(&ulite_uart_driver);
638err_uart:
639 printk(KERN_ERR "registering uartlite driver failed: err=%i", ret);
640 return ret;
641}
642
643void __exit ulite_exit(void)
644{
645 platform_driver_unregister(&ulite_platform_driver);
646 uart_unregister_driver(&ulite_uart_driver);
647}
648
649module_init(ulite_init);
650module_exit(ulite_exit);
651
652MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
653MODULE_DESCRIPTION("Xilinx uartlite serial driver");
654MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/ucc_uart.c b/drivers/tty/serial/ucc_uart.c
new file mode 100644
index 000000000000..c327218cad44
--- /dev/null
+++ b/drivers/tty/serial/ucc_uart.c
@@ -0,0 +1,1539 @@
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{
1199 struct device_node *np = ofdev->dev.of_node;
1200 const unsigned int *iprop; /* Integer OF properties */
1201 const char *sprop; /* String OF properties */
1202 struct uart_qe_port *qe_port = NULL;
1203 struct resource res;
1204 int ret;
1205
1206 /*
1207 * Determine if we need Soft-UART mode
1208 */
1209 if (of_find_property(np, "soft-uart", NULL)) {
1210 dev_dbg(&ofdev->dev, "using Soft-UART mode\n");
1211 soft_uart = 1;
1212 }
1213
1214 /*
1215 * If we are using Soft-UART, determine if we need to upload the
1216 * firmware, too.
1217 */
1218 if (soft_uart) {
1219 struct qe_firmware_info *qe_fw_info;
1220
1221 qe_fw_info = qe_get_firmware_info();
1222
1223 /* Check if the firmware has been uploaded. */
1224 if (qe_fw_info && strstr(qe_fw_info->id, "Soft-UART")) {
1225 firmware_loaded = 1;
1226 } else {
1227 char filename[32];
1228 unsigned int soc;
1229 unsigned int rev_h;
1230 unsigned int rev_l;
1231
1232 soc = soc_info(&rev_h, &rev_l);
1233 if (!soc) {
1234 dev_err(&ofdev->dev, "unknown CPU model\n");
1235 return -ENXIO;
1236 }
1237 sprintf(filename, "fsl_qe_ucode_uart_%u_%u%u.bin",
1238 soc, rev_h, rev_l);
1239
1240 dev_info(&ofdev->dev, "waiting for firmware %s\n",
1241 filename);
1242
1243 /*
1244 * We call request_firmware_nowait instead of
1245 * request_firmware so that the driver can load and
1246 * initialize the ports without holding up the rest of
1247 * the kernel. If hotplug support is enabled in the
1248 * kernel, then we use it.
1249 */
1250 ret = request_firmware_nowait(THIS_MODULE,
1251 FW_ACTION_HOTPLUG, filename, &ofdev->dev,
1252 GFP_KERNEL, &ofdev->dev, uart_firmware_cont);
1253 if (ret) {
1254 dev_err(&ofdev->dev,
1255 "could not load firmware %s\n",
1256 filename);
1257 return ret;
1258 }
1259 }
1260 }
1261
1262 qe_port = kzalloc(sizeof(struct uart_qe_port), GFP_KERNEL);
1263 if (!qe_port) {
1264 dev_err(&ofdev->dev, "can't allocate QE port structure\n");
1265 return -ENOMEM;
1266 }
1267
1268 /* Search for IRQ and mapbase */
1269 ret = of_address_to_resource(np, 0, &res);
1270 if (ret) {
1271 dev_err(&ofdev->dev, "missing 'reg' property in device tree\n");
1272 goto out_free;
1273 }
1274 if (!res.start) {
1275 dev_err(&ofdev->dev, "invalid 'reg' property in device tree\n");
1276 ret = -EINVAL;
1277 goto out_free;
1278 }
1279 qe_port->port.mapbase = res.start;
1280
1281 /* Get the UCC number (device ID) */
1282 /* UCCs are numbered 1-7 */
1283 iprop = of_get_property(np, "cell-index", NULL);
1284 if (!iprop) {
1285 iprop = of_get_property(np, "device-id", NULL);
1286 if (!iprop) {
1287 dev_err(&ofdev->dev, "UCC is unspecified in "
1288 "device tree\n");
1289 ret = -EINVAL;
1290 goto out_free;
1291 }
1292 }
1293
1294 if ((*iprop < 1) || (*iprop > UCC_MAX_NUM)) {
1295 dev_err(&ofdev->dev, "no support for UCC%u\n", *iprop);
1296 ret = -ENODEV;
1297 goto out_free;
1298 }
1299 qe_port->ucc_num = *iprop - 1;
1300
1301 /*
1302 * In the future, we should not require the BRG to be specified in the
1303 * device tree. If no clock-source is specified, then just pick a BRG
1304 * to use. This requires a new QE library function that manages BRG
1305 * assignments.
1306 */
1307
1308 sprop = of_get_property(np, "rx-clock-name", NULL);
1309 if (!sprop) {
1310 dev_err(&ofdev->dev, "missing rx-clock-name in device tree\n");
1311 ret = -ENODEV;
1312 goto out_free;
1313 }
1314
1315 qe_port->us_info.rx_clock = qe_clock_source(sprop);
1316 if ((qe_port->us_info.rx_clock < QE_BRG1) ||
1317 (qe_port->us_info.rx_clock > QE_BRG16)) {
1318 dev_err(&ofdev->dev, "rx-clock-name must be a BRG for UART\n");
1319 ret = -ENODEV;
1320 goto out_free;
1321 }
1322
1323#ifdef LOOPBACK
1324 /* In internal loopback mode, TX and RX must use the same clock */
1325 qe_port->us_info.tx_clock = qe_port->us_info.rx_clock;
1326#else
1327 sprop = of_get_property(np, "tx-clock-name", NULL);
1328 if (!sprop) {
1329 dev_err(&ofdev->dev, "missing tx-clock-name in device tree\n");
1330 ret = -ENODEV;
1331 goto out_free;
1332 }
1333 qe_port->us_info.tx_clock = qe_clock_source(sprop);
1334#endif
1335 if ((qe_port->us_info.tx_clock < QE_BRG1) ||
1336 (qe_port->us_info.tx_clock > QE_BRG16)) {
1337 dev_err(&ofdev->dev, "tx-clock-name must be a BRG for UART\n");
1338 ret = -ENODEV;
1339 goto out_free;
1340 }
1341
1342 /* Get the port number, numbered 0-3 */
1343 iprop = of_get_property(np, "port-number", NULL);
1344 if (!iprop) {
1345 dev_err(&ofdev->dev, "missing port-number in device tree\n");
1346 ret = -EINVAL;
1347 goto out_free;
1348 }
1349 qe_port->port.line = *iprop;
1350 if (qe_port->port.line >= UCC_MAX_UART) {
1351 dev_err(&ofdev->dev, "port-number must be 0-%u\n",
1352 UCC_MAX_UART - 1);
1353 ret = -EINVAL;
1354 goto out_free;
1355 }
1356
1357 qe_port->port.irq = irq_of_parse_and_map(np, 0);
1358 if (qe_port->port.irq == NO_IRQ) {
1359 dev_err(&ofdev->dev, "could not map IRQ for UCC%u\n",
1360 qe_port->ucc_num + 1);
1361 ret = -EINVAL;
1362 goto out_free;
1363 }
1364
1365 /*
1366 * Newer device trees have an "fsl,qe" compatible property for the QE
1367 * node, but we still need to support older device trees.
1368 */
1369 np = of_find_compatible_node(NULL, NULL, "fsl,qe");
1370 if (!np) {
1371 np = of_find_node_by_type(NULL, "qe");
1372 if (!np) {
1373 dev_err(&ofdev->dev, "could not find 'qe' node\n");
1374 ret = -EINVAL;
1375 goto out_free;
1376 }
1377 }
1378
1379 iprop = of_get_property(np, "brg-frequency", NULL);
1380 if (!iprop) {
1381 dev_err(&ofdev->dev,
1382 "missing brg-frequency in device tree\n");
1383 ret = -EINVAL;
1384 goto out_np;
1385 }
1386
1387 if (*iprop)
1388 qe_port->port.uartclk = *iprop;
1389 else {
1390 /*
1391 * Older versions of U-Boot do not initialize the brg-frequency
1392 * property, so in this case we assume the BRG frequency is
1393 * half the QE bus frequency.
1394 */
1395 iprop = of_get_property(np, "bus-frequency", NULL);
1396 if (!iprop) {
1397 dev_err(&ofdev->dev,
1398 "missing QE bus-frequency in device tree\n");
1399 ret = -EINVAL;
1400 goto out_np;
1401 }
1402 if (*iprop)
1403 qe_port->port.uartclk = *iprop / 2;
1404 else {
1405 dev_err(&ofdev->dev,
1406 "invalid QE bus-frequency in device tree\n");
1407 ret = -EINVAL;
1408 goto out_np;
1409 }
1410 }
1411
1412 spin_lock_init(&qe_port->port.lock);
1413 qe_port->np = np;
1414 qe_port->port.dev = &ofdev->dev;
1415 qe_port->port.ops = &qe_uart_pops;
1416 qe_port->port.iotype = UPIO_MEM;
1417
1418 qe_port->tx_nrfifos = TX_NUM_FIFO;
1419 qe_port->tx_fifosize = TX_BUF_SIZE;
1420 qe_port->rx_nrfifos = RX_NUM_FIFO;
1421 qe_port->rx_fifosize = RX_BUF_SIZE;
1422
1423 qe_port->wait_closing = UCC_WAIT_CLOSING;
1424 qe_port->port.fifosize = 512;
1425 qe_port->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
1426
1427 qe_port->us_info.ucc_num = qe_port->ucc_num;
1428 qe_port->us_info.regs = (phys_addr_t) res.start;
1429 qe_port->us_info.irq = qe_port->port.irq;
1430
1431 qe_port->us_info.rx_bd_ring_len = qe_port->rx_nrfifos;
1432 qe_port->us_info.tx_bd_ring_len = qe_port->tx_nrfifos;
1433
1434 /* Make sure ucc_slow_init() initializes both TX and RX */
1435 qe_port->us_info.init_tx = 1;
1436 qe_port->us_info.init_rx = 1;
1437
1438 /* Add the port to the uart sub-system. This will cause
1439 * qe_uart_config_port() to be called, so the us_info structure must
1440 * be initialized.
1441 */
1442 ret = uart_add_one_port(&ucc_uart_driver, &qe_port->port);
1443 if (ret) {
1444 dev_err(&ofdev->dev, "could not add /dev/ttyQE%u\n",
1445 qe_port->port.line);
1446 goto out_np;
1447 }
1448
1449 dev_set_drvdata(&ofdev->dev, qe_port);
1450
1451 dev_info(&ofdev->dev, "UCC%u assigned to /dev/ttyQE%u\n",
1452 qe_port->ucc_num + 1, qe_port->port.line);
1453
1454 /* Display the mknod command for this device */
1455 dev_dbg(&ofdev->dev, "mknod command is 'mknod /dev/ttyQE%u c %u %u'\n",
1456 qe_port->port.line, SERIAL_QE_MAJOR,
1457 SERIAL_QE_MINOR + qe_port->port.line);
1458
1459 return 0;
1460out_np:
1461 of_node_put(np);
1462out_free:
1463 kfree(qe_port);
1464 return ret;
1465}
1466
1467static int ucc_uart_remove(struct platform_device *ofdev)
1468{
1469 struct uart_qe_port *qe_port = dev_get_drvdata(&ofdev->dev);
1470
1471 dev_info(&ofdev->dev, "removing /dev/ttyQE%u\n", qe_port->port.line);
1472
1473 uart_remove_one_port(&ucc_uart_driver, &qe_port->port);
1474
1475 dev_set_drvdata(&ofdev->dev, NULL);
1476 kfree(qe_port);
1477
1478 return 0;
1479}
1480
1481static struct of_device_id ucc_uart_match[] = {
1482 {
1483 .type = "serial",
1484 .compatible = "ucc_uart",
1485 },
1486 {},
1487};
1488MODULE_DEVICE_TABLE(of, ucc_uart_match);
1489
1490static struct platform_driver ucc_uart_of_driver = {
1491 .driver = {
1492 .name = "ucc_uart",
1493 .owner = THIS_MODULE,
1494 .of_match_table = ucc_uart_match,
1495 },
1496 .probe = ucc_uart_probe,
1497 .remove = ucc_uart_remove,
1498};
1499
1500static int __init ucc_uart_init(void)
1501{
1502 int ret;
1503
1504 printk(KERN_INFO "Freescale QUICC Engine UART device driver\n");
1505#ifdef LOOPBACK
1506 printk(KERN_INFO "ucc-uart: Using loopback mode\n");
1507#endif
1508
1509 ret = uart_register_driver(&ucc_uart_driver);
1510 if (ret) {
1511 printk(KERN_ERR "ucc-uart: could not register UART driver\n");
1512 return ret;
1513 }
1514
1515 ret = platform_driver_register(&ucc_uart_of_driver);
1516 if (ret)
1517 printk(KERN_ERR
1518 "ucc-uart: could not register platform driver\n");
1519
1520 return ret;
1521}
1522
1523static void __exit ucc_uart_exit(void)
1524{
1525 printk(KERN_INFO
1526 "Freescale QUICC Engine UART device driver unloading\n");
1527
1528 platform_driver_unregister(&ucc_uart_of_driver);
1529 uart_unregister_driver(&ucc_uart_driver);
1530}
1531
1532module_init(ucc_uart_init);
1533module_exit(ucc_uart_exit);
1534
1535MODULE_DESCRIPTION("Freescale QUICC Engine (QE) UART");
1536MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1537MODULE_LICENSE("GPL v2");
1538MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_QE_MAJOR);
1539
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..37fc4e3d487c
--- /dev/null
+++ b/drivers/tty/serial/vt8500_serial.c
@@ -0,0 +1,646 @@
1/*
2 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
3 *
4 * Based on msm_serial.c, which is:
5 * Copyright (C) 2007 Google, Inc.
6 * Author: Robert Love <rlove@google.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#if defined(CONFIG_SERIAL_VT8500_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19# define SUPPORT_SYSRQ
20#endif
21
22#include <linux/hrtimer.h>
23#include <linux/delay.h>
24#include <linux/module.h>
25#include <linux/io.h>
26#include <linux/ioport.h>
27#include <linux/irq.h>
28#include <linux/init.h>
29#include <linux/console.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial_core.h>
33#include <linux/serial.h>
34#include <linux/slab.h>
35#include <linux/clk.h>
36#include <linux/platform_device.h>
37
38/*
39 * UART Register offsets
40 */
41
42#define VT8500_URTDR 0x0000 /* Transmit data */
43#define VT8500_URRDR 0x0004 /* Receive data */
44#define VT8500_URDIV 0x0008 /* Clock/Baud rate divisor */
45#define VT8500_URLCR 0x000C /* Line control */
46#define VT8500_URICR 0x0010 /* IrDA control */
47#define VT8500_URIER 0x0014 /* Interrupt enable */
48#define VT8500_URISR 0x0018 /* Interrupt status */
49#define VT8500_URUSR 0x001c /* UART status */
50#define VT8500_URFCR 0x0020 /* FIFO control */
51#define VT8500_URFIDX 0x0024 /* FIFO index */
52#define VT8500_URBKR 0x0028 /* Break signal count */
53#define VT8500_URTOD 0x002c /* Time out divisor */
54#define VT8500_TXFIFO 0x1000 /* Transmit FIFO (16x8) */
55#define VT8500_RXFIFO 0x1020 /* Receive FIFO (16x10) */
56
57/*
58 * Interrupt enable and status bits
59 */
60
61#define TXDE (1 << 0) /* Tx Data empty */
62#define RXDF (1 << 1) /* Rx Data full */
63#define TXFAE (1 << 2) /* Tx FIFO almost empty */
64#define TXFE (1 << 3) /* Tx FIFO empty */
65#define RXFAF (1 << 4) /* Rx FIFO almost full */
66#define RXFF (1 << 5) /* Rx FIFO full */
67#define TXUDR (1 << 6) /* Tx underrun */
68#define RXOVER (1 << 7) /* Rx overrun */
69#define PER (1 << 8) /* Parity error */
70#define FER (1 << 9) /* Frame error */
71#define TCTS (1 << 10) /* Toggle of CTS */
72#define RXTOUT (1 << 11) /* Rx timeout */
73#define BKDONE (1 << 12) /* Break signal done */
74#define ERR (1 << 13) /* AHB error response */
75
76#define RX_FIFO_INTS (RXFAF | RXFF | RXOVER | PER | FER | RXTOUT)
77#define TX_FIFO_INTS (TXFAE | TXFE | TXUDR)
78
79struct vt8500_port {
80 struct uart_port uart;
81 char name[16];
82 struct clk *clk;
83 unsigned int ier;
84};
85
86static inline void vt8500_write(struct uart_port *port, unsigned int val,
87 unsigned int off)
88{
89 writel(val, port->membase + off);
90}
91
92static inline unsigned int vt8500_read(struct uart_port *port, unsigned int off)
93{
94 return readl(port->membase + off);
95}
96
97static void vt8500_stop_tx(struct uart_port *port)
98{
99 struct vt8500_port *vt8500_port = container_of(port,
100 struct vt8500_port,
101 uart);
102
103 vt8500_port->ier &= ~TX_FIFO_INTS;
104 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
105}
106
107static void vt8500_stop_rx(struct uart_port *port)
108{
109 struct vt8500_port *vt8500_port = container_of(port,
110 struct vt8500_port,
111 uart);
112
113 vt8500_port->ier &= ~RX_FIFO_INTS;
114 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
115}
116
117static void vt8500_enable_ms(struct uart_port *port)
118{
119 struct vt8500_port *vt8500_port = container_of(port,
120 struct vt8500_port,
121 uart);
122
123 vt8500_port->ier |= TCTS;
124 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
125}
126
127static void handle_rx(struct uart_port *port)
128{
129 struct tty_struct *tty = tty_port_tty_get(&port->state->port);
130 if (!tty) {
131 /* Discard data: no tty available */
132 int count = (vt8500_read(port, VT8500_URFIDX) & 0x1f00) >> 8;
133 u16 ch;
134 while (count--)
135 ch = readw(port->membase + VT8500_RXFIFO);
136 return;
137 }
138
139 /*
140 * Handle overrun
141 */
142 if ((vt8500_read(port, VT8500_URISR) & RXOVER)) {
143 port->icount.overrun++;
144 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
145 }
146
147 /* and now the main RX loop */
148 while (vt8500_read(port, VT8500_URFIDX) & 0x1f00) {
149 unsigned int c;
150 char flag = TTY_NORMAL;
151
152 c = readw(port->membase + VT8500_RXFIFO) & 0x3ff;
153
154 /* Mask conditions we're ignorning. */
155 c &= ~port->read_status_mask;
156
157 if (c & FER) {
158 port->icount.frame++;
159 flag = TTY_FRAME;
160 } else if (c & PER) {
161 port->icount.parity++;
162 flag = TTY_PARITY;
163 }
164 port->icount.rx++;
165
166 if (!uart_handle_sysrq_char(port, c))
167 tty_insert_flip_char(tty, c, flag);
168 }
169
170 tty_flip_buffer_push(tty);
171 tty_kref_put(tty);
172}
173
174static void handle_tx(struct uart_port *port)
175{
176 struct circ_buf *xmit = &port->state->xmit;
177
178 if (port->x_char) {
179 writeb(port->x_char, port->membase + VT8500_TXFIFO);
180 port->icount.tx++;
181 port->x_char = 0;
182 }
183 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
184 vt8500_stop_tx(port);
185 return;
186 }
187
188 while ((vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16) {
189 if (uart_circ_empty(xmit))
190 break;
191
192 writeb(xmit->buf[xmit->tail], port->membase + VT8500_TXFIFO);
193
194 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
195 port->icount.tx++;
196 }
197
198 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
199 uart_write_wakeup(port);
200
201 if (uart_circ_empty(xmit))
202 vt8500_stop_tx(port);
203}
204
205static void vt8500_start_tx(struct uart_port *port)
206{
207 struct vt8500_port *vt8500_port = container_of(port,
208 struct vt8500_port,
209 uart);
210
211 vt8500_port->ier &= ~TX_FIFO_INTS;
212 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
213 handle_tx(port);
214 vt8500_port->ier |= TX_FIFO_INTS;
215 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
216}
217
218static void handle_delta_cts(struct uart_port *port)
219{
220 port->icount.cts++;
221 wake_up_interruptible(&port->state->port.delta_msr_wait);
222}
223
224static irqreturn_t vt8500_irq(int irq, void *dev_id)
225{
226 struct uart_port *port = dev_id;
227 unsigned long isr;
228
229 spin_lock(&port->lock);
230 isr = vt8500_read(port, VT8500_URISR);
231
232 /* Acknowledge active status bits */
233 vt8500_write(port, isr, VT8500_URISR);
234
235 if (isr & RX_FIFO_INTS)
236 handle_rx(port);
237 if (isr & TX_FIFO_INTS)
238 handle_tx(port);
239 if (isr & TCTS)
240 handle_delta_cts(port);
241
242 spin_unlock(&port->lock);
243
244 return IRQ_HANDLED;
245}
246
247static unsigned int vt8500_tx_empty(struct uart_port *port)
248{
249 return (vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16 ?
250 TIOCSER_TEMT : 0;
251}
252
253static unsigned int vt8500_get_mctrl(struct uart_port *port)
254{
255 unsigned int usr;
256
257 usr = vt8500_read(port, VT8500_URUSR);
258 if (usr & (1 << 4))
259 return TIOCM_CTS;
260 else
261 return 0;
262}
263
264static void vt8500_set_mctrl(struct uart_port *port, unsigned int mctrl)
265{
266}
267
268static void vt8500_break_ctl(struct uart_port *port, int break_ctl)
269{
270 if (break_ctl)
271 vt8500_write(port, vt8500_read(port, VT8500_URLCR) | (1 << 9),
272 VT8500_URLCR);
273}
274
275static int vt8500_set_baud_rate(struct uart_port *port, unsigned int baud)
276{
277 unsigned long div;
278 unsigned int loops = 1000;
279
280 div = vt8500_read(port, VT8500_URDIV) & ~(0x3ff);
281
282 if (unlikely((baud < 900) || (baud > 921600)))
283 div |= 7;
284 else
285 div |= (921600 / baud) - 1;
286
287 while ((vt8500_read(port, VT8500_URUSR) & (1 << 5)) && --loops)
288 cpu_relax();
289 vt8500_write(port, div, VT8500_URDIV);
290
291 return baud;
292}
293
294static int vt8500_startup(struct uart_port *port)
295{
296 struct vt8500_port *vt8500_port =
297 container_of(port, struct vt8500_port, uart);
298 int ret;
299
300 snprintf(vt8500_port->name, sizeof(vt8500_port->name),
301 "vt8500_serial%d", port->line);
302
303 ret = request_irq(port->irq, vt8500_irq, IRQF_TRIGGER_HIGH,
304 vt8500_port->name, port);
305 if (unlikely(ret))
306 return ret;
307
308 vt8500_write(port, 0x03, VT8500_URLCR); /* enable TX & RX */
309
310 return 0;
311}
312
313static void vt8500_shutdown(struct uart_port *port)
314{
315 struct vt8500_port *vt8500_port =
316 container_of(port, struct vt8500_port, uart);
317
318 vt8500_port->ier = 0;
319
320 /* disable interrupts and FIFOs */
321 vt8500_write(&vt8500_port->uart, 0, VT8500_URIER);
322 vt8500_write(&vt8500_port->uart, 0x880, VT8500_URFCR);
323 free_irq(port->irq, port);
324}
325
326static void vt8500_set_termios(struct uart_port *port,
327 struct ktermios *termios,
328 struct ktermios *old)
329{
330 struct vt8500_port *vt8500_port =
331 container_of(port, struct vt8500_port, uart);
332 unsigned long flags;
333 unsigned int baud, lcr;
334 unsigned int loops = 1000;
335
336 spin_lock_irqsave(&port->lock, flags);
337
338 /* calculate and set baud rate */
339 baud = uart_get_baud_rate(port, termios, old, 900, 921600);
340 baud = vt8500_set_baud_rate(port, baud);
341 if (tty_termios_baud_rate(termios))
342 tty_termios_encode_baud_rate(termios, baud, baud);
343
344 /* calculate parity */
345 lcr = vt8500_read(&vt8500_port->uart, VT8500_URLCR);
346 lcr &= ~((1 << 5) | (1 << 4));
347 if (termios->c_cflag & PARENB) {
348 lcr |= (1 << 4);
349 termios->c_cflag &= ~CMSPAR;
350 if (termios->c_cflag & PARODD)
351 lcr |= (1 << 5);
352 }
353
354 /* calculate bits per char */
355 lcr &= ~(1 << 2);
356 switch (termios->c_cflag & CSIZE) {
357 case CS7:
358 break;
359 case CS8:
360 default:
361 lcr |= (1 << 2);
362 termios->c_cflag &= ~CSIZE;
363 termios->c_cflag |= CS8;
364 break;
365 }
366
367 /* calculate stop bits */
368 lcr &= ~(1 << 3);
369 if (termios->c_cflag & CSTOPB)
370 lcr |= (1 << 3);
371
372 /* set parity, bits per char, and stop bit */
373 vt8500_write(&vt8500_port->uart, lcr, VT8500_URLCR);
374
375 /* Configure status bits to ignore based on termio flags. */
376 port->read_status_mask = 0;
377 if (termios->c_iflag & IGNPAR)
378 port->read_status_mask = FER | PER;
379
380 uart_update_timeout(port, termios->c_cflag, baud);
381
382 /* Reset FIFOs */
383 vt8500_write(&vt8500_port->uart, 0x88c, VT8500_URFCR);
384 while ((vt8500_read(&vt8500_port->uart, VT8500_URFCR) & 0xc)
385 && --loops)
386 cpu_relax();
387
388 /* Every possible FIFO-related interrupt */
389 vt8500_port->ier = RX_FIFO_INTS | TX_FIFO_INTS;
390
391 /*
392 * CTS flow control
393 */
394 if (UART_ENABLE_MS(&vt8500_port->uart, termios->c_cflag))
395 vt8500_port->ier |= TCTS;
396
397 vt8500_write(&vt8500_port->uart, 0x881, VT8500_URFCR);
398 vt8500_write(&vt8500_port->uart, vt8500_port->ier, VT8500_URIER);
399
400 spin_unlock_irqrestore(&port->lock, flags);
401}
402
403static const char *vt8500_type(struct uart_port *port)
404{
405 struct vt8500_port *vt8500_port =
406 container_of(port, struct vt8500_port, uart);
407 return vt8500_port->name;
408}
409
410static void vt8500_release_port(struct uart_port *port)
411{
412}
413
414static int vt8500_request_port(struct uart_port *port)
415{
416 return 0;
417}
418
419static void vt8500_config_port(struct uart_port *port, int flags)
420{
421 port->type = PORT_VT8500;
422}
423
424static int vt8500_verify_port(struct uart_port *port,
425 struct serial_struct *ser)
426{
427 if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_VT8500))
428 return -EINVAL;
429 if (unlikely(port->irq != ser->irq))
430 return -EINVAL;
431 return 0;
432}
433
434static struct vt8500_port *vt8500_uart_ports[4];
435static struct uart_driver vt8500_uart_driver;
436
437#ifdef CONFIG_SERIAL_VT8500_CONSOLE
438
439static inline void wait_for_xmitr(struct uart_port *port)
440{
441 unsigned int status, tmout = 10000;
442
443 /* Wait up to 10ms for the character(s) to be sent. */
444 do {
445 status = vt8500_read(port, VT8500_URFIDX);
446
447 if (--tmout == 0)
448 break;
449 udelay(1);
450 } while (status & 0x10);
451}
452
453static void vt8500_console_putchar(struct uart_port *port, int c)
454{
455 wait_for_xmitr(port);
456 writeb(c, port->membase + VT8500_TXFIFO);
457}
458
459static void vt8500_console_write(struct console *co, const char *s,
460 unsigned int count)
461{
462 struct vt8500_port *vt8500_port = vt8500_uart_ports[co->index];
463 unsigned long ier;
464
465 BUG_ON(co->index < 0 || co->index >= vt8500_uart_driver.nr);
466
467 ier = vt8500_read(&vt8500_port->uart, VT8500_URIER);
468 vt8500_write(&vt8500_port->uart, VT8500_URIER, 0);
469
470 uart_console_write(&vt8500_port->uart, s, count,
471 vt8500_console_putchar);
472
473 /*
474 * Finally, wait for transmitter to become empty
475 * and switch back to FIFO
476 */
477 wait_for_xmitr(&vt8500_port->uart);
478 vt8500_write(&vt8500_port->uart, VT8500_URIER, ier);
479}
480
481static int __init vt8500_console_setup(struct console *co, char *options)
482{
483 struct vt8500_port *vt8500_port;
484 int baud = 9600;
485 int bits = 8;
486 int parity = 'n';
487 int flow = 'n';
488
489 if (unlikely(co->index >= vt8500_uart_driver.nr || co->index < 0))
490 return -ENXIO;
491
492 vt8500_port = vt8500_uart_ports[co->index];
493
494 if (!vt8500_port)
495 return -ENODEV;
496
497 if (options)
498 uart_parse_options(options, &baud, &parity, &bits, &flow);
499
500 return uart_set_options(&vt8500_port->uart,
501 co, baud, parity, bits, flow);
502}
503
504static struct console vt8500_console = {
505 .name = "ttyWMT",
506 .write = vt8500_console_write,
507 .device = uart_console_device,
508 .setup = vt8500_console_setup,
509 .flags = CON_PRINTBUFFER,
510 .index = -1,
511 .data = &vt8500_uart_driver,
512};
513
514#define VT8500_CONSOLE (&vt8500_console)
515
516#else
517#define VT8500_CONSOLE NULL
518#endif
519
520static struct uart_ops vt8500_uart_pops = {
521 .tx_empty = vt8500_tx_empty,
522 .set_mctrl = vt8500_set_mctrl,
523 .get_mctrl = vt8500_get_mctrl,
524 .stop_tx = vt8500_stop_tx,
525 .start_tx = vt8500_start_tx,
526 .stop_rx = vt8500_stop_rx,
527 .enable_ms = vt8500_enable_ms,
528 .break_ctl = vt8500_break_ctl,
529 .startup = vt8500_startup,
530 .shutdown = vt8500_shutdown,
531 .set_termios = vt8500_set_termios,
532 .type = vt8500_type,
533 .release_port = vt8500_release_port,
534 .request_port = vt8500_request_port,
535 .config_port = vt8500_config_port,
536 .verify_port = vt8500_verify_port,
537};
538
539static struct uart_driver vt8500_uart_driver = {
540 .owner = THIS_MODULE,
541 .driver_name = "vt8500_serial",
542 .dev_name = "ttyWMT",
543 .nr = 6,
544 .cons = VT8500_CONSOLE,
545};
546
547static int __init vt8500_serial_probe(struct platform_device *pdev)
548{
549 struct vt8500_port *vt8500_port;
550 struct resource *mmres, *irqres;
551 int ret;
552
553 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
554 irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
555 if (!mmres || !irqres)
556 return -ENODEV;
557
558 vt8500_port = kzalloc(sizeof(struct vt8500_port), GFP_KERNEL);
559 if (!vt8500_port)
560 return -ENOMEM;
561
562 vt8500_port->uart.type = PORT_VT8500;
563 vt8500_port->uart.iotype = UPIO_MEM;
564 vt8500_port->uart.mapbase = mmres->start;
565 vt8500_port->uart.irq = irqres->start;
566 vt8500_port->uart.fifosize = 16;
567 vt8500_port->uart.ops = &vt8500_uart_pops;
568 vt8500_port->uart.line = pdev->id;
569 vt8500_port->uart.dev = &pdev->dev;
570 vt8500_port->uart.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
571 vt8500_port->uart.uartclk = 24000000;
572
573 snprintf(vt8500_port->name, sizeof(vt8500_port->name),
574 "VT8500 UART%d", pdev->id);
575
576 vt8500_port->uart.membase = ioremap(mmres->start,
577 mmres->end - mmres->start + 1);
578 if (!vt8500_port->uart.membase) {
579 ret = -ENOMEM;
580 goto err;
581 }
582
583 vt8500_uart_ports[pdev->id] = vt8500_port;
584
585 uart_add_one_port(&vt8500_uart_driver, &vt8500_port->uart);
586
587 platform_set_drvdata(pdev, vt8500_port);
588
589 return 0;
590
591err:
592 kfree(vt8500_port);
593 return ret;
594}
595
596static int __devexit vt8500_serial_remove(struct platform_device *pdev)
597{
598 struct vt8500_port *vt8500_port = platform_get_drvdata(pdev);
599
600 platform_set_drvdata(pdev, NULL);
601 uart_remove_one_port(&vt8500_uart_driver, &vt8500_port->uart);
602 kfree(vt8500_port);
603
604 return 0;
605}
606
607static struct platform_driver vt8500_platform_driver = {
608 .probe = vt8500_serial_probe,
609 .remove = vt8500_serial_remove,
610 .driver = {
611 .name = "vt8500_serial",
612 .owner = THIS_MODULE,
613 },
614};
615
616static int __init vt8500_serial_init(void)
617{
618 int ret;
619
620 ret = uart_register_driver(&vt8500_uart_driver);
621 if (unlikely(ret))
622 return ret;
623
624 ret = platform_driver_register(&vt8500_platform_driver);
625
626 if (unlikely(ret))
627 uart_unregister_driver(&vt8500_uart_driver);
628
629 return ret;
630}
631
632static void __exit vt8500_serial_exit(void)
633{
634#ifdef CONFIG_SERIAL_VT8500_CONSOLE
635 unregister_console(&vt8500_console);
636#endif
637 platform_driver_unregister(&vt8500_platform_driver);
638 uart_unregister_driver(&vt8500_uart_driver);
639}
640
641module_init(vt8500_serial_init);
642module_exit(vt8500_serial_exit);
643
644MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
645MODULE_DESCRIPTION("Driver for vt8500 serial device");
646MODULE_LICENSE("GPL");
diff --git a/drivers/tty/serial/xilinx_uartps.c b/drivers/tty/serial/xilinx_uartps.c
new file mode 100644
index 000000000000..19cc1e8149dd
--- /dev/null
+++ b/drivers/tty/serial/xilinx_uartps.c
@@ -0,0 +1,1113 @@
1/*
2 * Xilinx PS UART driver
3 *
4 * 2011 (c) Xilinx Inc.
5 *
6 * This program is free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation;
9 * either version 2 of the License, or (at your option) any
10 * later version.
11 *
12 */
13
14#include <linux/platform_device.h>
15#include <linux/serial_core.h>
16#include <linux/console.h>
17#include <linux/serial.h>
18#include <linux/irq.h>
19#include <linux/io.h>
20#include <linux/of.h>
21
22#define XUARTPS_TTY_NAME "ttyPS"
23#define XUARTPS_NAME "xuartps"
24#define XUARTPS_MAJOR 0 /* use dynamic node allocation */
25#define XUARTPS_MINOR 0 /* works best with devtmpfs */
26#define XUARTPS_NR_PORTS 2
27#define XUARTPS_FIFO_SIZE 16 /* FIFO size */
28#define XUARTPS_REGISTER_SPACE 0xFFF
29
30#define xuartps_readl(offset) ioread32(port->membase + offset)
31#define xuartps_writel(val, offset) iowrite32(val, port->membase + offset)
32
33/********************************Register Map********************************/
34/** UART
35 *
36 * Register offsets for the UART.
37 *
38 */
39#define XUARTPS_CR_OFFSET 0x00 /* Control Register [8:0] */
40#define XUARTPS_MR_OFFSET 0x04 /* Mode Register [10:0] */
41#define XUARTPS_IER_OFFSET 0x08 /* Interrupt Enable [10:0] */
42#define XUARTPS_IDR_OFFSET 0x0C /* Interrupt Disable [10:0] */
43#define XUARTPS_IMR_OFFSET 0x10 /* Interrupt Mask [10:0] */
44#define XUARTPS_ISR_OFFSET 0x14 /* Interrupt Status [10:0]*/
45#define XUARTPS_BAUDGEN_OFFSET 0x18 /* Baud Rate Generator [15:0] */
46#define XUARTPS_RXTOUT_OFFSET 0x1C /* RX Timeout [7:0] */
47#define XUARTPS_RXWM_OFFSET 0x20 /* RX FIFO Trigger Level [5:0] */
48#define XUARTPS_MODEMCR_OFFSET 0x24 /* Modem Control [5:0] */
49#define XUARTPS_MODEMSR_OFFSET 0x28 /* Modem Status [8:0] */
50#define XUARTPS_SR_OFFSET 0x2C /* Channel Status [11:0] */
51#define XUARTPS_FIFO_OFFSET 0x30 /* FIFO [15:0] or [7:0] */
52#define XUARTPS_BAUDDIV_OFFSET 0x34 /* Baud Rate Divider [7:0] */
53#define XUARTPS_FLOWDEL_OFFSET 0x38 /* Flow Delay [15:0] */
54#define XUARTPS_IRRX_PWIDTH_OFFSET 0x3C /* IR Minimum Received Pulse
55 Width [15:0] */
56#define XUARTPS_IRTX_PWIDTH_OFFSET 0x40 /* IR Transmitted pulse
57 Width [7:0] */
58#define XUARTPS_TXWM_OFFSET 0x44 /* TX FIFO Trigger Level [5:0] */
59
60/** Control Register
61 *
62 * The Control register (CR) controls the major functions of the device.
63 *
64 * Control Register Bit Definitions
65 */
66#define XUARTPS_CR_STOPBRK 0x00000100 /* Stop TX break */
67#define XUARTPS_CR_STARTBRK 0x00000080 /* Set TX break */
68#define XUARTPS_CR_TX_DIS 0x00000020 /* TX disabled. */
69#define XUARTPS_CR_TX_EN 0x00000010 /* TX enabled */
70#define XUARTPS_CR_RX_DIS 0x00000008 /* RX disabled. */
71#define XUARTPS_CR_RX_EN 0x00000004 /* RX enabled */
72#define XUARTPS_CR_TXRST 0x00000002 /* TX logic reset */
73#define XUARTPS_CR_RXRST 0x00000001 /* RX logic reset */
74#define XUARTPS_CR_RST_TO 0x00000040 /* Restart Timeout Counter */
75
76/** Mode Register
77 *
78 * The mode register (MR) defines the mode of transfer as well as the data
79 * format. If this register is modified during transmission or reception,
80 * data validity cannot be guaranteed.
81 *
82 * Mode Register Bit Definitions
83 *
84 */
85#define XUARTPS_MR_CLKSEL 0x00000001 /* Pre-scalar selection */
86#define XUARTPS_MR_CHMODE_L_LOOP 0x00000200 /* Local loop back mode */
87#define XUARTPS_MR_CHMODE_NORM 0x00000000 /* Normal mode */
88
89#define XUARTPS_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
90#define XUARTPS_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
91
92#define XUARTPS_MR_PARITY_NONE 0x00000020 /* No parity mode */
93#define XUARTPS_MR_PARITY_MARK 0x00000018 /* Mark parity mode */
94#define XUARTPS_MR_PARITY_SPACE 0x00000010 /* Space parity mode */
95#define XUARTPS_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
96#define XUARTPS_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
97
98#define XUARTPS_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
99#define XUARTPS_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
100#define XUARTPS_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
101
102/** Interrupt Registers
103 *
104 * Interrupt control logic uses the interrupt enable register (IER) and the
105 * interrupt disable register (IDR) to set the value of the bits in the
106 * interrupt mask register (IMR). The IMR determines whether to pass an
107 * interrupt to the interrupt status register (ISR).
108 * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
109 * interrupt. IMR and ISR are read only, and IER and IDR are write only.
110 * Reading either IER or IDR returns 0x00.
111 *
112 * All four registers have the same bit definitions.
113 */
114#define XUARTPS_IXR_TOUT 0x00000100 /* RX Timeout error interrupt */
115#define XUARTPS_IXR_PARITY 0x00000080 /* Parity error interrupt */
116#define XUARTPS_IXR_FRAMING 0x00000040 /* Framing error interrupt */
117#define XUARTPS_IXR_OVERRUN 0x00000020 /* Overrun error interrupt */
118#define XUARTPS_IXR_TXFULL 0x00000010 /* TX FIFO Full interrupt */
119#define XUARTPS_IXR_TXEMPTY 0x00000008 /* TX FIFO empty interrupt */
120#define XUARTPS_ISR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt */
121#define XUARTPS_IXR_RXTRIG 0x00000001 /* RX FIFO trigger interrupt */
122#define XUARTPS_IXR_RXFULL 0x00000004 /* RX FIFO full interrupt. */
123#define XUARTPS_IXR_RXEMPTY 0x00000002 /* RX FIFO empty interrupt. */
124#define XUARTPS_IXR_MASK 0x00001FFF /* Valid bit mask */
125
126/** Channel Status Register
127 *
128 * The channel status register (CSR) is provided to enable the control logic
129 * to monitor the status of bits in the channel interrupt status register,
130 * even if these are masked out by the interrupt mask register.
131 */
132#define XUARTPS_SR_RXEMPTY 0x00000002 /* RX FIFO empty */
133#define XUARTPS_SR_TXEMPTY 0x00000008 /* TX FIFO empty */
134#define XUARTPS_SR_TXFULL 0x00000010 /* TX FIFO full */
135#define XUARTPS_SR_RXTRIG 0x00000001 /* Rx Trigger */
136
137/**
138 * xuartps_isr - Interrupt handler
139 * @irq: Irq number
140 * @dev_id: Id of the port
141 *
142 * Returns IRQHANDLED
143 **/
144static irqreturn_t xuartps_isr(int irq, void *dev_id)
145{
146 struct uart_port *port = (struct uart_port *)dev_id;
147 struct tty_struct *tty;
148 unsigned long flags;
149 unsigned int isrstatus, numbytes;
150 unsigned int data;
151 char status = TTY_NORMAL;
152
153 /* Get the tty which could be NULL so don't assume it's valid */
154 tty = tty_port_tty_get(&port->state->port);
155
156 spin_lock_irqsave(&port->lock, flags);
157
158 /* Read the interrupt status register to determine which
159 * interrupt(s) is/are active.
160 */
161 isrstatus = xuartps_readl(XUARTPS_ISR_OFFSET);
162
163 /* drop byte with parity error if IGNPAR specified */
164 if (isrstatus & port->ignore_status_mask & XUARTPS_IXR_PARITY)
165 isrstatus &= ~(XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT);
166
167 isrstatus &= port->read_status_mask;
168 isrstatus &= ~port->ignore_status_mask;
169
170 if ((isrstatus & XUARTPS_IXR_TOUT) ||
171 (isrstatus & XUARTPS_IXR_RXTRIG)) {
172 /* Receive Timeout Interrupt */
173 while ((xuartps_readl(XUARTPS_SR_OFFSET) &
174 XUARTPS_SR_RXEMPTY) != XUARTPS_SR_RXEMPTY) {
175 data = xuartps_readl(XUARTPS_FIFO_OFFSET);
176 port->icount.rx++;
177
178 if (isrstatus & XUARTPS_IXR_PARITY) {
179 port->icount.parity++;
180 status = TTY_PARITY;
181 } else if (isrstatus & XUARTPS_IXR_FRAMING) {
182 port->icount.frame++;
183 status = TTY_FRAME;
184 } else if (isrstatus & XUARTPS_IXR_OVERRUN)
185 port->icount.overrun++;
186
187 if (tty)
188 uart_insert_char(port, isrstatus,
189 XUARTPS_IXR_OVERRUN, data,
190 status);
191 }
192 spin_unlock(&port->lock);
193 if (tty)
194 tty_flip_buffer_push(tty);
195 spin_lock(&port->lock);
196 }
197
198 /* Dispatch an appropriate handler */
199 if ((isrstatus & XUARTPS_IXR_TXEMPTY) == XUARTPS_IXR_TXEMPTY) {
200 if (uart_circ_empty(&port->state->xmit)) {
201 xuartps_writel(XUARTPS_IXR_TXEMPTY,
202 XUARTPS_IDR_OFFSET);
203 } else {
204 numbytes = port->fifosize;
205 /* Break if no more data available in the UART buffer */
206 while (numbytes--) {
207 if (uart_circ_empty(&port->state->xmit))
208 break;
209 /* Get the data from the UART circular buffer
210 * and write it to the xuartps's TX_FIFO
211 * register.
212 */
213 xuartps_writel(
214 port->state->xmit.buf[port->state->xmit.
215 tail], XUARTPS_FIFO_OFFSET);
216
217 port->icount.tx++;
218
219 /* Adjust the tail of the UART buffer and wrap
220 * the buffer if it reaches limit.
221 */
222 port->state->xmit.tail =
223 (port->state->xmit.tail + 1) & \
224 (UART_XMIT_SIZE - 1);
225 }
226
227 if (uart_circ_chars_pending(
228 &port->state->xmit) < WAKEUP_CHARS)
229 uart_write_wakeup(port);
230 }
231 }
232
233 xuartps_writel(isrstatus, XUARTPS_ISR_OFFSET);
234
235 /* be sure to release the lock and tty before leaving */
236 spin_unlock_irqrestore(&port->lock, flags);
237 tty_kref_put(tty);
238
239 return IRQ_HANDLED;
240}
241
242/**
243 * xuartps_set_baud_rate - Calculate and set the baud rate
244 * @port: Handle to the uart port structure
245 * @baud: Baud rate to set
246 *
247 * Returns baud rate, requested baud when possible, or actual baud when there
248 * was too much error
249 **/
250static unsigned int xuartps_set_baud_rate(struct uart_port *port,
251 unsigned int baud)
252{
253 unsigned int sel_clk;
254 unsigned int calc_baud = 0;
255 unsigned int brgr_val, brdiv_val;
256 unsigned int bauderror;
257
258 /* Formula to obtain baud rate is
259 * baud_tx/rx rate = sel_clk/CD * (BDIV + 1)
260 * input_clk = (Uart User Defined Clock or Apb Clock)
261 * depends on UCLKEN in MR Reg
262 * sel_clk = input_clk or input_clk/8;
263 * depends on CLKS in MR reg
264 * CD and BDIV depends on values in
265 * baud rate generate register
266 * baud rate clock divisor register
267 */
268 sel_clk = port->uartclk;
269 if (xuartps_readl(XUARTPS_MR_OFFSET) & XUARTPS_MR_CLKSEL)
270 sel_clk = sel_clk / 8;
271
272 /* Find the best values for baud generation */
273 for (brdiv_val = 4; brdiv_val < 255; brdiv_val++) {
274
275 brgr_val = sel_clk / (baud * (brdiv_val + 1));
276 if (brgr_val < 2 || brgr_val > 65535)
277 continue;
278
279 calc_baud = sel_clk / (brgr_val * (brdiv_val + 1));
280
281 if (baud > calc_baud)
282 bauderror = baud - calc_baud;
283 else
284 bauderror = calc_baud - baud;
285
286 /* use the values when percent error is acceptable */
287 if (((bauderror * 100) / baud) < 3) {
288 calc_baud = baud;
289 break;
290 }
291 }
292
293 /* Set the values for the new baud rate */
294 xuartps_writel(brgr_val, XUARTPS_BAUDGEN_OFFSET);
295 xuartps_writel(brdiv_val, XUARTPS_BAUDDIV_OFFSET);
296
297 return calc_baud;
298}
299
300/*----------------------Uart Operations---------------------------*/
301
302/**
303 * xuartps_start_tx - Start transmitting bytes
304 * @port: Handle to the uart port structure
305 *
306 **/
307static void xuartps_start_tx(struct uart_port *port)
308{
309 unsigned int status, numbytes = port->fifosize;
310
311 if (uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port))
312 return;
313
314 status = xuartps_readl(XUARTPS_CR_OFFSET);
315 /* Set the TX enable bit and clear the TX disable bit to enable the
316 * transmitter.
317 */
318 xuartps_writel((status & ~XUARTPS_CR_TX_DIS) | XUARTPS_CR_TX_EN,
319 XUARTPS_CR_OFFSET);
320
321 while (numbytes-- && ((xuartps_readl(XUARTPS_SR_OFFSET)
322 & XUARTPS_SR_TXFULL)) != XUARTPS_SR_TXFULL) {
323
324 /* Break if no more data available in the UART buffer */
325 if (uart_circ_empty(&port->state->xmit))
326 break;
327
328 /* Get the data from the UART circular buffer and
329 * write it to the xuartps's TX_FIFO register.
330 */
331 xuartps_writel(
332 port->state->xmit.buf[port->state->xmit.tail],
333 XUARTPS_FIFO_OFFSET);
334 port->icount.tx++;
335
336 /* Adjust the tail of the UART buffer and wrap
337 * the buffer if it reaches limit.
338 */
339 port->state->xmit.tail = (port->state->xmit.tail + 1) &
340 (UART_XMIT_SIZE - 1);
341 }
342
343 /* Enable the TX Empty interrupt */
344 xuartps_writel(XUARTPS_IXR_TXEMPTY, XUARTPS_IER_OFFSET);
345
346 if (uart_circ_chars_pending(&port->state->xmit) < WAKEUP_CHARS)
347 uart_write_wakeup(port);
348}
349
350/**
351 * xuartps_stop_tx - Stop TX
352 * @port: Handle to the uart port structure
353 *
354 **/
355static void xuartps_stop_tx(struct uart_port *port)
356{
357 unsigned int regval;
358
359 regval = xuartps_readl(XUARTPS_CR_OFFSET);
360 regval |= XUARTPS_CR_TX_DIS;
361 /* Disable the transmitter */
362 xuartps_writel(regval, XUARTPS_CR_OFFSET);
363}
364
365/**
366 * xuartps_stop_rx - Stop RX
367 * @port: Handle to the uart port structure
368 *
369 **/
370static void xuartps_stop_rx(struct uart_port *port)
371{
372 unsigned int regval;
373
374 regval = xuartps_readl(XUARTPS_CR_OFFSET);
375 regval |= XUARTPS_CR_RX_DIS;
376 /* Disable the receiver */
377 xuartps_writel(regval, XUARTPS_CR_OFFSET);
378}
379
380/**
381 * xuartps_tx_empty - Check whether TX is empty
382 * @port: Handle to the uart port structure
383 *
384 * Returns TIOCSER_TEMT on success, 0 otherwise
385 **/
386static unsigned int xuartps_tx_empty(struct uart_port *port)
387{
388 unsigned int status;
389
390 status = xuartps_readl(XUARTPS_ISR_OFFSET) & XUARTPS_IXR_TXEMPTY;
391 return status ? TIOCSER_TEMT : 0;
392}
393
394/**
395 * xuartps_break_ctl - Based on the input ctl we have to start or stop
396 * transmitting char breaks
397 * @port: Handle to the uart port structure
398 * @ctl: Value based on which start or stop decision is taken
399 *
400 **/
401static void xuartps_break_ctl(struct uart_port *port, int ctl)
402{
403 unsigned int status;
404 unsigned long flags;
405
406 spin_lock_irqsave(&port->lock, flags);
407
408 status = xuartps_readl(XUARTPS_CR_OFFSET);
409
410 if (ctl == -1)
411 xuartps_writel(XUARTPS_CR_STARTBRK | status,
412 XUARTPS_CR_OFFSET);
413 else {
414 if ((status & XUARTPS_CR_STOPBRK) == 0)
415 xuartps_writel(XUARTPS_CR_STOPBRK | status,
416 XUARTPS_CR_OFFSET);
417 }
418 spin_unlock_irqrestore(&port->lock, flags);
419}
420
421/**
422 * xuartps_set_termios - termios operations, handling data length, parity,
423 * stop bits, flow control, baud rate
424 * @port: Handle to the uart port structure
425 * @termios: Handle to the input termios structure
426 * @old: Values of the previously saved termios structure
427 *
428 **/
429static void xuartps_set_termios(struct uart_port *port,
430 struct ktermios *termios, struct ktermios *old)
431{
432 unsigned int cval = 0;
433 unsigned int baud;
434 unsigned long flags;
435 unsigned int ctrl_reg, mode_reg;
436
437 spin_lock_irqsave(&port->lock, flags);
438
439 /* Empty the receive FIFO 1st before making changes */
440 while ((xuartps_readl(XUARTPS_SR_OFFSET) &
441 XUARTPS_SR_RXEMPTY) != XUARTPS_SR_RXEMPTY) {
442 xuartps_readl(XUARTPS_FIFO_OFFSET);
443 }
444
445 /* Disable the TX and RX to set baud rate */
446 xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
447 (XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS),
448 XUARTPS_CR_OFFSET);
449
450 /* Min baud rate = 6bps and Max Baud Rate is 10Mbps for 100Mhz clk */
451 baud = uart_get_baud_rate(port, termios, old, 0, 10000000);
452 baud = xuartps_set_baud_rate(port, baud);
453 if (tty_termios_baud_rate(termios))
454 tty_termios_encode_baud_rate(termios, baud, baud);
455
456 /*
457 * Update the per-port timeout.
458 */
459 uart_update_timeout(port, termios->c_cflag, baud);
460
461 /* Set TX/RX Reset */
462 xuartps_writel(xuartps_readl(XUARTPS_CR_OFFSET) |
463 (XUARTPS_CR_TXRST | XUARTPS_CR_RXRST),
464 XUARTPS_CR_OFFSET);
465
466 ctrl_reg = xuartps_readl(XUARTPS_CR_OFFSET);
467
468 /* Clear the RX disable and TX disable bits and then set the TX enable
469 * bit and RX enable bit to enable the transmitter and receiver.
470 */
471 xuartps_writel(
472 (ctrl_reg & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
473 | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN),
474 XUARTPS_CR_OFFSET);
475
476 xuartps_writel(10, XUARTPS_RXTOUT_OFFSET);
477
478 port->read_status_mask = XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_RXTRIG |
479 XUARTPS_IXR_OVERRUN | XUARTPS_IXR_TOUT;
480 port->ignore_status_mask = 0;
481
482 if (termios->c_iflag & INPCK)
483 port->read_status_mask |= XUARTPS_IXR_PARITY |
484 XUARTPS_IXR_FRAMING;
485
486 if (termios->c_iflag & IGNPAR)
487 port->ignore_status_mask |= XUARTPS_IXR_PARITY |
488 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
489
490 /* ignore all characters if CREAD is not set */
491 if ((termios->c_cflag & CREAD) == 0)
492 port->ignore_status_mask |= XUARTPS_IXR_RXTRIG |
493 XUARTPS_IXR_TOUT | XUARTPS_IXR_PARITY |
494 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN;
495
496 mode_reg = xuartps_readl(XUARTPS_MR_OFFSET);
497
498 /* Handling Data Size */
499 switch (termios->c_cflag & CSIZE) {
500 case CS6:
501 cval |= XUARTPS_MR_CHARLEN_6_BIT;
502 break;
503 case CS7:
504 cval |= XUARTPS_MR_CHARLEN_7_BIT;
505 break;
506 default:
507 case CS8:
508 cval |= XUARTPS_MR_CHARLEN_8_BIT;
509 termios->c_cflag &= ~CSIZE;
510 termios->c_cflag |= CS8;
511 break;
512 }
513
514 /* Handling Parity and Stop Bits length */
515 if (termios->c_cflag & CSTOPB)
516 cval |= XUARTPS_MR_STOPMODE_2_BIT; /* 2 STOP bits */
517 else
518 cval |= XUARTPS_MR_STOPMODE_1_BIT; /* 1 STOP bit */
519
520 if (termios->c_cflag & PARENB) {
521 /* Mark or Space parity */
522 if (termios->c_cflag & CMSPAR) {
523 if (termios->c_cflag & PARODD)
524 cval |= XUARTPS_MR_PARITY_MARK;
525 else
526 cval |= XUARTPS_MR_PARITY_SPACE;
527 } else if (termios->c_cflag & PARODD)
528 cval |= XUARTPS_MR_PARITY_ODD;
529 else
530 cval |= XUARTPS_MR_PARITY_EVEN;
531 } else
532 cval |= XUARTPS_MR_PARITY_NONE;
533 xuartps_writel(cval , XUARTPS_MR_OFFSET);
534
535 spin_unlock_irqrestore(&port->lock, flags);
536}
537
538/**
539 * xuartps_startup - Called when an application opens a xuartps port
540 * @port: Handle to the uart port structure
541 *
542 * Returns 0 on success, negative error otherwise
543 **/
544static int xuartps_startup(struct uart_port *port)
545{
546 unsigned int retval = 0, status = 0;
547
548 retval = request_irq(port->irq, xuartps_isr, 0, XUARTPS_NAME,
549 (void *)port);
550 if (retval)
551 return retval;
552
553 /* Disable the TX and RX */
554 xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
555 XUARTPS_CR_OFFSET);
556
557 /* Set the Control Register with TX/RX Enable, TX/RX Reset,
558 * no break chars.
559 */
560 xuartps_writel(XUARTPS_CR_TXRST | XUARTPS_CR_RXRST,
561 XUARTPS_CR_OFFSET);
562
563 status = xuartps_readl(XUARTPS_CR_OFFSET);
564
565 /* Clear the RX disable and TX disable bits and then set the TX enable
566 * bit and RX enable bit to enable the transmitter and receiver.
567 */
568 xuartps_writel((status & ~(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS))
569 | (XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN |
570 XUARTPS_CR_STOPBRK), XUARTPS_CR_OFFSET);
571
572 /* Set the Mode Register with normal mode,8 data bits,1 stop bit,
573 * no parity.
574 */
575 xuartps_writel(XUARTPS_MR_CHMODE_NORM | XUARTPS_MR_STOPMODE_1_BIT
576 | XUARTPS_MR_PARITY_NONE | XUARTPS_MR_CHARLEN_8_BIT,
577 XUARTPS_MR_OFFSET);
578
579 /* Set the RX FIFO Trigger level to 14 assuming FIFO size as 16 */
580 xuartps_writel(14, XUARTPS_RXWM_OFFSET);
581
582 /* Receive Timeout register is enabled with value of 10 */
583 xuartps_writel(10, XUARTPS_RXTOUT_OFFSET);
584
585
586 /* Set the Interrupt Registers with desired interrupts */
587 xuartps_writel(XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_PARITY |
588 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN |
589 XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT, XUARTPS_IER_OFFSET);
590 xuartps_writel(~(XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_PARITY |
591 XUARTPS_IXR_FRAMING | XUARTPS_IXR_OVERRUN |
592 XUARTPS_IXR_RXTRIG | XUARTPS_IXR_TOUT), XUARTPS_IDR_OFFSET);
593
594 return retval;
595}
596
597/**
598 * xuartps_shutdown - Called when an application closes a xuartps port
599 * @port: Handle to the uart port structure
600 *
601 **/
602static void xuartps_shutdown(struct uart_port *port)
603{
604 int status;
605
606 /* Disable interrupts */
607 status = xuartps_readl(XUARTPS_IMR_OFFSET);
608 xuartps_writel(status, XUARTPS_IDR_OFFSET);
609
610 /* Disable the TX and RX */
611 xuartps_writel(XUARTPS_CR_TX_DIS | XUARTPS_CR_RX_DIS,
612 XUARTPS_CR_OFFSET);
613 free_irq(port->irq, port);
614}
615
616/**
617 * xuartps_type - Set UART type to xuartps port
618 * @port: Handle to the uart port structure
619 *
620 * Returns string on success, NULL otherwise
621 **/
622static const char *xuartps_type(struct uart_port *port)
623{
624 return port->type == PORT_XUARTPS ? XUARTPS_NAME : NULL;
625}
626
627/**
628 * xuartps_verify_port - Verify the port params
629 * @port: Handle to the uart port structure
630 * @ser: Handle to the structure whose members are compared
631 *
632 * Returns 0 if success otherwise -EINVAL
633 **/
634static int xuartps_verify_port(struct uart_port *port,
635 struct serial_struct *ser)
636{
637 if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS)
638 return -EINVAL;
639 if (port->irq != ser->irq)
640 return -EINVAL;
641 if (ser->io_type != UPIO_MEM)
642 return -EINVAL;
643 if (port->iobase != ser->port)
644 return -EINVAL;
645 if (ser->hub6 != 0)
646 return -EINVAL;
647 return 0;
648}
649
650/**
651 * xuartps_request_port - Claim the memory region attached to xuartps port,
652 * called when the driver adds a xuartps port via
653 * uart_add_one_port()
654 * @port: Handle to the uart port structure
655 *
656 * Returns 0, -ENOMEM if request fails
657 **/
658static int xuartps_request_port(struct uart_port *port)
659{
660 if (!request_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE,
661 XUARTPS_NAME)) {
662 return -ENOMEM;
663 }
664
665 port->membase = ioremap(port->mapbase, XUARTPS_REGISTER_SPACE);
666 if (!port->membase) {
667 dev_err(port->dev, "Unable to map registers\n");
668 release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
669 return -ENOMEM;
670 }
671 return 0;
672}
673
674/**
675 * xuartps_release_port - Release the memory region attached to a xuartps
676 * port, called when the driver removes a xuartps
677 * port via uart_remove_one_port().
678 * @port: Handle to the uart port structure
679 *
680 **/
681static void xuartps_release_port(struct uart_port *port)
682{
683 release_mem_region(port->mapbase, XUARTPS_REGISTER_SPACE);
684 iounmap(port->membase);
685 port->membase = NULL;
686}
687
688/**
689 * xuartps_config_port - Configure xuartps, called when the driver adds a
690 * xuartps port
691 * @port: Handle to the uart port structure
692 * @flags: If any
693 *
694 **/
695static void xuartps_config_port(struct uart_port *port, int flags)
696{
697 if (flags & UART_CONFIG_TYPE && xuartps_request_port(port) == 0)
698 port->type = PORT_XUARTPS;
699}
700
701/**
702 * xuartps_get_mctrl - Get the modem control state
703 *
704 * @port: Handle to the uart port structure
705 *
706 * Returns the modem control state
707 *
708 **/
709static unsigned int xuartps_get_mctrl(struct uart_port *port)
710{
711 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
712}
713
714static void xuartps_set_mctrl(struct uart_port *port, unsigned int mctrl)
715{
716 /* N/A */
717}
718
719static void xuartps_enable_ms(struct uart_port *port)
720{
721 /* N/A */
722}
723
724/** The UART operations structure
725 */
726static struct uart_ops xuartps_ops = {
727 .set_mctrl = xuartps_set_mctrl,
728 .get_mctrl = xuartps_get_mctrl,
729 .enable_ms = xuartps_enable_ms,
730
731 .start_tx = xuartps_start_tx, /* Start transmitting */
732 .stop_tx = xuartps_stop_tx, /* Stop transmission */
733 .stop_rx = xuartps_stop_rx, /* Stop reception */
734 .tx_empty = xuartps_tx_empty, /* Transmitter busy? */
735 .break_ctl = xuartps_break_ctl, /* Start/stop
736 * transmitting break
737 */
738 .set_termios = xuartps_set_termios, /* Set termios */
739 .startup = xuartps_startup, /* App opens xuartps */
740 .shutdown = xuartps_shutdown, /* App closes xuartps */
741 .type = xuartps_type, /* Set UART type */
742 .verify_port = xuartps_verify_port, /* Verification of port
743 * params
744 */
745 .request_port = xuartps_request_port, /* Claim resources
746 * associated with a
747 * xuartps port
748 */
749 .release_port = xuartps_release_port, /* Release resources
750 * associated with a
751 * xuartps port
752 */
753 .config_port = xuartps_config_port, /* Configure when driver
754 * adds a xuartps port
755 */
756};
757
758static struct uart_port xuartps_port[2];
759
760/**
761 * xuartps_get_port - Configure the port from the platform device resource
762 * info
763 *
764 * Returns a pointer to a uart_port or NULL for failure
765 **/
766static struct uart_port *xuartps_get_port(void)
767{
768 struct uart_port *port;
769 int id;
770
771 /* Find the next unused port */
772 for (id = 0; id < XUARTPS_NR_PORTS; id++)
773 if (xuartps_port[id].mapbase == 0)
774 break;
775
776 if (id >= XUARTPS_NR_PORTS)
777 return NULL;
778
779 port = &xuartps_port[id];
780
781 /* At this point, we've got an empty uart_port struct, initialize it */
782 spin_lock_init(&port->lock);
783 port->membase = NULL;
784 port->iobase = 1; /* mark port in use */
785 port->irq = 0;
786 port->type = PORT_UNKNOWN;
787 port->iotype = UPIO_MEM32;
788 port->flags = UPF_BOOT_AUTOCONF;
789 port->ops = &xuartps_ops;
790 port->fifosize = XUARTPS_FIFO_SIZE;
791 port->line = id;
792 port->dev = NULL;
793 return port;
794}
795
796/*-----------------------Console driver operations--------------------------*/
797
798#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
799/**
800 * xuartps_console_wait_tx - Wait for the TX to be full
801 * @port: Handle to the uart port structure
802 *
803 **/
804static void xuartps_console_wait_tx(struct uart_port *port)
805{
806 while ((xuartps_readl(XUARTPS_SR_OFFSET) & XUARTPS_SR_TXEMPTY)
807 != XUARTPS_SR_TXEMPTY)
808 barrier();
809}
810
811/**
812 * xuartps_console_putchar - write the character to the FIFO buffer
813 * @port: Handle to the uart port structure
814 * @ch: Character to be written
815 *
816 **/
817static void xuartps_console_putchar(struct uart_port *port, int ch)
818{
819 xuartps_console_wait_tx(port);
820 xuartps_writel(ch, XUARTPS_FIFO_OFFSET);
821}
822
823/**
824 * xuartps_console_write - perform write operation
825 * @port: Handle to the uart port structure
826 * @s: Pointer to character array
827 * @count: No of characters
828 **/
829static void xuartps_console_write(struct console *co, const char *s,
830 unsigned int count)
831{
832 struct uart_port *port = &xuartps_port[co->index];
833 unsigned long flags;
834 unsigned int imr;
835 int locked = 1;
836
837 if (oops_in_progress)
838 locked = spin_trylock_irqsave(&port->lock, flags);
839 else
840 spin_lock_irqsave(&port->lock, flags);
841
842 /* save and disable interrupt */
843 imr = xuartps_readl(XUARTPS_IMR_OFFSET);
844 xuartps_writel(imr, XUARTPS_IDR_OFFSET);
845
846 uart_console_write(port, s, count, xuartps_console_putchar);
847 xuartps_console_wait_tx(port);
848
849 /* restore interrupt state, it seems like there may be a h/w bug
850 * in that the interrupt enable register should not need to be
851 * written based on the data sheet
852 */
853 xuartps_writel(~imr, XUARTPS_IDR_OFFSET);
854 xuartps_writel(imr, XUARTPS_IER_OFFSET);
855
856 if (locked)
857 spin_unlock_irqrestore(&port->lock, flags);
858}
859
860/**
861 * xuartps_console_setup - Initialize the uart to default config
862 * @co: Console handle
863 * @options: Initial settings of uart
864 *
865 * Returns 0, -ENODEV if no device
866 **/
867static int __init xuartps_console_setup(struct console *co, char *options)
868{
869 struct uart_port *port = &xuartps_port[co->index];
870 int baud = 9600;
871 int bits = 8;
872 int parity = 'n';
873 int flow = 'n';
874
875 if (co->index < 0 || co->index >= XUARTPS_NR_PORTS)
876 return -EINVAL;
877
878 if (!port->mapbase) {
879 pr_debug("console on ttyPS%i not present\n", co->index);
880 return -ENODEV;
881 }
882
883 if (options)
884 uart_parse_options(options, &baud, &parity, &bits, &flow);
885
886 return uart_set_options(port, co, baud, parity, bits, flow);
887}
888
889static struct uart_driver xuartps_uart_driver;
890
891static struct console xuartps_console = {
892 .name = XUARTPS_TTY_NAME,
893 .write = xuartps_console_write,
894 .device = uart_console_device,
895 .setup = xuartps_console_setup,
896 .flags = CON_PRINTBUFFER,
897 .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
898 .data = &xuartps_uart_driver,
899};
900
901/**
902 * xuartps_console_init - Initialization call
903 *
904 * Returns 0 on success, negative error otherwise
905 **/
906static int __init xuartps_console_init(void)
907{
908 register_console(&xuartps_console);
909 return 0;
910}
911
912console_initcall(xuartps_console_init);
913
914#endif /* CONFIG_SERIAL_XILINX_PS_UART_CONSOLE */
915
916/** Structure Definitions
917 */
918static struct uart_driver xuartps_uart_driver = {
919 .owner = THIS_MODULE, /* Owner */
920 .driver_name = XUARTPS_NAME, /* Driver name */
921 .dev_name = XUARTPS_TTY_NAME, /* Node name */
922 .major = XUARTPS_MAJOR, /* Major number */
923 .minor = XUARTPS_MINOR, /* Minor number */
924 .nr = XUARTPS_NR_PORTS, /* Number of UART ports */
925#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
926 .cons = &xuartps_console, /* Console */
927#endif
928};
929
930/* ---------------------------------------------------------------------
931 * Platform bus binding
932 */
933/**
934 * xuartps_probe - Platform driver probe
935 * @pdev: Pointer to the platform device structure
936 *
937 * Returns 0 on success, negative error otherwise
938 **/
939static int __devinit xuartps_probe(struct platform_device *pdev)
940{
941 int rc;
942 struct uart_port *port;
943 struct resource *res, *res2;
944 int clk = 0;
945
946#ifdef CONFIG_OF
947 const unsigned int *prop;
948
949 prop = of_get_property(pdev->dev.of_node, "clock", NULL);
950 if (prop)
951 clk = be32_to_cpup(prop);
952#else
953 clk = *((unsigned int *)(pdev->dev.platform_data));
954#endif
955 if (!clk) {
956 dev_err(&pdev->dev, "no clock specified\n");
957 return -ENODEV;
958 }
959
960 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
961 if (!res)
962 return -ENODEV;
963
964 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
965 if (!res2)
966 return -ENODEV;
967
968 /* Initialize the port structure */
969 port = xuartps_get_port();
970
971 if (!port) {
972 dev_err(&pdev->dev, "Cannot get uart_port structure\n");
973 return -ENODEV;
974 } else {
975 /* Register the port.
976 * This function also registers this device with the tty layer
977 * and triggers invocation of the config_port() entry point.
978 */
979 port->mapbase = res->start;
980 port->irq = res2->start;
981 port->dev = &pdev->dev;
982 port->uartclk = clk;
983 dev_set_drvdata(&pdev->dev, port);
984 rc = uart_add_one_port(&xuartps_uart_driver, port);
985 if (rc) {
986 dev_err(&pdev->dev,
987 "uart_add_one_port() failed; err=%i\n", rc);
988 dev_set_drvdata(&pdev->dev, NULL);
989 return rc;
990 }
991 return 0;
992 }
993}
994
995/**
996 * xuartps_remove - called when the platform driver is unregistered
997 * @pdev: Pointer to the platform device structure
998 *
999 * Returns 0 on success, negative error otherwise
1000 **/
1001static int __devexit xuartps_remove(struct platform_device *pdev)
1002{
1003 struct uart_port *port = dev_get_drvdata(&pdev->dev);
1004 int rc = 0;
1005
1006 /* Remove the xuartps port from the serial core */
1007 if (port) {
1008 rc = uart_remove_one_port(&xuartps_uart_driver, port);
1009 dev_set_drvdata(&pdev->dev, NULL);
1010 port->mapbase = 0;
1011 }
1012 return rc;
1013}
1014
1015/**
1016 * xuartps_suspend - suspend event
1017 * @pdev: Pointer to the platform device structure
1018 * @state: State of the device
1019 *
1020 * Returns 0
1021 **/
1022static int xuartps_suspend(struct platform_device *pdev, pm_message_t state)
1023{
1024 /* Call the API provided in serial_core.c file which handles
1025 * the suspend.
1026 */
1027 uart_suspend_port(&xuartps_uart_driver, &xuartps_port[pdev->id]);
1028 return 0;
1029}
1030
1031/**
1032 * xuartps_resume - Resume after a previous suspend
1033 * @pdev: Pointer to the platform device structure
1034 *
1035 * Returns 0
1036 **/
1037static int xuartps_resume(struct platform_device *pdev)
1038{
1039 uart_resume_port(&xuartps_uart_driver, &xuartps_port[pdev->id]);
1040 return 0;
1041}
1042
1043/* Match table for of_platform binding */
1044
1045#ifdef CONFIG_OF
1046static struct of_device_id xuartps_of_match[] __devinitdata = {
1047 { .compatible = "xlnx,xuartps", },
1048 {}
1049};
1050MODULE_DEVICE_TABLE(of, xuartps_of_match);
1051#else
1052#define xuartps_of_match NULL
1053#endif
1054
1055static struct platform_driver xuartps_platform_driver = {
1056 .probe = xuartps_probe, /* Probe method */
1057 .remove = __exit_p(xuartps_remove), /* Detach method */
1058 .suspend = xuartps_suspend, /* Suspend */
1059 .resume = xuartps_resume, /* Resume after a suspend */
1060 .driver = {
1061 .owner = THIS_MODULE,
1062 .name = XUARTPS_NAME, /* Driver name */
1063 .of_match_table = xuartps_of_match,
1064 },
1065};
1066
1067/* ---------------------------------------------------------------------
1068 * Module Init and Exit
1069 */
1070/**
1071 * xuartps_init - Initial driver registration call
1072 *
1073 * Returns whether the registration was successful or not
1074 **/
1075static int __init xuartps_init(void)
1076{
1077 int retval = 0;
1078
1079 /* Register the xuartps driver with the serial core */
1080 retval = uart_register_driver(&xuartps_uart_driver);
1081 if (retval)
1082 return retval;
1083
1084 /* Register the platform driver */
1085 retval = platform_driver_register(&xuartps_platform_driver);
1086 if (retval)
1087 uart_unregister_driver(&xuartps_uart_driver);
1088
1089 return retval;
1090}
1091
1092/**
1093 * xuartps_exit - Driver unregistration call
1094 **/
1095static void __exit xuartps_exit(void)
1096{
1097 /* The order of unregistration is important. Unregister the
1098 * UART driver before the platform driver crashes the system.
1099 */
1100
1101 /* Unregister the platform driver */
1102 platform_driver_unregister(&xuartps_platform_driver);
1103
1104 /* Unregister the xuartps driver */
1105 uart_unregister_driver(&xuartps_uart_driver);
1106}
1107
1108module_init(xuartps_init);
1109module_exit(xuartps_exit);
1110
1111MODULE_DESCRIPTION("Driver for PS UART");
1112MODULE_AUTHOR("Xilinx Inc.");
1113MODULE_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 */