blob: 16b0a5aa14e23cb17d3cecfdde6d3874d3f3231b [file] [log] [blame]
Wilson Ding30530792016-02-16 19:14:53 +01001/*
2* ***************************************************************************
Paul Gortmaker89ebc272016-03-13 19:48:52 -04003* Marvell Armada-3700 Serial Driver
4* Author: Wilson Ding <dingwei@marvell.com>
Wilson Ding30530792016-02-16 19:14:53 +01005* Copyright (C) 2015 Marvell International Ltd.
6* ***************************************************************************
7* This program is free software: you can redistribute it and/or modify it
8* under the terms of the GNU General Public License as published by the Free
9* Software Foundation, either version 2 of the License, or any later version.
10*
11* This program is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License
17* along with this program. If not, see <http://www.gnu.org/licenses/>.
18* ***************************************************************************
19*/
20
21#include <linux/clk.h>
22#include <linux/console.h>
23#include <linux/delay.h>
24#include <linux/device.h>
25#include <linux/init.h>
26#include <linux/io.h>
27#include <linux/iopoll.h>
Wilson Ding30530792016-02-16 19:14:53 +010028#include <linux/of.h>
29#include <linux/of_address.h>
30#include <linux/of_device.h>
31#include <linux/of_irq.h>
32#include <linux/of_platform.h>
33#include <linux/platform_device.h>
34#include <linux/serial.h>
35#include <linux/serial_core.h>
36#include <linux/slab.h>
37#include <linux/tty.h>
38#include <linux/tty_flip.h>
39
40/* Register Map */
Miquel Raynal5218d762017-10-13 11:01:49 +020041#define UART_STD_RBR 0x00
Miquel Raynal53501e02017-10-13 11:01:56 +020042#define UART_EXT_RBR 0x18
Wilson Ding30530792016-02-16 19:14:53 +010043
Miquel Raynal5218d762017-10-13 11:01:49 +020044#define UART_STD_TSH 0x04
Miquel Raynal53501e02017-10-13 11:01:56 +020045#define UART_EXT_TSH 0x1C
Wilson Ding30530792016-02-16 19:14:53 +010046
Miquel Raynal5218d762017-10-13 11:01:49 +020047#define UART_STD_CTRL1 0x08
Miquel Raynal53501e02017-10-13 11:01:56 +020048#define UART_EXT_CTRL1 0x04
Wilson Ding30530792016-02-16 19:14:53 +010049#define CTRL_SOFT_RST BIT(31)
50#define CTRL_TXFIFO_RST BIT(15)
51#define CTRL_RXFIFO_RST BIT(14)
Wilson Ding30530792016-02-16 19:14:53 +010052#define CTRL_SND_BRK_SEQ BIT(11)
Wilson Ding30530792016-02-16 19:14:53 +010053#define CTRL_BRK_DET_INT BIT(3)
54#define CTRL_FRM_ERR_INT BIT(2)
55#define CTRL_PAR_ERR_INT BIT(1)
56#define CTRL_OVR_ERR_INT BIT(0)
Miquel Raynal5218d762017-10-13 11:01:49 +020057#define CTRL_BRK_INT (CTRL_BRK_DET_INT | CTRL_FRM_ERR_INT | \
58 CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT)
Wilson Ding30530792016-02-16 19:14:53 +010059
Miquel Raynal5218d762017-10-13 11:01:49 +020060#define UART_STD_CTRL2 UART_STD_CTRL1
Miquel Raynal53501e02017-10-13 11:01:56 +020061#define UART_EXT_CTRL2 0x20
Miquel Raynal5218d762017-10-13 11:01:49 +020062#define CTRL_STD_TX_RDY_INT BIT(5)
Miquel Raynal53501e02017-10-13 11:01:56 +020063#define CTRL_EXT_TX_RDY_INT BIT(6)
Miquel Raynal5218d762017-10-13 11:01:49 +020064#define CTRL_STD_RX_RDY_INT BIT(4)
Miquel Raynal53501e02017-10-13 11:01:56 +020065#define CTRL_EXT_RX_RDY_INT BIT(5)
Miquel Raynal5218d762017-10-13 11:01:49 +020066
67#define UART_STAT 0x0C
Wilson Ding30530792016-02-16 19:14:53 +010068#define STAT_TX_FIFO_EMP BIT(13)
Wilson Ding30530792016-02-16 19:14:53 +010069#define STAT_TX_FIFO_FUL BIT(11)
Wilson Ding30530792016-02-16 19:14:53 +010070#define STAT_TX_EMP BIT(6)
Miquel Raynal5218d762017-10-13 11:01:49 +020071#define STAT_STD_TX_RDY BIT(5)
Miquel Raynal53501e02017-10-13 11:01:56 +020072#define STAT_EXT_TX_RDY BIT(15)
Miquel Raynal5218d762017-10-13 11:01:49 +020073#define STAT_STD_RX_RDY BIT(4)
Miquel Raynal53501e02017-10-13 11:01:56 +020074#define STAT_EXT_RX_RDY BIT(14)
Wilson Ding30530792016-02-16 19:14:53 +010075#define STAT_BRK_DET BIT(3)
76#define STAT_FRM_ERR BIT(2)
77#define STAT_PAR_ERR BIT(1)
78#define STAT_OVR_ERR BIT(0)
79#define STAT_BRK_ERR (STAT_BRK_DET | STAT_FRM_ERR | STAT_FRM_ERR\
80 | STAT_PAR_ERR | STAT_OVR_ERR)
81
82#define UART_BRDV 0x10
Allen Yan68a0db12017-10-13 11:01:51 +020083#define BRDV_BAUD_MASK 0x3FF
Wilson Ding30530792016-02-16 19:14:53 +010084
Miquel Raynal3a75e912017-10-13 11:01:55 +020085#define MVEBU_NR_UARTS 2
Wilson Ding30530792016-02-16 19:14:53 +010086
87#define MVEBU_UART_TYPE "mvebu-uart"
Yehuda Yitschak02c33332017-10-13 11:01:47 +020088#define DRIVER_NAME "mvebu_serial"
Wilson Ding30530792016-02-16 19:14:53 +010089
Miquel Raynal95f78762017-10-13 11:01:54 +020090enum {
91 /* Either there is only one summed IRQ... */
92 UART_IRQ_SUM = 0,
93 /* ...or there are two separate IRQ for RX and TX */
94 UART_RX_IRQ = 0,
95 UART_TX_IRQ,
96 UART_IRQ_COUNT
97};
98
99/* Diverging register offsets */
Miquel Raynal5218d762017-10-13 11:01:49 +0200100struct uart_regs_layout {
101 unsigned int rbr;
102 unsigned int tsh;
103 unsigned int ctrl;
104 unsigned int intr;
Wilson Ding30530792016-02-16 19:14:53 +0100105};
106
Miquel Raynal5218d762017-10-13 11:01:49 +0200107/* Diverging flags */
108struct uart_flags {
109 unsigned int ctrl_tx_rdy_int;
110 unsigned int ctrl_rx_rdy_int;
111 unsigned int stat_tx_rdy;
112 unsigned int stat_rx_rdy;
113};
114
115/* Driver data, a structure for each UART port */
116struct mvebu_uart_driver_data {
117 bool is_ext;
118 struct uart_regs_layout regs;
119 struct uart_flags flags;
120};
121
122/* MVEBU UART driver structure */
123struct mvebu_uart {
124 struct uart_port *port;
125 struct clk *clk;
Miquel Raynal95f78762017-10-13 11:01:54 +0200126 int irq[UART_IRQ_COUNT];
127 unsigned char __iomem *nb;
Miquel Raynal5218d762017-10-13 11:01:49 +0200128 struct mvebu_uart_driver_data *data;
129};
130
131static struct mvebu_uart *to_mvuart(struct uart_port *port)
132{
133 return (struct mvebu_uart *)port->private_data;
134}
135
136#define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext)
137
138#define UART_RBR(port) (to_mvuart(port)->data->regs.rbr)
139#define UART_TSH(port) (to_mvuart(port)->data->regs.tsh)
140#define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl)
141#define UART_INTR(port) (to_mvuart(port)->data->regs.intr)
142
143#define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int)
144#define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int)
145#define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy)
146#define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy)
147
148static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS];
149
Wilson Ding30530792016-02-16 19:14:53 +0100150/* Core UART Driver Operations */
151static unsigned int mvebu_uart_tx_empty(struct uart_port *port)
152{
153 unsigned long flags;
154 unsigned int st;
155
156 spin_lock_irqsave(&port->lock, flags);
157 st = readl(port->membase + UART_STAT);
158 spin_unlock_irqrestore(&port->lock, flags);
159
160 return (st & STAT_TX_FIFO_EMP) ? TIOCSER_TEMT : 0;
161}
162
163static unsigned int mvebu_uart_get_mctrl(struct uart_port *port)
164{
165 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
166}
167
168static void mvebu_uart_set_mctrl(struct uart_port *port,
169 unsigned int mctrl)
170{
171/*
172 * Even if we do not support configuring the modem control lines, this
173 * function must be proided to the serial core
174 */
175}
176
177static void mvebu_uart_stop_tx(struct uart_port *port)
178{
Miquel Raynal5218d762017-10-13 11:01:49 +0200179 unsigned int ctl = readl(port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100180
Miquel Raynal5218d762017-10-13 11:01:49 +0200181 ctl &= ~CTRL_TX_RDY_INT(port);
182 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100183}
184
185static void mvebu_uart_start_tx(struct uart_port *port)
186{
Allen Yan30434b02017-10-13 11:01:53 +0200187 unsigned int ctl;
188 struct circ_buf *xmit = &port->state->xmit;
Wilson Ding30530792016-02-16 19:14:53 +0100189
Allen Yan30434b02017-10-13 11:01:53 +0200190 if (IS_EXTENDED(port) && !uart_circ_empty(xmit)) {
191 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
192 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
193 port->icount.tx++;
194 }
195
196 ctl = readl(port->membase + UART_INTR(port));
Miquel Raynal5218d762017-10-13 11:01:49 +0200197 ctl |= CTRL_TX_RDY_INT(port);
198 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100199}
200
201static void mvebu_uart_stop_rx(struct uart_port *port)
202{
Miquel Raynal5218d762017-10-13 11:01:49 +0200203 unsigned int ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100204
Miquel Raynal5218d762017-10-13 11:01:49 +0200205 ctl = readl(port->membase + UART_CTRL(port));
206 ctl &= ~CTRL_BRK_INT;
207 writel(ctl, port->membase + UART_CTRL(port));
208
209 ctl = readl(port->membase + UART_INTR(port));
210 ctl &= ~CTRL_RX_RDY_INT(port);
211 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100212}
213
214static void mvebu_uart_break_ctl(struct uart_port *port, int brk)
215{
216 unsigned int ctl;
217 unsigned long flags;
218
219 spin_lock_irqsave(&port->lock, flags);
Miquel Raynal5218d762017-10-13 11:01:49 +0200220 ctl = readl(port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100221 if (brk == -1)
222 ctl |= CTRL_SND_BRK_SEQ;
223 else
224 ctl &= ~CTRL_SND_BRK_SEQ;
Miquel Raynal5218d762017-10-13 11:01:49 +0200225 writel(ctl, port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100226 spin_unlock_irqrestore(&port->lock, flags);
227}
228
229static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status)
230{
231 struct tty_port *tport = &port->state->port;
232 unsigned char ch = 0;
233 char flag = 0;
234
235 do {
Miquel Raynal5218d762017-10-13 11:01:49 +0200236 if (status & STAT_RX_RDY(port)) {
237 ch = readl(port->membase + UART_RBR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100238 ch &= 0xff;
239 flag = TTY_NORMAL;
240 port->icount.rx++;
241
242 if (status & STAT_PAR_ERR)
243 port->icount.parity++;
244 }
245
246 if (status & STAT_BRK_DET) {
247 port->icount.brk++;
248 status &= ~(STAT_FRM_ERR | STAT_PAR_ERR);
249 if (uart_handle_break(port))
250 goto ignore_char;
251 }
252
253 if (status & STAT_OVR_ERR)
254 port->icount.overrun++;
255
256 if (status & STAT_FRM_ERR)
257 port->icount.frame++;
258
259 if (uart_handle_sysrq_char(port, ch))
260 goto ignore_char;
261
262 if (status & port->ignore_status_mask & STAT_PAR_ERR)
Miquel Raynal5218d762017-10-13 11:01:49 +0200263 status &= ~STAT_RX_RDY(port);
Wilson Ding30530792016-02-16 19:14:53 +0100264
265 status &= port->read_status_mask;
266
267 if (status & STAT_PAR_ERR)
268 flag = TTY_PARITY;
269
270 status &= ~port->ignore_status_mask;
271
Miquel Raynal5218d762017-10-13 11:01:49 +0200272 if (status & STAT_RX_RDY(port))
Wilson Ding30530792016-02-16 19:14:53 +0100273 tty_insert_flip_char(tport, ch, flag);
274
275 if (status & STAT_BRK_DET)
276 tty_insert_flip_char(tport, 0, TTY_BREAK);
277
278 if (status & STAT_FRM_ERR)
279 tty_insert_flip_char(tport, 0, TTY_FRAME);
280
281 if (status & STAT_OVR_ERR)
282 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
283
284ignore_char:
285 status = readl(port->membase + UART_STAT);
Miquel Raynal5218d762017-10-13 11:01:49 +0200286 } while (status & (STAT_RX_RDY(port) | STAT_BRK_DET));
Wilson Ding30530792016-02-16 19:14:53 +0100287
288 tty_flip_buffer_push(tport);
289}
290
291static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status)
292{
293 struct circ_buf *xmit = &port->state->xmit;
294 unsigned int count;
295 unsigned int st;
296
297 if (port->x_char) {
Miquel Raynal5218d762017-10-13 11:01:49 +0200298 writel(port->x_char, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100299 port->icount.tx++;
300 port->x_char = 0;
301 return;
302 }
303
304 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
305 mvebu_uart_stop_tx(port);
306 return;
307 }
308
309 for (count = 0; count < port->fifosize; count++) {
Miquel Raynal5218d762017-10-13 11:01:49 +0200310 writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100311 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
312 port->icount.tx++;
313
314 if (uart_circ_empty(xmit))
315 break;
316
317 st = readl(port->membase + UART_STAT);
318 if (st & STAT_TX_FIFO_FUL)
319 break;
320 }
321
322 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
323 uart_write_wakeup(port);
324
325 if (uart_circ_empty(xmit))
326 mvebu_uart_stop_tx(port);
327}
328
329static irqreturn_t mvebu_uart_isr(int irq, void *dev_id)
330{
331 struct uart_port *port = (struct uart_port *)dev_id;
332 unsigned int st = readl(port->membase + UART_STAT);
333
Miquel Raynal5218d762017-10-13 11:01:49 +0200334 if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
Miquel Raynal95f78762017-10-13 11:01:54 +0200335 STAT_BRK_DET))
336 mvebu_uart_rx_chars(port, st);
337
338 if (st & STAT_TX_RDY(port))
339 mvebu_uart_tx_chars(port, st);
340
341 return IRQ_HANDLED;
342}
343
344static irqreturn_t mvebu_uart_rx_isr(int irq, void *dev_id)
345{
346 struct uart_port *port = (struct uart_port *)dev_id;
347 unsigned int st = readl(port->membase + UART_STAT);
348
349 if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
Miquel Raynal5218d762017-10-13 11:01:49 +0200350 STAT_BRK_DET))
Wilson Ding30530792016-02-16 19:14:53 +0100351 mvebu_uart_rx_chars(port, st);
352
Miquel Raynal95f78762017-10-13 11:01:54 +0200353 return IRQ_HANDLED;
354}
355
356static irqreturn_t mvebu_uart_tx_isr(int irq, void *dev_id)
357{
358 struct uart_port *port = (struct uart_port *)dev_id;
359 unsigned int st = readl(port->membase + UART_STAT);
360
Miquel Raynal5218d762017-10-13 11:01:49 +0200361 if (st & STAT_TX_RDY(port))
Wilson Ding30530792016-02-16 19:14:53 +0100362 mvebu_uart_tx_chars(port, st);
363
364 return IRQ_HANDLED;
365}
366
367static int mvebu_uart_startup(struct uart_port *port)
368{
Miquel Raynal95f78762017-10-13 11:01:54 +0200369 struct mvebu_uart *mvuart = to_mvuart(port);
Miquel Raynal5218d762017-10-13 11:01:49 +0200370 unsigned int ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100371 int ret;
372
373 writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST,
Miquel Raynal5218d762017-10-13 11:01:49 +0200374 port->membase + UART_CTRL(port));
Wilson Ding30530792016-02-16 19:14:53 +0100375 udelay(1);
Allen Yan2ff23c42017-10-13 11:01:52 +0200376
377 /* Clear the error bits of state register before IRQ request */
378 ret = readl(port->membase + UART_STAT);
379 ret |= STAT_BRK_ERR;
380 writel(ret, port->membase + UART_STAT);
381
Miquel Raynal5218d762017-10-13 11:01:49 +0200382 writel(CTRL_BRK_INT, port->membase + UART_CTRL(port));
383
384 ctl = readl(port->membase + UART_INTR(port));
385 ctl |= CTRL_RX_RDY_INT(port);
386 writel(ctl, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100387
Miquel Raynal95f78762017-10-13 11:01:54 +0200388 if (!mvuart->irq[UART_TX_IRQ]) {
389 /* Old bindings with just one interrupt (UART0 only) */
390 ret = devm_request_irq(port->dev, mvuart->irq[UART_IRQ_SUM],
391 mvebu_uart_isr, port->irqflags,
392 dev_name(port->dev), port);
393 if (ret) {
394 dev_err(port->dev, "unable to request IRQ %d\n",
395 mvuart->irq[UART_IRQ_SUM]);
396 return ret;
397 }
398 } else {
399 /* New bindings with an IRQ for RX and TX (both UART) */
400 ret = devm_request_irq(port->dev, mvuart->irq[UART_RX_IRQ],
401 mvebu_uart_rx_isr, port->irqflags,
402 dev_name(port->dev), port);
403 if (ret) {
404 dev_err(port->dev, "unable to request IRQ %d\n",
405 mvuart->irq[UART_RX_IRQ]);
406 return ret;
407 }
408
409 ret = devm_request_irq(port->dev, mvuart->irq[UART_TX_IRQ],
410 mvebu_uart_tx_isr, port->irqflags,
411 dev_name(port->dev),
412 port);
413 if (ret) {
414 dev_err(port->dev, "unable to request IRQ %d\n",
415 mvuart->irq[UART_TX_IRQ]);
416 devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ],
417 port);
418 return ret;
419 }
Wilson Ding30530792016-02-16 19:14:53 +0100420 }
421
422 return 0;
423}
424
425static void mvebu_uart_shutdown(struct uart_port *port)
426{
Miquel Raynal95f78762017-10-13 11:01:54 +0200427 struct mvebu_uart *mvuart = to_mvuart(port);
428
Miquel Raynal5218d762017-10-13 11:01:49 +0200429 writel(0, port->membase + UART_INTR(port));
Thomas Petazzonic2c16592016-06-16 16:48:52 +0200430
Miquel Raynal95f78762017-10-13 11:01:54 +0200431 if (!mvuart->irq[UART_TX_IRQ]) {
432 devm_free_irq(port->dev, mvuart->irq[UART_IRQ_SUM], port);
433 } else {
434 devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ], port);
435 devm_free_irq(port->dev, mvuart->irq[UART_TX_IRQ], port);
436 }
Wilson Ding30530792016-02-16 19:14:53 +0100437}
438
Allen Yan68a0db12017-10-13 11:01:51 +0200439static int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud)
440{
441 struct mvebu_uart *mvuart = to_mvuart(port);
442 unsigned int baud_rate_div;
443 u32 brdv;
444
445 if (IS_ERR(mvuart->clk))
446 return -PTR_ERR(mvuart->clk);
447
448 /*
449 * The UART clock is divided by the value of the divisor to generate
450 * UCLK_OUT clock, which is 16 times faster than the baudrate.
451 * This prescaler can achieve all standard baudrates until 230400.
452 * Higher baudrates could be achieved for the extended UART by using the
453 * programmable oversampling stack (also called fractional divisor).
454 */
455 baud_rate_div = DIV_ROUND_UP(port->uartclk, baud * 16);
456 brdv = readl(port->membase + UART_BRDV);
457 brdv &= ~BRDV_BAUD_MASK;
458 brdv |= baud_rate_div;
459 writel(brdv, port->membase + UART_BRDV);
460
461 return 0;
462}
463
Wilson Ding30530792016-02-16 19:14:53 +0100464static void mvebu_uart_set_termios(struct uart_port *port,
465 struct ktermios *termios,
466 struct ktermios *old)
467{
468 unsigned long flags;
469 unsigned int baud;
470
471 spin_lock_irqsave(&port->lock, flags);
472
Miquel Raynal5218d762017-10-13 11:01:49 +0200473 port->read_status_mask = STAT_RX_RDY(port) | STAT_OVR_ERR |
474 STAT_TX_RDY(port) | STAT_TX_FIFO_FUL;
Wilson Ding30530792016-02-16 19:14:53 +0100475
476 if (termios->c_iflag & INPCK)
477 port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR;
478
479 port->ignore_status_mask = 0;
480 if (termios->c_iflag & IGNPAR)
481 port->ignore_status_mask |=
482 STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR;
483
484 if ((termios->c_cflag & CREAD) == 0)
Miquel Raynal5218d762017-10-13 11:01:49 +0200485 port->ignore_status_mask |= STAT_RX_RDY(port) | STAT_BRK_ERR;
Wilson Ding30530792016-02-16 19:14:53 +0100486
Allen Yan68a0db12017-10-13 11:01:51 +0200487 /*
488 * Maximum achievable frequency with simple baudrate divisor is 230400.
489 * Since the error per bit frame would be of more than 15%, achieving
490 * higher frequencies would require to implement the fractional divisor
491 * feature.
492 */
493 baud = uart_get_baud_rate(port, termios, old, 0, 230400);
494 if (mvebu_uart_baud_rate_set(port, baud)) {
495 /* No clock available, baudrate cannot be changed */
496 if (old)
497 baud = uart_get_baud_rate(port, old, NULL, 0, 230400);
498 } else {
499 tty_termios_encode_baud_rate(termios, baud, baud);
500 uart_update_timeout(port, termios->c_cflag, baud);
501 }
Wilson Ding30530792016-02-16 19:14:53 +0100502
Allen Yan68a0db12017-10-13 11:01:51 +0200503 /* Only the following flag changes are supported */
504 if (old) {
505 termios->c_iflag &= INPCK | IGNPAR;
506 termios->c_iflag |= old->c_iflag & ~(INPCK | IGNPAR);
507 termios->c_cflag &= CREAD | CBAUD;
508 termios->c_cflag |= old->c_cflag & ~(CREAD | CBAUD);
509 termios->c_lflag = old->c_lflag;
510 }
Wilson Ding30530792016-02-16 19:14:53 +0100511
512 spin_unlock_irqrestore(&port->lock, flags);
513}
514
515static const char *mvebu_uart_type(struct uart_port *port)
516{
517 return MVEBU_UART_TYPE;
518}
519
520static void mvebu_uart_release_port(struct uart_port *port)
521{
522 /* Nothing to do here */
523}
524
525static int mvebu_uart_request_port(struct uart_port *port)
526{
527 return 0;
528}
529
530#ifdef CONFIG_CONSOLE_POLL
531static int mvebu_uart_get_poll_char(struct uart_port *port)
532{
533 unsigned int st = readl(port->membase + UART_STAT);
534
Miquel Raynal5218d762017-10-13 11:01:49 +0200535 if (!(st & STAT_RX_RDY(port)))
Wilson Ding30530792016-02-16 19:14:53 +0100536 return NO_POLL_CHAR;
537
Miquel Raynal5218d762017-10-13 11:01:49 +0200538 return readl(port->membase + UART_RBR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100539}
540
541static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
542{
543 unsigned int st;
544
545 for (;;) {
546 st = readl(port->membase + UART_STAT);
547
548 if (!(st & STAT_TX_FIFO_FUL))
549 break;
550
551 udelay(1);
552 }
553
Miquel Raynal5218d762017-10-13 11:01:49 +0200554 writel(c, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100555}
556#endif
557
558static const struct uart_ops mvebu_uart_ops = {
559 .tx_empty = mvebu_uart_tx_empty,
560 .set_mctrl = mvebu_uart_set_mctrl,
561 .get_mctrl = mvebu_uart_get_mctrl,
562 .stop_tx = mvebu_uart_stop_tx,
563 .start_tx = mvebu_uart_start_tx,
564 .stop_rx = mvebu_uart_stop_rx,
565 .break_ctl = mvebu_uart_break_ctl,
566 .startup = mvebu_uart_startup,
567 .shutdown = mvebu_uart_shutdown,
568 .set_termios = mvebu_uart_set_termios,
569 .type = mvebu_uart_type,
570 .release_port = mvebu_uart_release_port,
571 .request_port = mvebu_uart_request_port,
572#ifdef CONFIG_CONSOLE_POLL
573 .poll_get_char = mvebu_uart_get_poll_char,
574 .poll_put_char = mvebu_uart_put_poll_char,
575#endif
576};
577
578/* Console Driver Operations */
579
580#ifdef CONFIG_SERIAL_MVEBU_CONSOLE
581/* Early Console */
582static void mvebu_uart_putc(struct uart_port *port, int c)
583{
584 unsigned int st;
585
586 for (;;) {
587 st = readl(port->membase + UART_STAT);
588 if (!(st & STAT_TX_FIFO_FUL))
589 break;
590 }
591
Miquel Raynal5218d762017-10-13 11:01:49 +0200592 /* At early stage, DT is not parsed yet, only use UART0 */
593 writel(c, port->membase + UART_STD_TSH);
Wilson Ding30530792016-02-16 19:14:53 +0100594
595 for (;;) {
596 st = readl(port->membase + UART_STAT);
597 if (st & STAT_TX_FIFO_EMP)
598 break;
599 }
600}
601
602static void mvebu_uart_putc_early_write(struct console *con,
603 const char *s,
604 unsigned n)
605{
606 struct earlycon_device *dev = con->data;
607
608 uart_console_write(&dev->port, s, n, mvebu_uart_putc);
609}
610
611static int __init
612mvebu_uart_early_console_setup(struct earlycon_device *device,
613 const char *opt)
614{
615 if (!device->port.membase)
616 return -ENODEV;
617
618 device->con->write = mvebu_uart_putc_early_write;
619
620 return 0;
621}
622
623EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
624OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
625 mvebu_uart_early_console_setup);
626
627static void wait_for_xmitr(struct uart_port *port)
628{
629 u32 val;
630
631 readl_poll_timeout_atomic(port->membase + UART_STAT, val,
632 (val & STAT_TX_EMP), 1, 10000);
633}
634
635static void mvebu_uart_console_putchar(struct uart_port *port, int ch)
636{
637 wait_for_xmitr(port);
Miquel Raynal5218d762017-10-13 11:01:49 +0200638 writel(ch, port->membase + UART_TSH(port));
Wilson Ding30530792016-02-16 19:14:53 +0100639}
640
641static void mvebu_uart_console_write(struct console *co, const char *s,
642 unsigned int count)
643{
644 struct uart_port *port = &mvebu_uart_ports[co->index];
645 unsigned long flags;
Miquel Raynal5218d762017-10-13 11:01:49 +0200646 unsigned int ier, intr, ctl;
Wilson Ding30530792016-02-16 19:14:53 +0100647 int locked = 1;
648
649 if (oops_in_progress)
650 locked = spin_trylock_irqsave(&port->lock, flags);
651 else
652 spin_lock_irqsave(&port->lock, flags);
653
Miquel Raynal5218d762017-10-13 11:01:49 +0200654 ier = readl(port->membase + UART_CTRL(port)) & CTRL_BRK_INT;
655 intr = readl(port->membase + UART_INTR(port)) &
656 (CTRL_RX_RDY_INT(port) | CTRL_TX_RDY_INT(port));
657 writel(0, port->membase + UART_CTRL(port));
658 writel(0, port->membase + UART_INTR(port));
Wilson Ding30530792016-02-16 19:14:53 +0100659
660 uart_console_write(port, s, count, mvebu_uart_console_putchar);
661
662 wait_for_xmitr(port);
663
664 if (ier)
Miquel Raynal5218d762017-10-13 11:01:49 +0200665 writel(ier, port->membase + UART_CTRL(port));
666
667 if (intr) {
668 ctl = intr | readl(port->membase + UART_INTR(port));
669 writel(ctl, port->membase + UART_INTR(port));
670 }
Wilson Ding30530792016-02-16 19:14:53 +0100671
672 if (locked)
673 spin_unlock_irqrestore(&port->lock, flags);
674}
675
676static int mvebu_uart_console_setup(struct console *co, char *options)
677{
678 struct uart_port *port;
679 int baud = 9600;
680 int bits = 8;
681 int parity = 'n';
682 int flow = 'n';
683
684 if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
685 return -EINVAL;
686
687 port = &mvebu_uart_ports[co->index];
688
689 if (!port->mapbase || !port->membase) {
690 pr_debug("console on ttyMV%i not present\n", co->index);
691 return -ENODEV;
692 }
693
694 if (options)
695 uart_parse_options(options, &baud, &parity, &bits, &flow);
696
697 return uart_set_options(port, co, baud, parity, bits, flow);
698}
699
700static struct uart_driver mvebu_uart_driver;
701
702static struct console mvebu_uart_console = {
703 .name = "ttyMV",
704 .write = mvebu_uart_console_write,
705 .device = uart_console_device,
706 .setup = mvebu_uart_console_setup,
707 .flags = CON_PRINTBUFFER,
708 .index = -1,
709 .data = &mvebu_uart_driver,
710};
711
712static int __init mvebu_uart_console_init(void)
713{
714 register_console(&mvebu_uart_console);
715 return 0;
716}
717
718console_initcall(mvebu_uart_console_init);
719
720
721#endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
722
723static struct uart_driver mvebu_uart_driver = {
724 .owner = THIS_MODULE,
Yehuda Yitschak02c33332017-10-13 11:01:47 +0200725 .driver_name = DRIVER_NAME,
Wilson Ding30530792016-02-16 19:14:53 +0100726 .dev_name = "ttyMV",
727 .nr = MVEBU_NR_UARTS,
728#ifdef CONFIG_SERIAL_MVEBU_CONSOLE
729 .cons = &mvebu_uart_console,
730#endif
731};
732
Miquel Raynal5218d762017-10-13 11:01:49 +0200733static const struct of_device_id mvebu_uart_of_match[];
734
Allen Yan94228f92017-10-13 11:01:48 +0200735/* Counter to keep track of each UART port id when not using CONFIG_OF */
736static int uart_num_counter;
737
Wilson Ding30530792016-02-16 19:14:53 +0100738static int mvebu_uart_probe(struct platform_device *pdev)
739{
740 struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Miquel Raynal5218d762017-10-13 11:01:49 +0200741 const struct of_device_id *match = of_match_device(mvebu_uart_of_match,
742 &pdev->dev);
Wilson Ding30530792016-02-16 19:14:53 +0100743 struct uart_port *port;
Miquel Raynal5218d762017-10-13 11:01:49 +0200744 struct mvebu_uart *mvuart;
Miquel Raynal95f78762017-10-13 11:01:54 +0200745 int ret, id, irq;
Wilson Ding30530792016-02-16 19:14:53 +0100746
Miquel Raynal95f78762017-10-13 11:01:54 +0200747 if (!reg) {
748 dev_err(&pdev->dev, "no registers defined\n");
Wilson Ding30530792016-02-16 19:14:53 +0100749 return -EINVAL;
750 }
751
Allen Yan94228f92017-10-13 11:01:48 +0200752 /* Assume that all UART ports have a DT alias or none has */
753 id = of_alias_get_id(pdev->dev.of_node, "serial");
754 if (!pdev->dev.of_node || id < 0)
755 pdev->id = uart_num_counter++;
756 else
757 pdev->id = id;
758
759 if (pdev->id >= MVEBU_NR_UARTS) {
760 dev_err(&pdev->dev, "cannot have more than %d UART ports\n",
761 MVEBU_NR_UARTS);
762 return -EINVAL;
763 }
764
765 port = &mvebu_uart_ports[pdev->id];
Wilson Ding30530792016-02-16 19:14:53 +0100766
767 spin_lock_init(&port->lock);
768
769 port->dev = &pdev->dev;
770 port->type = PORT_MVEBU;
771 port->ops = &mvebu_uart_ops;
772 port->regshift = 0;
773
774 port->fifosize = 32;
775 port->iotype = UPIO_MEM32;
776 port->flags = UPF_FIXED_PORT;
Allen Yan94228f92017-10-13 11:01:48 +0200777 port->line = pdev->id;
Wilson Ding30530792016-02-16 19:14:53 +0100778
Miquel Raynal95f78762017-10-13 11:01:54 +0200779 /*
780 * IRQ number is not stored in this structure because we may have two of
781 * them per port (RX and TX). Instead, use the driver UART structure
782 * array so called ->irq[].
783 */
784 port->irq = 0;
Wilson Ding30530792016-02-16 19:14:53 +0100785 port->irqflags = 0;
786 port->mapbase = reg->start;
787
788 port->membase = devm_ioremap_resource(&pdev->dev, reg);
789 if (IS_ERR(port->membase))
790 return -PTR_ERR(port->membase);
791
Miquel Raynal5218d762017-10-13 11:01:49 +0200792 mvuart = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart),
793 GFP_KERNEL);
794 if (!mvuart)
Wilson Ding30530792016-02-16 19:14:53 +0100795 return -ENOMEM;
796
Allen Yan68a0db12017-10-13 11:01:51 +0200797 /* Get controller data depending on the compatible string */
Miquel Raynal5218d762017-10-13 11:01:49 +0200798 mvuart->data = (struct mvebu_uart_driver_data *)match->data;
799 mvuart->port = port;
Wilson Ding30530792016-02-16 19:14:53 +0100800
Miquel Raynal5218d762017-10-13 11:01:49 +0200801 port->private_data = mvuart;
802 platform_set_drvdata(pdev, mvuart);
Wilson Ding30530792016-02-16 19:14:53 +0100803
Allen Yan68a0db12017-10-13 11:01:51 +0200804 /* Get fixed clock frequency */
805 mvuart->clk = devm_clk_get(&pdev->dev, NULL);
806 if (IS_ERR(mvuart->clk)) {
807 if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER)
808 return PTR_ERR(mvuart->clk);
809
810 if (IS_EXTENDED(port)) {
811 dev_err(&pdev->dev, "unable to get UART clock\n");
812 return PTR_ERR(mvuart->clk);
813 }
814 } else {
815 if (!clk_prepare_enable(mvuart->clk))
816 port->uartclk = clk_get_rate(mvuart->clk);
817 }
818
Miquel Raynal95f78762017-10-13 11:01:54 +0200819 /* Manage interrupts */
820 memset(mvuart->irq, 0, UART_IRQ_COUNT);
821 if (platform_irq_count(pdev) == 1) {
822 /* Old bindings: no name on the single unamed UART0 IRQ */
823 irq = platform_get_irq(pdev, 0);
824 if (irq < 0) {
825 dev_err(&pdev->dev, "unable to get UART IRQ\n");
826 return irq;
827 }
828
829 mvuart->irq[UART_IRQ_SUM] = irq;
830 } else {
831 /*
832 * New bindings: named interrupts (RX, TX) for both UARTS,
833 * only make use of uart-rx and uart-tx interrupts, do not use
834 * uart-sum of UART0 port.
835 */
836 irq = platform_get_irq_byname(pdev, "uart-rx");
837 if (irq < 0) {
838 dev_err(&pdev->dev, "unable to get 'uart-rx' IRQ\n");
839 return irq;
840 }
841
842 mvuart->irq[UART_RX_IRQ] = irq;
843
844 irq = platform_get_irq_byname(pdev, "uart-tx");
845 if (irq < 0) {
846 dev_err(&pdev->dev, "unable to get 'uart-tx' IRQ\n");
847 return irq;
848 }
849
850 mvuart->irq[UART_TX_IRQ] = irq;
851 }
852
Allen Yan9c3d3ee2017-10-13 11:01:50 +0200853 /* UART Soft Reset*/
854 writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port));
855 udelay(1);
856 writel(0, port->membase + UART_CTRL(port));
857
Wilson Ding30530792016-02-16 19:14:53 +0100858 ret = uart_add_one_port(&mvebu_uart_driver, port);
859 if (ret)
860 return ret;
861 return 0;
862}
863
Miquel Raynal5218d762017-10-13 11:01:49 +0200864static struct mvebu_uart_driver_data uart_std_driver_data = {
865 .is_ext = false,
866 .regs.rbr = UART_STD_RBR,
867 .regs.tsh = UART_STD_TSH,
868 .regs.ctrl = UART_STD_CTRL1,
869 .regs.intr = UART_STD_CTRL2,
870 .flags.ctrl_tx_rdy_int = CTRL_STD_TX_RDY_INT,
871 .flags.ctrl_rx_rdy_int = CTRL_STD_RX_RDY_INT,
872 .flags.stat_tx_rdy = STAT_STD_TX_RDY,
873 .flags.stat_rx_rdy = STAT_STD_RX_RDY,
874};
875
Miquel Raynal53501e02017-10-13 11:01:56 +0200876static struct mvebu_uart_driver_data uart_ext_driver_data = {
877 .is_ext = true,
878 .regs.rbr = UART_EXT_RBR,
879 .regs.tsh = UART_EXT_TSH,
880 .regs.ctrl = UART_EXT_CTRL1,
881 .regs.intr = UART_EXT_CTRL2,
882 .flags.ctrl_tx_rdy_int = CTRL_EXT_TX_RDY_INT,
883 .flags.ctrl_rx_rdy_int = CTRL_EXT_RX_RDY_INT,
884 .flags.stat_tx_rdy = STAT_EXT_TX_RDY,
885 .flags.stat_rx_rdy = STAT_EXT_RX_RDY,
886};
887
Wilson Ding30530792016-02-16 19:14:53 +0100888/* Match table for of_platform binding */
889static const struct of_device_id mvebu_uart_of_match[] = {
Miquel Raynal5218d762017-10-13 11:01:49 +0200890 {
891 .compatible = "marvell,armada-3700-uart",
892 .data = (void *)&uart_std_driver_data,
893 },
Miquel Raynal53501e02017-10-13 11:01:56 +0200894 {
895 .compatible = "marvell,armada-3700-uart-ext",
896 .data = (void *)&uart_ext_driver_data,
897 },
Wilson Ding30530792016-02-16 19:14:53 +0100898 {}
899};
Wilson Ding30530792016-02-16 19:14:53 +0100900
901static struct platform_driver mvebu_uart_platform_driver = {
902 .probe = mvebu_uart_probe,
Wilson Ding30530792016-02-16 19:14:53 +0100903 .driver = {
Wilson Ding30530792016-02-16 19:14:53 +0100904 .name = "mvebu-uart",
905 .of_match_table = of_match_ptr(mvebu_uart_of_match),
Paul Gortmaker89ebc272016-03-13 19:48:52 -0400906 .suppress_bind_attrs = true,
Wilson Ding30530792016-02-16 19:14:53 +0100907 },
908};
909
910static int __init mvebu_uart_init(void)
911{
912 int ret;
913
914 ret = uart_register_driver(&mvebu_uart_driver);
915 if (ret)
916 return ret;
917
918 ret = platform_driver_register(&mvebu_uart_platform_driver);
919 if (ret)
920 uart_unregister_driver(&mvebu_uart_driver);
921
922 return ret;
923}
Wilson Ding30530792016-02-16 19:14:53 +0100924arch_initcall(mvebu_uart_init);