r8169: remove erroneous processing of always set bit.
[linux-2.6.git] / drivers / tty / serial / altera_jtaguart.c
blob60e049b041a7a9425ec95db6689ab575e0fbbad2
1 /*
2 * altera_jtaguart.c -- Altera JTAG UART driver
4 * Based on mcf.c -- Freescale ColdFire UART driver
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>
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.
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>
29 #define DRV_NAME "altera_jtaguart"
32 * Altera JTAG UART register definitions according to the Altera JTAG UART
33 * datasheet: http://www.altera.com/literature/hb/nios2/n2cpu_nii51009.pdf
36 #define ALTERA_JTAGUART_SIZE 8
38 #define ALTERA_JTAGUART_DATA_REG 0
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
45 #define ALTERA_JTAGUART_CONTROL_REG 4
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
57 * Local per-uart structure.
59 struct altera_jtaguart {
60 struct uart_port port;
61 unsigned int sigs; /* Local copy of line sigs */
62 unsigned long imr; /* Local IMR mirror */
65 static unsigned int altera_jtaguart_tx_empty(struct uart_port *port)
67 return (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
68 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) ? TIOCSER_TEMT : 0;
71 static unsigned int altera_jtaguart_get_mctrl(struct uart_port *port)
73 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
76 static void altera_jtaguart_set_mctrl(struct uart_port *port, unsigned int sigs)
80 static void altera_jtaguart_start_tx(struct uart_port *port)
82 struct altera_jtaguart *pp =
83 container_of(port, struct altera_jtaguart, port);
85 pp->imr |= ALTERA_JTAGUART_CONTROL_WE_MSK;
86 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
89 static void altera_jtaguart_stop_tx(struct uart_port *port)
91 struct altera_jtaguart *pp =
92 container_of(port, struct altera_jtaguart, port);
94 pp->imr &= ~ALTERA_JTAGUART_CONTROL_WE_MSK;
95 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
98 static void altera_jtaguart_stop_rx(struct uart_port *port)
100 struct altera_jtaguart *pp =
101 container_of(port, struct altera_jtaguart, port);
103 pp->imr &= ~ALTERA_JTAGUART_CONTROL_RE_MSK;
104 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
107 static void altera_jtaguart_break_ctl(struct uart_port *port, int break_state)
111 static void altera_jtaguart_enable_ms(struct uart_port *port)
115 static void altera_jtaguart_set_termios(struct uart_port *port,
116 struct ktermios *termios,
117 struct ktermios *old)
119 /* Just copy the old termios settings back */
120 if (old)
121 tty_termios_copy_hw(termios, old);
124 static void altera_jtaguart_rx_chars(struct altera_jtaguart *pp)
126 struct uart_port *port = &pp->port;
127 unsigned char ch, flag;
128 unsigned long status;
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++;
136 if (uart_handle_sysrq_char(port, ch))
137 continue;
138 uart_insert_char(port, 0, 0, ch, flag);
141 tty_flip_buffer_push(port->state->port.tty);
144 static void altera_jtaguart_tx_chars(struct altera_jtaguart *pp)
146 struct uart_port *port = &pp->port;
147 struct circ_buf *xmit = &port->state->xmit;
148 unsigned int pending, count;
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;
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++;
173 if (pending < WAKEUP_CHARS)
174 uart_write_wakeup(port);
178 if (pending == 0) {
179 pp->imr &= ~ALTERA_JTAGUART_CONTROL_WE_MSK;
180 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
184 static irqreturn_t altera_jtaguart_interrupt(int irq, void *data)
186 struct uart_port *port = data;
187 struct altera_jtaguart *pp =
188 container_of(port, struct altera_jtaguart, port);
189 unsigned int isr;
191 isr = (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) >>
192 ALTERA_JTAGUART_CONTROL_RI_OFF) & pp->imr;
194 spin_lock(&port->lock);
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);
201 spin_unlock(&port->lock);
203 return IRQ_RETVAL(isr);
206 static void altera_jtaguart_config_port(struct uart_port *port, int flags)
208 port->type = PORT_ALTERA_JTAGUART;
210 /* Clear mask, so no surprise interrupts. */
211 writel(0, port->membase + ALTERA_JTAGUART_CONTROL_REG);
214 static int altera_jtaguart_startup(struct uart_port *port)
216 struct altera_jtaguart *pp =
217 container_of(port, struct altera_jtaguart, port);
218 unsigned long flags;
219 int ret;
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;
229 spin_lock_irqsave(&port->lock, flags);
231 /* Enable RX interrupts now */
232 pp->imr = ALTERA_JTAGUART_CONTROL_RE_MSK;
233 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
235 spin_unlock_irqrestore(&port->lock, flags);
237 return 0;
240 static void altera_jtaguart_shutdown(struct uart_port *port)
242 struct altera_jtaguart *pp =
243 container_of(port, struct altera_jtaguart, port);
244 unsigned long flags;
246 spin_lock_irqsave(&port->lock, flags);
248 /* Disable all interrupts now */
249 pp->imr = 0;
250 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
252 spin_unlock_irqrestore(&port->lock, flags);
254 free_irq(port->irq, port);
257 static const char *altera_jtaguart_type(struct uart_port *port)
259 return (port->type == PORT_ALTERA_JTAGUART) ? "Altera JTAG UART" : NULL;
262 static int altera_jtaguart_request_port(struct uart_port *port)
264 /* UARTs always present */
265 return 0;
268 static void altera_jtaguart_release_port(struct uart_port *port)
270 /* Nothing to release... */
273 static int altera_jtaguart_verify_port(struct uart_port *port,
274 struct serial_struct *ser)
276 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ALTERA_JTAGUART)
277 return -EINVAL;
278 return 0;
282 * Define the basic serial functions we support.
284 static 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,
303 #define ALTERA_JTAGUART_MAXPORTS 1
304 static struct altera_jtaguart altera_jtaguart_ports[ALTERA_JTAGUART_MAXPORTS];
306 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE)
308 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
309 static void altera_jtaguart_console_putc(struct console *co, const char c)
311 struct uart_port *port = &(altera_jtaguart_ports + co->index)->port;
312 unsigned long status;
313 unsigned long flags;
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 */
322 spin_unlock_irqrestore(&port->lock, flags);
323 cpu_relax();
324 spin_lock_irqsave(&port->lock, flags);
326 writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
327 spin_unlock_irqrestore(&port->lock, flags);
329 #else
330 static void altera_jtaguart_console_putc(struct console *co, const char c)
332 struct uart_port *port = &(altera_jtaguart_ports + co->index)->port;
333 unsigned long flags;
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);
342 writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
343 spin_unlock_irqrestore(&port->lock, flags);
345 #endif
347 static void altera_jtaguart_console_write(struct console *co, const char *s,
348 unsigned int count)
350 for (; count; count--, s++) {
351 altera_jtaguart_console_putc(co, *s);
352 if (*s == '\n')
353 altera_jtaguart_console_putc(co, '\r');
357 static int __init altera_jtaguart_console_setup(struct console *co,
358 char *options)
360 struct uart_port *port;
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;
370 static struct uart_driver altera_jtaguart_driver;
372 static 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,
382 static int __init altera_jtaguart_console_init(void)
384 register_console(&altera_jtaguart_console);
385 return 0;
388 console_initcall(altera_jtaguart_console_init);
390 #define ALTERA_JTAGUART_CONSOLE (&altera_jtaguart_console)
392 #else
394 #define ALTERA_JTAGUART_CONSOLE NULL
396 #endif /* CONFIG_ALTERA_JTAGUART_CONSOLE */
398 static 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,
408 static int __devinit altera_jtaguart_probe(struct platform_device *pdev)
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;
415 /* -1 emphasizes that the platform must have one port, no .N suffix */
416 if (i == -1)
417 i = 0;
419 if (i >= ALTERA_JTAGUART_MAXPORTS)
420 return -EINVAL;
422 port = &altera_jtaguart_ports[i].port;
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;
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;
440 port->membase = ioremap(port->mapbase, ALTERA_JTAGUART_SIZE);
441 if (!port->membase)
442 return -ENOMEM;
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;
450 uart_add_one_port(&altera_jtaguart_driver, port);
452 return 0;
455 static int __devexit altera_jtaguart_remove(struct platform_device *pdev)
457 struct uart_port *port;
458 int i = pdev->id;
460 if (i == -1)
461 i = 0;
463 port = &altera_jtaguart_ports[i].port;
464 uart_remove_one_port(&altera_jtaguart_driver, port);
466 return 0;
469 #ifdef CONFIG_OF
470 static struct of_device_id altera_jtaguart_match[] = {
471 { .compatible = "ALTR,juart-1.0", },
474 MODULE_DEVICE_TABLE(of, altera_jtaguart_match);
475 #else
476 #define altera_jtaguart_match NULL
477 #endif /* CONFIG_OF */
479 static 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,
489 static int __init altera_jtaguart_init(void)
491 int rc;
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;
501 return 0;
504 static void __exit altera_jtaguart_exit(void)
506 platform_driver_unregister(&altera_jtaguart_platform_driver);
507 uart_unregister_driver(&altera_jtaguart_driver);
510 module_init(altera_jtaguart_init);
511 module_exit(altera_jtaguart_exit);
513 MODULE_DESCRIPTION("Altera JTAG UART driver");
514 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
515 MODULE_LICENSE("GPL");
516 MODULE_ALIAS("platform:" DRV_NAME);