2 * uartlite.c: Serial driver for Xilinx uartlite serial controller
4 * Peter Korsgaard <jacmet@sunsite.dk>
6 * This file is licensed under the terms of the GNU General Public License
7 * version 2. This program is licensed "as is" without any warranty of any
8 * kind, whether express or implied.
11 #include <linux/platform_device.h>
12 #include <linux/module.h>
13 #include <linux/console.h>
14 #include <linux/serial.h>
15 #include <linux/serial_core.h>
16 #include <linux/tty.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
21 #define ULITE_NAME "ttyUL"
22 #define ULITE_MAJOR 204
23 #define ULITE_MINOR 187
24 #define ULITE_NR_UARTS 4
26 /* For register details see datasheet:
27 http://www.xilinx.com/bvdocs/ipcenter/data_sheet/opb_uartlite.pdf
31 #define ULITE_STATUS 0x08
32 #define ULITE_CONTROL 0x0c
34 #define ULITE_REGION 16
36 #define ULITE_STATUS_RXVALID 0x01
37 #define ULITE_STATUS_RXFULL 0x02
38 #define ULITE_STATUS_TXEMPTY 0x04
39 #define ULITE_STATUS_TXFULL 0x08
40 #define ULITE_STATUS_IE 0x10
41 #define ULITE_STATUS_OVERRUN 0x20
42 #define ULITE_STATUS_FRAME 0x40
43 #define ULITE_STATUS_PARITY 0x80
45 #define ULITE_CONTROL_RST_TX 0x01
46 #define ULITE_CONTROL_RST_RX 0x02
47 #define ULITE_CONTROL_IE 0x10
50 static struct uart_port ulite_ports
[ULITE_NR_UARTS
];
52 static int ulite_receive(struct uart_port
*port
, int stat
)
54 struct tty_struct
*tty
= port
->info
->tty
;
56 char flag
= TTY_NORMAL
;
58 if ((stat
& (ULITE_STATUS_RXVALID
| ULITE_STATUS_OVERRUN
59 | ULITE_STATUS_FRAME
)) == 0)
63 if (stat
& ULITE_STATUS_RXVALID
) {
65 ch
= in_be32((void*)port
->membase
+ ULITE_RX
);
67 if (stat
& ULITE_STATUS_PARITY
)
68 port
->icount
.parity
++;
71 if (stat
& ULITE_STATUS_OVERRUN
)
72 port
->icount
.overrun
++;
74 if (stat
& ULITE_STATUS_FRAME
)
78 /* drop byte with parity error if IGNPAR specificed */
79 if (stat
& port
->ignore_status_mask
& ULITE_STATUS_PARITY
)
80 stat
&= ~ULITE_STATUS_RXVALID
;
82 stat
&= port
->read_status_mask
;
84 if (stat
& ULITE_STATUS_PARITY
)
88 stat
&= ~port
->ignore_status_mask
;
90 if (stat
& ULITE_STATUS_RXVALID
)
91 tty_insert_flip_char(tty
, ch
, flag
);
93 if (stat
& ULITE_STATUS_FRAME
)
94 tty_insert_flip_char(tty
, 0, TTY_FRAME
);
96 if (stat
& ULITE_STATUS_OVERRUN
)
97 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
102 static int ulite_transmit(struct uart_port
*port
, int stat
)
104 struct circ_buf
*xmit
= &port
->info
->xmit
;
106 if (stat
& ULITE_STATUS_TXFULL
)
110 out_be32((void*)port
->membase
+ ULITE_TX
, port
->x_char
);
116 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
119 out_be32((void*)port
->membase
+ ULITE_TX
, xmit
->buf
[xmit
->tail
]);
120 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
-1);
124 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
125 uart_write_wakeup(port
);
130 static irqreturn_t
ulite_isr(int irq
, void *dev_id
)
132 struct uart_port
*port
= (struct uart_port
*)dev_id
;
136 int stat
= in_be32((void*)port
->membase
+ ULITE_STATUS
);
137 busy
= ulite_receive(port
, stat
);
138 busy
|= ulite_transmit(port
, stat
);
141 tty_flip_buffer_push(port
->info
->tty
);
146 static unsigned int ulite_tx_empty(struct uart_port
*port
)
151 spin_lock_irqsave(&port
->lock
, flags
);
152 ret
= in_be32((void*)port
->membase
+ ULITE_STATUS
);
153 spin_unlock_irqrestore(&port
->lock
, flags
);
155 return ret
& ULITE_STATUS_TXEMPTY
? TIOCSER_TEMT
: 0;
158 static unsigned int ulite_get_mctrl(struct uart_port
*port
)
160 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
163 static void ulite_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
168 static void ulite_stop_tx(struct uart_port
*port
)
173 static void ulite_start_tx(struct uart_port
*port
)
175 ulite_transmit(port
, in_be32((void*)port
->membase
+ ULITE_STATUS
));
178 static void ulite_stop_rx(struct uart_port
*port
)
180 /* don't forward any more data (like !CREAD) */
181 port
->ignore_status_mask
= ULITE_STATUS_RXVALID
| ULITE_STATUS_PARITY
182 | ULITE_STATUS_FRAME
| ULITE_STATUS_OVERRUN
;
185 static void ulite_enable_ms(struct uart_port
*port
)
190 static void ulite_break_ctl(struct uart_port
*port
, int ctl
)
195 static int ulite_startup(struct uart_port
*port
)
199 ret
= request_irq(port
->irq
, ulite_isr
,
200 IRQF_DISABLED
| IRQF_SAMPLE_RANDOM
, "uartlite", port
);
204 out_be32((void*)port
->membase
+ ULITE_CONTROL
,
205 ULITE_CONTROL_RST_RX
| ULITE_CONTROL_RST_TX
);
206 out_be32((void*)port
->membase
+ ULITE_CONTROL
, ULITE_CONTROL_IE
);
211 static void ulite_shutdown(struct uart_port
*port
)
213 out_be32((void*)port
->membase
+ ULITE_CONTROL
, 0);
214 in_be32((void*)port
->membase
+ ULITE_CONTROL
); /* dummy */
215 free_irq(port
->irq
, port
);
218 static void ulite_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
219 struct ktermios
*old
)
224 spin_lock_irqsave(&port
->lock
, flags
);
226 port
->read_status_mask
= ULITE_STATUS_RXVALID
| ULITE_STATUS_OVERRUN
227 | ULITE_STATUS_TXFULL
;
229 if (termios
->c_iflag
& INPCK
)
230 port
->read_status_mask
|=
231 ULITE_STATUS_PARITY
| ULITE_STATUS_FRAME
;
233 port
->ignore_status_mask
= 0;
234 if (termios
->c_iflag
& IGNPAR
)
235 port
->ignore_status_mask
|= ULITE_STATUS_PARITY
236 | ULITE_STATUS_FRAME
| ULITE_STATUS_OVERRUN
;
238 /* ignore all characters if CREAD is not set */
239 if ((termios
->c_cflag
& CREAD
) == 0)
240 port
->ignore_status_mask
|=
241 ULITE_STATUS_RXVALID
| ULITE_STATUS_PARITY
242 | ULITE_STATUS_FRAME
| ULITE_STATUS_OVERRUN
;
245 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 460800);
246 uart_update_timeout(port
, termios
->c_cflag
, baud
);
248 spin_unlock_irqrestore(&port
->lock
, flags
);
251 static const char *ulite_type(struct uart_port
*port
)
253 return port
->type
== PORT_UARTLITE
? "uartlite" : NULL
;
256 static void ulite_release_port(struct uart_port
*port
)
258 release_mem_region(port
->mapbase
, ULITE_REGION
);
259 iounmap(port
->membase
);
260 port
->membase
= NULL
;
263 static int ulite_request_port(struct uart_port
*port
)
265 if (!request_mem_region(port
->mapbase
, ULITE_REGION
, "uartlite")) {
266 dev_err(port
->dev
, "Memory region busy\n");
270 port
->membase
= ioremap(port
->mapbase
, ULITE_REGION
);
271 if (!port
->membase
) {
272 dev_err(port
->dev
, "Unable to map registers\n");
273 release_mem_region(port
->mapbase
, ULITE_REGION
);
280 static void ulite_config_port(struct uart_port
*port
, int flags
)
282 if (!ulite_request_port(port
))
283 port
->type
= PORT_UARTLITE
;
286 static int ulite_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
288 /* we don't want the core code to modify any port params */
292 static struct uart_ops ulite_ops
= {
293 .tx_empty
= ulite_tx_empty
,
294 .set_mctrl
= ulite_set_mctrl
,
295 .get_mctrl
= ulite_get_mctrl
,
296 .stop_tx
= ulite_stop_tx
,
297 .start_tx
= ulite_start_tx
,
298 .stop_rx
= ulite_stop_rx
,
299 .enable_ms
= ulite_enable_ms
,
300 .break_ctl
= ulite_break_ctl
,
301 .startup
= ulite_startup
,
302 .shutdown
= ulite_shutdown
,
303 .set_termios
= ulite_set_termios
,
305 .release_port
= ulite_release_port
,
306 .request_port
= ulite_request_port
,
307 .config_port
= ulite_config_port
,
308 .verify_port
= ulite_verify_port
311 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
312 static void ulite_console_wait_tx(struct uart_port
*port
)
316 /* wait up to 10ms for the character(s) to be sent */
317 for (i
= 0; i
< 10000; i
++) {
318 if (in_be32((void*)port
->membase
+ ULITE_STATUS
) & ULITE_STATUS_TXEMPTY
)
324 static void ulite_console_putchar(struct uart_port
*port
, int ch
)
326 ulite_console_wait_tx(port
);
327 out_be32((void*)port
->membase
+ ULITE_TX
, ch
);
330 static void ulite_console_write(struct console
*co
, const char *s
,
333 struct uart_port
*port
= &ulite_ports
[co
->index
];
338 if (oops_in_progress
) {
339 locked
= spin_trylock_irqsave(&port
->lock
, flags
);
341 spin_lock_irqsave(&port
->lock
, flags
);
343 /* save and disable interrupt */
344 ier
= in_be32((void*)port
->membase
+ ULITE_STATUS
) & ULITE_STATUS_IE
;
345 out_be32((void*)port
->membase
+ ULITE_CONTROL
, 0);
347 uart_console_write(port
, s
, count
, ulite_console_putchar
);
349 ulite_console_wait_tx(port
);
351 /* restore interrupt state */
353 out_be32((void*)port
->membase
+ ULITE_CONTROL
, ULITE_CONTROL_IE
);
356 spin_unlock_irqrestore(&port
->lock
, flags
);
359 static int __init
ulite_console_setup(struct console
*co
, char *options
)
361 struct uart_port
*port
;
367 if (co
->index
< 0 || co
->index
>= ULITE_NR_UARTS
)
370 port
= &ulite_ports
[co
->index
];
372 /* not initialized yet? */
377 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
379 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
382 static struct uart_driver ulite_uart_driver
;
384 static struct console ulite_console
= {
386 .write
= ulite_console_write
,
387 .device
= uart_console_device
,
388 .setup
= ulite_console_setup
,
389 .flags
= CON_PRINTBUFFER
,
390 .index
= -1, /* Specified on the cmdline (e.g. console=ttyUL0 ) */
391 .data
= &ulite_uart_driver
,
394 static int __init
ulite_console_init(void)
396 register_console(&ulite_console
);
400 console_initcall(ulite_console_init
);
402 #endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
404 static struct uart_driver ulite_uart_driver
= {
405 .owner
= THIS_MODULE
,
406 .driver_name
= "uartlite",
407 .dev_name
= ULITE_NAME
,
408 .major
= ULITE_MAJOR
,
409 .minor
= ULITE_MINOR
,
410 .nr
= ULITE_NR_UARTS
,
411 #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
412 .cons
= &ulite_console
,
416 static int __devinit
ulite_assign(struct device
*dev
, int id
, u32 base
, int irq
)
418 struct uart_port
*port
;
421 /* if id = -1; then scan for a free id and use that */
423 for (id
= 0; id
< ULITE_NR_UARTS
; id
++)
424 if (ulite_ports
[id
].mapbase
== 0)
427 if (id
< 0 || id
>= ULITE_NR_UARTS
) {
428 dev_err(dev
, "%s%i too large\n", ULITE_NAME
, id
);
432 if (ulite_ports
[id
].mapbase
) {
433 dev_err(dev
, "cannot assign to %s%i; it is already in use\n",
438 port
= &ulite_ports
[id
];
440 spin_lock_init(&port
->lock
);
443 port
->iotype
= UPIO_MEM
;
444 port
->iobase
= 1; /* mark port in use */
445 port
->mapbase
= base
;
446 port
->membase
= NULL
;
447 port
->ops
= &ulite_ops
;
449 port
->flags
= UPF_BOOT_AUTOCONF
;
451 port
->type
= PORT_UNKNOWN
;
454 dev_set_drvdata(dev
, port
);
456 /* Register the port */
457 rc
= uart_add_one_port(&ulite_uart_driver
, port
);
459 dev_err(dev
, "uart_add_one_port() failed; err=%i\n", rc
);
461 dev_set_drvdata(dev
, NULL
);
468 static int __devinit
ulite_release(struct device
*dev
)
470 struct uart_port
*port
= dev_get_drvdata(dev
);
474 rc
= uart_remove_one_port(&ulite_uart_driver
, port
);
475 dev_set_drvdata(dev
, NULL
);
482 static int __devinit
ulite_probe(struct platform_device
*pdev
)
484 struct resource
*res
, *res2
;
486 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
490 res2
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
494 return ulite_assign(&pdev
->dev
, pdev
->id
, res
->start
, res2
->start
);
497 static int ulite_remove(struct platform_device
*pdev
)
499 return ulite_release(&pdev
->dev
);
502 static struct platform_driver ulite_platform_driver
= {
503 .probe
= ulite_probe
,
504 .remove
= ulite_remove
,
506 .owner
= THIS_MODULE
,
511 int __init
ulite_init(void)
515 ret
= uart_register_driver(&ulite_uart_driver
);
519 ret
= platform_driver_register(&ulite_platform_driver
);
521 uart_unregister_driver(&ulite_uart_driver
);
526 void __exit
ulite_exit(void)
528 platform_driver_unregister(&ulite_platform_driver
);
529 uart_unregister_driver(&ulite_uart_driver
);
532 module_init(ulite_init
);
533 module_exit(ulite_exit
);
535 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
536 MODULE_DESCRIPTION("Xilinx uartlite serial driver");
537 MODULE_LICENSE("GPL");