e7d1aaf07294a18aeeca7719044f330026c45cab
[cascardo/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/of.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/io.h>
28 #include <linux/altera_jtaguart.h>
29
30 #define DRV_NAME "altera_jtaguart"
31
32 /*
33  * Altera JTAG UART register definitions according to the Altera JTAG UART
34  * datasheet: http://www.altera.com/literature/hb/nios2/n2cpu_nii51009.pdf
35  */
36
37 #define ALTERA_JTAGUART_SIZE                    8
38
39 #define ALTERA_JTAGUART_DATA_REG                0
40
41 #define ALTERA_JTAGUART_DATA_DATA_MSK           0x000000FF
42 #define ALTERA_JTAGUART_DATA_RVALID_MSK         0x00008000
43 #define ALTERA_JTAGUART_DATA_RAVAIL_MSK         0xFFFF0000
44 #define ALTERA_JTAGUART_DATA_RAVAIL_OFF         16
45
46 #define ALTERA_JTAGUART_CONTROL_REG             4
47
48 #define ALTERA_JTAGUART_CONTROL_RE_MSK          0x00000001
49 #define ALTERA_JTAGUART_CONTROL_WE_MSK          0x00000002
50 #define ALTERA_JTAGUART_CONTROL_RI_MSK          0x00000100
51 #define ALTERA_JTAGUART_CONTROL_RI_OFF          8
52 #define ALTERA_JTAGUART_CONTROL_WI_MSK          0x00000200
53 #define ALTERA_JTAGUART_CONTROL_AC_MSK          0x00000400
54 #define ALTERA_JTAGUART_CONTROL_WSPACE_MSK      0xFFFF0000
55 #define ALTERA_JTAGUART_CONTROL_WSPACE_OFF      16
56
57 /*
58  * Local per-uart structure.
59  */
60 struct altera_jtaguart {
61         struct uart_port port;
62         unsigned int sigs;      /* Local copy of line sigs */
63         unsigned long imr;      /* Local IMR mirror */
64 };
65
66 static unsigned int altera_jtaguart_tx_empty(struct uart_port *port)
67 {
68         return (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
69                 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) ? TIOCSER_TEMT : 0;
70 }
71
72 static unsigned int altera_jtaguart_get_mctrl(struct uart_port *port)
73 {
74         return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
75 }
76
77 static void altera_jtaguart_set_mctrl(struct uart_port *port, unsigned int sigs)
78 {
79 }
80
81 static void altera_jtaguart_start_tx(struct uart_port *port)
82 {
83         struct altera_jtaguart *pp =
84             container_of(port, struct altera_jtaguart, port);
85
86         pp->imr |= ALTERA_JTAGUART_CONTROL_WE_MSK;
87         writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
88 }
89
90 static void altera_jtaguart_stop_tx(struct uart_port *port)
91 {
92         struct altera_jtaguart *pp =
93             container_of(port, struct altera_jtaguart, port);
94
95         pp->imr &= ~ALTERA_JTAGUART_CONTROL_WE_MSK;
96         writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
97 }
98
99 static void altera_jtaguart_stop_rx(struct uart_port *port)
100 {
101         struct altera_jtaguart *pp =
102             container_of(port, struct altera_jtaguart, port);
103
104         pp->imr &= ~ALTERA_JTAGUART_CONTROL_RE_MSK;
105         writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
106 }
107
108 static void altera_jtaguart_break_ctl(struct uart_port *port, int break_state)
109 {
110 }
111
112 static void altera_jtaguart_set_termios(struct uart_port *port,
113                                         struct ktermios *termios,
114                                         struct ktermios *old)
115 {
116         /* Just copy the old termios settings back */
117         if (old)
118                 tty_termios_copy_hw(termios, old);
119 }
120
121 static void altera_jtaguart_rx_chars(struct altera_jtaguart *pp)
122 {
123         struct uart_port *port = &pp->port;
124         unsigned char ch, flag;
125         unsigned long status;
126
127         while ((status = readl(port->membase + ALTERA_JTAGUART_DATA_REG)) &
128                ALTERA_JTAGUART_DATA_RVALID_MSK) {
129                 ch = status & ALTERA_JTAGUART_DATA_DATA_MSK;
130                 flag = TTY_NORMAL;
131                 port->icount.rx++;
132
133                 if (uart_handle_sysrq_char(port, ch))
134                         continue;
135                 uart_insert_char(port, 0, 0, ch, flag);
136         }
137
138         spin_unlock(&port->lock);
139         tty_flip_buffer_push(&port->state->port);
140         spin_lock(&port->lock);
141 }
142
143 static void altera_jtaguart_tx_chars(struct altera_jtaguart *pp)
144 {
145         struct uart_port *port = &pp->port;
146         struct circ_buf *xmit = &port->state->xmit;
147         unsigned int pending, count;
148
149         if (port->x_char) {
150                 /* Send special char - probably flow control */
151                 writel(port->x_char, port->membase + ALTERA_JTAGUART_DATA_REG);
152                 port->x_char = 0;
153                 port->icount.tx++;
154                 return;
155         }
156
157         pending = uart_circ_chars_pending(xmit);
158         if (pending > 0) {
159                 count = (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
160                                 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) >>
161                         ALTERA_JTAGUART_CONTROL_WSPACE_OFF;
162                 if (count > pending)
163                         count = pending;
164                 if (count > 0) {
165                         pending -= count;
166                         while (count--) {
167                                 writel(xmit->buf[xmit->tail],
168                                        port->membase + ALTERA_JTAGUART_DATA_REG);
169                                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
170                                 port->icount.tx++;
171                         }
172                         if (pending < WAKEUP_CHARS)
173                                 uart_write_wakeup(port);
174                 }
175         }
176
177         if (pending == 0) {
178                 pp->imr &= ~ALTERA_JTAGUART_CONTROL_WE_MSK;
179                 writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
180         }
181 }
182
183 static irqreturn_t altera_jtaguart_interrupt(int irq, void *data)
184 {
185         struct uart_port *port = data;
186         struct altera_jtaguart *pp =
187             container_of(port, struct altera_jtaguart, port);
188         unsigned int isr;
189
190         isr = (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) >>
191                ALTERA_JTAGUART_CONTROL_RI_OFF) & pp->imr;
192
193         spin_lock(&port->lock);
194
195         if (isr & ALTERA_JTAGUART_CONTROL_RE_MSK)
196                 altera_jtaguart_rx_chars(pp);
197         if (isr & ALTERA_JTAGUART_CONTROL_WE_MSK)
198                 altera_jtaguart_tx_chars(pp);
199
200         spin_unlock(&port->lock);
201
202         return IRQ_RETVAL(isr);
203 }
204
205 static void altera_jtaguart_config_port(struct uart_port *port, int flags)
206 {
207         port->type = PORT_ALTERA_JTAGUART;
208
209         /* Clear mask, so no surprise interrupts. */
210         writel(0, port->membase + ALTERA_JTAGUART_CONTROL_REG);
211 }
212
213 static int altera_jtaguart_startup(struct uart_port *port)
214 {
215         struct altera_jtaguart *pp =
216             container_of(port, struct altera_jtaguart, port);
217         unsigned long flags;
218         int ret;
219
220         ret = request_irq(port->irq, altera_jtaguart_interrupt, 0,
221                         DRV_NAME, port);
222         if (ret) {
223                 pr_err(DRV_NAME ": unable to attach Altera JTAG UART %d "
224                        "interrupt vector=%d\n", port->line, port->irq);
225                 return ret;
226         }
227
228         spin_lock_irqsave(&port->lock, flags);
229
230         /* Enable RX interrupts now */
231         pp->imr = ALTERA_JTAGUART_CONTROL_RE_MSK;
232         writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
233
234         spin_unlock_irqrestore(&port->lock, flags);
235
236         return 0;
237 }
238
239 static void altera_jtaguart_shutdown(struct uart_port *port)
240 {
241         struct altera_jtaguart *pp =
242             container_of(port, struct altera_jtaguart, port);
243         unsigned long flags;
244
245         spin_lock_irqsave(&port->lock, flags);
246
247         /* Disable all interrupts now */
248         pp->imr = 0;
249         writel(pp->imr, port->membase + ALTERA_JTAGUART_CONTROL_REG);
250
251         spin_unlock_irqrestore(&port->lock, flags);
252
253         free_irq(port->irq, port);
254 }
255
256 static const char *altera_jtaguart_type(struct uart_port *port)
257 {
258         return (port->type == PORT_ALTERA_JTAGUART) ? "Altera JTAG UART" : NULL;
259 }
260
261 static int altera_jtaguart_request_port(struct uart_port *port)
262 {
263         /* UARTs always present */
264         return 0;
265 }
266
267 static void altera_jtaguart_release_port(struct uart_port *port)
268 {
269         /* Nothing to release... */
270 }
271
272 static int altera_jtaguart_verify_port(struct uart_port *port,
273                                        struct serial_struct *ser)
274 {
275         if (ser->type != PORT_UNKNOWN && ser->type != PORT_ALTERA_JTAGUART)
276                 return -EINVAL;
277         return 0;
278 }
279
280 /*
281  *      Define the basic serial functions we support.
282  */
283 static struct uart_ops altera_jtaguart_ops = {
284         .tx_empty       = altera_jtaguart_tx_empty,
285         .get_mctrl      = altera_jtaguart_get_mctrl,
286         .set_mctrl      = altera_jtaguart_set_mctrl,
287         .start_tx       = altera_jtaguart_start_tx,
288         .stop_tx        = altera_jtaguart_stop_tx,
289         .stop_rx        = altera_jtaguart_stop_rx,
290         .break_ctl      = altera_jtaguart_break_ctl,
291         .startup        = altera_jtaguart_startup,
292         .shutdown       = altera_jtaguart_shutdown,
293         .set_termios    = altera_jtaguart_set_termios,
294         .type           = altera_jtaguart_type,
295         .request_port   = altera_jtaguart_request_port,
296         .release_port   = altera_jtaguart_release_port,
297         .config_port    = altera_jtaguart_config_port,
298         .verify_port    = altera_jtaguart_verify_port,
299 };
300
301 #define ALTERA_JTAGUART_MAXPORTS 1
302 static struct altera_jtaguart altera_jtaguart_ports[ALTERA_JTAGUART_MAXPORTS];
303
304 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE)
305
306 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
307 static void altera_jtaguart_console_putc(struct console *co, const char c)
308 {
309         struct uart_port *port = &(altera_jtaguart_ports + co->index)->port;
310         unsigned long status;
311         unsigned long flags;
312
313         spin_lock_irqsave(&port->lock, flags);
314         while (((status = readl(port->membase + ALTERA_JTAGUART_CONTROL_REG)) &
315                 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
316                 if ((status & ALTERA_JTAGUART_CONTROL_AC_MSK) == 0) {
317                         spin_unlock_irqrestore(&port->lock, flags);
318                         return; /* no connection activity */
319                 }
320                 spin_unlock_irqrestore(&port->lock, flags);
321                 cpu_relax();
322                 spin_lock_irqsave(&port->lock, flags);
323         }
324         writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
325         spin_unlock_irqrestore(&port->lock, flags);
326 }
327 #else
328 static void altera_jtaguart_console_putc(struct console *co, const char c)
329 {
330         struct uart_port *port = &(altera_jtaguart_ports + co->index)->port;
331         unsigned long flags;
332
333         spin_lock_irqsave(&port->lock, flags);
334         while ((readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
335                 ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
336                 spin_unlock_irqrestore(&port->lock, flags);
337                 cpu_relax();
338                 spin_lock_irqsave(&port->lock, flags);
339         }
340         writel(c, port->membase + ALTERA_JTAGUART_DATA_REG);
341         spin_unlock_irqrestore(&port->lock, flags);
342 }
343 #endif
344
345 static void altera_jtaguart_console_write(struct console *co, const char *s,
346                                           unsigned int count)
347 {
348         for (; count; count--, s++) {
349                 altera_jtaguart_console_putc(co, *s);
350                 if (*s == '\n')
351                         altera_jtaguart_console_putc(co, '\r');
352         }
353 }
354
355 static int __init altera_jtaguart_console_setup(struct console *co,
356                                                 char *options)
357 {
358         struct uart_port *port;
359
360         if (co->index < 0 || co->index >= ALTERA_JTAGUART_MAXPORTS)
361                 return -EINVAL;
362         port = &altera_jtaguart_ports[co->index].port;
363         if (port->membase == NULL)
364                 return -ENODEV;
365         return 0;
366 }
367
368 static struct uart_driver altera_jtaguart_driver;
369
370 static struct console altera_jtaguart_console = {
371         .name   = "ttyJ",
372         .write  = altera_jtaguart_console_write,
373         .device = uart_console_device,
374         .setup  = altera_jtaguart_console_setup,
375         .flags  = CON_PRINTBUFFER,
376         .index  = -1,
377         .data   = &altera_jtaguart_driver,
378 };
379
380 static int __init altera_jtaguart_console_init(void)
381 {
382         register_console(&altera_jtaguart_console);
383         return 0;
384 }
385
386 console_initcall(altera_jtaguart_console_init);
387
388 #define ALTERA_JTAGUART_CONSOLE (&altera_jtaguart_console)
389
390 #else
391
392 #define ALTERA_JTAGUART_CONSOLE NULL
393
394 #endif /* CONFIG_ALTERA_JTAGUART_CONSOLE */
395
396 static struct uart_driver altera_jtaguart_driver = {
397         .owner          = THIS_MODULE,
398         .driver_name    = "altera_jtaguart",
399         .dev_name       = "ttyJ",
400         .major          = ALTERA_JTAGUART_MAJOR,
401         .minor          = ALTERA_JTAGUART_MINOR,
402         .nr             = ALTERA_JTAGUART_MAXPORTS,
403         .cons           = ALTERA_JTAGUART_CONSOLE,
404 };
405
406 static int altera_jtaguart_probe(struct platform_device *pdev)
407 {
408         struct altera_jtaguart_platform_uart *platp =
409                         dev_get_platdata(&pdev->dev);
410         struct uart_port *port;
411         struct resource *res_irq, *res_mem;
412         int i = pdev->id;
413
414         /* -1 emphasizes that the platform must have one port, no .N suffix */
415         if (i == -1)
416                 i = 0;
417
418         if (i >= ALTERA_JTAGUART_MAXPORTS)
419                 return -EINVAL;
420
421         port = &altera_jtaguart_ports[i].port;
422
423         res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
424         if (res_mem)
425                 port->mapbase = res_mem->start;
426         else if (platp)
427                 port->mapbase = platp->mapbase;
428         else
429                 return -ENODEV;
430
431         res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
432         if (res_irq)
433                 port->irq = res_irq->start;
434         else if (platp)
435                 port->irq = platp->irq;
436         else
437                 return -ENODEV;
438
439         port->membase = ioremap(port->mapbase, ALTERA_JTAGUART_SIZE);
440         if (!port->membase)
441                 return -ENOMEM;
442
443         port->line = i;
444         port->type = PORT_ALTERA_JTAGUART;
445         port->iotype = SERIAL_IO_MEM;
446         port->ops = &altera_jtaguart_ops;
447         port->flags = UPF_BOOT_AUTOCONF;
448
449         uart_add_one_port(&altera_jtaguart_driver, port);
450
451         return 0;
452 }
453
454 static int altera_jtaguart_remove(struct platform_device *pdev)
455 {
456         struct uart_port *port;
457         int i = pdev->id;
458
459         if (i == -1)
460                 i = 0;
461
462         port = &altera_jtaguart_ports[i].port;
463         uart_remove_one_port(&altera_jtaguart_driver, port);
464
465         return 0;
466 }
467
468 #ifdef CONFIG_OF
469 static struct of_device_id altera_jtaguart_match[] = {
470         { .compatible = "ALTR,juart-1.0", },
471         { .compatible = "altr,juart-1.0", },
472         {},
473 };
474 MODULE_DEVICE_TABLE(of, altera_jtaguart_match);
475 #endif /* CONFIG_OF */
476
477 static struct platform_driver altera_jtaguart_platform_driver = {
478         .probe  = altera_jtaguart_probe,
479         .remove = altera_jtaguart_remove,
480         .driver = {
481                 .name           = DRV_NAME,
482                 .owner          = THIS_MODULE,
483                 .of_match_table = of_match_ptr(altera_jtaguart_match),
484         },
485 };
486
487 static int __init altera_jtaguart_init(void)
488 {
489         int rc;
490
491         rc = uart_register_driver(&altera_jtaguart_driver);
492         if (rc)
493                 return rc;
494         rc = platform_driver_register(&altera_jtaguart_platform_driver);
495         if (rc)
496                 uart_unregister_driver(&altera_jtaguart_driver);
497         return rc;
498 }
499
500 static void __exit altera_jtaguart_exit(void)
501 {
502         platform_driver_unregister(&altera_jtaguart_platform_driver);
503         uart_unregister_driver(&altera_jtaguart_driver);
504 }
505
506 module_init(altera_jtaguart_init);
507 module_exit(altera_jtaguart_exit);
508
509 MODULE_DESCRIPTION("Altera JTAG UART driver");
510 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
511 MODULE_LICENSE("GPL");
512 MODULE_ALIAS("platform:" DRV_NAME);