Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us