blob: 98f193a8339221a05db37927555d79abf3c55daa [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Driver for CLPS711x serial ports
4 *
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 *
7 * Copyright 1999 ARM Limited
8 * Copyright (C) 2000 Deep Blue Solutions Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#if defined(CONFIG_SERIAL_CLPS711X_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
12#define SUPPORT_SYSRQ
13#endif
14
15#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/device.h>
Alexander Shiyana1c25f22012-10-14 11:05:34 +040017#include <linux/console.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/serial_core.h>
19#include <linux/serial.h>
Alexander Shiyanc08f0152012-10-14 11:05:26 +040020#include <linux/clk.h>
Alexander Shiyanbc000242013-12-11 19:50:50 +040021#include <linux/io.h>
Alexander Shiyana1c25f22012-10-14 11:05:34 +040022#include <linux/tty.h>
23#include <linux/tty_flip.h>
24#include <linux/ioport.h>
Alexander Shiyanbc000242013-12-11 19:50:50 +040025#include <linux/of.h>
Alexander Shiyan95113722012-10-14 11:05:23 +040026#include <linux/platform_device.h>
Alexander Shiyanbc000242013-12-11 19:50:50 +040027#include <linux/regmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Alexander Shiyanbc000242013-12-11 19:50:50 +040029#include <linux/mfd/syscon.h>
30#include <linux/mfd/syscon/clps711x.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Alexander Shiyan62b0a1b2014-09-06 07:20:15 +040032#include "serial_mctrl_gpio.h"
33
Alexander Shiyanbc000242013-12-11 19:50:50 +040034#define UART_CLPS711X_DEVNAME "ttyCL"
Alexander Shiyan117d5d42012-10-14 11:05:24 +040035#define UART_CLPS711X_NR 2
36#define UART_CLPS711X_MAJOR 204
37#define UART_CLPS711X_MINOR 40
Alexander Shiyan95113722012-10-14 11:05:23 +040038
Alexander Shiyanbc000242013-12-11 19:50:50 +040039#define UARTDR_OFFSET (0x00)
40#define UBRLCR_OFFSET (0x40)
41
42#define UARTDR_FRMERR (1 << 8)
43#define UARTDR_PARERR (1 << 9)
44#define UARTDR_OVERR (1 << 10)
45
46#define UBRLCR_BAUD_MASK ((1 << 12) - 1)
47#define UBRLCR_BREAK (1 << 12)
48#define UBRLCR_PRTEN (1 << 13)
49#define UBRLCR_EVENPRT (1 << 14)
50#define UBRLCR_XSTOP (1 << 15)
51#define UBRLCR_FIFOEN (1 << 16)
52#define UBRLCR_WRDLEN5 (0 << 17)
53#define UBRLCR_WRDLEN6 (1 << 17)
54#define UBRLCR_WRDLEN7 (2 << 17)
55#define UBRLCR_WRDLEN8 (3 << 17)
56#define UBRLCR_WRDLEN_MASK (3 << 17)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Alexander Shiyan117d5d42012-10-14 11:05:24 +040058struct clps711x_port {
Alexander Shiyanbc000242013-12-11 19:50:50 +040059 struct uart_port port;
60 unsigned int tx_enabled;
61 int rx_irq;
62 struct regmap *syscon;
Alexander Shiyan62b0a1b2014-09-06 07:20:15 +040063 struct mctrl_gpios *gpios;
Alexander Shiyanbc000242013-12-11 19:50:50 +040064};
65
66static struct uart_driver clps711x_uart = {
67 .owner = THIS_MODULE,
68 .driver_name = UART_CLPS711X_DEVNAME,
69 .dev_name = UART_CLPS711X_DEVNAME,
70 .major = UART_CLPS711X_MAJOR,
71 .minor = UART_CLPS711X_MINOR,
72 .nr = UART_CLPS711X_NR,
Alexander Shiyan117d5d42012-10-14 11:05:24 +040073};
74
Alexander Shiyana1c25f22012-10-14 11:05:34 +040075static void uart_clps711x_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040077 struct clps711x_port *s = dev_get_drvdata(port->dev);
78
Alexander Shiyanbc000242013-12-11 19:50:50 +040079 if (s->tx_enabled) {
80 disable_irq(port->irq);
81 s->tx_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83}
84
Alexander Shiyana1c25f22012-10-14 11:05:34 +040085static void uart_clps711x_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040087 struct clps711x_port *s = dev_get_drvdata(port->dev);
88
Alexander Shiyanbc000242013-12-11 19:50:50 +040089 if (!s->tx_enabled) {
90 s->tx_enabled = 1;
91 enable_irq(port->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93}
94
Alexander Shiyan135cc792012-10-14 11:05:31 +040095static irqreturn_t uart_clps711x_int_rx(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 struct uart_port *port = dev_id;
Alexander Shiyanbc000242013-12-11 19:50:50 +040098 struct clps711x_port *s = dev_get_drvdata(port->dev);
99 unsigned int status, flg;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400100 u16 ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Alexander Shiyanf27de952012-10-14 11:05:30 +0400102 for (;;) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400103 u32 sysflg = 0;
104
Alexander Shiyanbc000242013-12-11 19:50:50 +0400105 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
106 if (sysflg & SYSFLG_URXFE)
Alexander Shiyanf27de952012-10-14 11:05:30 +0400107 break;
108
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400109 ch = readw(port->membase + UARTDR_OFFSET);
Alexander Shiyanf27de952012-10-14 11:05:30 +0400110 status = ch & (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR);
111 ch &= 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 port->icount.rx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 flg = TTY_NORMAL;
115
Alexander Shiyanf27de952012-10-14 11:05:30 +0400116 if (unlikely(status)) {
117 if (status & UARTDR_PARERR)
Russell King2a9604b2005-04-26 15:32:00 +0100118 port->icount.parity++;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400119 else if (status & UARTDR_FRMERR)
Russell King2a9604b2005-04-26 15:32:00 +0100120 port->icount.frame++;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400121 else if (status & UARTDR_OVERR)
Russell King2a9604b2005-04-26 15:32:00 +0100122 port->icount.overrun++;
123
Alexander Shiyanf27de952012-10-14 11:05:30 +0400124 status &= port->read_status_mask;
Russell King2a9604b2005-04-26 15:32:00 +0100125
Alexander Shiyanf27de952012-10-14 11:05:30 +0400126 if (status & UARTDR_PARERR)
Russell King2a9604b2005-04-26 15:32:00 +0100127 flg = TTY_PARITY;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400128 else if (status & UARTDR_FRMERR)
Russell King2a9604b2005-04-26 15:32:00 +0100129 flg = TTY_FRAME;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400130 else if (status & UARTDR_OVERR)
131 flg = TTY_OVERRUN;
Russell King2a9604b2005-04-26 15:32:00 +0100132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
David Howells7d12e782006-10-05 14:55:46 +0100134 if (uart_handle_sysrq_char(port, ch))
Alexander Shiyanf27de952012-10-14 11:05:30 +0400135 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Alexander Shiyanf27de952012-10-14 11:05:30 +0400137 if (status & port->ignore_status_mask)
138 continue;
Russell King2a9604b2005-04-26 15:32:00 +0100139
Alexander Shiyanf27de952012-10-14 11:05:30 +0400140 uart_insert_char(port, status, UARTDR_OVERR, ch, flg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Alexander Shiyanf27de952012-10-14 11:05:30 +0400142
Jiri Slaby2e124b42013-01-03 15:53:06 +0100143 tty_flip_buffer_push(&port->state->port);
Alexander Shiyanf27de952012-10-14 11:05:30 +0400144
Russell King2a9604b2005-04-26 15:32:00 +0100145 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Alexander Shiyan135cc792012-10-14 11:05:31 +0400148static irqreturn_t uart_clps711x_int_tx(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct uart_port *port = dev_id;
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400151 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700152 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 if (port->x_char) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400155 writew(port->x_char, port->membase + UARTDR_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 port->icount.tx++;
157 port->x_char = 0;
158 return IRQ_HANDLED;
159 }
Alexander Shiyan7a6fbc92012-03-27 12:22:49 +0400160
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400161 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Alexander Shiyanbc000242013-12-11 19:50:50 +0400162 if (s->tx_enabled) {
163 disable_irq_nosync(port->irq);
164 s->tx_enabled = 0;
165 }
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400166 return IRQ_HANDLED;
167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Alexander Shiyancf03a882012-10-14 11:05:27 +0400169 while (!uart_circ_empty(xmit)) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400170 u32 sysflg = 0;
171
172 writew(xmit->buf[xmit->tail], port->membase + UARTDR_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
174 port->icount.tx++;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400175
176 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
177 if (sysflg & SYSFLG_UTXFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 break;
Alexander Shiyancf03a882012-10-14 11:05:27 +0400179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
182 uart_write_wakeup(port);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return IRQ_HANDLED;
185}
186
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400187static unsigned int uart_clps711x_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400189 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400190 u32 sysflg = 0;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400191
192 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
193
194 return (sysflg & SYSFLG_UBUSY) ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400197static unsigned int uart_clps711x_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Alexander Shiyan62b0a1b2014-09-06 07:20:15 +0400199 unsigned int result = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400200 struct clps711x_port *s = dev_get_drvdata(port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Alexander Shiyan62b0a1b2014-09-06 07:20:15 +0400202 return mctrl_gpio_get(s->gpios, &result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400205static void uart_clps711x_set_mctrl(struct uart_port *port, unsigned int mctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Alexander Shiyan62b0a1b2014-09-06 07:20:15 +0400207 struct clps711x_port *s = dev_get_drvdata(port->dev);
208
209 mctrl_gpio_set(s->gpios, mctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400212static void uart_clps711x_break_ctl(struct uart_port *port, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 unsigned int ubrlcr;
215
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400216 ubrlcr = readl(port->membase + UBRLCR_OFFSET);
Alexander Shiyanec335522012-10-14 11:05:29 +0400217 if (break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 ubrlcr |= UBRLCR_BREAK;
219 else
220 ubrlcr &= ~UBRLCR_BREAK;
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400221 writel(ubrlcr, port->membase + UBRLCR_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Peter Hurley732a84a2014-11-05 13:11:43 -0500224static void uart_clps711x_set_ldisc(struct uart_port *port,
225 struct ktermios *termios)
Alexander Shiyan71b9e8c2013-12-31 20:49:41 +0400226{
227 if (!port->line) {
228 struct clps711x_port *s = dev_get_drvdata(port->dev);
229
230 regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON1_SIREN,
Peter Hurley732a84a2014-11-05 13:11:43 -0500231 (termios->c_line == N_IRDA) ? SYSCON1_SIREN : 0);
Alexander Shiyan71b9e8c2013-12-31 20:49:41 +0400232 }
233}
234
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400235static int uart_clps711x_startup(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400237 struct clps711x_port *s = dev_get_drvdata(port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Alexander Shiyanf52ede22012-10-14 11:05:32 +0400239 /* Disable break */
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400240 writel(readl(port->membase + UBRLCR_OFFSET) & ~UBRLCR_BREAK,
241 port->membase + UBRLCR_OFFSET);
Alexander Shiyanf52ede22012-10-14 11:05:32 +0400242
243 /* Enable the port */
Alexander Shiyanbc000242013-12-11 19:50:50 +0400244 return regmap_update_bits(s->syscon, SYSCON_OFFSET,
245 SYSCON_UARTEN, SYSCON_UARTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400248static void uart_clps711x_shutdown(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400250 struct clps711x_port *s = dev_get_drvdata(port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Alexander Shiyanf52ede22012-10-14 11:05:32 +0400252 /* Disable the port */
Alexander Shiyanbc000242013-12-11 19:50:50 +0400253 regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON_UARTEN, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400256static void uart_clps711x_set_termios(struct uart_port *port,
257 struct ktermios *termios,
258 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400260 u32 ubrlcr;
261 unsigned int baud, quot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400263 /* Mask termios capabilities we don't support */
264 termios->c_cflag &= ~CMSPAR;
265 termios->c_iflag &= ~(BRKINT | IGNBRK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400267 /* Ask the core to calculate the divisor for us */
268 baud = uart_get_baud_rate(port, termios, old, port->uartclk / 4096,
269 port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 quot = uart_get_divisor(port, baud);
271
272 switch (termios->c_cflag & CSIZE) {
273 case CS5:
274 ubrlcr = UBRLCR_WRDLEN5;
275 break;
276 case CS6:
277 ubrlcr = UBRLCR_WRDLEN6;
278 break;
279 case CS7:
280 ubrlcr = UBRLCR_WRDLEN7;
281 break;
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400282 case CS8:
283 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 ubrlcr = UBRLCR_WRDLEN8;
285 break;
286 }
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (termios->c_cflag & CSTOPB)
289 ubrlcr |= UBRLCR_XSTOP;
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (termios->c_cflag & PARENB) {
292 ubrlcr |= UBRLCR_PRTEN;
293 if (!(termios->c_cflag & PARODD))
294 ubrlcr |= UBRLCR_EVENPRT;
295 }
Alexander Shiyancf03a882012-10-14 11:05:27 +0400296
297 /* Enable FIFO */
298 ubrlcr |= UBRLCR_FIFOEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400300 /* Set read status mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 port->read_status_mask = UARTDR_OVERR;
302 if (termios->c_iflag & INPCK)
303 port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
304
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400305 /* Set status ignore mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 port->ignore_status_mask = 0;
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400307 if (!(termios->c_cflag & CREAD))
308 port->ignore_status_mask |= UARTDR_OVERR | UARTDR_PARERR |
309 UARTDR_FRMERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400311 uart_update_timeout(port, termios->c_cflag, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400313 writel(ubrlcr | (quot - 1), port->membase + UBRLCR_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400316static const char *uart_clps711x_type(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400318 return (port->type == PORT_CLPS711X) ? "CLPS711X" : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400321static void uart_clps711x_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 if (flags & UART_CONFIG_TYPE)
324 port->type = PORT_CLPS711X;
325}
326
Alexander Shiyanbc000242013-12-11 19:50:50 +0400327static void uart_clps711x_nop_void(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329}
330
Alexander Shiyanbc000242013-12-11 19:50:50 +0400331static int uart_clps711x_nop_int(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 return 0;
334}
335
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400336static const struct uart_ops uart_clps711x_ops = {
337 .tx_empty = uart_clps711x_tx_empty,
338 .set_mctrl = uart_clps711x_set_mctrl,
339 .get_mctrl = uart_clps711x_get_mctrl,
340 .stop_tx = uart_clps711x_stop_tx,
341 .start_tx = uart_clps711x_start_tx,
Alexander Shiyanbc000242013-12-11 19:50:50 +0400342 .stop_rx = uart_clps711x_nop_void,
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400343 .break_ctl = uart_clps711x_break_ctl,
Alexander Shiyan71b9e8c2013-12-31 20:49:41 +0400344 .set_ldisc = uart_clps711x_set_ldisc,
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400345 .startup = uart_clps711x_startup,
346 .shutdown = uart_clps711x_shutdown,
347 .set_termios = uart_clps711x_set_termios,
348 .type = uart_clps711x_type,
349 .config_port = uart_clps711x_config_port,
Alexander Shiyanbc000242013-12-11 19:50:50 +0400350 .release_port = uart_clps711x_nop_void,
351 .request_port = uart_clps711x_nop_int,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352};
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400355static void uart_clps711x_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +0000356{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400357 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyan2f310b82014-03-27 13:38:19 +0400358 u32 sysflg = 0;
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400359
Alexander Shiyan63e3ad32014-03-11 15:30:01 +0400360 /* Wait for FIFO is not full */
Alexander Shiyan2f310b82014-03-27 13:38:19 +0400361 do {
Alexander Shiyanbc000242013-12-11 19:50:50 +0400362 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
Alexander Shiyan2f310b82014-03-27 13:38:19 +0400363 } while (sysflg & SYSFLG_UTXFF);
Alexander Shiyanbc000242013-12-11 19:50:50 +0400364
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400365 writew(ch, port->membase + UARTDR_OFFSET);
Russell Kingd3587882006-03-20 20:00:09 +0000366}
367
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400368static void uart_clps711x_console_write(struct console *co, const char *c,
369 unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400371 struct uart_port *port = clps711x_uart.state[co->index].uart_port;
372 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyan2f310b82014-03-27 13:38:19 +0400373 u32 sysflg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400375 uart_console_write(port, c, n, uart_clps711x_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400377 /* Wait for transmitter to become empty */
Alexander Shiyan2f310b82014-03-27 13:38:19 +0400378 do {
Alexander Shiyanbc000242013-12-11 19:50:50 +0400379 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
Alexander Shiyan2f310b82014-03-27 13:38:19 +0400380 } while (sysflg & SYSFLG_UBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400383static int uart_clps711x_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400385 int baud = 38400, bits = 8, parity = 'n', flow = 'n';
Alexander Shiyanbc000242013-12-11 19:50:50 +0400386 int ret, index = co->index;
387 struct clps711x_port *s;
388 struct uart_port *port;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400389 unsigned int quot;
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400390 u32 ubrlcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Alexander Shiyanbc000242013-12-11 19:50:50 +0400392 if (index < 0 || index >= UART_CLPS711X_NR)
393 return -EINVAL;
394
395 port = clps711x_uart.state[index].uart_port;
396 if (!port)
397 return -ENODEV;
398
399 s = dev_get_drvdata(port->dev);
400
401 if (!options) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400402 u32 syscon = 0;
403
Alexander Shiyanbc000242013-12-11 19:50:50 +0400404 regmap_read(s->syscon, SYSCON_OFFSET, &syscon);
405 if (syscon & SYSCON_UARTEN) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400406 ubrlcr = readl(port->membase + UBRLCR_OFFSET);
Alexander Shiyanbc000242013-12-11 19:50:50 +0400407
408 if (ubrlcr & UBRLCR_PRTEN) {
409 if (ubrlcr & UBRLCR_EVENPRT)
410 parity = 'e';
411 else
412 parity = 'o';
413 }
414
415 if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
416 bits = 7;
417
418 quot = ubrlcr & UBRLCR_BAUD_MASK;
419 baud = port->uartclk / (16 * (quot + 1));
420 }
421 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 uart_parse_options(options, &baud, &parity, &bits, &flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Alexander Shiyanbc000242013-12-11 19:50:50 +0400424 ret = uart_set_options(port, co, baud, parity, bits, flow);
425 if (ret)
426 return ret;
427
428 return regmap_update_bits(s->syscon, SYSCON_OFFSET,
429 SYSCON_UARTEN, SYSCON_UARTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
Alexander Shiyanbc000242013-12-11 19:50:50 +0400431
432static struct console clps711x_console = {
433 .name = UART_CLPS711X_DEVNAME,
434 .device = uart_console_device,
435 .write = uart_clps711x_console_write,
436 .setup = uart_clps711x_console_setup,
437 .flags = CON_PRINTBUFFER,
438 .index = -1,
439};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440#endif
441
Bill Pemberton9671f092012-11-19 13:21:50 -0500442static int uart_clps711x_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400444 struct device_node *np = pdev->dev.of_node;
445 int ret, index = np ? of_alias_get_id(np, "serial") : pdev->id;
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400446 struct clps711x_port *s;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400447 struct resource *res;
448 struct clk *uart_clk;
Guenter Roeck8f5405c2016-02-09 07:06:47 -0800449 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Alexander Shiyanbc000242013-12-11 19:50:50 +0400451 if (index < 0 || index >= UART_CLPS711X_NR)
452 return -EINVAL;
453
454 s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
455 if (!s)
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400456 return -ENOMEM;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400457
458 uart_clk = devm_clk_get(&pdev->dev, NULL);
459 if (IS_ERR(uart_clk))
460 return PTR_ERR(uart_clk);
461
462 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
463 s->port.membase = devm_ioremap_resource(&pdev->dev, res);
464 if (IS_ERR(s->port.membase))
465 return PTR_ERR(s->port.membase);
466
Guenter Roeck8f5405c2016-02-09 07:06:47 -0800467 irq = platform_get_irq(pdev, 0);
468 if (irq < 0)
469 return irq;
470 s->port.irq = irq;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400471
472 s->rx_irq = platform_get_irq(pdev, 1);
Guenter Roeck8f5405c2016-02-09 07:06:47 -0800473 if (s->rx_irq < 0)
Alexander Shiyanbc000242013-12-11 19:50:50 +0400474 return s->rx_irq;
475
476 if (!np) {
477 char syscon_name[9];
478
479 sprintf(syscon_name, "syscon.%i", index + 1);
480 s->syscon = syscon_regmap_lookup_by_pdevname(syscon_name);
481 if (IS_ERR(s->syscon))
482 return PTR_ERR(s->syscon);
Alexander Shiyanbc000242013-12-11 19:50:50 +0400483 } else {
484 s->syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
485 if (IS_ERR(s->syscon))
486 return PTR_ERR(s->syscon);
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400487 }
Alexander Shiyanbc000242013-12-11 19:50:50 +0400488
489 s->port.line = index;
490 s->port.dev = &pdev->dev;
491 s->port.iotype = UPIO_MEM32;
492 s->port.mapbase = res->start;
493 s->port.type = PORT_CLPS711X;
494 s->port.fifosize = 16;
495 s->port.flags = UPF_SKIP_TEST | UPF_FIXED_TYPE;
496 s->port.uartclk = clk_get_rate(uart_clk);
497 s->port.ops = &uart_clps711x_ops;
498
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400499 platform_set_drvdata(pdev, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Uwe Kleine-König7d8c70d2015-09-30 10:19:40 +0200501 s->gpios = mctrl_gpio_init_noauto(&pdev->dev, 0);
Uwe Kleine-Königf059a452015-02-12 15:24:39 +0100502 if (IS_ERR(s->gpios))
503 return PTR_ERR(s->gpios);
Alexander Shiyan62b0a1b2014-09-06 07:20:15 +0400504
Alexander Shiyanbc000242013-12-11 19:50:50 +0400505 ret = uart_add_one_port(&clps711x_uart, &s->port);
506 if (ret)
507 return ret;
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400508
Alexander Shiyanbc000242013-12-11 19:50:50 +0400509 /* Disable port */
510 if (!uart_console(&s->port))
511 regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON_UARTEN, 0);
512
513 s->tx_enabled = 1;
514
515 ret = devm_request_irq(&pdev->dev, s->port.irq, uart_clps711x_int_tx, 0,
516 dev_name(&pdev->dev), &s->port);
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400517 if (ret) {
Alexander Shiyanbc000242013-12-11 19:50:50 +0400518 uart_remove_one_port(&clps711x_uart, &s->port);
Jingoo Han43b829b2013-06-25 10:08:49 +0900519 return ret;
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Alexander Shiyanbc000242013-12-11 19:50:50 +0400522 ret = devm_request_irq(&pdev->dev, s->rx_irq, uart_clps711x_int_rx, 0,
523 dev_name(&pdev->dev), &s->port);
524 if (ret)
525 uart_remove_one_port(&clps711x_uart, &s->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Alexander Shiyanbc000242013-12-11 19:50:50 +0400527 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500530static int uart_clps711x_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400532 struct clps711x_port *s = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Alexander Shiyanbc000242013-12-11 19:50:50 +0400534 return uart_remove_one_port(&clps711x_uart, &s->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Alexander Shiyanbc000242013-12-11 19:50:50 +0400537static const struct of_device_id __maybe_unused clps711x_uart_dt_ids[] = {
Alexander Shiyand3053452016-06-04 10:09:57 +0300538 { .compatible = "cirrus,ep7209-uart", },
Alexander Shiyanbc000242013-12-11 19:50:50 +0400539 { }
540};
541MODULE_DEVICE_TABLE(of, clps711x_uart_dt_ids);
542
543static struct platform_driver clps711x_uart_platform = {
Alexander Shiyan95113722012-10-14 11:05:23 +0400544 .driver = {
Alexander Shiyanbc000242013-12-11 19:50:50 +0400545 .name = "clps711x-uart",
Alexander Shiyanbc000242013-12-11 19:50:50 +0400546 .of_match_table = of_match_ptr(clps711x_uart_dt_ids),
Alexander Shiyan95113722012-10-14 11:05:23 +0400547 },
548 .probe = uart_clps711x_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500549 .remove = uart_clps711x_remove,
Alexander Shiyan95113722012-10-14 11:05:23 +0400550};
Alexander Shiyan95113722012-10-14 11:05:23 +0400551
552static int __init uart_clps711x_init(void)
553{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400554 int ret;
555
556#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
557 clps711x_uart.cons = &clps711x_console;
558 clps711x_console.data = &clps711x_uart;
559#endif
560
561 ret = uart_register_driver(&clps711x_uart);
562 if (ret)
563 return ret;
564
565 return platform_driver_register(&clps711x_uart_platform);
Alexander Shiyan95113722012-10-14 11:05:23 +0400566}
567module_init(uart_clps711x_init);
568
569static void __exit uart_clps711x_exit(void)
570{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400571 platform_driver_unregister(&clps711x_uart_platform);
572 uart_unregister_driver(&clps711x_uart);
Alexander Shiyan95113722012-10-14 11:05:23 +0400573}
574module_exit(uart_clps711x_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576MODULE_AUTHOR("Deep Blue Solutions Ltd");
Alexander Shiyan95113722012-10-14 11:05:23 +0400577MODULE_DESCRIPTION("CLPS711X serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578MODULE_LICENSE("GPL");