blob: 6ac5270c74ddca55d0364b89d0e8f6ff481f8c25 [file] [log] [blame]
Vineet Gupta2ac4ad22012-10-27 12:47:12 +05301/*
2 * ARC On-Chip(fpga) UART Driver
3 *
4 * Copyright (C) 2010-2012 Synopsys, Inc. (www.synopsys.com)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * vineetg: July 10th 2012
11 * -Decoupled the driver from arch/arc
12 * +Using platform_get_resource() for irq/membase (thx to bfin_uart.c)
13 * +Using early_platform_xxx() for early console (thx to mach-shmobile/xxx)
14 *
15 * Vineetg: Aug 21st 2010
16 * -Is uart_tx_stopped() not done in tty write path as it has already been
17 * taken care of, in serial core
18 *
19 * Vineetg: Aug 18th 2010
20 * -New Serial Core based ARC UART driver
21 * -Derived largely from blackfin driver albiet with some major tweaks
22 *
23 * TODO:
24 * -check if sysreq works
25 */
26
27#if defined(CONFIG_SERIAL_ARC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
28#define SUPPORT_SYSRQ
29#endif
30
31#include <linux/module.h>
32#include <linux/serial.h>
33#include <linux/console.h>
34#include <linux/sysrq.h>
35#include <linux/platform_device.h>
36#include <linux/tty.h>
37#include <linux/tty_flip.h>
38#include <linux/serial_core.h>
39#include <linux/io.h>
Vineet Guptaea28fd52013-01-11 11:50:23 +053040#include <linux/of.h>
41#include <linux/of_platform.h>
Vineet Gupta2ac4ad22012-10-27 12:47:12 +053042
43/*************************************
44 * ARC UART Hardware Specs
45 ************************************/
46#define ARC_UART_TX_FIFO_SIZE 1
47
48/*
49 * UART Register set (this is not a Standards Compliant IP)
50 * Also each reg is Word aligned, but only 8 bits wide
51 */
52#define R_ID0 0
53#define R_ID1 4
54#define R_ID2 8
55#define R_ID3 12
56#define R_DATA 16
57#define R_STS 20
58#define R_BAUDL 24
59#define R_BAUDH 28
60
61/* Bits for UART Status Reg (R/W) */
62#define RXIENB 0x04 /* Receive Interrupt Enable */
63#define TXIENB 0x40 /* Transmit Interrupt Enable */
64
65#define RXEMPTY 0x20 /* Receive FIFO Empty: No char receivede */
66#define TXEMPTY 0x80 /* Transmit FIFO Empty, thus char can be written into */
67
68#define RXFULL 0x08 /* Receive FIFO full */
69#define RXFULL1 0x10 /* Receive FIFO has space for 1 char (tot space=4) */
70
71#define RXFERR 0x01 /* Frame Error: Stop Bit not detected */
72#define RXOERR 0x02 /* OverFlow Err: Char recv but RXFULL still set */
73
74/* Uart bit fiddling helpers: lowest level */
75#define RBASE(uart, reg) (uart->port.membase + reg)
76#define UART_REG_SET(u, r, v) writeb((v), RBASE(u, r))
77#define UART_REG_GET(u, r) readb(RBASE(u, r))
78
79#define UART_REG_OR(u, r, v) UART_REG_SET(u, r, UART_REG_GET(u, r) | (v))
80#define UART_REG_CLR(u, r, v) UART_REG_SET(u, r, UART_REG_GET(u, r) & ~(v))
81
82/* Uart bit fiddling helpers: API level */
83#define UART_SET_DATA(uart, val) UART_REG_SET(uart, R_DATA, val)
84#define UART_GET_DATA(uart) UART_REG_GET(uart, R_DATA)
85
86#define UART_SET_BAUDH(uart, val) UART_REG_SET(uart, R_BAUDH, val)
87#define UART_SET_BAUDL(uart, val) UART_REG_SET(uart, R_BAUDL, val)
88
89#define UART_CLR_STATUS(uart, val) UART_REG_CLR(uart, R_STS, val)
90#define UART_GET_STATUS(uart) UART_REG_GET(uart, R_STS)
91
92#define UART_ALL_IRQ_DISABLE(uart) UART_REG_CLR(uart, R_STS, RXIENB|TXIENB)
93#define UART_RX_IRQ_DISABLE(uart) UART_REG_CLR(uart, R_STS, RXIENB)
94#define UART_TX_IRQ_DISABLE(uart) UART_REG_CLR(uart, R_STS, TXIENB)
95
96#define UART_ALL_IRQ_ENABLE(uart) UART_REG_OR(uart, R_STS, RXIENB|TXIENB)
97#define UART_RX_IRQ_ENABLE(uart) UART_REG_OR(uart, R_STS, RXIENB)
98#define UART_TX_IRQ_ENABLE(uart) UART_REG_OR(uart, R_STS, TXIENB)
99
100#define ARC_SERIAL_DEV_NAME "ttyARC"
101
102struct arc_uart_port {
103 struct uart_port port;
104 unsigned long baud;
105 int is_emulated; /* H/w vs. Instruction Set Simulator */
106};
107
108#define to_arc_port(uport) container_of(uport, struct arc_uart_port, port)
109
110static struct arc_uart_port arc_uart_ports[CONFIG_SERIAL_ARC_NR_PORTS];
111
112#ifdef CONFIG_SERIAL_ARC_CONSOLE
113static struct console arc_console;
114#endif
115
116#define DRIVER_NAME "arc-uart"
117
118static struct uart_driver arc_uart_driver = {
119 .owner = THIS_MODULE,
120 .driver_name = DRIVER_NAME,
121 .dev_name = ARC_SERIAL_DEV_NAME,
122 .major = 0,
123 .minor = 0,
124 .nr = CONFIG_SERIAL_ARC_NR_PORTS,
125#ifdef CONFIG_SERIAL_ARC_CONSOLE
126 .cons = &arc_console,
127#endif
128};
129
130static void arc_serial_stop_rx(struct uart_port *port)
131{
132 struct arc_uart_port *uart = to_arc_port(port);
133
134 UART_RX_IRQ_DISABLE(uart);
135}
136
137static void arc_serial_stop_tx(struct uart_port *port)
138{
139 struct arc_uart_port *uart = to_arc_port(port);
140
141 while (!(UART_GET_STATUS(uart) & TXEMPTY))
142 cpu_relax();
143
144 UART_TX_IRQ_DISABLE(uart);
145}
146
147/*
148 * Return TIOCSER_TEMT when transmitter is not busy.
149 */
150static unsigned int arc_serial_tx_empty(struct uart_port *port)
151{
152 struct arc_uart_port *uart = to_arc_port(port);
153 unsigned int stat;
154
155 stat = UART_GET_STATUS(uart);
156 if (stat & TXEMPTY)
157 return TIOCSER_TEMT;
158
159 return 0;
160}
161
162/*
163 * Driver internal routine, used by both tty(serial core) as well as tx-isr
164 * -Called under spinlock in either cases
Jiri Slabyee797062013-03-07 13:12:34 +0100165 * -also tty->stopped has already been checked
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530166 * = by uart_start( ) before calling us
167 * = tx_ist checks that too before calling
168 */
169static void arc_serial_tx_chars(struct arc_uart_port *uart)
170{
171 struct circ_buf *xmit = &uart->port.state->xmit;
172 int sent = 0;
173 unsigned char ch;
174
175 if (unlikely(uart->port.x_char)) {
176 UART_SET_DATA(uart, uart->port.x_char);
177 uart->port.icount.tx++;
178 uart->port.x_char = 0;
179 sent = 1;
180 } else if (xmit->tail != xmit->head) { /* TODO: uart_circ_empty */
181 ch = xmit->buf[xmit->tail];
182 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
183 uart->port.icount.tx++;
184 while (!(UART_GET_STATUS(uart) & TXEMPTY))
185 cpu_relax();
186 UART_SET_DATA(uart, ch);
187 sent = 1;
188 }
189
190 /*
191 * If num chars in xmit buffer are too few, ask tty layer for more.
192 * By Hard ISR to schedule processing in software interrupt part
193 */
194 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
195 uart_write_wakeup(&uart->port);
196
197 if (sent)
198 UART_TX_IRQ_ENABLE(uart);
199}
200
201/*
202 * port is locked and interrupts are disabled
203 * uart_start( ) calls us under the port spinlock irqsave
204 */
205static void arc_serial_start_tx(struct uart_port *port)
206{
207 struct arc_uart_port *uart = to_arc_port(port);
208
209 arc_serial_tx_chars(uart);
210}
211
Vineet Gupta5284eba2013-08-01 21:49:19 -0700212static void arc_serial_rx_chars(struct arc_uart_port *uart, unsigned int status)
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530213{
Vineet Gupta5284eba2013-08-01 21:49:19 -0700214 unsigned int ch, flg = 0;
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530215
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530216 /*
217 * UART has 4 deep RX-FIFO. Driver's recongnition of this fact
218 * is very subtle. Here's how ...
219 * Upon getting a RX-Intr, such that RX-EMPTY=0, meaning data available,
220 * driver reads the DATA Reg and keeps doing that in a loop, until
221 * RX-EMPTY=1. Multiple chars being avail, with a single Interrupt,
222 * before RX-EMPTY=0, implies some sort of buffering going on in the
223 * controller, which is indeed the Rx-FIFO.
224 */
Vineet Gupta5284eba2013-08-01 21:49:19 -0700225 do {
226 /*
227 * This could be an Rx Intr for err (no data),
228 * so check err and clear that Intr first
229 */
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530230 if (unlikely(status & (RXOERR | RXFERR))) {
231 if (status & RXOERR) {
232 uart->port.icount.overrun++;
233 flg = TTY_OVERRUN;
234 UART_CLR_STATUS(uart, RXOERR);
235 }
236
237 if (status & RXFERR) {
238 uart->port.icount.frame++;
239 flg = TTY_FRAME;
240 UART_CLR_STATUS(uart, RXFERR);
241 }
242 } else
243 flg = TTY_NORMAL;
244
Vineet Gupta5284eba2013-08-01 21:49:19 -0700245 if (status & RXEMPTY)
246 continue;
247
248 ch = UART_GET_DATA(uart);
249 uart->port.icount.rx++;
250
Vineet Gupta00a135b2013-08-01 21:49:20 -0700251 if (!(uart_handle_sysrq_char(&uart->port, ch)))
252 uart_insert_char(&uart->port, status, RXOERR, ch, flg);
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530253
Jiri Slaby2e124b42013-01-03 15:53:06 +0100254 tty_flip_buffer_push(&uart->port.state->port);
Vineet Gupta5284eba2013-08-01 21:49:19 -0700255 } while (!((status = UART_GET_STATUS(uart)) & RXEMPTY));
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530256}
257
258/*
259 * A note on the Interrupt handling state machine of this driver
260 *
261 * kernel printk writes funnel thru the console driver framework and in order
262 * to keep things simple as well as efficient, it writes to UART in polled
263 * mode, in one shot, and exits.
264 *
265 * OTOH, Userland output (via tty layer), uses interrupt based writes as there
266 * can be undeterministic delay between char writes.
267 *
268 * Thus Rx-interrupts are always enabled, while tx-interrupts are by default
269 * disabled.
270 *
271 * When tty has some data to send out, serial core calls driver's start_tx
272 * which
273 * -checks-if-tty-buffer-has-char-to-send
274 * -writes-data-to-uart
275 * -enable-tx-intr
276 *
277 * Once data bits are pushed out, controller raises the Tx-room-avail-Interrupt.
278 * The first thing Tx ISR does is disable further Tx interrupts (as this could
279 * be the last char to send, before settling down into the quiet polled mode).
280 * It then calls the exact routine used by tty layer write to send out any
281 * more char in tty buffer. In case of sending, it re-enables Tx-intr. In case
282 * of no data, it remains disabled.
283 * This is how the transmit state machine is dynamically switched on/off
284 */
285
286static irqreturn_t arc_serial_isr(int irq, void *dev_id)
287{
288 struct arc_uart_port *uart = dev_id;
289 unsigned int status;
290
291 status = UART_GET_STATUS(uart);
292
293 /*
294 * Single IRQ for both Rx (data available) Tx (room available) Interrupt
295 * notifications from the UART Controller.
296 * To demultiplex between the two, we check the relevant bits
297 */
Vineet Gupta5284eba2013-08-01 21:49:19 -0700298 if (status & RXIENB) {
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530299
300 /* already in ISR, no need of xx_irqsave */
301 spin_lock(&uart->port.lock);
Vineet Gupta5284eba2013-08-01 21:49:19 -0700302 arc_serial_rx_chars(uart, status);
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530303 spin_unlock(&uart->port.lock);
304 }
305
306 if ((status & TXIENB) && (status & TXEMPTY)) {
307
308 /* Unconditionally disable further Tx-Interrupts.
309 * will be enabled by tx_chars() if needed.
310 */
311 UART_TX_IRQ_DISABLE(uart);
312
313 spin_lock(&uart->port.lock);
314
315 if (!uart_tx_stopped(&uart->port))
316 arc_serial_tx_chars(uart);
317
318 spin_unlock(&uart->port.lock);
319 }
320
321 return IRQ_HANDLED;
322}
323
324static unsigned int arc_serial_get_mctrl(struct uart_port *port)
325{
326 /*
327 * Pretend we have a Modem status reg and following bits are
328 * always set, to satify the serial core state machine
329 * (DSR) Data Set Ready
330 * (CTS) Clear To Send
331 * (CAR) Carrier Detect
332 */
333 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
334}
335
336static void arc_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
337{
338 /* MCR not present */
339}
340
341/* Enable Modem Status Interrupts */
342
343static void arc_serial_enable_ms(struct uart_port *port)
344{
345 /* MSR not present */
346}
347
348static void arc_serial_break_ctl(struct uart_port *port, int break_state)
349{
350 /* ARC UART doesn't support sending Break signal */
351}
352
353static int arc_serial_startup(struct uart_port *port)
354{
355 struct arc_uart_port *uart = to_arc_port(port);
356
357 /* Before we hook up the ISR, Disable all UART Interrupts */
358 UART_ALL_IRQ_DISABLE(uart);
359
360 if (request_irq(uart->port.irq, arc_serial_isr, 0, "arc uart rx-tx",
361 uart)) {
362 dev_warn(uart->port.dev, "Unable to attach ARC UART intr\n");
363 return -EBUSY;
364 }
365
366 UART_RX_IRQ_ENABLE(uart); /* Only Rx IRQ enabled to begin with */
367
368 return 0;
369}
370
371/* This is not really needed */
372static void arc_serial_shutdown(struct uart_port *port)
373{
374 struct arc_uart_port *uart = to_arc_port(port);
375 free_irq(uart->port.irq, uart);
376}
377
378static void
379arc_serial_set_termios(struct uart_port *port, struct ktermios *new,
380 struct ktermios *old)
381{
382 struct arc_uart_port *uart = to_arc_port(port);
383 unsigned int baud, uartl, uarth, hw_val;
384 unsigned long flags;
385
386 /*
387 * Use the generic handler so that any specially encoded baud rates
388 * such as SPD_xx flags or "%B0" can be handled
389 * Max Baud I suppose will not be more than current 115K * 4
390 * Formula for ARC UART is: hw-val = ((CLK/(BAUD*4)) -1)
391 * spread over two 8-bit registers
392 */
393 baud = uart_get_baud_rate(port, new, old, 0, 460800);
394
395 hw_val = port->uartclk / (uart->baud * 4) - 1;
396 uartl = hw_val & 0xFF;
397 uarth = (hw_val >> 8) & 0xFF;
398
399 /*
400 * UART ISS(Instruction Set simulator) emulation has a subtle bug:
401 * A existing value of Baudh = 0 is used as a indication to startup
402 * it's internal state machine.
403 * Thus if baudh is set to 0, 2 times, it chokes.
404 * This happens with BAUD=115200 and the formaula above
405 * Until that is fixed, when running on ISS, we will set baudh to !0
406 */
407 if (uart->is_emulated)
408 uarth = 1;
409
410 spin_lock_irqsave(&port->lock, flags);
411
412 UART_ALL_IRQ_DISABLE(uart);
413
414 UART_SET_BAUDL(uart, uartl);
415 UART_SET_BAUDH(uart, uarth);
416
417 UART_RX_IRQ_ENABLE(uart);
418
419 /*
420 * UART doesn't support Parity/Hardware Flow Control;
421 * Only supports 8N1 character size
422 */
423 new->c_cflag &= ~(CMSPAR|CRTSCTS|CSIZE);
424 new->c_cflag |= CS8;
425
426 if (old)
427 tty_termios_copy_hw(new, old);
428
429 /* Don't rewrite B0 */
430 if (tty_termios_baud_rate(new))
431 tty_termios_encode_baud_rate(new, baud, baud);
432
433 uart_update_timeout(port, new->c_cflag, baud);
434
435 spin_unlock_irqrestore(&port->lock, flags);
436}
437
438static const char *arc_serial_type(struct uart_port *port)
439{
440 struct arc_uart_port *uart = to_arc_port(port);
441
442 return uart->port.type == PORT_ARC ? DRIVER_NAME : NULL;
443}
444
445static void arc_serial_release_port(struct uart_port *port)
446{
447}
448
449static int arc_serial_request_port(struct uart_port *port)
450{
451 return 0;
452}
453
454/*
455 * Verify the new serial_struct (for TIOCSSERIAL).
456 */
457static int
458arc_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
459{
460 if (port->type != PORT_UNKNOWN && ser->type != PORT_ARC)
461 return -EINVAL;
462
463 return 0;
464}
465
466/*
467 * Configure/autoconfigure the port.
468 */
469static void arc_serial_config_port(struct uart_port *port, int flags)
470{
471 struct arc_uart_port *uart = to_arc_port(port);
472
473 if (flags & UART_CONFIG_TYPE)
474 uart->port.type = PORT_ARC;
475}
476
477#if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_ARC_CONSOLE)
478
479static void arc_serial_poll_putchar(struct uart_port *port, unsigned char chr)
480{
481 struct arc_uart_port *uart = to_arc_port(port);
482
483 while (!(UART_GET_STATUS(uart) & TXEMPTY))
484 cpu_relax();
485
486 UART_SET_DATA(uart, chr);
487}
488#endif
489
490#ifdef CONFIG_CONSOLE_POLL
491static int arc_serial_poll_getchar(struct uart_port *port)
492{
493 struct arc_uart_port *uart = to_arc_port(port);
494 unsigned char chr;
495
496 while (!(UART_GET_STATUS(uart) & RXEMPTY))
497 cpu_relax();
498
499 chr = UART_GET_DATA(uart);
500 return chr;
501}
502#endif
503
504static struct uart_ops arc_serial_pops = {
505 .tx_empty = arc_serial_tx_empty,
506 .set_mctrl = arc_serial_set_mctrl,
507 .get_mctrl = arc_serial_get_mctrl,
508 .stop_tx = arc_serial_stop_tx,
509 .start_tx = arc_serial_start_tx,
510 .stop_rx = arc_serial_stop_rx,
511 .enable_ms = arc_serial_enable_ms,
512 .break_ctl = arc_serial_break_ctl,
513 .startup = arc_serial_startup,
514 .shutdown = arc_serial_shutdown,
515 .set_termios = arc_serial_set_termios,
516 .type = arc_serial_type,
517 .release_port = arc_serial_release_port,
518 .request_port = arc_serial_request_port,
519 .config_port = arc_serial_config_port,
520 .verify_port = arc_serial_verify_port,
521#ifdef CONFIG_CONSOLE_POLL
522 .poll_put_char = arc_serial_poll_putchar,
523 .poll_get_char = arc_serial_poll_getchar,
524#endif
525};
526
Bill Pemberton9671f092012-11-19 13:21:50 -0500527static int
Vineet Gupta026bb292013-01-11 11:50:20 +0530528arc_uart_init_one(struct platform_device *pdev, int dev_id)
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530529{
530 struct resource *res, *res2;
531 unsigned long *plat_data;
Vineet Gupta026bb292013-01-11 11:50:20 +0530532 struct arc_uart_port *uart = &arc_uart_ports[dev_id];
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530533
Jingoo Han574de552013-07-30 17:06:57 +0900534 plat_data = (unsigned long *)dev_get_platdata(&pdev->dev);
Vineet Guptad6c0d062013-01-11 11:50:22 +0530535 if (!plat_data)
536 return -ENODEV;
537
538 uart->is_emulated = !!plat_data[0]; /* workaround ISS bug */
Vineet Guptaea28fd52013-01-11 11:50:23 +0530539
540 if (is_early_platform_device(pdev)) {
541 uart->port.uartclk = plat_data[1];
542 uart->baud = plat_data[2];
543 } else {
544 struct device_node *np = pdev->dev.of_node;
545 u32 val;
546
547 if (of_property_read_u32(np, "clock-frequency", &val)) {
548 dev_err(&pdev->dev, "clock-frequency property NOTset\n");
549 return -EINVAL;
550 }
551 uart->port.uartclk = val;
552
Vineet Gupta11c62d42013-02-11 14:11:41 +0530553 if (of_property_read_u32(np, "current-speed", &val)) {
554 dev_err(&pdev->dev, "current-speed property NOT set\n");
Vineet Guptaea28fd52013-01-11 11:50:23 +0530555 return -EINVAL;
556 }
557 uart->baud = val;
558 }
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530559
560 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
561 if (!res)
562 return -ENODEV;
563
564 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
565 if (!res2)
566 return -ENODEV;
567
568 uart->port.mapbase = res->start;
569 uart->port.membase = ioremap_nocache(res->start, resource_size(res));
570 if (!uart->port.membase)
571 /* No point of dev_err since UART itself is hosed here */
572 return -ENXIO;
573
574 uart->port.irq = res2->start;
575 uart->port.dev = &pdev->dev;
576 uart->port.iotype = UPIO_MEM;
577 uart->port.flags = UPF_BOOT_AUTOCONF;
Vineet Gupta026bb292013-01-11 11:50:20 +0530578 uart->port.line = dev_id;
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530579 uart->port.ops = &arc_serial_pops;
580
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530581 uart->port.fifosize = ARC_UART_TX_FIFO_SIZE;
582
583 /*
584 * uart_insert_char( ) uses it in decideding whether to ignore a
585 * char or not. Explicitly setting it here, removes the subtelty
586 */
587 uart->port.ignore_status_mask = 0;
588
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530589 return 0;
590}
591
592#ifdef CONFIG_SERIAL_ARC_CONSOLE
593
Bill Pemberton9671f092012-11-19 13:21:50 -0500594static int arc_serial_console_setup(struct console *co, char *options)
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530595{
596 struct uart_port *port;
597 int baud = 115200;
598 int bits = 8;
599 int parity = 'n';
600 int flow = 'n';
601
602 if (co->index < 0 || co->index >= CONFIG_SERIAL_ARC_NR_PORTS)
603 return -ENODEV;
604
605 /*
606 * The uart port backing the console (e.g. ttyARC1) might not have been
607 * init yet. If so, defer the console setup to after the port.
608 */
609 port = &arc_uart_ports[co->index].port;
610 if (!port->membase)
611 return -ENODEV;
612
613 if (options)
614 uart_parse_options(options, &baud, &parity, &bits, &flow);
615
616 /*
617 * Serial core will call port->ops->set_termios( )
618 * which will set the baud reg
619 */
620 return uart_set_options(port, co, baud, parity, bits, flow);
621}
622
623static void arc_serial_console_putchar(struct uart_port *port, int ch)
624{
625 arc_serial_poll_putchar(port, (unsigned char)ch);
626}
627
628/*
629 * Interrupts are disabled on entering
630 */
631static void arc_serial_console_write(struct console *co, const char *s,
632 unsigned int count)
633{
634 struct uart_port *port = &arc_uart_ports[co->index].port;
635 unsigned long flags;
636
637 spin_lock_irqsave(&port->lock, flags);
638 uart_console_write(port, s, count, arc_serial_console_putchar);
639 spin_unlock_irqrestore(&port->lock, flags);
640}
641
642static struct console arc_console = {
643 .name = ARC_SERIAL_DEV_NAME,
644 .write = arc_serial_console_write,
645 .device = uart_console_device,
646 .setup = arc_serial_console_setup,
647 .flags = CON_PRINTBUFFER,
648 .index = -1,
649 .data = &arc_uart_driver
650};
651
652static __init void early_serial_write(struct console *con, const char *s,
653 unsigned int n)
654{
655 struct uart_port *port = &arc_uart_ports[con->index].port;
656 unsigned int i;
657
658 for (i = 0; i < n; i++, s++) {
659 if (*s == '\n')
660 arc_serial_poll_putchar(port, '\r');
661 arc_serial_poll_putchar(port, *s);
662 }
663}
664
Vineet Gupta795b5bb2013-01-16 13:47:23 +0530665static struct console arc_early_serial_console __initdata = {
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530666 .name = "early_ARCuart",
667 .write = early_serial_write,
668 .flags = CON_PRINTBUFFER | CON_BOOT,
669 .index = -1
670};
671
Vineet Guptae163d1f2013-01-11 11:50:21 +0530672static int __init arc_serial_probe_earlyprintk(struct platform_device *pdev)
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530673{
Vineet Gupta026bb292013-01-11 11:50:20 +0530674 int dev_id = pdev->id < 0 ? 0 : pdev->id;
675 int rc;
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530676
Vineet Gupta026bb292013-01-11 11:50:20 +0530677 arc_early_serial_console.index = dev_id;
678
679 rc = arc_uart_init_one(pdev, dev_id);
680 if (rc)
681 panic("early console init failed\n");
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530682
683 arc_serial_console_setup(&arc_early_serial_console, NULL);
684
685 register_console(&arc_early_serial_console);
686 return 0;
687}
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530688#endif /* CONFIG_SERIAL_ARC_CONSOLE */
689
Bill Pemberton9671f092012-11-19 13:21:50 -0500690static int arc_serial_probe(struct platform_device *pdev)
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530691{
Vineet Gupta026bb292013-01-11 11:50:20 +0530692 int rc, dev_id;
Vineet Guptaea28fd52013-01-11 11:50:23 +0530693 struct device_node *np = pdev->dev.of_node;
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530694
Vineet Guptaea28fd52013-01-11 11:50:23 +0530695 /* no device tree device */
696 if (!np)
697 return -ENODEV;
698
699 dev_id = of_alias_get_id(np, "serial");
Vineet Gupta11c62d42013-02-11 14:11:41 +0530700 if (dev_id < 0)
701 dev_id = 0;
Vineet Guptaea28fd52013-01-11 11:50:23 +0530702
Vineet Gupta026bb292013-01-11 11:50:20 +0530703 rc = arc_uart_init_one(pdev, dev_id);
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530704 if (rc)
705 return rc;
706
Vineet Gupta026bb292013-01-11 11:50:20 +0530707 rc = uart_add_one_port(&arc_uart_driver, &arc_uart_ports[dev_id].port);
708 return rc;
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530709}
710
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500711static int arc_serial_remove(struct platform_device *pdev)
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530712{
713 /* This will never be called */
714 return 0;
715}
716
Vineet Guptaea28fd52013-01-11 11:50:23 +0530717static const struct of_device_id arc_uart_dt_ids[] = {
718 { .compatible = "snps,arc-uart" },
719 { /* Sentinel */ }
720};
721MODULE_DEVICE_TABLE(of, arc_uart_dt_ids);
722
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530723static struct platform_driver arc_platform_driver = {
724 .probe = arc_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500725 .remove = arc_serial_remove,
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530726 .driver = {
727 .name = DRIVER_NAME,
728 .owner = THIS_MODULE,
Vineet Guptaea28fd52013-01-11 11:50:23 +0530729 .of_match_table = arc_uart_dt_ids,
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530730 },
731};
732
733#ifdef CONFIG_SERIAL_ARC_CONSOLE
Vineet Guptae163d1f2013-01-11 11:50:21 +0530734
Vineet Gupta795b5bb2013-01-16 13:47:23 +0530735static struct platform_driver early_arc_platform_driver __initdata = {
Vineet Guptae163d1f2013-01-11 11:50:21 +0530736 .probe = arc_serial_probe_earlyprintk,
737 .remove = arc_serial_remove,
738 .driver = {
739 .name = DRIVER_NAME,
740 .owner = THIS_MODULE,
741 },
742};
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530743/*
744 * Register an early platform driver of "earlyprintk" class.
745 * ARCH platform code installs the driver and probes the early devices
746 * The installation could rely on user specifying earlyprintk=xyx in cmd line
747 * or it could be done independently, for all "earlyprintk" class drivers.
748 * [see arch/arc/plat-arcfpga/platform.c]
749 */
Vineet Guptae163d1f2013-01-11 11:50:21 +0530750early_platform_init("earlyprintk", &early_arc_platform_driver);
Vineet Gupta2ac4ad22012-10-27 12:47:12 +0530751
752#endif /* CONFIG_SERIAL_ARC_CONSOLE */
753
754static int __init arc_serial_init(void)
755{
756 int ret;
757
758 ret = uart_register_driver(&arc_uart_driver);
759 if (ret)
760 return ret;
761
762 ret = platform_driver_register(&arc_platform_driver);
763 if (ret)
764 uart_unregister_driver(&arc_uart_driver);
765
766 return ret;
767}
768
769static void __exit arc_serial_exit(void)
770{
771 platform_driver_unregister(&arc_platform_driver);
772 uart_unregister_driver(&arc_uart_driver);
773}
774
775module_init(arc_serial_init);
776module_exit(arc_serial_exit);
777
778MODULE_LICENSE("GPL");
779MODULE_ALIAS("plat-arcfpga/uart");
780MODULE_AUTHOR("Vineet Gupta");
781MODULE_DESCRIPTION("ARC(Synopsys) On-Chip(fpga) serial driver");