2 * Driver for CLPS711x serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
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.
15 #if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/console.h>
22 #include <linux/serial_core.h>
23 #include <linux/serial.h>
25 #include <linux/clk.h>
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
28 #include <linux/ioport.h>
29 #include <linux/platform_device.h>
31 #include <mach/hardware.h>
33 #define UART_CLPS711X_NAME "uart-clps711x"
34 #define UART_CLPS711X_NR 2
35 #define UART_CLPS711X_MAJOR 204
36 #define UART_CLPS711X_MINOR 40
38 #define UBRLCR(port) ((port)->line ? UBRLCR2 : UBRLCR1)
39 #define UARTDR(port) ((port)->line ? UARTDR2 : UARTDR1)
40 #define SYSFLG(port) ((port)->line ? SYSFLG2 : SYSFLG1)
41 #define SYSCON(port) ((port)->line ? SYSCON2 : SYSCON1)
42 #define TX_IRQ(port) ((port)->line ? IRQ_UTXINT2 : IRQ_UTXINT1)
43 #define RX_IRQ(port) ((port)->line ? IRQ_URXINT2 : IRQ_URXINT1)
45 struct clps711x_port
{
46 struct uart_driver uart
;
48 struct uart_port port
[UART_CLPS711X_NR
];
49 int tx_enabled
[UART_CLPS711X_NR
];
50 #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
51 struct console console
;
55 static void uart_clps711x_stop_tx(struct uart_port
*port
)
57 struct clps711x_port
*s
= dev_get_drvdata(port
->dev
);
59 if (s
->tx_enabled
[port
->line
]) {
60 disable_irq(TX_IRQ(port
));
61 s
->tx_enabled
[port
->line
] = 0;
65 static void uart_clps711x_start_tx(struct uart_port
*port
)
67 struct clps711x_port
*s
= dev_get_drvdata(port
->dev
);
69 if (!s
->tx_enabled
[port
->line
]) {
70 enable_irq(TX_IRQ(port
));
71 s
->tx_enabled
[port
->line
] = 1;
75 static void uart_clps711x_stop_rx(struct uart_port
*port
)
77 disable_irq(RX_IRQ(port
));
80 static void uart_clps711x_enable_ms(struct uart_port
*port
)
85 static irqreturn_t
uart_clps711x_int_rx(int irq
, void *dev_id
)
87 struct uart_port
*port
= dev_id
;
88 unsigned int status
, ch
, flg
;
91 status
= clps_readl(SYSFLG(port
));
92 if (status
& SYSFLG_URXFE
)
95 ch
= clps_readw(UARTDR(port
));
96 status
= ch
& (UARTDR_FRMERR
| UARTDR_PARERR
| UARTDR_OVERR
);
102 if (unlikely(status
)) {
103 if (status
& UARTDR_PARERR
)
104 port
->icount
.parity
++;
105 else if (status
& UARTDR_FRMERR
)
106 port
->icount
.frame
++;
107 else if (status
& UARTDR_OVERR
)
108 port
->icount
.overrun
++;
110 status
&= port
->read_status_mask
;
112 if (status
& UARTDR_PARERR
)
114 else if (status
& UARTDR_FRMERR
)
116 else if (status
& UARTDR_OVERR
)
120 if (uart_handle_sysrq_char(port
, ch
))
123 if (status
& port
->ignore_status_mask
)
126 uart_insert_char(port
, status
, UARTDR_OVERR
, ch
, flg
);
129 tty_flip_buffer_push(&port
->state
->port
);
134 static irqreturn_t
uart_clps711x_int_tx(int irq
, void *dev_id
)
136 struct uart_port
*port
= dev_id
;
137 struct clps711x_port
*s
= dev_get_drvdata(port
->dev
);
138 struct circ_buf
*xmit
= &port
->state
->xmit
;
141 clps_writew(port
->x_char
, UARTDR(port
));
147 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
148 disable_irq_nosync(TX_IRQ(port
));
149 s
->tx_enabled
[port
->line
] = 0;
153 while (!uart_circ_empty(xmit
)) {
154 clps_writew(xmit
->buf
[xmit
->tail
], UARTDR(port
));
155 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
157 if (clps_readl(SYSFLG(port
) & SYSFLG_UTXFF
))
161 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
162 uart_write_wakeup(port
);
167 static unsigned int uart_clps711x_tx_empty(struct uart_port
*port
)
169 return (clps_readl(SYSFLG(port
) & SYSFLG_UBUSY
)) ? 0 : TIOCSER_TEMT
;
172 static unsigned int uart_clps711x_get_mctrl(struct uart_port
*port
)
174 unsigned int status
, result
= 0;
176 if (port
->line
== 0) {
177 status
= clps_readl(SYSFLG1
);
178 if (status
& SYSFLG1_DCD
)
180 if (status
& SYSFLG1_DSR
)
182 if (status
& SYSFLG1_CTS
)
185 result
= TIOCM_DSR
| TIOCM_CTS
| TIOCM_CAR
;
190 static void uart_clps711x_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
195 static void uart_clps711x_break_ctl(struct uart_port
*port
, int break_state
)
200 spin_lock_irqsave(&port
->lock
, flags
);
202 ubrlcr
= clps_readl(UBRLCR(port
));
204 ubrlcr
|= UBRLCR_BREAK
;
206 ubrlcr
&= ~UBRLCR_BREAK
;
207 clps_writel(ubrlcr
, UBRLCR(port
));
209 spin_unlock_irqrestore(&port
->lock
, flags
);
212 static int uart_clps711x_startup(struct uart_port
*port
)
214 struct clps711x_port
*s
= dev_get_drvdata(port
->dev
);
217 s
->tx_enabled
[port
->line
] = 1;
218 /* Allocate the IRQs */
219 ret
= devm_request_irq(port
->dev
, TX_IRQ(port
), uart_clps711x_int_tx
,
220 0, UART_CLPS711X_NAME
" TX", port
);
224 ret
= devm_request_irq(port
->dev
, RX_IRQ(port
), uart_clps711x_int_rx
,
225 0, UART_CLPS711X_NAME
" RX", port
);
227 devm_free_irq(port
->dev
, TX_IRQ(port
), port
);
232 clps_writel(clps_readl(UBRLCR(port
)) & ~UBRLCR_BREAK
, UBRLCR(port
));
234 /* Enable the port */
235 clps_writel(clps_readl(SYSCON(port
)) | SYSCON_UARTEN
, SYSCON(port
));
240 static void uart_clps711x_shutdown(struct uart_port
*port
)
242 /* Free the interrupts */
243 devm_free_irq(port
->dev
, TX_IRQ(port
), port
);
244 devm_free_irq(port
->dev
, RX_IRQ(port
), port
);
246 /* Disable the port */
247 clps_writel(clps_readl(SYSCON(port
)) & ~SYSCON_UARTEN
, SYSCON(port
));
250 static void uart_clps711x_set_termios(struct uart_port
*port
,
251 struct ktermios
*termios
,
252 struct ktermios
*old
)
254 unsigned int ubrlcr
, baud
, quot
;
257 /* Mask termios capabilities we don't support */
258 termios
->c_cflag
&= ~CMSPAR
;
259 termios
->c_iflag
&= ~(BRKINT
| IGNBRK
);
261 /* Ask the core to calculate the divisor for us */
262 baud
= uart_get_baud_rate(port
, termios
, old
, port
->uartclk
/ 4096,
264 quot
= uart_get_divisor(port
, baud
);
266 switch (termios
->c_cflag
& CSIZE
) {
268 ubrlcr
= UBRLCR_WRDLEN5
;
271 ubrlcr
= UBRLCR_WRDLEN6
;
274 ubrlcr
= UBRLCR_WRDLEN7
;
278 ubrlcr
= UBRLCR_WRDLEN8
;
282 if (termios
->c_cflag
& CSTOPB
)
283 ubrlcr
|= UBRLCR_XSTOP
;
285 if (termios
->c_cflag
& PARENB
) {
286 ubrlcr
|= UBRLCR_PRTEN
;
287 if (!(termios
->c_cflag
& PARODD
))
288 ubrlcr
|= UBRLCR_EVENPRT
;
292 ubrlcr
|= UBRLCR_FIFOEN
;
294 spin_lock_irqsave(&port
->lock
, flags
);
296 /* Set read status mask */
297 port
->read_status_mask
= UARTDR_OVERR
;
298 if (termios
->c_iflag
& INPCK
)
299 port
->read_status_mask
|= UARTDR_PARERR
| UARTDR_FRMERR
;
301 /* Set status ignore mask */
302 port
->ignore_status_mask
= 0;
303 if (!(termios
->c_cflag
& CREAD
))
304 port
->ignore_status_mask
|= UARTDR_OVERR
| UARTDR_PARERR
|
307 uart_update_timeout(port
, termios
->c_cflag
, baud
);
309 clps_writel(ubrlcr
| (quot
- 1), UBRLCR(port
));
311 spin_unlock_irqrestore(&port
->lock
, flags
);
314 static const char *uart_clps711x_type(struct uart_port
*port
)
316 return (port
->type
== PORT_CLPS711X
) ? "CLPS711X" : NULL
;
319 static void uart_clps711x_config_port(struct uart_port
*port
, int flags
)
321 if (flags
& UART_CONFIG_TYPE
)
322 port
->type
= PORT_CLPS711X
;
325 static void uart_clps711x_release_port(struct uart_port
*port
)
330 static int uart_clps711x_request_port(struct uart_port
*port
)
336 static const struct uart_ops uart_clps711x_ops
= {
337 .tx_empty
= uart_clps711x_tx_empty
,
338 .set_mctrl
= uart_clps711x_set_mctrl
,
339 .get_mctrl
= uart_clps711x_get_mctrl
,
340 .stop_tx
= uart_clps711x_stop_tx
,
341 .start_tx
= uart_clps711x_start_tx
,
342 .stop_rx
= uart_clps711x_stop_rx
,
343 .enable_ms
= uart_clps711x_enable_ms
,
344 .break_ctl
= uart_clps711x_break_ctl
,
345 .startup
= uart_clps711x_startup
,
346 .shutdown
= uart_clps711x_shutdown
,
347 .set_termios
= uart_clps711x_set_termios
,
348 .type
= uart_clps711x_type
,
349 .config_port
= uart_clps711x_config_port
,
350 .release_port
= uart_clps711x_release_port
,
351 .request_port
= uart_clps711x_request_port
,
354 #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
355 static void uart_clps711x_console_putchar(struct uart_port
*port
, int ch
)
357 while (clps_readl(SYSFLG(port
)) & SYSFLG_UTXFF
)
360 clps_writew(ch
, UARTDR(port
));
363 static void uart_clps711x_console_write(struct console
*co
, const char *c
,
366 struct clps711x_port
*s
= (struct clps711x_port
*)co
->data
;
367 struct uart_port
*port
= &s
->port
[co
->index
];
370 /* Ensure that the port is enabled */
371 syscon
= clps_readl(SYSCON(port
));
372 clps_writel(syscon
| SYSCON_UARTEN
, SYSCON(port
));
374 uart_console_write(port
, c
, n
, uart_clps711x_console_putchar
);
376 /* Wait for transmitter to become empty */
377 while (clps_readl(SYSFLG(port
)) & SYSFLG_UBUSY
)
380 /* Restore the uart state */
381 clps_writel(syscon
, SYSCON(port
));
384 static void uart_clps711x_console_get_options(struct uart_port
*port
,
385 int *baud
, int *parity
,
388 if (clps_readl(SYSCON(port
)) & SYSCON_UARTEN
) {
389 unsigned int ubrlcr
, quot
;
391 ubrlcr
= clps_readl(UBRLCR(port
));
394 if (ubrlcr
& UBRLCR_PRTEN
) {
395 if (ubrlcr
& UBRLCR_EVENPRT
)
401 if ((ubrlcr
& UBRLCR_WRDLEN_MASK
) == UBRLCR_WRDLEN7
)
406 quot
= ubrlcr
& UBRLCR_BAUD_MASK
;
407 *baud
= port
->uartclk
/ (16 * (quot
+ 1));
411 static int uart_clps711x_console_setup(struct console
*co
, char *options
)
413 int baud
= 38400, bits
= 8, parity
= 'n', flow
= 'n';
414 struct clps711x_port
*s
= (struct clps711x_port
*)co
->data
;
415 struct uart_port
*port
= &s
->port
[(co
->index
> 0) ? co
->index
: 0];
418 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
420 uart_clps711x_console_get_options(port
, &baud
, &parity
, &bits
);
422 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
426 static int uart_clps711x_probe(struct platform_device
*pdev
)
428 struct clps711x_port
*s
;
431 s
= devm_kzalloc(&pdev
->dev
, sizeof(struct clps711x_port
), GFP_KERNEL
);
433 dev_err(&pdev
->dev
, "Error allocating port structure\n");
436 platform_set_drvdata(pdev
, s
);
438 s
->uart_clk
= devm_clk_get(&pdev
->dev
, "uart");
439 if (IS_ERR(s
->uart_clk
)) {
440 dev_err(&pdev
->dev
, "Can't get UART clocks\n");
441 ret
= PTR_ERR(s
->uart_clk
);
445 s
->uart
.owner
= THIS_MODULE
;
446 s
->uart
.dev_name
= "ttyCL";
447 s
->uart
.major
= UART_CLPS711X_MAJOR
;
448 s
->uart
.minor
= UART_CLPS711X_MINOR
;
449 s
->uart
.nr
= UART_CLPS711X_NR
;
450 #ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
451 s
->uart
.cons
= &s
->console
;
452 s
->uart
.cons
->device
= uart_console_device
;
453 s
->uart
.cons
->write
= uart_clps711x_console_write
;
454 s
->uart
.cons
->setup
= uart_clps711x_console_setup
;
455 s
->uart
.cons
->flags
= CON_PRINTBUFFER
;
456 s
->uart
.cons
->index
= -1;
457 s
->uart
.cons
->data
= s
;
458 strcpy(s
->uart
.cons
->name
, "ttyCL");
460 ret
= uart_register_driver(&s
->uart
);
462 dev_err(&pdev
->dev
, "Registering UART driver failed\n");
463 devm_clk_put(&pdev
->dev
, s
->uart_clk
);
467 for (i
= 0; i
< UART_CLPS711X_NR
; i
++) {
469 s
->port
[i
].dev
= &pdev
->dev
;
470 s
->port
[i
].irq
= TX_IRQ(&s
->port
[i
]);
471 s
->port
[i
].iobase
= SYSCON(&s
->port
[i
]);
472 s
->port
[i
].type
= PORT_CLPS711X
;
473 s
->port
[i
].fifosize
= 16;
474 s
->port
[i
].flags
= UPF_SKIP_TEST
| UPF_FIXED_TYPE
;
475 s
->port
[i
].uartclk
= clk_get_rate(s
->uart_clk
);
476 s
->port
[i
].ops
= &uart_clps711x_ops
;
477 WARN_ON(uart_add_one_port(&s
->uart
, &s
->port
[i
]));
483 platform_set_drvdata(pdev
, NULL
);
488 static int uart_clps711x_remove(struct platform_device
*pdev
)
490 struct clps711x_port
*s
= platform_get_drvdata(pdev
);
493 for (i
= 0; i
< UART_CLPS711X_NR
; i
++)
494 uart_remove_one_port(&s
->uart
, &s
->port
[i
]);
496 devm_clk_put(&pdev
->dev
, s
->uart_clk
);
497 uart_unregister_driver(&s
->uart
);
498 platform_set_drvdata(pdev
, NULL
);
503 static struct platform_driver clps711x_uart_driver
= {
505 .name
= UART_CLPS711X_NAME
,
506 .owner
= THIS_MODULE
,
508 .probe
= uart_clps711x_probe
,
509 .remove
= uart_clps711x_remove
,
511 module_platform_driver(clps711x_uart_driver
);
513 static struct platform_device clps711x_uart_device
= {
514 .name
= UART_CLPS711X_NAME
,
517 static int __init
uart_clps711x_init(void)
519 return platform_device_register(&clps711x_uart_device
);
521 module_init(uart_clps711x_init
);
523 static void __exit
uart_clps711x_exit(void)
525 platform_device_unregister(&clps711x_uart_device
);
527 module_exit(uart_clps711x_exit
);
529 MODULE_AUTHOR("Deep Blue Solutions Ltd");
530 MODULE_DESCRIPTION("CLPS711X serial driver");
531 MODULE_LICENSE("GPL");