]>
Commit | Line | Data |
---|---|---|
5bcd6010 TK |
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 | int __init early_altera_jtaguart_setup(struct altera_jtaguart_platform_uart | |
309 | *platp) | |
310 | { | |
311 | struct uart_port *port; | |
312 | int i; | |
313 | ||
314 | for (i = 0; i < ALTERA_JTAGUART_MAXPORTS && platp[i].mapbase; i++) { | |
315 | port = &altera_jtaguart_ports[i].port; | |
316 | ||
317 | port->line = i; | |
318 | port->type = PORT_ALTERA_JTAGUART; | |
319 | port->mapbase = platp[i].mapbase; | |
320 | port->membase = ioremap(port->mapbase, ALTERA_JTAGUART_SIZE); | |
321 | port->iotype = SERIAL_IO_MEM; | |
322 | port->irq = platp[i].irq; | |
323 | port->flags = ASYNC_BOOT_AUTOCONF; | |
324 | port->ops = &altera_jtaguart_ops; | |
325 | } | |
326 | ||
327 | return 0; | |
328 | } | |
329 | ||
330 | #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS) | |
331 | static void altera_jtaguart_console_putc(struct console *co, const char c) | |
332 | { | |
333 | struct uart_port *port = &(altera_jtaguart_ports + co->index)->port; | |
334 | unsigned long status; | |
335 | unsigned long flags; | |
336 | ||
337 | spin_lock_irqsave(&port->lock, flags); | |
338 | while (((status = readl(port->membase + ALTERA_JTAGUART_CONTROL_REG)) & | |
339 | ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) { | |
340 | if ((status & ALTERA_JTAGUART_CONTROL_AC_MSK) == 0) { | |
341 | spin_unlock_irqrestore(&port->lock, flags); | |
342 | return; /* no connection activity */ | |
343 | } | |
344 | spin_unlock_irqrestore(&port->lock, flags); | |
345 | cpu_relax(); | |
346 | spin_lock_irqsave(&port->lock, flags); | |
347 | } | |
348 | writel(c, port->membase + ALTERA_JTAGUART_DATA_REG); | |
349 | spin_unlock_irqrestore(&port->lock, flags); | |
350 | } | |
351 | #else | |
352 | static void altera_jtaguart_console_putc(struct console *co, const char c) | |
353 | { | |
354 | struct uart_port *port = &(altera_jtaguart_ports + co->index)->port; | |
355 | unsigned long flags; | |
356 | ||
357 | spin_lock_irqsave(&port->lock, flags); | |
358 | while ((readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) & | |
359 | ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) { | |
360 | spin_unlock_irqrestore(&port->lock, flags); | |
361 | cpu_relax(); | |
362 | spin_lock_irqsave(&port->lock, flags); | |
363 | } | |
364 | writel(c, port->membase + ALTERA_JTAGUART_DATA_REG); | |
365 | spin_unlock_irqrestore(&port->lock, flags); | |
366 | } | |
367 | #endif | |
368 | ||
369 | static void altera_jtaguart_console_write(struct console *co, const char *s, | |
370 | unsigned int count) | |
371 | { | |
372 | for (; count; count--, s++) { | |
373 | altera_jtaguart_console_putc(co, *s); | |
374 | if (*s == '\n') | |
375 | altera_jtaguart_console_putc(co, '\r'); | |
376 | } | |
377 | } | |
378 | ||
379 | static int __init altera_jtaguart_console_setup(struct console *co, | |
380 | char *options) | |
381 | { | |
382 | struct uart_port *port; | |
383 | ||
384 | if (co->index < 0 || co->index >= ALTERA_JTAGUART_MAXPORTS) | |
385 | return -EINVAL; | |
386 | port = &altera_jtaguart_ports[co->index].port; | |
387 | if (port->membase == 0) | |
388 | return -ENODEV; | |
389 | return 0; | |
390 | } | |
391 | ||
392 | static struct uart_driver altera_jtaguart_driver; | |
393 | ||
394 | static struct console altera_jtaguart_console = { | |
395 | .name = "ttyJ", | |
396 | .write = altera_jtaguart_console_write, | |
397 | .device = uart_console_device, | |
398 | .setup = altera_jtaguart_console_setup, | |
399 | .flags = CON_PRINTBUFFER, | |
400 | .index = -1, | |
401 | .data = &altera_jtaguart_driver, | |
402 | }; | |
403 | ||
404 | static int __init altera_jtaguart_console_init(void) | |
405 | { | |
406 | register_console(&altera_jtaguart_console); | |
407 | return 0; | |
408 | } | |
409 | ||
410 | console_initcall(altera_jtaguart_console_init); | |
411 | ||
412 | #define ALTERA_JTAGUART_CONSOLE (&altera_jtaguart_console) | |
413 | ||
414 | #else | |
415 | ||
416 | #define ALTERA_JTAGUART_CONSOLE NULL | |
417 | ||
418 | #endif /* CONFIG_ALTERA_JTAGUART_CONSOLE */ | |
419 | ||
420 | static struct uart_driver altera_jtaguart_driver = { | |
421 | .owner = THIS_MODULE, | |
422 | .driver_name = "altera_jtaguart", | |
423 | .dev_name = "ttyJ", | |
424 | .major = ALTERA_JTAGUART_MAJOR, | |
425 | .minor = ALTERA_JTAGUART_MINOR, | |
426 | .nr = ALTERA_JTAGUART_MAXPORTS, | |
427 | .cons = ALTERA_JTAGUART_CONSOLE, | |
428 | }; | |
429 | ||
430 | static int __devinit altera_jtaguart_probe(struct platform_device *pdev) | |
431 | { | |
432 | struct altera_jtaguart_platform_uart *platp = pdev->dev.platform_data; | |
433 | struct uart_port *port; | |
434 | int i; | |
435 | ||
436 | for (i = 0; i < ALTERA_JTAGUART_MAXPORTS && platp[i].mapbase; i++) { | |
437 | port = &altera_jtaguart_ports[i].port; | |
438 | ||
439 | port->line = i; | |
440 | port->type = PORT_ALTERA_JTAGUART; | |
441 | port->mapbase = platp[i].mapbase; | |
442 | port->membase = ioremap(port->mapbase, ALTERA_JTAGUART_SIZE); | |
443 | port->iotype = SERIAL_IO_MEM; | |
444 | port->irq = platp[i].irq; | |
445 | port->ops = &altera_jtaguart_ops; | |
446 | port->flags = ASYNC_BOOT_AUTOCONF; | |
447 | ||
448 | uart_add_one_port(&altera_jtaguart_driver, port); | |
449 | } | |
450 | ||
451 | return 0; | |
452 | } | |
453 | ||
454 | static int __devexit altera_jtaguart_remove(struct platform_device *pdev) | |
455 | { | |
456 | struct uart_port *port; | |
457 | int i; | |
458 | ||
459 | for (i = 0; i < ALTERA_JTAGUART_MAXPORTS; i++) { | |
460 | port = &altera_jtaguart_ports[i].port; | |
461 | if (port) | |
462 | uart_remove_one_port(&altera_jtaguart_driver, port); | |
463 | } | |
464 | ||
465 | return 0; | |
466 | } | |
467 | ||
468 | static struct platform_driver altera_jtaguart_platform_driver = { | |
469 | .probe = altera_jtaguart_probe, | |
470 | .remove = __devexit_p(altera_jtaguart_remove), | |
471 | .driver = { | |
472 | .name = DRV_NAME, | |
473 | .owner = THIS_MODULE, | |
474 | }, | |
475 | }; | |
476 | ||
477 | static int __init altera_jtaguart_init(void) | |
478 | { | |
479 | int rc; | |
480 | ||
481 | rc = uart_register_driver(&altera_jtaguart_driver); | |
482 | if (rc) | |
483 | return rc; | |
484 | rc = platform_driver_register(&altera_jtaguart_platform_driver); | |
485 | if (rc) { | |
486 | uart_unregister_driver(&altera_jtaguart_driver); | |
487 | return rc; | |
488 | } | |
489 | return 0; | |
490 | } | |
491 | ||
492 | static void __exit altera_jtaguart_exit(void) | |
493 | { | |
494 | platform_driver_unregister(&altera_jtaguart_platform_driver); | |
495 | uart_unregister_driver(&altera_jtaguart_driver); | |
496 | } | |
497 | ||
498 | module_init(altera_jtaguart_init); | |
499 | module_exit(altera_jtaguart_exit); | |
500 | ||
501 | MODULE_DESCRIPTION("Altera JTAG UART driver"); | |
502 | MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>"); | |
503 | MODULE_LICENSE("GPL"); | |
504 | MODULE_ALIAS("platform:" DRV_NAME); |