Merge branch 'devicetree/next' of git://git.secretlab.ca/git/linux-2.6
[deliverable/linux.git] / drivers / tty / serial / altera_jtaguart.c
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 */
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 */
63 };
64
65 static 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
71 static unsigned int altera_jtaguart_get_mctrl(struct uart_port *port)
72 {
73 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
74 }
75
76 static void altera_jtaguart_set_mctrl(struct uart_port *port, unsigned int sigs)
77 {
78 }
79
80 static 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
89 static 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
98 static 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
107 static void altera_jtaguart_break_ctl(struct uart_port *port, int break_state)
108 {
109 }
110
111 static void altera_jtaguart_enable_ms(struct uart_port *port)
112 {
113 }
114
115 static 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
124 static 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
144 static 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
184 static 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
206 static 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
214 static 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
240 static 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
257 static const char *altera_jtaguart_type(struct uart_port *port)
258 {
259 return (port->type == PORT_ALTERA_JTAGUART) ? "Altera JTAG UART" : NULL;
260 }
261
262 static int altera_jtaguart_request_port(struct uart_port *port)
263 {
264 /* UARTs always present */
265 return 0;
266 }
267
268 static void altera_jtaguart_release_port(struct uart_port *port)
269 {
270 /* Nothing to release... */
271 }
272
273 static 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 */
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,
301 };
302
303 #define ALTERA_JTAGUART_MAXPORTS 1
304 static 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)
309 static 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
330 static 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
347 static 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
357 static 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
370 static struct uart_driver altera_jtaguart_driver;
371
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,
380 };
381
382 static int __init altera_jtaguart_console_init(void)
383 {
384 register_console(&altera_jtaguart_console);
385 return 0;
386 }
387
388 console_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
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,
406 };
407
408 static 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
455 static 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
470 static struct of_device_id altera_jtaguart_match[] = {
471 { .compatible = "ALTR,juart-1.0", },
472 {},
473 };
474 MODULE_DEVICE_TABLE(of, altera_jtaguart_match);
475 #else
476 #define altera_jtaguart_match NULL
477 #endif /* CONFIG_OF */
478
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,
486 },
487 };
488
489 static 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
504 static void __exit altera_jtaguart_exit(void)
505 {
506 platform_driver_unregister(&altera_jtaguart_platform_driver);
507 uart_unregister_driver(&altera_jtaguart_driver);
508 }
509
510 module_init(altera_jtaguart_init);
511 module_exit(altera_jtaguart_exit);
512
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);
This page took 0.041591 seconds and 5 git commands to generate.