blob: 5beafd2d2218d96d92f008374f2451b8db53e018 [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 Shiyan62b0a1b2014-09-06 07:20:15 +040036#include "serial_mctrl_gpio.h"
37
Alexander Shiyanbc000242013-12-11 19:50:50 +040038#define UART_CLPS711X_DEVNAME "ttyCL"
Alexander Shiyan117d5d42012-10-14 11:05:24 +040039#define UART_CLPS711X_NR 2
40#define UART_CLPS711X_MAJOR 204
41#define UART_CLPS711X_MINOR 40
Alexander Shiyan95113722012-10-14 11:05:23 +040042
Alexander Shiyanbc000242013-12-11 19:50:50 +040043#define UARTDR_OFFSET (0x00)
44#define UBRLCR_OFFSET (0x40)
45
46#define UARTDR_FRMERR (1 << 8)
47#define UARTDR_PARERR (1 << 9)
48#define UARTDR_OVERR (1 << 10)
49
50#define UBRLCR_BAUD_MASK ((1 << 12) - 1)
51#define UBRLCR_BREAK (1 << 12)
52#define UBRLCR_PRTEN (1 << 13)
53#define UBRLCR_EVENPRT (1 << 14)
54#define UBRLCR_XSTOP (1 << 15)
55#define UBRLCR_FIFOEN (1 << 16)
56#define UBRLCR_WRDLEN5 (0 << 17)
57#define UBRLCR_WRDLEN6 (1 << 17)
58#define UBRLCR_WRDLEN7 (2 << 17)
59#define UBRLCR_WRDLEN8 (3 << 17)
60#define UBRLCR_WRDLEN_MASK (3 << 17)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Alexander Shiyan117d5d42012-10-14 11:05:24 +040062struct clps711x_port {
Alexander Shiyanbc000242013-12-11 19:50:50 +040063 struct uart_port port;
64 unsigned int tx_enabled;
65 int rx_irq;
66 struct regmap *syscon;
Alexander Shiyan62b0a1b2014-09-06 07:20:15 +040067 struct mctrl_gpios *gpios;
Alexander Shiyanbc000242013-12-11 19:50:50 +040068};
69
70static struct uart_driver clps711x_uart = {
71 .owner = THIS_MODULE,
72 .driver_name = UART_CLPS711X_DEVNAME,
73 .dev_name = UART_CLPS711X_DEVNAME,
74 .major = UART_CLPS711X_MAJOR,
75 .minor = UART_CLPS711X_MINOR,
76 .nr = UART_CLPS711X_NR,
Alexander Shiyan117d5d42012-10-14 11:05:24 +040077};
78
Alexander Shiyana1c25f22012-10-14 11:05:34 +040079static void uart_clps711x_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040081 struct clps711x_port *s = dev_get_drvdata(port->dev);
82
Alexander Shiyanbc000242013-12-11 19:50:50 +040083 if (s->tx_enabled) {
84 disable_irq(port->irq);
85 s->tx_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87}
88
Alexander Shiyana1c25f22012-10-14 11:05:34 +040089static void uart_clps711x_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +040091 struct clps711x_port *s = dev_get_drvdata(port->dev);
92
Alexander Shiyanbc000242013-12-11 19:50:50 +040093 if (!s->tx_enabled) {
94 s->tx_enabled = 1;
95 enable_irq(port->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97}
98
Alexander Shiyan135cc792012-10-14 11:05:31 +040099static irqreturn_t uart_clps711x_int_rx(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 struct uart_port *port = dev_id;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400102 struct clps711x_port *s = dev_get_drvdata(port->dev);
103 unsigned int status, flg;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400104 u16 ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Alexander Shiyanf27de952012-10-14 11:05:30 +0400106 for (;;) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400107 u32 sysflg = 0;
108
Alexander Shiyanbc000242013-12-11 19:50:50 +0400109 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
110 if (sysflg & SYSFLG_URXFE)
Alexander Shiyanf27de952012-10-14 11:05:30 +0400111 break;
112
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400113 ch = readw(port->membase + UARTDR_OFFSET);
Alexander Shiyanf27de952012-10-14 11:05:30 +0400114 status = ch & (UARTDR_FRMERR | UARTDR_PARERR | UARTDR_OVERR);
115 ch &= 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 port->icount.rx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 flg = TTY_NORMAL;
119
Alexander Shiyanf27de952012-10-14 11:05:30 +0400120 if (unlikely(status)) {
121 if (status & UARTDR_PARERR)
Russell King2a9604b2005-04-26 15:32:00 +0100122 port->icount.parity++;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400123 else if (status & UARTDR_FRMERR)
Russell King2a9604b2005-04-26 15:32:00 +0100124 port->icount.frame++;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400125 else if (status & UARTDR_OVERR)
Russell King2a9604b2005-04-26 15:32:00 +0100126 port->icount.overrun++;
127
Alexander Shiyanf27de952012-10-14 11:05:30 +0400128 status &= port->read_status_mask;
Russell King2a9604b2005-04-26 15:32:00 +0100129
Alexander Shiyanf27de952012-10-14 11:05:30 +0400130 if (status & UARTDR_PARERR)
Russell King2a9604b2005-04-26 15:32:00 +0100131 flg = TTY_PARITY;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400132 else if (status & UARTDR_FRMERR)
Russell King2a9604b2005-04-26 15:32:00 +0100133 flg = TTY_FRAME;
Alexander Shiyanf27de952012-10-14 11:05:30 +0400134 else if (status & UARTDR_OVERR)
135 flg = TTY_OVERRUN;
Russell King2a9604b2005-04-26 15:32:00 +0100136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
David Howells7d12e782006-10-05 14:55:46 +0100138 if (uart_handle_sysrq_char(port, ch))
Alexander Shiyanf27de952012-10-14 11:05:30 +0400139 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Alexander Shiyanf27de952012-10-14 11:05:30 +0400141 if (status & port->ignore_status_mask)
142 continue;
Russell King2a9604b2005-04-26 15:32:00 +0100143
Alexander Shiyanf27de952012-10-14 11:05:30 +0400144 uart_insert_char(port, status, UARTDR_OVERR, ch, flg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
Alexander Shiyanf27de952012-10-14 11:05:30 +0400146
Jiri Slaby2e124b42013-01-03 15:53:06 +0100147 tty_flip_buffer_push(&port->state->port);
Alexander Shiyanf27de952012-10-14 11:05:30 +0400148
Russell King2a9604b2005-04-26 15:32:00 +0100149 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Alexander Shiyan135cc792012-10-14 11:05:31 +0400152static irqreturn_t uart_clps711x_int_tx(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct uart_port *port = dev_id;
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400155 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alan Coxebd2c8f2009-09-19 13:13:28 -0700156 struct circ_buf *xmit = &port->state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if (port->x_char) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400159 writew(port->x_char, port->membase + UARTDR_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 port->icount.tx++;
161 port->x_char = 0;
162 return IRQ_HANDLED;
163 }
Alexander Shiyan7a6fbc92012-03-27 12:22:49 +0400164
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400165 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Alexander Shiyanbc000242013-12-11 19:50:50 +0400166 if (s->tx_enabled) {
167 disable_irq_nosync(port->irq);
168 s->tx_enabled = 0;
169 }
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400170 return IRQ_HANDLED;
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Alexander Shiyancf03a882012-10-14 11:05:27 +0400173 while (!uart_circ_empty(xmit)) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400174 u32 sysflg = 0;
175
176 writew(xmit->buf[xmit->tail], port->membase + UARTDR_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
178 port->icount.tx++;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400179
180 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
181 if (sysflg & SYSFLG_UTXFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 break;
Alexander Shiyancf03a882012-10-14 11:05:27 +0400183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
186 uart_write_wakeup(port);
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return IRQ_HANDLED;
189}
190
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400191static unsigned int uart_clps711x_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400193 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400194 u32 sysflg = 0;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400195
196 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
197
198 return (sysflg & SYSFLG_UBUSY) ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400201static unsigned int uart_clps711x_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Alexander Shiyan62b0a1b2014-09-06 07:20:15 +0400203 unsigned int result = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400204 struct clps711x_port *s = dev_get_drvdata(port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Alexander Shiyan62b0a1b2014-09-06 07:20:15 +0400206 return mctrl_gpio_get(s->gpios, &result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400209static void uart_clps711x_set_mctrl(struct uart_port *port, unsigned int mctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Alexander Shiyan62b0a1b2014-09-06 07:20:15 +0400211 struct clps711x_port *s = dev_get_drvdata(port->dev);
212
213 mctrl_gpio_set(s->gpios, mctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400216static void uart_clps711x_break_ctl(struct uart_port *port, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 unsigned int ubrlcr;
219
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400220 ubrlcr = readl(port->membase + UBRLCR_OFFSET);
Alexander Shiyanec335522012-10-14 11:05:29 +0400221 if (break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 ubrlcr |= UBRLCR_BREAK;
223 else
224 ubrlcr &= ~UBRLCR_BREAK;
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400225 writel(ubrlcr, port->membase + UBRLCR_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Peter Hurley732a84a2014-11-05 13:11:43 -0500228static void uart_clps711x_set_ldisc(struct uart_port *port,
229 struct ktermios *termios)
Alexander Shiyan71b9e8c2013-12-31 20:49:41 +0400230{
231 if (!port->line) {
232 struct clps711x_port *s = dev_get_drvdata(port->dev);
233
234 regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON1_SIREN,
Peter Hurley732a84a2014-11-05 13:11:43 -0500235 (termios->c_line == N_IRDA) ? SYSCON1_SIREN : 0);
Alexander Shiyan71b9e8c2013-12-31 20:49:41 +0400236 }
237}
238
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400239static int uart_clps711x_startup(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Alexander Shiyan3c7e9eb2012-10-14 11:05:25 +0400241 struct clps711x_port *s = dev_get_drvdata(port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Alexander Shiyanf52ede22012-10-14 11:05:32 +0400243 /* Disable break */
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400244 writel(readl(port->membase + UBRLCR_OFFSET) & ~UBRLCR_BREAK,
245 port->membase + UBRLCR_OFFSET);
Alexander Shiyanf52ede22012-10-14 11:05:32 +0400246
247 /* Enable the port */
Alexander Shiyanbc000242013-12-11 19:50:50 +0400248 return regmap_update_bits(s->syscon, SYSCON_OFFSET,
249 SYSCON_UARTEN, SYSCON_UARTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400252static void uart_clps711x_shutdown(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400254 struct clps711x_port *s = dev_get_drvdata(port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Alexander Shiyanf52ede22012-10-14 11:05:32 +0400256 /* Disable the port */
Alexander Shiyanbc000242013-12-11 19:50:50 +0400257 regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON_UARTEN, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400260static void uart_clps711x_set_termios(struct uart_port *port,
261 struct ktermios *termios,
262 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400264 u32 ubrlcr;
265 unsigned int baud, quot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400267 /* Mask termios capabilities we don't support */
268 termios->c_cflag &= ~CMSPAR;
269 termios->c_iflag &= ~(BRKINT | IGNBRK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400271 /* Ask the core to calculate the divisor for us */
272 baud = uart_get_baud_rate(port, termios, old, port->uartclk / 4096,
273 port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 quot = uart_get_divisor(port, baud);
275
276 switch (termios->c_cflag & CSIZE) {
277 case CS5:
278 ubrlcr = UBRLCR_WRDLEN5;
279 break;
280 case CS6:
281 ubrlcr = UBRLCR_WRDLEN6;
282 break;
283 case CS7:
284 ubrlcr = UBRLCR_WRDLEN7;
285 break;
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400286 case CS8:
287 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 ubrlcr = UBRLCR_WRDLEN8;
289 break;
290 }
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (termios->c_cflag & CSTOPB)
293 ubrlcr |= UBRLCR_XSTOP;
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (termios->c_cflag & PARENB) {
296 ubrlcr |= UBRLCR_PRTEN;
297 if (!(termios->c_cflag & PARODD))
298 ubrlcr |= UBRLCR_EVENPRT;
299 }
Alexander Shiyancf03a882012-10-14 11:05:27 +0400300
301 /* Enable FIFO */
302 ubrlcr |= UBRLCR_FIFOEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400304 /* Set read status mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 port->read_status_mask = UARTDR_OVERR;
306 if (termios->c_iflag & INPCK)
307 port->read_status_mask |= UARTDR_PARERR | UARTDR_FRMERR;
308
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400309 /* Set status ignore mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 port->ignore_status_mask = 0;
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400311 if (!(termios->c_cflag & CREAD))
312 port->ignore_status_mask |= UARTDR_OVERR | UARTDR_PARERR |
313 UARTDR_FRMERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Alexander Shiyan7ae75e92012-10-14 11:05:33 +0400315 uart_update_timeout(port, termios->c_cflag, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400317 writel(ubrlcr | (quot - 1), port->membase + UBRLCR_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400320static const char *uart_clps711x_type(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400322 return (port->type == PORT_CLPS711X) ? "CLPS711X" : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400325static void uart_clps711x_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 if (flags & UART_CONFIG_TYPE)
328 port->type = PORT_CLPS711X;
329}
330
Alexander Shiyanbc000242013-12-11 19:50:50 +0400331static void uart_clps711x_nop_void(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333}
334
Alexander Shiyanbc000242013-12-11 19:50:50 +0400335static int uart_clps711x_nop_int(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 return 0;
338}
339
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400340static const struct uart_ops uart_clps711x_ops = {
341 .tx_empty = uart_clps711x_tx_empty,
342 .set_mctrl = uart_clps711x_set_mctrl,
343 .get_mctrl = uart_clps711x_get_mctrl,
344 .stop_tx = uart_clps711x_stop_tx,
345 .start_tx = uart_clps711x_start_tx,
Alexander Shiyanbc000242013-12-11 19:50:50 +0400346 .stop_rx = uart_clps711x_nop_void,
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400347 .break_ctl = uart_clps711x_break_ctl,
Alexander Shiyan71b9e8c2013-12-31 20:49:41 +0400348 .set_ldisc = uart_clps711x_set_ldisc,
Alexander Shiyana1c25f22012-10-14 11:05:34 +0400349 .startup = uart_clps711x_startup,
350 .shutdown = uart_clps711x_shutdown,
351 .set_termios = uart_clps711x_set_termios,
352 .type = uart_clps711x_type,
353 .config_port = uart_clps711x_config_port,
Alexander Shiyanbc000242013-12-11 19:50:50 +0400354 .release_port = uart_clps711x_nop_void,
355 .request_port = uart_clps711x_nop_int,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356};
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400359static void uart_clps711x_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +0000360{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400361 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyan2f310b82014-03-27 13:38:19 +0400362 u32 sysflg = 0;
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400363
Alexander Shiyan63e3ad32014-03-11 15:30:01 +0400364 /* Wait for FIFO is not full */
Alexander Shiyan2f310b82014-03-27 13:38:19 +0400365 do {
Alexander Shiyanbc000242013-12-11 19:50:50 +0400366 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
Alexander Shiyan2f310b82014-03-27 13:38:19 +0400367 } while (sysflg & SYSFLG_UTXFF);
Alexander Shiyanbc000242013-12-11 19:50:50 +0400368
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400369 writew(ch, port->membase + UARTDR_OFFSET);
Russell Kingd3587882006-03-20 20:00:09 +0000370}
371
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400372static void uart_clps711x_console_write(struct console *co, const char *c,
373 unsigned n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400375 struct uart_port *port = clps711x_uart.state[co->index].uart_port;
376 struct clps711x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyan2f310b82014-03-27 13:38:19 +0400377 u32 sysflg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400379 uart_console_write(port, c, n, uart_clps711x_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400381 /* Wait for transmitter to become empty */
Alexander Shiyan2f310b82014-03-27 13:38:19 +0400382 do {
Alexander Shiyanbc000242013-12-11 19:50:50 +0400383 regmap_read(s->syscon, SYSFLG_OFFSET, &sysflg);
Alexander Shiyan2f310b82014-03-27 13:38:19 +0400384 } while (sysflg & SYSFLG_UBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400387static int uart_clps711x_console_setup(struct console *co, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400389 int baud = 38400, bits = 8, parity = 'n', flow = 'n';
Alexander Shiyanbc000242013-12-11 19:50:50 +0400390 int ret, index = co->index;
391 struct clps711x_port *s;
392 struct uart_port *port;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400393 unsigned int quot;
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400394 u32 ubrlcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Alexander Shiyanbc000242013-12-11 19:50:50 +0400396 if (index < 0 || index >= UART_CLPS711X_NR)
397 return -EINVAL;
398
399 port = clps711x_uart.state[index].uart_port;
400 if (!port)
401 return -ENODEV;
402
403 s = dev_get_drvdata(port->dev);
404
405 if (!options) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400406 u32 syscon = 0;
407
Alexander Shiyanbc000242013-12-11 19:50:50 +0400408 regmap_read(s->syscon, SYSCON_OFFSET, &syscon);
409 if (syscon & SYSCON_UARTEN) {
Alexander Shiyan093a9e22013-12-31 20:49:42 +0400410 ubrlcr = readl(port->membase + UBRLCR_OFFSET);
Alexander Shiyanbc000242013-12-11 19:50:50 +0400411
412 if (ubrlcr & UBRLCR_PRTEN) {
413 if (ubrlcr & UBRLCR_EVENPRT)
414 parity = 'e';
415 else
416 parity = 'o';
417 }
418
419 if ((ubrlcr & UBRLCR_WRDLEN_MASK) == UBRLCR_WRDLEN7)
420 bits = 7;
421
422 quot = ubrlcr & UBRLCR_BAUD_MASK;
423 baud = port->uartclk / (16 * (quot + 1));
424 }
425 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 uart_parse_options(options, &baud, &parity, &bits, &flow);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Alexander Shiyanbc000242013-12-11 19:50:50 +0400428 ret = uart_set_options(port, co, baud, parity, bits, flow);
429 if (ret)
430 return ret;
431
432 return regmap_update_bits(s->syscon, SYSCON_OFFSET,
433 SYSCON_UARTEN, SYSCON_UARTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
Alexander Shiyanbc000242013-12-11 19:50:50 +0400435
436static struct console clps711x_console = {
437 .name = UART_CLPS711X_DEVNAME,
438 .device = uart_console_device,
439 .write = uart_clps711x_console_write,
440 .setup = uart_clps711x_console_setup,
441 .flags = CON_PRINTBUFFER,
442 .index = -1,
443};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444#endif
445
Bill Pemberton9671f092012-11-19 13:21:50 -0500446static int uart_clps711x_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400448 struct device_node *np = pdev->dev.of_node;
449 int ret, index = np ? of_alias_get_id(np, "serial") : pdev->id;
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400450 struct clps711x_port *s;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400451 struct resource *res;
452 struct clk *uart_clk;
Guenter Roeck8f5405c2016-02-09 07:06:47 -0800453 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Alexander Shiyanbc000242013-12-11 19:50:50 +0400455 if (index < 0 || index >= UART_CLPS711X_NR)
456 return -EINVAL;
457
458 s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
459 if (!s)
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400460 return -ENOMEM;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400461
462 uart_clk = devm_clk_get(&pdev->dev, NULL);
463 if (IS_ERR(uart_clk))
464 return PTR_ERR(uart_clk);
465
466 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
467 s->port.membase = devm_ioremap_resource(&pdev->dev, res);
468 if (IS_ERR(s->port.membase))
469 return PTR_ERR(s->port.membase);
470
Guenter Roeck8f5405c2016-02-09 07:06:47 -0800471 irq = platform_get_irq(pdev, 0);
472 if (irq < 0)
473 return irq;
474 s->port.irq = irq;
Alexander Shiyanbc000242013-12-11 19:50:50 +0400475
476 s->rx_irq = platform_get_irq(pdev, 1);
Guenter Roeck8f5405c2016-02-09 07:06:47 -0800477 if (s->rx_irq < 0)
Alexander Shiyanbc000242013-12-11 19:50:50 +0400478 return s->rx_irq;
479
480 if (!np) {
481 char syscon_name[9];
482
483 sprintf(syscon_name, "syscon.%i", index + 1);
484 s->syscon = syscon_regmap_lookup_by_pdevname(syscon_name);
485 if (IS_ERR(s->syscon))
486 return PTR_ERR(s->syscon);
Alexander Shiyanbc000242013-12-11 19:50:50 +0400487 } else {
488 s->syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
489 if (IS_ERR(s->syscon))
490 return PTR_ERR(s->syscon);
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400491 }
Alexander Shiyanbc000242013-12-11 19:50:50 +0400492
493 s->port.line = index;
494 s->port.dev = &pdev->dev;
495 s->port.iotype = UPIO_MEM32;
496 s->port.mapbase = res->start;
497 s->port.type = PORT_CLPS711X;
498 s->port.fifosize = 16;
499 s->port.flags = UPF_SKIP_TEST | UPF_FIXED_TYPE;
500 s->port.uartclk = clk_get_rate(uart_clk);
501 s->port.ops = &uart_clps711x_ops;
502
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400503 platform_set_drvdata(pdev, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Uwe Kleine-König7d8c70d2015-09-30 10:19:40 +0200505 s->gpios = mctrl_gpio_init_noauto(&pdev->dev, 0);
Uwe Kleine-Königf059a452015-02-12 15:24:39 +0100506 if (IS_ERR(s->gpios))
507 return PTR_ERR(s->gpios);
Alexander Shiyan62b0a1b2014-09-06 07:20:15 +0400508
Alexander Shiyanbc000242013-12-11 19:50:50 +0400509 ret = uart_add_one_port(&clps711x_uart, &s->port);
510 if (ret)
511 return ret;
Alexander Shiyanc08f0152012-10-14 11:05:26 +0400512
Alexander Shiyanbc000242013-12-11 19:50:50 +0400513 /* Disable port */
514 if (!uart_console(&s->port))
515 regmap_update_bits(s->syscon, SYSCON_OFFSET, SYSCON_UARTEN, 0);
516
517 s->tx_enabled = 1;
518
519 ret = devm_request_irq(&pdev->dev, s->port.irq, uart_clps711x_int_tx, 0,
520 dev_name(&pdev->dev), &s->port);
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400521 if (ret) {
Alexander Shiyanbc000242013-12-11 19:50:50 +0400522 uart_remove_one_port(&clps711x_uart, &s->port);
Jingoo Han43b829b2013-06-25 10:08:49 +0900523 return ret;
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Alexander Shiyanbc000242013-12-11 19:50:50 +0400526 ret = devm_request_irq(&pdev->dev, s->rx_irq, uart_clps711x_int_rx, 0,
527 dev_name(&pdev->dev), &s->port);
528 if (ret)
529 uart_remove_one_port(&clps711x_uart, &s->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Alexander Shiyanbc000242013-12-11 19:50:50 +0400531 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500534static int uart_clps711x_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Alexander Shiyan117d5d42012-10-14 11:05:24 +0400536 struct clps711x_port *s = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Alexander Shiyanbc000242013-12-11 19:50:50 +0400538 return uart_remove_one_port(&clps711x_uart, &s->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Alexander Shiyanbc000242013-12-11 19:50:50 +0400541static const struct of_device_id __maybe_unused clps711x_uart_dt_ids[] = {
542 { .compatible = "cirrus,clps711x-uart", },
543 { }
544};
545MODULE_DEVICE_TABLE(of, clps711x_uart_dt_ids);
546
547static struct platform_driver clps711x_uart_platform = {
Alexander Shiyan95113722012-10-14 11:05:23 +0400548 .driver = {
Alexander Shiyanbc000242013-12-11 19:50:50 +0400549 .name = "clps711x-uart",
Alexander Shiyanbc000242013-12-11 19:50:50 +0400550 .of_match_table = of_match_ptr(clps711x_uart_dt_ids),
Alexander Shiyan95113722012-10-14 11:05:23 +0400551 },
552 .probe = uart_clps711x_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500553 .remove = uart_clps711x_remove,
Alexander Shiyan95113722012-10-14 11:05:23 +0400554};
Alexander Shiyan95113722012-10-14 11:05:23 +0400555
556static int __init uart_clps711x_init(void)
557{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400558 int ret;
559
560#ifdef CONFIG_SERIAL_CLPS711X_CONSOLE
561 clps711x_uart.cons = &clps711x_console;
562 clps711x_console.data = &clps711x_uart;
563#endif
564
565 ret = uart_register_driver(&clps711x_uart);
566 if (ret)
567 return ret;
568
569 return platform_driver_register(&clps711x_uart_platform);
Alexander Shiyan95113722012-10-14 11:05:23 +0400570}
571module_init(uart_clps711x_init);
572
573static void __exit uart_clps711x_exit(void)
574{
Alexander Shiyanbc000242013-12-11 19:50:50 +0400575 platform_driver_unregister(&clps711x_uart_platform);
576 uart_unregister_driver(&clps711x_uart);
Alexander Shiyan95113722012-10-14 11:05:23 +0400577}
578module_exit(uart_clps711x_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580MODULE_AUTHOR("Deep Blue Solutions Ltd");
Alexander Shiyan95113722012-10-14 11:05:23 +0400581MODULE_DESCRIPTION("CLPS711X serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582MODULE_LICENSE("GPL");