blob: b0eacb83f831ec5f3e526ea31b4a93dfa75f8ecf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for CLPS711x serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
16#define SUPPORT_SYSRQ
17#endif
18
19#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/device.h>
Alexander Shiyana1c25f22012-10-14 11:05:34 +040021#include <linux/console.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/serial_core.h>
23#include <linux/serial.h>
Alexander Shiyanc08f0152012-10-14 11:05:26 +040024#include <linux/clk.h>
Alexander Shiyanbc000242013-12-11 19:50:50 +040025#include <linux/io.h>
Alexander Shiyana1c25f22012-10-14 11:05:34 +040026#include <linux/tty.h>
27#include <linux/tty_flip.h>
28#include <linux/ioport.h>
Alexander Shiyanbc000242013-12-11 19:50:50 +040029#include <linux/of.h>
Alexander Shiyan95113722012-10-14 11:05:23 +040030#include <linux/platform_device.h>
Alexander Shiyanbc000242013-12-11 19:50:50 +040031#include <linux/regmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Alexander Shiyanbc000242013-12-11 19:50:50 +040033#include <linux/mfd/syscon.h>
34#include <linux/mfd/syscon/clps711x.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Alexander Shiyanbc000242013-12-11 19:50:50 +040036#define UART_CLPS711X_DEVNAME "ttyCL"
Alexander Shiyan117d5d42012-10-14 11:05:24 +040037#define UART_CLPS711X_NR 2
38#define UART_CLPS711X_MAJOR 204
39#define UART_CLPS711X_MINOR 40
Alexander Shiyan95113722012-10-14 11:05:23 +040040
Alexander Shiyanbc000242013-12-11 19:50:50 +040041#define UARTDR_OFFSET (0x00)
42#define UBRLCR_OFFSET (0x40)
43
44#define UARTDR_FRMERR (1 << 8)
45#define UARTDR_PARERR (1 << 9)
46#define UARTDR_OVERR (1 << 10)
47
48#define UBRLCR_BAUD_MASK ((1 << 12) - 1)
49#define UBRLCR_BREAK (1 << 12)
50#define UBRLCR_PRTEN (1 << 13)
51#define UBRLCR_EVENPRT (1 << 14)
52#define UBRLCR_XSTOP (1 << 15)
53#define UBRLCR_FIFOEN (1 << 16)
54#define UBRLCR_WRDLEN5 (0 << 17)
55#define UBRLCR_WRDLEN6 (1 << 17)
56#define UBRLCR_WRDLEN7 (2 << 17)
57#define UBRLCR_WRDLEN8 (3 << 17)
58#define UBRLCR_WRDLEN_MASK (3 << 17)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Alexander Shiyan117d5d42012-10-14 11:05:24 +040060struct clps711x_port {
Alexander Shiyanbc000242013-12-11 19:50:50 +040061 struct uart_port port;
62 unsigned int tx_enabled;
63 int rx_irq;
64 struct regmap *syscon;
65 bool use_ms;
66};
67
68static struct uart_driver clps711x_uart = {
69 .owner = THIS_MODULE,
70 .driver_name = UART_CLPS711X_DEVNAME,
71 .dev_name = UART_CLPS711X_DEVNAME,
72 .major = UART_CLPS711X_MAJOR,
73 .minor = UART_CLPS711X_MINOR,
74 .nr = UART_CLPS711X_NR,
Alexander Shiyan117d5d42012-10-14 11:05:24 +040075};
76
Alexander Shiyana1c25f22012-10-14 11:05:34 +040077static void uart_clps711x_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040079 struct clps711x_port *s = dev_get_drvdata(port->dev);
80
Alexander Shiyanbc000242013-12-11 19:50:50 +040081 if (s->tx_enabled) {
82 disable_irq(port->irq);
83 s->tx_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85}
86
Alexander Shiyana1c25f22012-10-14 11:05:34 +040087static void uart_clps711x_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040089 struct clps711x_port *s = dev_get_drvdata(port->dev);
90
Alexander Shiyanbc000242013-12-11 19:50:50 +040091 if (!s->tx_enabled) {
92 s->tx_enabled = 1;
93 enable_irq(port->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95}
96
Alexander Shiyan135cc792012-10-14 11:05:31 +040097static irqreturn_t uart_clps711x_int_rx(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct uart_port *port = dev_id;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400100 struct clps711x_port *s = dev_get_drvdata(port->dev);
101 unsigned int status, flg;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400102 u16 ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Alexander Shiyanf27de952012-10-14 11:05:30 +0400104 for (;;) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400105 u32 sysflg = 0;
106
Alexander Shiyanbc000242013-12-11 19:50:50 +0400107 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
108 if (sysflg & SYSFLG_URXFE)
Alexander Shiyanf27de952012-10-14 11:05:30 +0400109 break;
110
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400111 ch = readw(port->membase + UARTDR_OFFSET);
Alexander Shiyanf27de952012-10-14 11:05:30 +0400112 status = ch & (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR);
113 ch &= 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 port->icount.rx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 flg = TTY_NORMAL;
117
Alexander Shiyanf27de952012-10-14 11:05:30 +0400118 if (unlikely(status)) {
119 if (status & UARTDR_PARERR)
Russell King2a9604b2005-04-26 15:32:00 +0100120 port->icount.parity++;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400121 else if (status & UARTDR_FRMERR)
Russell King2a9604b2005-04-26 15:32:00 +0100122 port->icount.frame++;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400123 else if (status & UARTDR_OVERR)
Russell King2a9604b2005-04-26 15:32:00 +0100124 port->icount.overrun++;
125
Alexander Shiyanf27de952012-10-14 11:05:30 +0400126 status &= port->read_status_mask;
Russell King2a9604b2005-04-26 15:32:00 +0100127
Alexander Shiyanf27de952012-10-14 11:05:30 +0400128 if (status & UARTDR_PARERR)
Russell King2a9604b2005-04-26 15:32:00 +0100129 flg = TTY_PARITY;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400130 else if (status & UARTDR_FRMERR)
Russell King2a9604b2005-04-26 15:32:00 +0100131 flg = TTY_FRAME;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400132 else if (status & UARTDR_OVERR)
133 flg = TTY_OVERRUN;
Russell King2a9604b2005-04-26 15:32:00 +0100134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
David Howells7d12e782006-10-05 14:55:46 +0100136 if (uart_handle_sysrq_char(port, ch))
Alexander Shiyanf27de952012-10-14 11:05:30 +0400137 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Alexander Shiyanf27de952012-10-14 11:05:30 +0400139 if (status & port->ignore_status_mask)
140 continue;
Russell King2a9604b2005-04-26 15:32:00 +0100141
Alexander Shiyanf27de952012-10-14 11:05:30 +0400142 uart_insert_char(port, status, UARTDR_OVERR, ch, flg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Alexander Shiyanf27de952012-10-14 11:05:30 +0400144
Jiri Slaby2e124b42013-01-03 15:53:06 +0100145 tty_flip_buffer_push(&port->state->port);
Alexander Shiyanf27de952012-10-14 11:05:30 +0400146
Russell King2a9604b2005-04-26 15:32:00 +0100147 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Alexander Shiyan135cc792012-10-14 11:05:31 +0400150static irqreturn_t uart_clps711x_int_tx(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct uart_port *port = dev_id;
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400153 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700154 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (port->x_char) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400157 writew(port->x_char, port->membase + UARTDR_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 port->icount.tx++;
159 port->x_char = 0;
160 return IRQ_HANDLED;
161 }
Alexander Shiyan7a6fbc92012-03-27 12:22:49 +0400162
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400163 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Alexander Shiyanbc000242013-12-11 19:50:50 +0400164 if (s->tx_enabled) {
165 disable_irq_nosync(port->irq);
166 s->tx_enabled = 0;
167 }
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400168 return IRQ_HANDLED;
169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Alexander Shiyancf03a882012-10-14 11:05:27 +0400171 while (!uart_circ_empty(xmit)) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400172 u32 sysflg = 0;
173
174 writew(xmit->buf[xmit->tail], port->membase + UARTDR_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
176 port->icount.tx++;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400177
178 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
179 if (sysflg & SYSFLG_UTXFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 break;
Alexander Shiyancf03a882012-10-14 11:05:27 +0400181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
184 uart_write_wakeup(port);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return IRQ_HANDLED;
187}
188
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400189static unsigned int uart_clps711x_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400191 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400192 u32 sysflg = 0;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400193
194 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
195
196 return (sysflg & SYSFLG_UBUSY) ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400199static unsigned int uart_clps711x_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400201 struct clps711x_port *s = dev_get_drvdata(port->dev);
202 unsigned int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Alexander Shiyanbc000242013-12-11 19:50:50 +0400204 if (s->use_ms) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400205 u32 sysflg = 0;
206
Alexander Shiyanbc000242013-12-11 19:50:50 +0400207 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
208 if (sysflg & SYSFLG1_DCD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 result |= TIOCM_CAR;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400210 if (sysflg & SYSFLG1_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 result |= TIOCM_DSR;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400212 if (sysflg & SYSFLG1_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 result |= TIOCM_CTS;
Alexander Shiyan1593daf2012-10-14 11:05:28 +0400214 } else
215 result = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 return result;
218}
219
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400220static void uart_clps711x_set_mctrl(struct uart_port *port, unsigned int mctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400222 /* Do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400225static void uart_clps711x_break_ctl(struct uart_port *port, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 unsigned int ubrlcr;
228
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400229 ubrlcr = readl(port->membase + UBRLCR_OFFSET);
Alexander Shiyanec335522012-10-14 11:05:29 +0400230 if (break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 ubrlcr |= UBRLCR_BREAK;
232 else
233 ubrlcr &= ~UBRLCR_BREAK;
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400234 writel(ubrlcr, port->membase + UBRLCR_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Alexander Shiyan71b9e8c2013-12-31 20:49:41 +0400237static void uart_clps711x_set_ldisc(struct uart_port *port, int ld)
238{
239 if (!port->line) {
240 struct clps711x_port *s = dev_get_drvdata(port->dev);
241
242 regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON1_SIREN,
243 (ld == N_IRDA) ? SYSCON1_SIREN : 0);
244 }
245}
246
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400247static int uart_clps711x_startup(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400249 struct clps711x_port *s = dev_get_drvdata(port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Alexander Shiyanf52ede22012-10-14 11:05:32 +0400251 /* Disable break */
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400252 writel(readl(port->membase + UBRLCR_OFFSET) & ~UBRLCR_BREAK,
253 port->membase + UBRLCR_OFFSET);
Alexander Shiyanf52ede22012-10-14 11:05:32 +0400254
255 /* Enable the port */
Alexander Shiyanbc000242013-12-11 19:50:50 +0400256 return regmap_update_bits(s->syscon, SYSCON_OFFSET,
257 SYSCON_UARTEN, SYSCON_UARTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400260static void uart_clps711x_shutdown(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400262 struct clps711x_port *s = dev_get_drvdata(port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Alexander Shiyanf52ede22012-10-14 11:05:32 +0400264 /* Disable the port */
Alexander Shiyanbc000242013-12-11 19:50:50 +0400265 regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON_UARTEN, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400268static void uart_clps711x_set_termios(struct uart_port *port,
269 struct ktermios *termios,
270 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400272 u32 ubrlcr;
273 unsigned int baud, quot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400275 /* Mask termios capabilities we don't support */
276 termios->c_cflag &= ~CMSPAR;
277 termios->c_iflag &= ~(BRKINT | IGNBRK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400279 /* Ask the core to calculate the divisor for us */
280 baud = uart_get_baud_rate(port, termios, old, port->uartclk / 4096,
281 port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 quot = uart_get_divisor(port, baud);
283
284 switch (termios->c_cflag & CSIZE) {
285 case CS5:
286 ubrlcr = UBRLCR_WRDLEN5;
287 break;
288 case CS6:
289 ubrlcr = UBRLCR_WRDLEN6;
290 break;
291 case CS7:
292 ubrlcr = UBRLCR_WRDLEN7;
293 break;
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400294 case CS8:
295 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 ubrlcr = UBRLCR_WRDLEN8;
297 break;
298 }
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (termios->c_cflag & CSTOPB)
301 ubrlcr |= UBRLCR_XSTOP;
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (termios->c_cflag & PARENB) {
304 ubrlcr |= UBRLCR_PRTEN;
305 if (!(termios->c_cflag & PARODD))
306 ubrlcr |= UBRLCR_EVENPRT;
307 }
Alexander Shiyancf03a882012-10-14 11:05:27 +0400308
309 /* Enable FIFO */
310 ubrlcr |= UBRLCR_FIFOEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400312 /* Set read status mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 port->read_status_mask = UARTDR_OVERR;
314 if (termios->c_iflag & INPCK)
315 port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
316
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400317 /* Set status ignore mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 port->ignore_status_mask = 0;
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400319 if (!(termios->c_cflag & CREAD))
320 port->ignore_status_mask |= UARTDR_OVERR | UARTDR_PARERR |
321 UARTDR_FRMERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400323 uart_update_timeout(port, termios->c_cflag, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400325 writel(ubrlcr | (quot - 1), port->membase + UBRLCR_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400328static const char *uart_clps711x_type(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400330 return (port->type == PORT_CLPS711X) ? "CLPS711X" : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400333static void uart_clps711x_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 if (flags & UART_CONFIG_TYPE)
336 port->type = PORT_CLPS711X;
337}
338
Alexander Shiyanbc000242013-12-11 19:50:50 +0400339static void uart_clps711x_nop_void(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341}
342
Alexander Shiyanbc000242013-12-11 19:50:50 +0400343static int uart_clps711x_nop_int(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 return 0;
346}
347
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400348static const struct uart_ops uart_clps711x_ops = {
349 .tx_empty = uart_clps711x_tx_empty,
350 .set_mctrl = uart_clps711x_set_mctrl,
351 .get_mctrl = uart_clps711x_get_mctrl,
352 .stop_tx = uart_clps711x_stop_tx,
353 .start_tx = uart_clps711x_start_tx,
Alexander Shiyanbc000242013-12-11 19:50:50 +0400354 .stop_rx = uart_clps711x_nop_void,
355 .enable_ms = uart_clps711x_nop_void,
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400356 .break_ctl = uart_clps711x_break_ctl,
Alexander Shiyan71b9e8c2013-12-31 20:49:41 +0400357 .set_ldisc = uart_clps711x_set_ldisc,
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400358 .startup = uart_clps711x_startup,
359 .shutdown = uart_clps711x_shutdown,
360 .set_termios = uart_clps711x_set_termios,
361 .type = uart_clps711x_type,
362 .config_port = uart_clps711x_config_port,
Alexander Shiyanbc000242013-12-11 19:50:50 +0400363 .release_port = uart_clps711x_nop_void,
364 .request_port = uart_clps711x_nop_int,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365};
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400368static void uart_clps711x_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +0000369{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400370 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400371 u32 sysflg = 0;
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400372
Alexander Shiyanbc000242013-12-11 19:50:50 +0400373 do {
374 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
375 } while (sysflg & SYSFLG_UTXFF);
376
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400377 writew(ch, port->membase + UARTDR_OFFSET);
Russell Kingd3587882006-03-20 20:00:09 +0000378}
379
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400380static void uart_clps711x_console_write(struct console *co, const char *c,
381 unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400383 struct uart_port *port = clps711x_uart.state[co->index].uart_port;
384 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400385 u32 sysflg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400387 uart_console_write(port, c, n, uart_clps711x_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400389 /* Wait for transmitter to become empty */
Alexander Shiyanbc000242013-12-11 19:50:50 +0400390 do {
391 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
392 } while (sysflg & SYSFLG_UBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400395static int uart_clps711x_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400397 int baud = 38400, bits = 8, parity = 'n', flow = 'n';
Alexander Shiyanbc000242013-12-11 19:50:50 +0400398 int ret, index = co->index;
399 struct clps711x_port *s;
400 struct uart_port *port;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400401 unsigned int quot;
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400402 u32 ubrlcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Alexander Shiyanbc000242013-12-11 19:50:50 +0400404 if (index < 0 || index >= UART_CLPS711X_NR)
405 return -EINVAL;
406
407 port = clps711x_uart.state[index].uart_port;
408 if (!port)
409 return -ENODEV;
410
411 s = dev_get_drvdata(port->dev);
412
413 if (!options) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400414 u32 syscon = 0;
415
Alexander Shiyanbc000242013-12-11 19:50:50 +0400416 regmap_read(s->syscon, SYSCON_OFFSET, &syscon);
417 if (syscon & SYSCON_UARTEN) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400418 ubrlcr = readl(port->membase + UBRLCR_OFFSET);
Alexander Shiyanbc000242013-12-11 19:50:50 +0400419
420 if (ubrlcr & UBRLCR_PRTEN) {
421 if (ubrlcr & UBRLCR_EVENPRT)
422 parity = 'e';
423 else
424 parity = 'o';
425 }
426
427 if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
428 bits = 7;
429
430 quot = ubrlcr & UBRLCR_BAUD_MASK;
431 baud = port->uartclk / (16 * (quot + 1));
432 }
433 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 uart_parse_options(options, &baud, &parity, &bits, &flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Alexander Shiyanbc000242013-12-11 19:50:50 +0400436 ret = uart_set_options(port, co, baud, parity, bits, flow);
437 if (ret)
438 return ret;
439
440 return regmap_update_bits(s->syscon, SYSCON_OFFSET,
441 SYSCON_UARTEN, SYSCON_UARTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
Alexander Shiyanbc000242013-12-11 19:50:50 +0400443
444static struct console clps711x_console = {
445 .name = UART_CLPS711X_DEVNAME,
446 .device = uart_console_device,
447 .write = uart_clps711x_console_write,
448 .setup = uart_clps711x_console_setup,
449 .flags = CON_PRINTBUFFER,
450 .index = -1,
451};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452#endif
453
Bill Pemberton9671f092012-11-19 13:21:50 -0500454static int uart_clps711x_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400456 struct device_node *np = pdev->dev.of_node;
457 int ret, index = np ? of_alias_get_id(np, "serial") : pdev->id;
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400458 struct clps711x_port *s;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400459 struct resource *res;
460 struct clk *uart_clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Alexander Shiyanbc000242013-12-11 19:50:50 +0400462 if (index < 0 || index >= UART_CLPS711X_NR)
463 return -EINVAL;
464
465 s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
466 if (!s)
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400467 return -ENOMEM;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400468
469 uart_clk = devm_clk_get(&pdev->dev, NULL);
470 if (IS_ERR(uart_clk))
471 return PTR_ERR(uart_clk);
472
473 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
474 s->port.membase = devm_ioremap_resource(&pdev->dev, res);
475 if (IS_ERR(s->port.membase))
476 return PTR_ERR(s->port.membase);
477
478 s->port.irq = platform_get_irq(pdev, 0);
479 if (IS_ERR_VALUE(s->port.irq))
480 return s->port.irq;
481
482 s->rx_irq = platform_get_irq(pdev, 1);
483 if (IS_ERR_VALUE(s->rx_irq))
484 return s->rx_irq;
485
486 if (!np) {
487 char syscon_name[9];
488
489 sprintf(syscon_name, "syscon.%i", index + 1);
490 s->syscon = syscon_regmap_lookup_by_pdevname(syscon_name);
491 if (IS_ERR(s->syscon))
492 return PTR_ERR(s->syscon);
493
494 s->use_ms = !index;
495 } else {
496 s->syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
497 if (IS_ERR(s->syscon))
498 return PTR_ERR(s->syscon);
499
Alexander Shiyan71b9e8c2013-12-31 20:49:41 +0400500 if (!index)
Alexander Shiyanbc000242013-12-11 19:50:50 +0400501 s->use_ms = of_property_read_bool(np, "uart-use-ms");
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400502 }
Alexander Shiyanbc000242013-12-11 19:50:50 +0400503
504 s->port.line = index;
505 s->port.dev = &pdev->dev;
506 s->port.iotype = UPIO_MEM32;
507 s->port.mapbase = res->start;
508 s->port.type = PORT_CLPS711X;
509 s->port.fifosize = 16;
510 s->port.flags = UPF_SKIP_TEST | UPF_FIXED_TYPE;
511 s->port.uartclk = clk_get_rate(uart_clk);
512 s->port.ops = &uart_clps711x_ops;
513
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400514 platform_set_drvdata(pdev, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Alexander Shiyanbc000242013-12-11 19:50:50 +0400516 ret = uart_add_one_port(&clps711x_uart, &s->port);
517 if (ret)
518 return ret;
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400519
Alexander Shiyanbc000242013-12-11 19:50:50 +0400520 /* Disable port */
521 if (!uart_console(&s->port))
522 regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON_UARTEN, 0);
523
524 s->tx_enabled = 1;
525
526 ret = devm_request_irq(&pdev->dev, s->port.irq, uart_clps711x_int_tx, 0,
527 dev_name(&pdev->dev), &s->port);
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400528 if (ret) {
Alexander Shiyanbc000242013-12-11 19:50:50 +0400529 uart_remove_one_port(&clps711x_uart, &s->port);
Jingoo Han43b829b2013-06-25 10:08:49 +0900530 return ret;
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Alexander Shiyanbc000242013-12-11 19:50:50 +0400533 ret = devm_request_irq(&pdev->dev, s->rx_irq, uart_clps711x_int_rx, 0,
534 dev_name(&pdev->dev), &s->port);
535 if (ret)
536 uart_remove_one_port(&clps711x_uart, &s->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Alexander Shiyanbc000242013-12-11 19:50:50 +0400538 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500541static int uart_clps711x_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400543 struct clps711x_port *s = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Alexander Shiyanbc000242013-12-11 19:50:50 +0400545 return uart_remove_one_port(&clps711x_uart, &s->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Alexander Shiyanbc000242013-12-11 19:50:50 +0400548static const struct of_device_id __maybe_unused clps711x_uart_dt_ids[] = {
549 { .compatible = "cirrus,clps711x-uart", },
550 { }
551};
552MODULE_DEVICE_TABLE(of, clps711x_uart_dt_ids);
553
554static struct platform_driver clps711x_uart_platform = {
Alexander Shiyan95113722012-10-14 11:05:23 +0400555 .driver = {
Alexander Shiyanbc000242013-12-11 19:50:50 +0400556 .name = "clps711x-uart",
557 .owner = THIS_MODULE,
558 .of_match_table = of_match_ptr(clps711x_uart_dt_ids),
Alexander Shiyan95113722012-10-14 11:05:23 +0400559 },
560 .probe = uart_clps711x_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500561 .remove = uart_clps711x_remove,
Alexander Shiyan95113722012-10-14 11:05:23 +0400562};
Alexander Shiyan95113722012-10-14 11:05:23 +0400563
564static int __init uart_clps711x_init(void)
565{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400566 int ret;
567
568#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
569 clps711x_uart.cons = &clps711x_console;
570 clps711x_console.data = &clps711x_uart;
571#endif
572
573 ret = uart_register_driver(&clps711x_uart);
574 if (ret)
575 return ret;
576
577 return platform_driver_register(&clps711x_uart_platform);
Alexander Shiyan95113722012-10-14 11:05:23 +0400578}
579module_init(uart_clps711x_init);
580
581static void __exit uart_clps711x_exit(void)
582{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400583 platform_driver_unregister(&clps711x_uart_platform);
584 uart_unregister_driver(&clps711x_uart);
Alexander Shiyan95113722012-10-14 11:05:23 +0400585}
586module_exit(uart_clps711x_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588MODULE_AUTHOR("Deep Blue Solutions Ltd");
Alexander Shiyan95113722012-10-14 11:05:23 +0400589MODULE_DESCRIPTION("CLPS711X serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590MODULE_LICENSE("GPL");