blob: f2257191518521b37718390cb4f277a09dec4008 [file] [log] [blame]
Alexey Charkov304e1262010-11-08 20:33:20 +03001/*
Alexey Charkov304e1262010-11-08 20:33:20 +03002 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
3 *
4 * Based on msm_serial.c, which is:
5 * Copyright (C) 2007 Google, Inc.
6 * Author: Robert Love <rlove@google.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#if defined(CONFIG_SERIAL_VT8500_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
19# define SUPPORT_SYSRQ
20#endif
21
22#include <linux/hrtimer.h>
23#include <linux/delay.h>
24#include <linux/module.h>
25#include <linux/io.h>
26#include <linux/ioport.h>
27#include <linux/irq.h>
28#include <linux/init.h>
29#include <linux/console.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial_core.h>
33#include <linux/serial.h>
34#include <linux/slab.h>
35#include <linux/clk.h>
Tony Prisk40011302012-08-03 20:56:25 +120036#include <linux/of.h>
Alexey Charkovae382732014-09-06 21:21:12 +040037#include <linux/of_device.h>
Sachin Kamat82b23132013-03-04 14:24:39 +053038#include <linux/err.h>
Alexey Charkov304e1262010-11-08 20:33:20 +030039
40/*
41 * UART Register offsets
42 */
43
44#define VT8500_URTDR 0x0000 /* Transmit data */
45#define VT8500_URRDR 0x0004 /* Receive data */
46#define VT8500_URDIV 0x0008 /* Clock/Baud rate divisor */
47#define VT8500_URLCR 0x000C /* Line control */
48#define VT8500_URICR 0x0010 /* IrDA control */
49#define VT8500_URIER 0x0014 /* Interrupt enable */
50#define VT8500_URISR 0x0018 /* Interrupt status */
51#define VT8500_URUSR 0x001c /* UART status */
52#define VT8500_URFCR 0x0020 /* FIFO control */
53#define VT8500_URFIDX 0x0024 /* FIFO index */
54#define VT8500_URBKR 0x0028 /* Break signal count */
55#define VT8500_URTOD 0x002c /* Time out divisor */
56#define VT8500_TXFIFO 0x1000 /* Transmit FIFO (16x8) */
57#define VT8500_RXFIFO 0x1020 /* Receive FIFO (16x10) */
58
59/*
60 * Interrupt enable and status bits
61 */
62
63#define TXDE (1 << 0) /* Tx Data empty */
64#define RXDF (1 << 1) /* Rx Data full */
65#define TXFAE (1 << 2) /* Tx FIFO almost empty */
66#define TXFE (1 << 3) /* Tx FIFO empty */
67#define RXFAF (1 << 4) /* Rx FIFO almost full */
68#define RXFF (1 << 5) /* Rx FIFO full */
69#define TXUDR (1 << 6) /* Tx underrun */
70#define RXOVER (1 << 7) /* Rx overrun */
71#define PER (1 << 8) /* Parity error */
72#define FER (1 << 9) /* Frame error */
73#define TCTS (1 << 10) /* Toggle of CTS */
74#define RXTOUT (1 << 11) /* Rx timeout */
75#define BKDONE (1 << 12) /* Break signal done */
76#define ERR (1 << 13) /* AHB error response */
77
78#define RX_FIFO_INTS (RXFAF | RXFF | RXOVER | PER | FER | RXTOUT)
79#define TX_FIFO_INTS (TXFAE | TXFE | TXUDR)
80
Alexey Charkovae382732014-09-06 21:21:12 +040081/*
82 * Line control bits
83 */
84
85#define VT8500_TXEN (1 << 0) /* Enable transmit logic */
86#define VT8500_RXEN (1 << 1) /* Enable receive logic */
87#define VT8500_CS8 (1 << 2) /* 8-bit data length (vs. 7-bit) */
88#define VT8500_CSTOPB (1 << 3) /* 2 stop bits (vs. 1) */
89#define VT8500_PARENB (1 << 4) /* Enable parity */
90#define VT8500_PARODD (1 << 5) /* Odd parity (vs. even) */
91#define VT8500_RTS (1 << 6) /* Ready to send */
92#define VT8500_LOOPBK (1 << 7) /* Enable internal loopback */
93#define VT8500_DMA (1 << 8) /* Enable DMA mode (needs FIFO) */
94#define VT8500_BREAK (1 << 9) /* Initiate break signal */
95#define VT8500_PSLVERR (1 << 10) /* APB error upon empty RX FIFO read */
96#define VT8500_SWRTSCTS (1 << 11) /* Software-controlled RTS/CTS */
97
98/*
99 * Capability flags (driver-internal)
100 */
101
102#define VT8500_HAS_SWRTSCTS_SWITCH (1 << 1)
103
Tony Prisk40011302012-08-03 20:56:25 +1200104#define VT8500_MAX_PORTS 6
105
Alexey Charkov304e1262010-11-08 20:33:20 +0300106struct vt8500_port {
107 struct uart_port uart;
108 char name[16];
109 struct clk *clk;
110 unsigned int ier;
Alexey Charkovae382732014-09-06 21:21:12 +0400111 unsigned int vt8500_uart_flags;
Alexey Charkov304e1262010-11-08 20:33:20 +0300112};
113
Tony Prisk40011302012-08-03 20:56:25 +1200114/*
115 * we use this variable to keep track of which ports
116 * have been allocated as we can't use pdev->id in
117 * devicetree
118 */
119static unsigned long vt8500_ports_in_use;
120
Alexey Charkov304e1262010-11-08 20:33:20 +0300121static inline void vt8500_write(struct uart_port *port, unsigned int val,
122 unsigned int off)
123{
124 writel(val, port->membase + off);
125}
126
127static inline unsigned int vt8500_read(struct uart_port *port, unsigned int off)
128{
129 return readl(port->membase + off);
130}
131
132static void vt8500_stop_tx(struct uart_port *port)
133{
134 struct vt8500_port *vt8500_port = container_of(port,
135 struct vt8500_port,
136 uart);
137
138 vt8500_port->ier &= ~TX_FIFO_INTS;
139 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
140}
141
142static void vt8500_stop_rx(struct uart_port *port)
143{
144 struct vt8500_port *vt8500_port = container_of(port,
145 struct vt8500_port,
146 uart);
147
148 vt8500_port->ier &= ~RX_FIFO_INTS;
149 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
150}
151
152static void vt8500_enable_ms(struct uart_port *port)
153{
154 struct vt8500_port *vt8500_port = container_of(port,
155 struct vt8500_port,
156 uart);
157
158 vt8500_port->ier |= TCTS;
159 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
160}
161
162static void handle_rx(struct uart_port *port)
163{
Jiri Slaby92a19f92013-01-03 15:53:03 +0100164 struct tty_port *tport = &port->state->port;
Alexey Charkov304e1262010-11-08 20:33:20 +0300165
166 /*
167 * Handle overrun
168 */
169 if ((vt8500_read(port, VT8500_URISR) & RXOVER)) {
170 port->icount.overrun++;
Jiri Slaby92a19f92013-01-03 15:53:03 +0100171 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
Alexey Charkov304e1262010-11-08 20:33:20 +0300172 }
173
174 /* and now the main RX loop */
175 while (vt8500_read(port, VT8500_URFIDX) & 0x1f00) {
176 unsigned int c;
177 char flag = TTY_NORMAL;
178
179 c = readw(port->membase + VT8500_RXFIFO) & 0x3ff;
180
181 /* Mask conditions we're ignorning. */
182 c &= ~port->read_status_mask;
183
184 if (c & FER) {
185 port->icount.frame++;
186 flag = TTY_FRAME;
187 } else if (c & PER) {
188 port->icount.parity++;
189 flag = TTY_PARITY;
190 }
191 port->icount.rx++;
192
193 if (!uart_handle_sysrq_char(port, c))
Jiri Slaby92a19f92013-01-03 15:53:03 +0100194 tty_insert_flip_char(tport, c, flag);
Alexey Charkov304e1262010-11-08 20:33:20 +0300195 }
196
Viresh Kumarde49df52013-08-19 20:14:29 +0530197 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100198 tty_flip_buffer_push(tport);
Viresh Kumarde49df52013-08-19 20:14:29 +0530199 spin_lock(&port->lock);
Alexey Charkov304e1262010-11-08 20:33:20 +0300200}
201
202static void handle_tx(struct uart_port *port)
203{
204 struct circ_buf *xmit = &port->state->xmit;
205
206 if (port->x_char) {
207 writeb(port->x_char, port->membase + VT8500_TXFIFO);
208 port->icount.tx++;
209 port->x_char = 0;
210 }
211 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
212 vt8500_stop_tx(port);
213 return;
214 }
215
216 while ((vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16) {
217 if (uart_circ_empty(xmit))
218 break;
219
220 writeb(xmit->buf[xmit->tail], port->membase + VT8500_TXFIFO);
221
222 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
223 port->icount.tx++;
224 }
225
226 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
227 uart_write_wakeup(port);
228
229 if (uart_circ_empty(xmit))
230 vt8500_stop_tx(port);
231}
232
233static void vt8500_start_tx(struct uart_port *port)
234{
235 struct vt8500_port *vt8500_port = container_of(port,
236 struct vt8500_port,
237 uart);
238
239 vt8500_port->ier &= ~TX_FIFO_INTS;
240 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
241 handle_tx(port);
242 vt8500_port->ier |= TX_FIFO_INTS;
243 vt8500_write(port, vt8500_port->ier, VT8500_URIER);
244}
245
246static void handle_delta_cts(struct uart_port *port)
247{
248 port->icount.cts++;
249 wake_up_interruptible(&port->state->port.delta_msr_wait);
250}
251
252static irqreturn_t vt8500_irq(int irq, void *dev_id)
253{
254 struct uart_port *port = dev_id;
255 unsigned long isr;
256
257 spin_lock(&port->lock);
258 isr = vt8500_read(port, VT8500_URISR);
259
260 /* Acknowledge active status bits */
261 vt8500_write(port, isr, VT8500_URISR);
262
263 if (isr & RX_FIFO_INTS)
264 handle_rx(port);
265 if (isr & TX_FIFO_INTS)
266 handle_tx(port);
267 if (isr & TCTS)
268 handle_delta_cts(port);
269
270 spin_unlock(&port->lock);
271
272 return IRQ_HANDLED;
273}
274
275static unsigned int vt8500_tx_empty(struct uart_port *port)
276{
277 return (vt8500_read(port, VT8500_URFIDX) & 0x1f) < 16 ?
278 TIOCSER_TEMT : 0;
279}
280
281static unsigned int vt8500_get_mctrl(struct uart_port *port)
282{
283 unsigned int usr;
284
285 usr = vt8500_read(port, VT8500_URUSR);
286 if (usr & (1 << 4))
287 return TIOCM_CTS;
288 else
289 return 0;
290}
291
292static void vt8500_set_mctrl(struct uart_port *port, unsigned int mctrl)
293{
294}
295
296static void vt8500_break_ctl(struct uart_port *port, int break_ctl)
297{
298 if (break_ctl)
Alexey Charkovae382732014-09-06 21:21:12 +0400299 vt8500_write(port,
300 vt8500_read(port, VT8500_URLCR) | VT8500_BREAK,
Alexey Charkov304e1262010-11-08 20:33:20 +0300301 VT8500_URLCR);
302}
303
304static int vt8500_set_baud_rate(struct uart_port *port, unsigned int baud)
305{
306 unsigned long div;
307 unsigned int loops = 1000;
308
309 div = vt8500_read(port, VT8500_URDIV) & ~(0x3ff);
310
311 if (unlikely((baud < 900) || (baud > 921600)))
312 div |= 7;
313 else
314 div |= (921600 / baud) - 1;
315
316 while ((vt8500_read(port, VT8500_URUSR) & (1 << 5)) && --loops)
317 cpu_relax();
318 vt8500_write(port, div, VT8500_URDIV);
319
320 return baud;
321}
322
323static int vt8500_startup(struct uart_port *port)
324{
325 struct vt8500_port *vt8500_port =
326 container_of(port, struct vt8500_port, uart);
327 int ret;
328
329 snprintf(vt8500_port->name, sizeof(vt8500_port->name),
330 "vt8500_serial%d", port->line);
331
332 ret = request_irq(port->irq, vt8500_irq, IRQF_TRIGGER_HIGH,
333 vt8500_port->name, port);
334 if (unlikely(ret))
335 return ret;
336
337 vt8500_write(port, 0x03, VT8500_URLCR); /* enable TX & RX */
338
339 return 0;
340}
341
342static void vt8500_shutdown(struct uart_port *port)
343{
344 struct vt8500_port *vt8500_port =
345 container_of(port, struct vt8500_port, uart);
346
347 vt8500_port->ier = 0;
348
349 /* disable interrupts and FIFOs */
350 vt8500_write(&vt8500_port->uart, 0, VT8500_URIER);
351 vt8500_write(&vt8500_port->uart, 0x880, VT8500_URFCR);
352 free_irq(port->irq, port);
353}
354
355static void vt8500_set_termios(struct uart_port *port,
356 struct ktermios *termios,
357 struct ktermios *old)
358{
359 struct vt8500_port *vt8500_port =
360 container_of(port, struct vt8500_port, uart);
361 unsigned long flags;
362 unsigned int baud, lcr;
363 unsigned int loops = 1000;
364
365 spin_lock_irqsave(&port->lock, flags);
366
367 /* calculate and set baud rate */
368 baud = uart_get_baud_rate(port, termios, old, 900, 921600);
369 baud = vt8500_set_baud_rate(port, baud);
370 if (tty_termios_baud_rate(termios))
371 tty_termios_encode_baud_rate(termios, baud, baud);
372
373 /* calculate parity */
374 lcr = vt8500_read(&vt8500_port->uart, VT8500_URLCR);
Alexey Charkovae382732014-09-06 21:21:12 +0400375 lcr &= ~(VT8500_PARENB | VT8500_PARODD);
Alexey Charkov304e1262010-11-08 20:33:20 +0300376 if (termios->c_cflag & PARENB) {
Alexey Charkovae382732014-09-06 21:21:12 +0400377 lcr |= VT8500_PARENB;
Alexey Charkov304e1262010-11-08 20:33:20 +0300378 termios->c_cflag &= ~CMSPAR;
379 if (termios->c_cflag & PARODD)
Alexey Charkovae382732014-09-06 21:21:12 +0400380 lcr |= VT8500_PARODD;
Alexey Charkov304e1262010-11-08 20:33:20 +0300381 }
382
383 /* calculate bits per char */
Alexey Charkovae382732014-09-06 21:21:12 +0400384 lcr &= ~VT8500_CS8;
Alexey Charkov304e1262010-11-08 20:33:20 +0300385 switch (termios->c_cflag & CSIZE) {
386 case CS7:
387 break;
388 case CS8:
389 default:
Alexey Charkovae382732014-09-06 21:21:12 +0400390 lcr |= VT8500_CS8;
Alexey Charkov304e1262010-11-08 20:33:20 +0300391 termios->c_cflag &= ~CSIZE;
392 termios->c_cflag |= CS8;
393 break;
394 }
395
396 /* calculate stop bits */
Alexey Charkovae382732014-09-06 21:21:12 +0400397 lcr &= ~VT8500_CSTOPB;
Alexey Charkov304e1262010-11-08 20:33:20 +0300398 if (termios->c_cflag & CSTOPB)
Alexey Charkovae382732014-09-06 21:21:12 +0400399 lcr |= VT8500_CSTOPB;
400
401 lcr &= ~VT8500_SWRTSCTS;
402 if (vt8500_port->vt8500_uart_flags & VT8500_HAS_SWRTSCTS_SWITCH)
403 lcr |= VT8500_SWRTSCTS;
Alexey Charkov304e1262010-11-08 20:33:20 +0300404
405 /* set parity, bits per char, and stop bit */
406 vt8500_write(&vt8500_port->uart, lcr, VT8500_URLCR);
407
408 /* Configure status bits to ignore based on termio flags. */
409 port->read_status_mask = 0;
410 if (termios->c_iflag & IGNPAR)
411 port->read_status_mask = FER | PER;
412
413 uart_update_timeout(port, termios->c_cflag, baud);
414
415 /* Reset FIFOs */
416 vt8500_write(&vt8500_port->uart, 0x88c, VT8500_URFCR);
417 while ((vt8500_read(&vt8500_port->uart, VT8500_URFCR) & 0xc)
418 && --loops)
419 cpu_relax();
420
421 /* Every possible FIFO-related interrupt */
422 vt8500_port->ier = RX_FIFO_INTS | TX_FIFO_INTS;
423
424 /*
425 * CTS flow control
426 */
427 if (UART_ENABLE_MS(&vt8500_port->uart, termios->c_cflag))
428 vt8500_port->ier |= TCTS;
429
430 vt8500_write(&vt8500_port->uart, 0x881, VT8500_URFCR);
431 vt8500_write(&vt8500_port->uart, vt8500_port->ier, VT8500_URIER);
432
433 spin_unlock_irqrestore(&port->lock, flags);
434}
435
436static const char *vt8500_type(struct uart_port *port)
437{
438 struct vt8500_port *vt8500_port =
439 container_of(port, struct vt8500_port, uart);
440 return vt8500_port->name;
441}
442
443static void vt8500_release_port(struct uart_port *port)
444{
445}
446
447static int vt8500_request_port(struct uart_port *port)
448{
449 return 0;
450}
451
452static void vt8500_config_port(struct uart_port *port, int flags)
453{
454 port->type = PORT_VT8500;
455}
456
457static int vt8500_verify_port(struct uart_port *port,
458 struct serial_struct *ser)
459{
460 if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_VT8500))
461 return -EINVAL;
462 if (unlikely(port->irq != ser->irq))
463 return -EINVAL;
464 return 0;
465}
466
Tony Prisk40011302012-08-03 20:56:25 +1200467static struct vt8500_port *vt8500_uart_ports[VT8500_MAX_PORTS];
Alexey Charkov304e1262010-11-08 20:33:20 +0300468static struct uart_driver vt8500_uart_driver;
469
470#ifdef CONFIG_SERIAL_VT8500_CONSOLE
471
472static inline void wait_for_xmitr(struct uart_port *port)
473{
474 unsigned int status, tmout = 10000;
475
476 /* Wait up to 10ms for the character(s) to be sent. */
477 do {
478 status = vt8500_read(port, VT8500_URFIDX);
479
480 if (--tmout == 0)
481 break;
482 udelay(1);
483 } while (status & 0x10);
484}
485
486static void vt8500_console_putchar(struct uart_port *port, int c)
487{
488 wait_for_xmitr(port);
489 writeb(c, port->membase + VT8500_TXFIFO);
490}
491
492static void vt8500_console_write(struct console *co, const char *s,
493 unsigned int count)
494{
495 struct vt8500_port *vt8500_port = vt8500_uart_ports[co->index];
496 unsigned long ier;
497
498 BUG_ON(co->index < 0 || co->index >= vt8500_uart_driver.nr);
499
500 ier = vt8500_read(&vt8500_port->uart, VT8500_URIER);
501 vt8500_write(&vt8500_port->uart, VT8500_URIER, 0);
502
503 uart_console_write(&vt8500_port->uart, s, count,
504 vt8500_console_putchar);
505
506 /*
507 * Finally, wait for transmitter to become empty
508 * and switch back to FIFO
509 */
510 wait_for_xmitr(&vt8500_port->uart);
511 vt8500_write(&vt8500_port->uart, VT8500_URIER, ier);
512}
513
514static int __init vt8500_console_setup(struct console *co, char *options)
515{
516 struct vt8500_port *vt8500_port;
517 int baud = 9600;
518 int bits = 8;
519 int parity = 'n';
520 int flow = 'n';
521
522 if (unlikely(co->index >= vt8500_uart_driver.nr || co->index < 0))
523 return -ENXIO;
524
525 vt8500_port = vt8500_uart_ports[co->index];
526
527 if (!vt8500_port)
528 return -ENODEV;
529
530 if (options)
531 uart_parse_options(options, &baud, &parity, &bits, &flow);
532
533 return uart_set_options(&vt8500_port->uart,
534 co, baud, parity, bits, flow);
535}
536
537static struct console vt8500_console = {
538 .name = "ttyWMT",
539 .write = vt8500_console_write,
540 .device = uart_console_device,
541 .setup = vt8500_console_setup,
542 .flags = CON_PRINTBUFFER,
543 .index = -1,
544 .data = &vt8500_uart_driver,
545};
546
547#define VT8500_CONSOLE (&vt8500_console)
548
549#else
550#define VT8500_CONSOLE NULL
551#endif
552
553static struct uart_ops vt8500_uart_pops = {
554 .tx_empty = vt8500_tx_empty,
555 .set_mctrl = vt8500_set_mctrl,
556 .get_mctrl = vt8500_get_mctrl,
557 .stop_tx = vt8500_stop_tx,
558 .start_tx = vt8500_start_tx,
559 .stop_rx = vt8500_stop_rx,
560 .enable_ms = vt8500_enable_ms,
561 .break_ctl = vt8500_break_ctl,
562 .startup = vt8500_startup,
563 .shutdown = vt8500_shutdown,
564 .set_termios = vt8500_set_termios,
565 .type = vt8500_type,
566 .release_port = vt8500_release_port,
567 .request_port = vt8500_request_port,
568 .config_port = vt8500_config_port,
569 .verify_port = vt8500_verify_port,
570};
571
572static struct uart_driver vt8500_uart_driver = {
573 .owner = THIS_MODULE,
574 .driver_name = "vt8500_serial",
575 .dev_name = "ttyWMT",
576 .nr = 6,
577 .cons = VT8500_CONSOLE,
578};
579
Alexey Charkovae382732014-09-06 21:21:12 +0400580static unsigned int vt8500_flags; /* none required so far */
581static unsigned int wm8880_flags = VT8500_HAS_SWRTSCTS_SWITCH;
582
583static const struct of_device_id wmt_dt_ids[] = {
584 { .compatible = "via,vt8500-uart", .data = &vt8500_flags},
585 { .compatible = "wm,wm8880-uart", .data = &wm8880_flags},
586 {}
587};
588
Bill Pemberton9671f092012-11-19 13:21:50 -0500589static int vt8500_serial_probe(struct platform_device *pdev)
Alexey Charkov304e1262010-11-08 20:33:20 +0300590{
591 struct vt8500_port *vt8500_port;
592 struct resource *mmres, *irqres;
Tony Prisk40011302012-08-03 20:56:25 +1200593 struct device_node *np = pdev->dev.of_node;
Alexey Charkovae382732014-09-06 21:21:12 +0400594 const struct of_device_id *match;
595 const unsigned int *flags;
Alexey Charkov304e1262010-11-08 20:33:20 +0300596 int ret;
Tony Prisk40011302012-08-03 20:56:25 +1200597 int port;
Alexey Charkov304e1262010-11-08 20:33:20 +0300598
Alexey Charkovae382732014-09-06 21:21:12 +0400599 match = of_match_device(wmt_dt_ids, &pdev->dev);
600 if (!match)
601 return -EINVAL;
602
603 flags = match->data;
604
Alexey Charkov304e1262010-11-08 20:33:20 +0300605 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
606 irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
607 if (!mmres || !irqres)
608 return -ENODEV;
609
Roel Kluind969de82013-10-14 23:21:15 +0200610 if (np) {
Tony Prisk40011302012-08-03 20:56:25 +1200611 port = of_alias_get_id(np, "serial");
Tony Prisk27dd2e02013-01-17 08:05:40 +1300612 if (port >= VT8500_MAX_PORTS)
Tony Prisk40011302012-08-03 20:56:25 +1200613 port = -1;
Roel Kluind969de82013-10-14 23:21:15 +0200614 } else {
Tony Prisk40011302012-08-03 20:56:25 +1200615 port = -1;
Roel Kluind969de82013-10-14 23:21:15 +0200616 }
Tony Prisk40011302012-08-03 20:56:25 +1200617
618 if (port < 0) {
619 /* calculate the port id */
620 port = find_first_zero_bit(&vt8500_ports_in_use,
621 sizeof(vt8500_ports_in_use));
622 }
623
Tony Prisk27dd2e02013-01-17 08:05:40 +1300624 if (port >= VT8500_MAX_PORTS)
Tony Prisk40011302012-08-03 20:56:25 +1200625 return -ENODEV;
626
627 /* reserve the port id */
628 if (test_and_set_bit(port, &vt8500_ports_in_use)) {
629 /* port already in use - shouldn't really happen */
630 return -EBUSY;
631 }
632
Tony Prisk49abd902013-01-18 15:05:32 +1300633 vt8500_port = devm_kzalloc(&pdev->dev, sizeof(struct vt8500_port),
634 GFP_KERNEL);
Wei Yongjun59c2e852012-10-08 10:35:46 +0800635 if (!vt8500_port)
636 return -ENOMEM;
637
Sachin Kamat82b23132013-03-04 14:24:39 +0530638 vt8500_port->uart.membase = devm_ioremap_resource(&pdev->dev, mmres);
639 if (IS_ERR(vt8500_port->uart.membase))
640 return PTR_ERR(vt8500_port->uart.membase);
Tony Prisk12faa352013-01-18 15:05:31 +1300641
642 vt8500_port->clk = of_clk_get(pdev->dev.of_node, 0);
643 if (IS_ERR(vt8500_port->clk)) {
644 dev_err(&pdev->dev, "failed to get clock\n");
Tony Prisk49abd902013-01-18 15:05:32 +1300645 return -EINVAL;
Tony Prisk12faa352013-01-18 15:05:31 +1300646 }
647
648 ret = clk_prepare_enable(vt8500_port->clk);
649 if (ret) {
650 dev_err(&pdev->dev, "failed to enable clock\n");
Tony Prisk49abd902013-01-18 15:05:32 +1300651 return ret;
Tony Prisk12faa352013-01-18 15:05:31 +1300652 }
653
Alexey Charkovae382732014-09-06 21:21:12 +0400654 vt8500_port->vt8500_uart_flags = *flags;
Alexey Charkov304e1262010-11-08 20:33:20 +0300655 vt8500_port->uart.type = PORT_VT8500;
656 vt8500_port->uart.iotype = UPIO_MEM;
657 vt8500_port->uart.mapbase = mmres->start;
658 vt8500_port->uart.irq = irqres->start;
659 vt8500_port->uart.fifosize = 16;
660 vt8500_port->uart.ops = &vt8500_uart_pops;
Tony Prisk40011302012-08-03 20:56:25 +1200661 vt8500_port->uart.line = port;
Alexey Charkov304e1262010-11-08 20:33:20 +0300662 vt8500_port->uart.dev = &pdev->dev;
663 vt8500_port->uart.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
Tony Prisk40011302012-08-03 20:56:25 +1200664
Tony Prisk5771a802013-03-09 18:44:37 +1300665 vt8500_port->uart.uartclk = clk_get_rate(vt8500_port->clk);
Alexey Charkov304e1262010-11-08 20:33:20 +0300666
667 snprintf(vt8500_port->name, sizeof(vt8500_port->name),
668 "VT8500 UART%d", pdev->id);
669
Tony Prisk40011302012-08-03 20:56:25 +1200670 vt8500_uart_ports[port] = vt8500_port;
Alexey Charkov304e1262010-11-08 20:33:20 +0300671
672 uart_add_one_port(&vt8500_uart_driver, &vt8500_port->uart);
673
674 platform_set_drvdata(pdev, vt8500_port);
675
676 return 0;
Alexey Charkov304e1262010-11-08 20:33:20 +0300677}
678
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500679static int vt8500_serial_remove(struct platform_device *pdev)
Alexey Charkov304e1262010-11-08 20:33:20 +0300680{
681 struct vt8500_port *vt8500_port = platform_get_drvdata(pdev);
682
Tony Prisk12faa352013-01-18 15:05:31 +1300683 clk_disable_unprepare(vt8500_port->clk);
Alexey Charkov304e1262010-11-08 20:33:20 +0300684 uart_remove_one_port(&vt8500_uart_driver, &vt8500_port->uart);
Alexey Charkov304e1262010-11-08 20:33:20 +0300685
686 return 0;
687}
688
689static struct platform_driver vt8500_platform_driver = {
690 .probe = vt8500_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500691 .remove = vt8500_serial_remove,
Alexey Charkov304e1262010-11-08 20:33:20 +0300692 .driver = {
693 .name = "vt8500_serial",
694 .owner = THIS_MODULE,
Sachin Kamat86c346d2013-05-22 17:06:29 +0530695 .of_match_table = wmt_dt_ids,
Alexey Charkov304e1262010-11-08 20:33:20 +0300696 },
697};
698
699static int __init vt8500_serial_init(void)
700{
701 int ret;
702
703 ret = uart_register_driver(&vt8500_uart_driver);
704 if (unlikely(ret))
705 return ret;
706
707 ret = platform_driver_register(&vt8500_platform_driver);
708
709 if (unlikely(ret))
710 uart_unregister_driver(&vt8500_uart_driver);
711
712 return ret;
713}
714
715static void __exit vt8500_serial_exit(void)
716{
717#ifdef CONFIG_SERIAL_VT8500_CONSOLE
718 unregister_console(&vt8500_console);
719#endif
720 platform_driver_unregister(&vt8500_platform_driver);
721 uart_unregister_driver(&vt8500_uart_driver);
722}
723
724module_init(vt8500_serial_init);
725module_exit(vt8500_serial_exit);
726
727MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
728MODULE_DESCRIPTION("Driver for vt8500 serial device");
Tony Prisk40011302012-08-03 20:56:25 +1200729MODULE_LICENSE("GPL v2");