blob: 10d64a3697fba78827f23ddb83a0b5de933c7868 [file] [log] [blame]
Benjamin Krill5886188d2009-01-07 10:32:38 +01001/*
2 * Serial Port driver for a NWP uart device
3 *
4 * Copyright (C) 2008 IBM Corp., Benjamin Krill <ben@codiert.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <linux/init.h>
Paul Gortmaker0e648f42011-05-27 10:46:24 -040013#include <linux/export.h>
Benjamin Krill5886188d2009-01-07 10:32:38 +010014#include <linux/console.h>
15#include <linux/serial.h>
16#include <linux/serial_reg.h>
17#include <linux/serial_core.h>
18#include <linux/tty.h>
Jiri Slabyee160a32011-09-01 16:20:57 +020019#include <linux/tty_flip.h>
Benjamin Krill5886188d2009-01-07 10:32:38 +010020#include <linux/irqreturn.h>
21#include <linux/mutex.h>
22#include <linux/of_platform.h>
23#include <linux/of_device.h>
24#include <linux/nwpserial.h>
25#include <asm/prom.h>
26#include <asm/dcr.h>
27
28#define NWPSERIAL_NR 2
29
30#define NWPSERIAL_STATUS_RXVALID 0x1
31#define NWPSERIAL_STATUS_TXFULL 0x2
32
33struct nwpserial_port {
34 struct uart_port port;
35 dcr_host_t dcr_host;
36 unsigned int ier;
37 unsigned int mcr;
38};
39
40static DEFINE_MUTEX(nwpserial_mutex);
41static struct nwpserial_port nwpserial_ports[NWPSERIAL_NR];
42
43static void wait_for_bits(struct nwpserial_port *up, int bits)
44{
45 unsigned int status, tmout = 10000;
46
47 /* Wait up to 10ms for the character(s) to be sent. */
48 do {
49 status = dcr_read(up->dcr_host, UART_LSR);
50
51 if (--tmout == 0)
52 break;
53 udelay(1);
54 } while ((status & bits) != bits);
55}
56
57#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE
58static void nwpserial_console_putchar(struct uart_port *port, int c)
59{
60 struct nwpserial_port *up;
61 up = container_of(port, struct nwpserial_port, port);
62 /* check if tx buffer is full */
63 wait_for_bits(up, UART_LSR_THRE);
64 dcr_write(up->dcr_host, UART_TX, c);
65 up->port.icount.tx++;
66}
67
68static void
69nwpserial_console_write(struct console *co, const char *s, unsigned int count)
70{
71 struct nwpserial_port *up = &nwpserial_ports[co->index];
72 unsigned long flags;
73 int locked = 1;
74
75 if (oops_in_progress)
76 locked = spin_trylock_irqsave(&up->port.lock, flags);
77 else
78 spin_lock_irqsave(&up->port.lock, flags);
79
80 /* save and disable interrupt */
81 up->ier = dcr_read(up->dcr_host, UART_IER);
82 dcr_write(up->dcr_host, UART_IER, up->ier & ~UART_IER_RDI);
83
84 uart_console_write(&up->port, s, count, nwpserial_console_putchar);
85
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +020086 /* wait for transmitter to become empty */
Benjamin Krill5886188d2009-01-07 10:32:38 +010087 while ((dcr_read(up->dcr_host, UART_LSR) & UART_LSR_THRE) == 0)
88 cpu_relax();
89
90 /* restore interrupt state */
91 dcr_write(up->dcr_host, UART_IER, up->ier);
92
93 if (locked)
94 spin_unlock_irqrestore(&up->port.lock, flags);
95}
96
97static struct uart_driver nwpserial_reg;
98static struct console nwpserial_console = {
99 .name = "ttySQ",
100 .write = nwpserial_console_write,
101 .device = uart_console_device,
102 .flags = CON_PRINTBUFFER,
103 .index = -1,
104 .data = &nwpserial_reg,
105};
106#define NWPSERIAL_CONSOLE (&nwpserial_console)
107#else
108#define NWPSERIAL_CONSOLE NULL
109#endif /* CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE */
110
111/**************************************************************************/
112
113static int nwpserial_request_port(struct uart_port *port)
114{
115 return 0;
116}
117
118static void nwpserial_release_port(struct uart_port *port)
119{
120 /* N/A */
121}
122
123static void nwpserial_config_port(struct uart_port *port, int flags)
124{
125 port->type = PORT_NWPSERIAL;
126}
127
128static irqreturn_t nwpserial_interrupt(int irq, void *dev_id)
129{
130 struct nwpserial_port *up = dev_id;
Jiri Slaby92a19f92013-01-03 15:53:03 +0100131 struct tty_port *port = &up->port.state->port;
132 struct tty_struct *tty = port->tty;
Benjamin Krill5886188d2009-01-07 10:32:38 +0100133 irqreturn_t ret;
134 unsigned int iir;
135 unsigned char ch;
136
137 spin_lock(&up->port.lock);
138
139 /* check if the uart was the interrupt source. */
140 iir = dcr_read(up->dcr_host, UART_IIR);
141 if (!iir) {
142 ret = IRQ_NONE;
143 goto out;
144 }
145
146 do {
147 up->port.icount.rx++;
148 ch = dcr_read(up->dcr_host, UART_RX);
149 if (up->port.ignore_status_mask != NWPSERIAL_STATUS_RXVALID)
Jiri Slaby92a19f92013-01-03 15:53:03 +0100150 tty_insert_flip_char(port, ch, TTY_NORMAL);
Benjamin Krill951c4df2009-05-13 05:56:54 +0000151 } while (dcr_read(up->dcr_host, UART_LSR) & UART_LSR_DR);
Benjamin Krill5886188d2009-01-07 10:32:38 +0100152
153 tty_flip_buffer_push(tty);
154 ret = IRQ_HANDLED;
155
Benjamin Krill951c4df2009-05-13 05:56:54 +0000156 /* clear interrupt */
157 dcr_write(up->dcr_host, UART_IIR, 1);
Benjamin Krill5886188d2009-01-07 10:32:38 +0100158out:
159 spin_unlock(&up->port.lock);
160 return ret;
161}
162
163static int nwpserial_startup(struct uart_port *port)
164{
165 struct nwpserial_port *up;
166 int err;
167
168 up = container_of(port, struct nwpserial_port, port);
169
170 /* disable flow control by default */
171 up->mcr = dcr_read(up->dcr_host, UART_MCR) & ~UART_MCR_AFE;
172 dcr_write(up->dcr_host, UART_MCR, up->mcr);
173
174 /* register interrupt handler */
175 err = request_irq(up->port.irq, nwpserial_interrupt,
176 IRQF_SHARED, "nwpserial", up);
177 if (err)
178 return err;
179
180 /* enable interrupts */
181 up->ier = UART_IER_RDI;
182 dcr_write(up->dcr_host, UART_IER, up->ier);
183
184 /* enable receiving */
185 up->port.ignore_status_mask &= ~NWPSERIAL_STATUS_RXVALID;
186
187 return 0;
188}
189
190static void nwpserial_shutdown(struct uart_port *port)
191{
192 struct nwpserial_port *up;
193 up = container_of(port, struct nwpserial_port, port);
194
195 /* disable receiving */
196 up->port.ignore_status_mask |= NWPSERIAL_STATUS_RXVALID;
197
198 /* disable interrupts from this port */
199 up->ier = 0;
200 dcr_write(up->dcr_host, UART_IER, up->ier);
201
202 /* free irq */
203 free_irq(up->port.irq, port);
204}
205
206static int nwpserial_verify_port(struct uart_port *port,
207 struct serial_struct *ser)
208{
209 return -EINVAL;
210}
211
212static const char *nwpserial_type(struct uart_port *port)
213{
214 return port->type == PORT_NWPSERIAL ? "nwpserial" : NULL;
215}
216
217static void nwpserial_set_termios(struct uart_port *port,
218 struct ktermios *termios, struct ktermios *old)
219{
220 struct nwpserial_port *up;
221 up = container_of(port, struct nwpserial_port, port);
222
223 up->port.read_status_mask = NWPSERIAL_STATUS_RXVALID
224 | NWPSERIAL_STATUS_TXFULL;
225
226 up->port.ignore_status_mask = 0;
227 /* ignore all characters if CREAD is not set */
228 if ((termios->c_cflag & CREAD) == 0)
229 up->port.ignore_status_mask |= NWPSERIAL_STATUS_RXVALID;
230
231 /* Copy back the old hardware settings */
232 if (old)
233 tty_termios_copy_hw(termios, old);
234}
235
236static void nwpserial_break_ctl(struct uart_port *port, int ctl)
237{
238 /* N/A */
239}
240
241static void nwpserial_enable_ms(struct uart_port *port)
242{
243 /* N/A */
244}
245
246static void nwpserial_stop_rx(struct uart_port *port)
247{
248 struct nwpserial_port *up;
249 up = container_of(port, struct nwpserial_port, port);
250 /* don't forward any more data (like !CREAD) */
251 up->port.ignore_status_mask = NWPSERIAL_STATUS_RXVALID;
252}
253
254static void nwpserial_putchar(struct nwpserial_port *up, unsigned char c)
255{
256 /* check if tx buffer is full */
257 wait_for_bits(up, UART_LSR_THRE);
258 dcr_write(up->dcr_host, UART_TX, c);
259 up->port.icount.tx++;
260}
261
262static void nwpserial_start_tx(struct uart_port *port)
263{
264 struct nwpserial_port *up;
265 struct circ_buf *xmit;
266 up = container_of(port, struct nwpserial_port, port);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700267 xmit = &up->port.state->xmit;
Benjamin Krill5886188d2009-01-07 10:32:38 +0100268
269 if (port->x_char) {
270 nwpserial_putchar(up, up->port.x_char);
271 port->x_char = 0;
272 }
273
274 while (!(uart_circ_empty(xmit) || uart_tx_stopped(&up->port))) {
275 nwpserial_putchar(up, xmit->buf[xmit->tail]);
276 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
277 }
278}
279
280static unsigned int nwpserial_get_mctrl(struct uart_port *port)
281{
282 return 0;
283}
284
285static void nwpserial_set_mctrl(struct uart_port *port, unsigned int mctrl)
286{
287 /* N/A */
288}
289
290static void nwpserial_stop_tx(struct uart_port *port)
291{
292 /* N/A */
293}
294
295static unsigned int nwpserial_tx_empty(struct uart_port *port)
296{
297 struct nwpserial_port *up;
298 unsigned long flags;
299 int ret;
300 up = container_of(port, struct nwpserial_port, port);
301
302 spin_lock_irqsave(&up->port.lock, flags);
303 ret = dcr_read(up->dcr_host, UART_LSR);
304 spin_unlock_irqrestore(&up->port.lock, flags);
305
306 return ret & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
307}
308
309static struct uart_ops nwpserial_pops = {
310 .tx_empty = nwpserial_tx_empty,
311 .set_mctrl = nwpserial_set_mctrl,
312 .get_mctrl = nwpserial_get_mctrl,
313 .stop_tx = nwpserial_stop_tx,
314 .start_tx = nwpserial_start_tx,
315 .stop_rx = nwpserial_stop_rx,
316 .enable_ms = nwpserial_enable_ms,
317 .break_ctl = nwpserial_break_ctl,
318 .startup = nwpserial_startup,
319 .shutdown = nwpserial_shutdown,
320 .set_termios = nwpserial_set_termios,
321 .type = nwpserial_type,
322 .release_port = nwpserial_release_port,
323 .request_port = nwpserial_request_port,
324 .config_port = nwpserial_config_port,
325 .verify_port = nwpserial_verify_port,
326};
327
328static struct uart_driver nwpserial_reg = {
329 .owner = THIS_MODULE,
330 .driver_name = "nwpserial",
331 .dev_name = "ttySQ",
332 .major = TTY_MAJOR,
333 .minor = 68,
334 .nr = NWPSERIAL_NR,
335 .cons = NWPSERIAL_CONSOLE,
336};
337
338int nwpserial_register_port(struct uart_port *port)
339{
340 struct nwpserial_port *up = NULL;
341 int ret = -1;
342 int i;
343 static int first = 1;
344 int dcr_len;
345 int dcr_base;
346 struct device_node *dn;
347
348 mutex_lock(&nwpserial_mutex);
349
Grant Likely2dc11582010-08-06 09:25:50 -0600350 dn = port->dev->of_node;
Benjamin Krill5886188d2009-01-07 10:32:38 +0100351 if (dn == NULL)
352 goto out;
353
354 /* get dcr base. */
355 dcr_base = dcr_resource_start(dn, 0);
356
357 /* find matching entry */
358 for (i = 0; i < NWPSERIAL_NR; i++)
359 if (nwpserial_ports[i].port.iobase == dcr_base) {
360 up = &nwpserial_ports[i];
361 break;
362 }
363
364 /* we didn't find a mtching entry, search for a free port */
365 if (up == NULL)
366 for (i = 0; i < NWPSERIAL_NR; i++)
367 if (nwpserial_ports[i].port.type == PORT_UNKNOWN &&
368 nwpserial_ports[i].port.iobase == 0) {
369 up = &nwpserial_ports[i];
370 break;
371 }
372
373 if (up == NULL) {
374 ret = -EBUSY;
375 goto out;
376 }
377
378 if (first)
379 uart_register_driver(&nwpserial_reg);
380 first = 0;
381
382 up->port.membase = port->membase;
383 up->port.irq = port->irq;
384 up->port.uartclk = port->uartclk;
385 up->port.fifosize = port->fifosize;
386 up->port.regshift = port->regshift;
387 up->port.iotype = port->iotype;
388 up->port.flags = port->flags;
389 up->port.mapbase = port->mapbase;
390 up->port.private_data = port->private_data;
391
392 if (port->dev)
393 up->port.dev = port->dev;
394
395 if (up->port.iobase != dcr_base) {
396 up->port.ops = &nwpserial_pops;
397 up->port.fifosize = 16;
398
399 spin_lock_init(&up->port.lock);
400
401 up->port.iobase = dcr_base;
402 dcr_len = dcr_resource_len(dn, 0);
403
404 up->dcr_host = dcr_map(dn, dcr_base, dcr_len);
405 if (!DCR_MAP_OK(up->dcr_host)) {
406 printk(KERN_ERR "Cannot map DCR resources for NWPSERIAL");
407 goto out;
408 }
409 }
410
411 ret = uart_add_one_port(&nwpserial_reg, &up->port);
412 if (ret == 0)
413 ret = up->port.line;
414
415out:
416 mutex_unlock(&nwpserial_mutex);
417
418 return ret;
419}
420EXPORT_SYMBOL(nwpserial_register_port);
421
422void nwpserial_unregister_port(int line)
423{
424 struct nwpserial_port *up = &nwpserial_ports[line];
425 mutex_lock(&nwpserial_mutex);
426 uart_remove_one_port(&nwpserial_reg, &up->port);
427
428 up->port.type = PORT_UNKNOWN;
429
430 mutex_unlock(&nwpserial_mutex);
431}
432EXPORT_SYMBOL(nwpserial_unregister_port);
433
434#ifdef CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE
435static int __init nwpserial_console_init(void)
436{
437 struct nwpserial_port *up = NULL;
438 struct device_node *dn;
439 const char *name;
440 int dcr_base;
441 int dcr_len;
442 int i;
443
444 /* search for a free port */
445 for (i = 0; i < NWPSERIAL_NR; i++)
446 if (nwpserial_ports[i].port.type == PORT_UNKNOWN) {
447 up = &nwpserial_ports[i];
448 break;
449 }
450
451 if (up == NULL)
452 return -1;
453
454 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
455 if (name == NULL)
456 return -1;
457
458 dn = of_find_node_by_path(name);
459 if (!dn)
460 return -1;
461
462 spin_lock_init(&up->port.lock);
463 up->port.ops = &nwpserial_pops;
464 up->port.type = PORT_NWPSERIAL;
465 up->port.fifosize = 16;
466
467 dcr_base = dcr_resource_start(dn, 0);
468 dcr_len = dcr_resource_len(dn, 0);
469 up->port.iobase = dcr_base;
470
471 up->dcr_host = dcr_map(dn, dcr_base, dcr_len);
472 if (!DCR_MAP_OK(up->dcr_host)) {
473 printk("Cannot map DCR resources for SERIAL");
474 return -1;
475 }
476 register_console(&nwpserial_console);
477 return 0;
478}
479console_initcall(nwpserial_console_init);
480#endif /* CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL_CONSOLE */