blob: 8681f13450562a75e28ab802f070604b72fee72f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/drivers/serial/21285.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Driver for the serial port on the 21285 StrongArm-110 core logic chip.
5 *
6 * Based on drivers/char/serial.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/tty.h>
10#include <linux/ioport.h>
11#include <linux/init.h>
12#include <linux/console.h>
13#include <linux/device.h>
14#include <linux/tty_flip.h>
15#include <linux/serial_core.h>
16#include <linux/serial.h>
Russell King99730222009-03-25 10:21:35 +000017#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/irq.h>
20#include <asm/mach-types.h>
21#include <asm/hardware/dec21285.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010022#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define BAUD_BASE (mem_fclk_21285/64)
25
26#define SERIAL_21285_NAME "ttyFB"
27#define SERIAL_21285_MAJOR 204
28#define SERIAL_21285_MINOR 4
29
30#define RXSTAT_DUMMY_READ 0x80000000
31#define RXSTAT_FRAME (1 << 0)
32#define RXSTAT_PARITY (1 << 1)
33#define RXSTAT_OVERRUN (1 << 2)
34#define RXSTAT_ANYERR (RXSTAT_FRAME|RXSTAT_PARITY|RXSTAT_OVERRUN)
35
36#define H_UBRLCR_BREAK (1 << 0)
37#define H_UBRLCR_PARENB (1 << 1)
38#define H_UBRLCR_PAREVN (1 << 2)
39#define H_UBRLCR_STOPB (1 << 3)
40#define H_UBRLCR_FIFO (1 << 4)
41
42static const char serial21285_name[] = "Footbridge UART";
43
44#define tx_enabled(port) ((port)->unused[0])
45#define rx_enabled(port) ((port)->unused[1])
46
47/*
48 * The documented expression for selecting the divisor is:
49 * BAUD_BASE / baud - 1
50 * However, typically BAUD_BASE is not divisible by baud, so
51 * we want to select the divisor that gives us the minimum
52 * error. Therefore, we want:
53 * int(BAUD_BASE / baud - 0.5) ->
54 * int(BAUD_BASE / baud - (baud >> 1) / baud) ->
55 * int((BAUD_BASE - (baud >> 1)) / baud)
56 */
57
Russell Kingb129a8c2005-08-31 10:12:14 +010058static void serial21285_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 if (tx_enabled(port)) {
Simon Kagstromcc20b902010-01-11 15:57:24 +010061 disable_irq_nosync(IRQ_CONTX);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 tx_enabled(port) = 0;
63 }
64}
65
Russell Kingb129a8c2005-08-31 10:12:14 +010066static void serial21285_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 if (!tx_enabled(port)) {
69 enable_irq(IRQ_CONTX);
70 tx_enabled(port) = 1;
71 }
72}
73
74static void serial21285_stop_rx(struct uart_port *port)
75{
76 if (rx_enabled(port)) {
Simon Kagstromcc20b902010-01-11 15:57:24 +010077 disable_irq_nosync(IRQ_CONRX);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 rx_enabled(port) = 0;
79 }
80}
81
82static void serial21285_enable_ms(struct uart_port *port)
83{
84}
85
David Howells7d12e782006-10-05 14:55:46 +010086static irqreturn_t serial21285_rx_chars(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 struct uart_port *port = dev_id;
Alan Coxebd2c8f2009-09-19 13:13:28 -070089 struct tty_struct *tty = port->state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned int status, ch, flag, rxs, max_count = 256;
91
92 status = *CSR_UARTFLG;
93 while (!(status & 0x10) && max_count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 ch = *CSR_UARTDR;
95 flag = TTY_NORMAL;
96 port->icount.rx++;
97
98 rxs = *CSR_RXSTAT | RXSTAT_DUMMY_READ;
Russell King45849282005-04-26 15:29:44 +010099 if (unlikely(rxs & RXSTAT_ANYERR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (rxs & RXSTAT_PARITY)
101 port->icount.parity++;
102 else if (rxs & RXSTAT_FRAME)
103 port->icount.frame++;
104 if (rxs & RXSTAT_OVERRUN)
105 port->icount.overrun++;
106
107 rxs &= port->read_status_mask;
108
109 if (rxs & RXSTAT_PARITY)
110 flag = TTY_PARITY;
111 else if (rxs & RXSTAT_FRAME)
112 flag = TTY_FRAME;
113 }
114
Russell King05ab3012005-05-09 23:21:59 +0100115 uart_insert_char(port, rxs, RXSTAT_OVERRUN, ch, flag);
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 status = *CSR_UARTFLG;
118 }
119 tty_flip_buffer_push(tty);
120
121 return IRQ_HANDLED;
122}
123
David Howells7d12e782006-10-05 14:55:46 +0100124static irqreturn_t serial21285_tx_chars(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 struct uart_port *port = dev_id;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700127 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int count = 256;
129
130 if (port->x_char) {
131 *CSR_UARTDR = port->x_char;
132 port->icount.tx++;
133 port->x_char = 0;
134 goto out;
135 }
136 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100137 serial21285_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto out;
139 }
140
141 do {
142 *CSR_UARTDR = xmit->buf[xmit->tail];
143 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
144 port->icount.tx++;
145 if (uart_circ_empty(xmit))
146 break;
147 } while (--count > 0 && !(*CSR_UARTFLG & 0x20));
148
149 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
150 uart_write_wakeup(port);
151
152 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100153 serial21285_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 out:
156 return IRQ_HANDLED;
157}
158
159static unsigned int serial21285_tx_empty(struct uart_port *port)
160{
161 return (*CSR_UARTFLG & 8) ? 0 : TIOCSER_TEMT;
162}
163
164/* no modem control lines */
165static unsigned int serial21285_get_mctrl(struct uart_port *port)
166{
167 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
168}
169
170static void serial21285_set_mctrl(struct uart_port *port, unsigned int mctrl)
171{
172}
173
174static void serial21285_break_ctl(struct uart_port *port, int break_state)
175{
176 unsigned long flags;
177 unsigned int h_lcr;
178
179 spin_lock_irqsave(&port->lock, flags);
180 h_lcr = *CSR_H_UBRLCR;
181 if (break_state)
182 h_lcr |= H_UBRLCR_BREAK;
183 else
184 h_lcr &= ~H_UBRLCR_BREAK;
185 *CSR_H_UBRLCR = h_lcr;
186 spin_unlock_irqrestore(&port->lock, flags);
187}
188
189static int serial21285_startup(struct uart_port *port)
190{
191 int ret;
192
193 tx_enabled(port) = 1;
194 rx_enabled(port) = 1;
195
196 ret = request_irq(IRQ_CONRX, serial21285_rx_chars, 0,
197 serial21285_name, port);
198 if (ret == 0) {
199 ret = request_irq(IRQ_CONTX, serial21285_tx_chars, 0,
200 serial21285_name, port);
201 if (ret)
202 free_irq(IRQ_CONRX, port);
203 }
204
205 return ret;
206}
207
208static void serial21285_shutdown(struct uart_port *port)
209{
210 free_irq(IRQ_CONTX, port);
211 free_irq(IRQ_CONRX, port);
212}
213
214static void
Alan Cox606d0992006-12-08 02:38:45 -0800215serial21285_set_termios(struct uart_port *port, struct ktermios *termios,
216 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 unsigned long flags;
219 unsigned int baud, quot, h_lcr;
220
221 /*
222 * We don't support modem control lines.
223 */
224 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
225 termios->c_cflag |= CLOCAL;
226
227 /*
228 * We don't support BREAK character recognition.
229 */
230 termios->c_iflag &= ~(IGNBRK | BRKINT);
231
232 /*
233 * Ask the core to calculate the divisor for us.
234 */
235 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
236 quot = uart_get_divisor(port, baud);
237
Alan Coxebd2c8f2009-09-19 13:13:28 -0700238 if (port->state && port->state->port.tty) {
239 struct tty_struct *tty = port->state->port.tty;
Russell King2f65baf2008-01-31 21:30:24 +0000240 unsigned int b = port->uartclk / (16 * quot);
241 tty_encode_baud_rate(tty, b, b);
242 }
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 switch (termios->c_cflag & CSIZE) {
245 case CS5:
246 h_lcr = 0x00;
247 break;
248 case CS6:
249 h_lcr = 0x20;
250 break;
251 case CS7:
252 h_lcr = 0x40;
253 break;
254 default: /* CS8 */
255 h_lcr = 0x60;
256 break;
257 }
258
259 if (termios->c_cflag & CSTOPB)
260 h_lcr |= H_UBRLCR_STOPB;
261 if (termios->c_cflag & PARENB) {
262 h_lcr |= H_UBRLCR_PARENB;
263 if (!(termios->c_cflag & PARODD))
264 h_lcr |= H_UBRLCR_PAREVN;
265 }
266
267 if (port->fifosize)
268 h_lcr |= H_UBRLCR_FIFO;
269
270 spin_lock_irqsave(&port->lock, flags);
271
272 /*
273 * Update the per-port timeout.
274 */
275 uart_update_timeout(port, termios->c_cflag, baud);
276
277 /*
278 * Which character status flags are we interested in?
279 */
280 port->read_status_mask = RXSTAT_OVERRUN;
281 if (termios->c_iflag & INPCK)
282 port->read_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
283
284 /*
285 * Which character status flags should we ignore?
286 */
287 port->ignore_status_mask = 0;
288 if (termios->c_iflag & IGNPAR)
289 port->ignore_status_mask |= RXSTAT_FRAME | RXSTAT_PARITY;
290 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
291 port->ignore_status_mask |= RXSTAT_OVERRUN;
292
293 /*
294 * Ignore all characters if CREAD is not set.
295 */
296 if ((termios->c_cflag & CREAD) == 0)
297 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
298
299 quot -= 1;
300
301 *CSR_UARTCON = 0;
302 *CSR_L_UBRLCR = quot & 0xff;
303 *CSR_M_UBRLCR = (quot >> 8) & 0x0f;
304 *CSR_H_UBRLCR = h_lcr;
305 *CSR_UARTCON = 1;
306
307 spin_unlock_irqrestore(&port->lock, flags);
308}
309
310static const char *serial21285_type(struct uart_port *port)
311{
312 return port->type == PORT_21285 ? "DC21285" : NULL;
313}
314
315static void serial21285_release_port(struct uart_port *port)
316{
317 release_mem_region(port->mapbase, 32);
318}
319
320static int serial21285_request_port(struct uart_port *port)
321{
322 return request_mem_region(port->mapbase, 32, serial21285_name)
323 != NULL ? 0 : -EBUSY;
324}
325
326static void serial21285_config_port(struct uart_port *port, int flags)
327{
328 if (flags & UART_CONFIG_TYPE && serial21285_request_port(port) == 0)
329 port->type = PORT_21285;
330}
331
332/*
333 * verify the new serial_struct (for TIOCSSERIAL).
334 */
335static int serial21285_verify_port(struct uart_port *port, struct serial_struct *ser)
336{
337 int ret = 0;
338 if (ser->type != PORT_UNKNOWN && ser->type != PORT_21285)
339 ret = -EINVAL;
340 if (ser->irq != NO_IRQ)
341 ret = -EINVAL;
342 if (ser->baud_base != port->uartclk / 16)
343 ret = -EINVAL;
344 return ret;
345}
346
347static struct uart_ops serial21285_ops = {
348 .tx_empty = serial21285_tx_empty,
349 .get_mctrl = serial21285_get_mctrl,
350 .set_mctrl = serial21285_set_mctrl,
351 .stop_tx = serial21285_stop_tx,
352 .start_tx = serial21285_start_tx,
353 .stop_rx = serial21285_stop_rx,
354 .enable_ms = serial21285_enable_ms,
355 .break_ctl = serial21285_break_ctl,
356 .startup = serial21285_startup,
357 .shutdown = serial21285_shutdown,
358 .set_termios = serial21285_set_termios,
359 .type = serial21285_type,
360 .release_port = serial21285_release_port,
361 .request_port = serial21285_request_port,
362 .config_port = serial21285_config_port,
363 .verify_port = serial21285_verify_port,
364};
365
366static struct uart_port serial21285_port = {
367 .mapbase = 0x42000160,
Russell King9b4a1612006-02-05 10:48:10 +0000368 .iotype = UPIO_MEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 .irq = NO_IRQ,
370 .fifosize = 16,
371 .ops = &serial21285_ops,
Russell Kingce8337c2006-01-21 19:28:15 +0000372 .flags = UPF_BOOT_AUTOCONF,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373};
374
375static void serial21285_setup_ports(void)
376{
377 serial21285_port.uartclk = mem_fclk_21285 / 4;
378}
379
380#ifdef CONFIG_SERIAL_21285_CONSOLE
Russell Kingd3587882006-03-20 20:00:09 +0000381static void serial21285_console_putchar(struct uart_port *port, int ch)
382{
383 while (*CSR_UARTFLG & 0x20)
384 barrier();
385 *CSR_UARTDR = ch;
386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388static void
389serial21285_console_write(struct console *co, const char *s,
390 unsigned int count)
391{
Russell Kingd3587882006-03-20 20:00:09 +0000392 uart_console_write(&serial21285_port, s, count, serial21285_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395static void __init
396serial21285_get_options(struct uart_port *port, int *baud,
397 int *parity, int *bits)
398{
399 if (*CSR_UARTCON == 1) {
400 unsigned int tmp;
401
402 tmp = *CSR_H_UBRLCR;
403 switch (tmp & 0x60) {
404 case 0x00:
405 *bits = 5;
406 break;
407 case 0x20:
408 *bits = 6;
409 break;
410 case 0x40:
411 *bits = 7;
412 break;
413 default:
414 case 0x60:
415 *bits = 8;
416 break;
417 }
418
419 if (tmp & H_UBRLCR_PARENB) {
420 *parity = 'o';
421 if (tmp & H_UBRLCR_PAREVN)
422 *parity = 'e';
423 }
424
425 tmp = *CSR_L_UBRLCR | (*CSR_M_UBRLCR << 8);
426
427 *baud = port->uartclk / (16 * (tmp + 1));
428 }
429}
430
431static int __init serial21285_console_setup(struct console *co, char *options)
432{
433 struct uart_port *port = &serial21285_port;
434 int baud = 9600;
435 int bits = 8;
436 int parity = 'n';
437 int flow = 'n';
438
439 if (machine_is_personal_server())
440 baud = 57600;
441
442 /*
443 * Check whether an invalid uart number has been specified, and
444 * if so, search for the first available port that does have
445 * console support.
446 */
447 if (options)
448 uart_parse_options(options, &baud, &parity, &bits, &flow);
449 else
450 serial21285_get_options(port, &baud, &parity, &bits);
451
452 return uart_set_options(port, co, baud, parity, bits, flow);
453}
454
Vincent Sanders2d934862005-09-14 22:36:03 +0100455static struct uart_driver serial21285_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457static struct console serial21285_console =
458{
459 .name = SERIAL_21285_NAME,
460 .write = serial21285_console_write,
461 .device = uart_console_device,
462 .setup = serial21285_console_setup,
463 .flags = CON_PRINTBUFFER,
464 .index = -1,
465 .data = &serial21285_reg,
466};
467
468static int __init rs285_console_init(void)
469{
470 serial21285_setup_ports();
471 register_console(&serial21285_console);
472 return 0;
473}
474console_initcall(rs285_console_init);
475
476#define SERIAL_21285_CONSOLE &serial21285_console
477#else
478#define SERIAL_21285_CONSOLE NULL
479#endif
480
481static struct uart_driver serial21285_reg = {
482 .owner = THIS_MODULE,
483 .driver_name = "ttyFB",
484 .dev_name = "ttyFB",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 .major = SERIAL_21285_MAJOR,
486 .minor = SERIAL_21285_MINOR,
487 .nr = 1,
488 .cons = SERIAL_21285_CONSOLE,
489};
490
491static int __init serial21285_init(void)
492{
493 int ret;
494
Adrian Bunkd87a6d92008-07-16 21:53:31 +0100495 printk(KERN_INFO "Serial: 21285 driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 serial21285_setup_ports();
498
499 ret = uart_register_driver(&serial21285_reg);
500 if (ret == 0)
501 uart_add_one_port(&serial21285_reg, &serial21285_port);
502
503 return ret;
504}
505
506static void __exit serial21285_exit(void)
507{
508 uart_remove_one_port(&serial21285_reg, &serial21285_port);
509 uart_unregister_driver(&serial21285_reg);
510}
511
512module_init(serial21285_init);
513module_exit(serial21285_exit);
514
515MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +0100516MODULE_DESCRIPTION("Intel Footbridge (21285) serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517MODULE_ALIAS_CHARDEV(SERIAL_21285_MAJOR, SERIAL_21285_MINOR);