blob: 47e74f9b01aedc9e8ff0bf876f60e9daecc584d9 [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{
Alexey Charkov8c986d32014-09-06 21:21:13 +0400294 unsigned int lcr = vt8500_read(port, VT8500_URLCR);
295
296 if (mctrl & TIOCM_RTS)
297 lcr |= VT8500_RTS;
298 else
299 lcr &= ~VT8500_RTS;
300
301 vt8500_write(port, lcr, VT8500_URLCR);
Alexey Charkov304e1262010-11-08 20:33:20 +0300302}
303
304static void vt8500_break_ctl(struct uart_port *port, int break_ctl)
305{
306 if (break_ctl)
Alexey Charkovae382732014-09-06 21:21:12 +0400307 vt8500_write(port,
308 vt8500_read(port, VT8500_URLCR) | VT8500_BREAK,
Alexey Charkov304e1262010-11-08 20:33:20 +0300309 VT8500_URLCR);
310}
311
312static int vt8500_set_baud_rate(struct uart_port *port, unsigned int baud)
313{
314 unsigned long div;
315 unsigned int loops = 1000;
316
317 div = vt8500_read(port, VT8500_URDIV) & ~(0x3ff);
318
319 if (unlikely((baud < 900) || (baud > 921600)))
320 div |= 7;
321 else
322 div |= (921600 / baud) - 1;
323
324 while ((vt8500_read(port, VT8500_URUSR) & (1 << 5)) && --loops)
325 cpu_relax();
326 vt8500_write(port, div, VT8500_URDIV);
327
328 return baud;
329}
330
331static int vt8500_startup(struct uart_port *port)
332{
333 struct vt8500_port *vt8500_port =
334 container_of(port, struct vt8500_port, uart);
335 int ret;
336
337 snprintf(vt8500_port->name, sizeof(vt8500_port->name),
338 "vt8500_serial%d", port->line);
339
340 ret = request_irq(port->irq, vt8500_irq, IRQF_TRIGGER_HIGH,
341 vt8500_port->name, port);
342 if (unlikely(ret))
343 return ret;
344
345 vt8500_write(port, 0x03, VT8500_URLCR); /* enable TX & RX */
346
347 return 0;
348}
349
350static void vt8500_shutdown(struct uart_port *port)
351{
352 struct vt8500_port *vt8500_port =
353 container_of(port, struct vt8500_port, uart);
354
355 vt8500_port->ier = 0;
356
357 /* disable interrupts and FIFOs */
358 vt8500_write(&vt8500_port->uart, 0, VT8500_URIER);
359 vt8500_write(&vt8500_port->uart, 0x880, VT8500_URFCR);
360 free_irq(port->irq, port);
361}
362
363static void vt8500_set_termios(struct uart_port *port,
364 struct ktermios *termios,
365 struct ktermios *old)
366{
367 struct vt8500_port *vt8500_port =
368 container_of(port, struct vt8500_port, uart);
369 unsigned long flags;
370 unsigned int baud, lcr;
371 unsigned int loops = 1000;
372
373 spin_lock_irqsave(&port->lock, flags);
374
375 /* calculate and set baud rate */
376 baud = uart_get_baud_rate(port, termios, old, 900, 921600);
377 baud = vt8500_set_baud_rate(port, baud);
378 if (tty_termios_baud_rate(termios))
379 tty_termios_encode_baud_rate(termios, baud, baud);
380
381 /* calculate parity */
382 lcr = vt8500_read(&vt8500_port->uart, VT8500_URLCR);
Alexey Charkovae382732014-09-06 21:21:12 +0400383 lcr &= ~(VT8500_PARENB | VT8500_PARODD);
Alexey Charkov304e1262010-11-08 20:33:20 +0300384 if (termios->c_cflag & PARENB) {
Alexey Charkovae382732014-09-06 21:21:12 +0400385 lcr |= VT8500_PARENB;
Alexey Charkov304e1262010-11-08 20:33:20 +0300386 termios->c_cflag &= ~CMSPAR;
387 if (termios->c_cflag & PARODD)
Alexey Charkovae382732014-09-06 21:21:12 +0400388 lcr |= VT8500_PARODD;
Alexey Charkov304e1262010-11-08 20:33:20 +0300389 }
390
391 /* calculate bits per char */
Alexey Charkovae382732014-09-06 21:21:12 +0400392 lcr &= ~VT8500_CS8;
Alexey Charkov304e1262010-11-08 20:33:20 +0300393 switch (termios->c_cflag & CSIZE) {
394 case CS7:
395 break;
396 case CS8:
397 default:
Alexey Charkovae382732014-09-06 21:21:12 +0400398 lcr |= VT8500_CS8;
Alexey Charkov304e1262010-11-08 20:33:20 +0300399 termios->c_cflag &= ~CSIZE;
400 termios->c_cflag |= CS8;
401 break;
402 }
403
404 /* calculate stop bits */
Alexey Charkovae382732014-09-06 21:21:12 +0400405 lcr &= ~VT8500_CSTOPB;
Alexey Charkov304e1262010-11-08 20:33:20 +0300406 if (termios->c_cflag & CSTOPB)
Alexey Charkovae382732014-09-06 21:21:12 +0400407 lcr |= VT8500_CSTOPB;
408
409 lcr &= ~VT8500_SWRTSCTS;
410 if (vt8500_port->vt8500_uart_flags & VT8500_HAS_SWRTSCTS_SWITCH)
411 lcr |= VT8500_SWRTSCTS;
Alexey Charkov304e1262010-11-08 20:33:20 +0300412
413 /* set parity, bits per char, and stop bit */
414 vt8500_write(&vt8500_port->uart, lcr, VT8500_URLCR);
415
416 /* Configure status bits to ignore based on termio flags. */
417 port->read_status_mask = 0;
418 if (termios->c_iflag & IGNPAR)
419 port->read_status_mask = FER | PER;
420
421 uart_update_timeout(port, termios->c_cflag, baud);
422
423 /* Reset FIFOs */
424 vt8500_write(&vt8500_port->uart, 0x88c, VT8500_URFCR);
425 while ((vt8500_read(&vt8500_port->uart, VT8500_URFCR) & 0xc)
426 && --loops)
427 cpu_relax();
428
429 /* Every possible FIFO-related interrupt */
430 vt8500_port->ier = RX_FIFO_INTS | TX_FIFO_INTS;
431
432 /*
433 * CTS flow control
434 */
435 if (UART_ENABLE_MS(&vt8500_port->uart, termios->c_cflag))
436 vt8500_port->ier |= TCTS;
437
438 vt8500_write(&vt8500_port->uart, 0x881, VT8500_URFCR);
439 vt8500_write(&vt8500_port->uart, vt8500_port->ier, VT8500_URIER);
440
441 spin_unlock_irqrestore(&port->lock, flags);
442}
443
444static const char *vt8500_type(struct uart_port *port)
445{
446 struct vt8500_port *vt8500_port =
447 container_of(port, struct vt8500_port, uart);
448 return vt8500_port->name;
449}
450
451static void vt8500_release_port(struct uart_port *port)
452{
453}
454
455static int vt8500_request_port(struct uart_port *port)
456{
457 return 0;
458}
459
460static void vt8500_config_port(struct uart_port *port, int flags)
461{
462 port->type = PORT_VT8500;
463}
464
465static int vt8500_verify_port(struct uart_port *port,
466 struct serial_struct *ser)
467{
468 if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_VT8500))
469 return -EINVAL;
470 if (unlikely(port->irq != ser->irq))
471 return -EINVAL;
472 return 0;
473}
474
Tony Prisk40011302012-08-03 20:56:25 +1200475static struct vt8500_port *vt8500_uart_ports[VT8500_MAX_PORTS];
Alexey Charkov304e1262010-11-08 20:33:20 +0300476static struct uart_driver vt8500_uart_driver;
477
478#ifdef CONFIG_SERIAL_VT8500_CONSOLE
479
480static inline void wait_for_xmitr(struct uart_port *port)
481{
482 unsigned int status, tmout = 10000;
483
484 /* Wait up to 10ms for the character(s) to be sent. */
485 do {
486 status = vt8500_read(port, VT8500_URFIDX);
487
488 if (--tmout == 0)
489 break;
490 udelay(1);
491 } while (status & 0x10);
492}
493
494static void vt8500_console_putchar(struct uart_port *port, int c)
495{
496 wait_for_xmitr(port);
497 writeb(c, port->membase + VT8500_TXFIFO);
498}
499
500static void vt8500_console_write(struct console *co, const char *s,
501 unsigned int count)
502{
503 struct vt8500_port *vt8500_port = vt8500_uart_ports[co->index];
504 unsigned long ier;
505
506 BUG_ON(co->index < 0 || co->index >= vt8500_uart_driver.nr);
507
508 ier = vt8500_read(&vt8500_port->uart, VT8500_URIER);
509 vt8500_write(&vt8500_port->uart, VT8500_URIER, 0);
510
511 uart_console_write(&vt8500_port->uart, s, count,
512 vt8500_console_putchar);
513
514 /*
515 * Finally, wait for transmitter to become empty
516 * and switch back to FIFO
517 */
518 wait_for_xmitr(&vt8500_port->uart);
519 vt8500_write(&vt8500_port->uart, VT8500_URIER, ier);
520}
521
522static int __init vt8500_console_setup(struct console *co, char *options)
523{
524 struct vt8500_port *vt8500_port;
525 int baud = 9600;
526 int bits = 8;
527 int parity = 'n';
528 int flow = 'n';
529
530 if (unlikely(co->index >= vt8500_uart_driver.nr || co->index < 0))
531 return -ENXIO;
532
533 vt8500_port = vt8500_uart_ports[co->index];
534
535 if (!vt8500_port)
536 return -ENODEV;
537
538 if (options)
539 uart_parse_options(options, &baud, &parity, &bits, &flow);
540
541 return uart_set_options(&vt8500_port->uart,
542 co, baud, parity, bits, flow);
543}
544
545static struct console vt8500_console = {
546 .name = "ttyWMT",
547 .write = vt8500_console_write,
548 .device = uart_console_device,
549 .setup = vt8500_console_setup,
550 .flags = CON_PRINTBUFFER,
551 .index = -1,
552 .data = &vt8500_uart_driver,
553};
554
555#define VT8500_CONSOLE (&vt8500_console)
556
557#else
558#define VT8500_CONSOLE NULL
559#endif
560
561static struct uart_ops vt8500_uart_pops = {
562 .tx_empty = vt8500_tx_empty,
563 .set_mctrl = vt8500_set_mctrl,
564 .get_mctrl = vt8500_get_mctrl,
565 .stop_tx = vt8500_stop_tx,
566 .start_tx = vt8500_start_tx,
567 .stop_rx = vt8500_stop_rx,
568 .enable_ms = vt8500_enable_ms,
569 .break_ctl = vt8500_break_ctl,
570 .startup = vt8500_startup,
571 .shutdown = vt8500_shutdown,
572 .set_termios = vt8500_set_termios,
573 .type = vt8500_type,
574 .release_port = vt8500_release_port,
575 .request_port = vt8500_request_port,
576 .config_port = vt8500_config_port,
577 .verify_port = vt8500_verify_port,
578};
579
580static struct uart_driver vt8500_uart_driver = {
581 .owner = THIS_MODULE,
582 .driver_name = "vt8500_serial",
583 .dev_name = "ttyWMT",
584 .nr = 6,
585 .cons = VT8500_CONSOLE,
586};
587
Alexey Charkovae382732014-09-06 21:21:12 +0400588static unsigned int vt8500_flags; /* none required so far */
589static unsigned int wm8880_flags = VT8500_HAS_SWRTSCTS_SWITCH;
590
591static const struct of_device_id wmt_dt_ids[] = {
592 { .compatible = "via,vt8500-uart", .data = &vt8500_flags},
593 { .compatible = "wm,wm8880-uart", .data = &wm8880_flags},
594 {}
595};
596
Bill Pemberton9671f092012-11-19 13:21:50 -0500597static int vt8500_serial_probe(struct platform_device *pdev)
Alexey Charkov304e1262010-11-08 20:33:20 +0300598{
599 struct vt8500_port *vt8500_port;
600 struct resource *mmres, *irqres;
Tony Prisk40011302012-08-03 20:56:25 +1200601 struct device_node *np = pdev->dev.of_node;
Alexey Charkovae382732014-09-06 21:21:12 +0400602 const struct of_device_id *match;
603 const unsigned int *flags;
Alexey Charkov304e1262010-11-08 20:33:20 +0300604 int ret;
Tony Prisk40011302012-08-03 20:56:25 +1200605 int port;
Alexey Charkov304e1262010-11-08 20:33:20 +0300606
Alexey Charkovae382732014-09-06 21:21:12 +0400607 match = of_match_device(wmt_dt_ids, &pdev->dev);
608 if (!match)
609 return -EINVAL;
610
611 flags = match->data;
612
Alexey Charkov304e1262010-11-08 20:33:20 +0300613 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
614 irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
615 if (!mmres || !irqres)
616 return -ENODEV;
617
Roel Kluind969de82013-10-14 23:21:15 +0200618 if (np) {
Tony Prisk40011302012-08-03 20:56:25 +1200619 port = of_alias_get_id(np, "serial");
Tony Prisk27dd2e02013-01-17 08:05:40 +1300620 if (port >= VT8500_MAX_PORTS)
Tony Prisk40011302012-08-03 20:56:25 +1200621 port = -1;
Roel Kluind969de82013-10-14 23:21:15 +0200622 } else {
Tony Prisk40011302012-08-03 20:56:25 +1200623 port = -1;
Roel Kluind969de82013-10-14 23:21:15 +0200624 }
Tony Prisk40011302012-08-03 20:56:25 +1200625
626 if (port < 0) {
627 /* calculate the port id */
628 port = find_first_zero_bit(&vt8500_ports_in_use,
629 sizeof(vt8500_ports_in_use));
630 }
631
Tony Prisk27dd2e02013-01-17 08:05:40 +1300632 if (port >= VT8500_MAX_PORTS)
Tony Prisk40011302012-08-03 20:56:25 +1200633 return -ENODEV;
634
635 /* reserve the port id */
636 if (test_and_set_bit(port, &vt8500_ports_in_use)) {
637 /* port already in use - shouldn't really happen */
638 return -EBUSY;
639 }
640
Tony Prisk49abd902013-01-18 15:05:32 +1300641 vt8500_port = devm_kzalloc(&pdev->dev, sizeof(struct vt8500_port),
642 GFP_KERNEL);
Wei Yongjun59c2e852012-10-08 10:35:46 +0800643 if (!vt8500_port)
644 return -ENOMEM;
645
Sachin Kamat82b23132013-03-04 14:24:39 +0530646 vt8500_port->uart.membase = devm_ioremap_resource(&pdev->dev, mmres);
647 if (IS_ERR(vt8500_port->uart.membase))
648 return PTR_ERR(vt8500_port->uart.membase);
Tony Prisk12faa352013-01-18 15:05:31 +1300649
650 vt8500_port->clk = of_clk_get(pdev->dev.of_node, 0);
651 if (IS_ERR(vt8500_port->clk)) {
652 dev_err(&pdev->dev, "failed to get clock\n");
Tony Prisk49abd902013-01-18 15:05:32 +1300653 return -EINVAL;
Tony Prisk12faa352013-01-18 15:05:31 +1300654 }
655
656 ret = clk_prepare_enable(vt8500_port->clk);
657 if (ret) {
658 dev_err(&pdev->dev, "failed to enable clock\n");
Tony Prisk49abd902013-01-18 15:05:32 +1300659 return ret;
Tony Prisk12faa352013-01-18 15:05:31 +1300660 }
661
Alexey Charkovae382732014-09-06 21:21:12 +0400662 vt8500_port->vt8500_uart_flags = *flags;
Alexey Charkov304e1262010-11-08 20:33:20 +0300663 vt8500_port->uart.type = PORT_VT8500;
664 vt8500_port->uart.iotype = UPIO_MEM;
665 vt8500_port->uart.mapbase = mmres->start;
666 vt8500_port->uart.irq = irqres->start;
667 vt8500_port->uart.fifosize = 16;
668 vt8500_port->uart.ops = &vt8500_uart_pops;
Tony Prisk40011302012-08-03 20:56:25 +1200669 vt8500_port->uart.line = port;
Alexey Charkov304e1262010-11-08 20:33:20 +0300670 vt8500_port->uart.dev = &pdev->dev;
671 vt8500_port->uart.flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
Tony Prisk40011302012-08-03 20:56:25 +1200672
Tony Prisk5771a802013-03-09 18:44:37 +1300673 vt8500_port->uart.uartclk = clk_get_rate(vt8500_port->clk);
Alexey Charkov304e1262010-11-08 20:33:20 +0300674
675 snprintf(vt8500_port->name, sizeof(vt8500_port->name),
676 "VT8500 UART%d", pdev->id);
677
Tony Prisk40011302012-08-03 20:56:25 +1200678 vt8500_uart_ports[port] = vt8500_port;
Alexey Charkov304e1262010-11-08 20:33:20 +0300679
680 uart_add_one_port(&vt8500_uart_driver, &vt8500_port->uart);
681
682 platform_set_drvdata(pdev, vt8500_port);
683
684 return 0;
Alexey Charkov304e1262010-11-08 20:33:20 +0300685}
686
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500687static int vt8500_serial_remove(struct platform_device *pdev)
Alexey Charkov304e1262010-11-08 20:33:20 +0300688{
689 struct vt8500_port *vt8500_port = platform_get_drvdata(pdev);
690
Tony Prisk12faa352013-01-18 15:05:31 +1300691 clk_disable_unprepare(vt8500_port->clk);
Alexey Charkov304e1262010-11-08 20:33:20 +0300692 uart_remove_one_port(&vt8500_uart_driver, &vt8500_port->uart);
Alexey Charkov304e1262010-11-08 20:33:20 +0300693
694 return 0;
695}
696
697static struct platform_driver vt8500_platform_driver = {
698 .probe = vt8500_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500699 .remove = vt8500_serial_remove,
Alexey Charkov304e1262010-11-08 20:33:20 +0300700 .driver = {
701 .name = "vt8500_serial",
702 .owner = THIS_MODULE,
Sachin Kamat86c346d2013-05-22 17:06:29 +0530703 .of_match_table = wmt_dt_ids,
Alexey Charkov304e1262010-11-08 20:33:20 +0300704 },
705};
706
707static int __init vt8500_serial_init(void)
708{
709 int ret;
710
711 ret = uart_register_driver(&vt8500_uart_driver);
712 if (unlikely(ret))
713 return ret;
714
715 ret = platform_driver_register(&vt8500_platform_driver);
716
717 if (unlikely(ret))
718 uart_unregister_driver(&vt8500_uart_driver);
719
720 return ret;
721}
722
723static void __exit vt8500_serial_exit(void)
724{
725#ifdef CONFIG_SERIAL_VT8500_CONSOLE
726 unregister_console(&vt8500_console);
727#endif
728 platform_driver_unregister(&vt8500_platform_driver);
729 uart_unregister_driver(&vt8500_uart_driver);
730}
731
732module_init(vt8500_serial_init);
733module_exit(vt8500_serial_exit);
734
735MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
736MODULE_DESCRIPTION("Driver for vt8500 serial device");
Tony Prisk40011302012-08-03 20:56:25 +1200737MODULE_LICENSE("GPL v2");