• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * ***************************************************************************
4 * Marvell Armada-3700 Serial Driver
5 * Author: Wilson Ding <dingwei@marvell.com>
6 * Copyright (C) 2015 Marvell International Ltd.
7 * ***************************************************************************
8 */
9 
10 #include <linux/clk.h>
11 #include <linux/console.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/init.h>
15 #include <linux/io.h>
16 #include <linux/iopoll.h>
17 #include <linux/of.h>
18 #include <linux/of_address.h>
19 #include <linux/of_device.h>
20 #include <linux/of_irq.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/serial.h>
24 #include <linux/serial_core.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
28 
29 /* Register Map */
30 #define UART_STD_RBR		0x00
31 #define UART_EXT_RBR		0x18
32 
33 #define UART_STD_TSH		0x04
34 #define UART_EXT_TSH		0x1C
35 
36 #define UART_STD_CTRL1		0x08
37 #define UART_EXT_CTRL1		0x04
38 #define  CTRL_SOFT_RST		BIT(31)
39 #define  CTRL_TXFIFO_RST	BIT(15)
40 #define  CTRL_RXFIFO_RST	BIT(14)
41 #define  CTRL_SND_BRK_SEQ	BIT(11)
42 #define  CTRL_BRK_DET_INT	BIT(3)
43 #define  CTRL_FRM_ERR_INT	BIT(2)
44 #define  CTRL_PAR_ERR_INT	BIT(1)
45 #define  CTRL_OVR_ERR_INT	BIT(0)
46 #define  CTRL_BRK_INT		(CTRL_BRK_DET_INT | CTRL_FRM_ERR_INT | \
47 				CTRL_PAR_ERR_INT | CTRL_OVR_ERR_INT)
48 
49 #define UART_STD_CTRL2		UART_STD_CTRL1
50 #define UART_EXT_CTRL2		0x20
51 #define  CTRL_STD_TX_RDY_INT	BIT(5)
52 #define  CTRL_EXT_TX_RDY_INT	BIT(6)
53 #define  CTRL_STD_RX_RDY_INT	BIT(4)
54 #define  CTRL_EXT_RX_RDY_INT	BIT(5)
55 
56 #define UART_STAT		0x0C
57 #define  STAT_TX_FIFO_EMP	BIT(13)
58 #define  STAT_TX_FIFO_FUL	BIT(11)
59 #define  STAT_TX_EMP		BIT(6)
60 #define  STAT_STD_TX_RDY	BIT(5)
61 #define  STAT_EXT_TX_RDY	BIT(15)
62 #define  STAT_STD_RX_RDY	BIT(4)
63 #define  STAT_EXT_RX_RDY	BIT(14)
64 #define  STAT_BRK_DET		BIT(3)
65 #define  STAT_FRM_ERR		BIT(2)
66 #define  STAT_PAR_ERR		BIT(1)
67 #define  STAT_OVR_ERR		BIT(0)
68 #define  STAT_BRK_ERR		(STAT_BRK_DET | STAT_FRM_ERR \
69 				 | STAT_PAR_ERR | STAT_OVR_ERR)
70 
71 #define UART_BRDV		0x10
72 #define  BRDV_BAUD_MASK         0x3FF
73 
74 #define UART_OSAMP		0x14
75 #define  OSAMP_DEFAULT_DIVISOR	16
76 #define  OSAMP_DIVISORS_MASK	0x3F3F3F3F
77 
78 #define MVEBU_NR_UARTS		2
79 
80 #define MVEBU_UART_TYPE		"mvebu-uart"
81 #define DRIVER_NAME		"mvebu_serial"
82 
83 enum {
84 	/* Either there is only one summed IRQ... */
85 	UART_IRQ_SUM = 0,
86 	/* ...or there are two separate IRQ for RX and TX */
87 	UART_RX_IRQ = 0,
88 	UART_TX_IRQ,
89 	UART_IRQ_COUNT
90 };
91 
92 /* Diverging register offsets */
93 struct uart_regs_layout {
94 	unsigned int rbr;
95 	unsigned int tsh;
96 	unsigned int ctrl;
97 	unsigned int intr;
98 };
99 
100 /* Diverging flags */
101 struct uart_flags {
102 	unsigned int ctrl_tx_rdy_int;
103 	unsigned int ctrl_rx_rdy_int;
104 	unsigned int stat_tx_rdy;
105 	unsigned int stat_rx_rdy;
106 };
107 
108 /* Driver data, a structure for each UART port */
109 struct mvebu_uart_driver_data {
110 	bool is_ext;
111 	struct uart_regs_layout regs;
112 	struct uart_flags flags;
113 };
114 
115 /* Saved registers during suspend */
116 struct mvebu_uart_pm_regs {
117 	unsigned int rbr;
118 	unsigned int tsh;
119 	unsigned int ctrl;
120 	unsigned int intr;
121 	unsigned int stat;
122 	unsigned int brdv;
123 	unsigned int osamp;
124 };
125 
126 /* MVEBU UART driver structure */
127 struct mvebu_uart {
128 	struct uart_port *port;
129 	struct clk *clk;
130 	int irq[UART_IRQ_COUNT];
131 	unsigned char __iomem *nb;
132 	struct mvebu_uart_driver_data *data;
133 #if defined(CONFIG_PM)
134 	struct mvebu_uart_pm_regs pm_regs;
135 #endif /* CONFIG_PM */
136 };
137 
to_mvuart(struct uart_port * port)138 static struct mvebu_uart *to_mvuart(struct uart_port *port)
139 {
140 	return (struct mvebu_uart *)port->private_data;
141 }
142 
143 #define IS_EXTENDED(port) (to_mvuart(port)->data->is_ext)
144 
145 #define UART_RBR(port) (to_mvuart(port)->data->regs.rbr)
146 #define UART_TSH(port) (to_mvuart(port)->data->regs.tsh)
147 #define UART_CTRL(port) (to_mvuart(port)->data->regs.ctrl)
148 #define UART_INTR(port) (to_mvuart(port)->data->regs.intr)
149 
150 #define CTRL_TX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_tx_rdy_int)
151 #define CTRL_RX_RDY_INT(port) (to_mvuart(port)->data->flags.ctrl_rx_rdy_int)
152 #define STAT_TX_RDY(port) (to_mvuart(port)->data->flags.stat_tx_rdy)
153 #define STAT_RX_RDY(port) (to_mvuart(port)->data->flags.stat_rx_rdy)
154 
155 static struct uart_port mvebu_uart_ports[MVEBU_NR_UARTS];
156 
157 /* Core UART Driver Operations */
mvebu_uart_tx_empty(struct uart_port * port)158 static unsigned int mvebu_uart_tx_empty(struct uart_port *port)
159 {
160 	unsigned long flags;
161 	unsigned int st;
162 
163 	spin_lock_irqsave(&port->lock, flags);
164 	st = readl(port->membase + UART_STAT);
165 	spin_unlock_irqrestore(&port->lock, flags);
166 
167 	return (st & STAT_TX_EMP) ? TIOCSER_TEMT : 0;
168 }
169 
mvebu_uart_get_mctrl(struct uart_port * port)170 static unsigned int mvebu_uart_get_mctrl(struct uart_port *port)
171 {
172 	return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
173 }
174 
mvebu_uart_set_mctrl(struct uart_port * port,unsigned int mctrl)175 static void mvebu_uart_set_mctrl(struct uart_port *port,
176 				 unsigned int mctrl)
177 {
178 /*
179  * Even if we do not support configuring the modem control lines, this
180  * function must be proided to the serial core
181  */
182 }
183 
mvebu_uart_stop_tx(struct uart_port * port)184 static void mvebu_uart_stop_tx(struct uart_port *port)
185 {
186 	unsigned int ctl = readl(port->membase + UART_INTR(port));
187 
188 	ctl &= ~CTRL_TX_RDY_INT(port);
189 	writel(ctl, port->membase + UART_INTR(port));
190 }
191 
mvebu_uart_start_tx(struct uart_port * port)192 static void mvebu_uart_start_tx(struct uart_port *port)
193 {
194 	unsigned int ctl;
195 	struct circ_buf *xmit = &port->state->xmit;
196 
197 	if (IS_EXTENDED(port) && !uart_circ_empty(xmit)) {
198 		writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
199 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
200 		port->icount.tx++;
201 	}
202 
203 	ctl = readl(port->membase + UART_INTR(port));
204 	ctl |= CTRL_TX_RDY_INT(port);
205 	writel(ctl, port->membase + UART_INTR(port));
206 }
207 
mvebu_uart_stop_rx(struct uart_port * port)208 static void mvebu_uart_stop_rx(struct uart_port *port)
209 {
210 	unsigned int ctl;
211 
212 	ctl = readl(port->membase + UART_CTRL(port));
213 	ctl &= ~CTRL_BRK_INT;
214 	writel(ctl, port->membase + UART_CTRL(port));
215 
216 	ctl = readl(port->membase + UART_INTR(port));
217 	ctl &= ~CTRL_RX_RDY_INT(port);
218 	writel(ctl, port->membase + UART_INTR(port));
219 }
220 
mvebu_uart_break_ctl(struct uart_port * port,int brk)221 static void mvebu_uart_break_ctl(struct uart_port *port, int brk)
222 {
223 	unsigned int ctl;
224 	unsigned long flags;
225 
226 	spin_lock_irqsave(&port->lock, flags);
227 	ctl = readl(port->membase + UART_CTRL(port));
228 	if (brk == -1)
229 		ctl |= CTRL_SND_BRK_SEQ;
230 	else
231 		ctl &= ~CTRL_SND_BRK_SEQ;
232 	writel(ctl, port->membase + UART_CTRL(port));
233 	spin_unlock_irqrestore(&port->lock, flags);
234 }
235 
mvebu_uart_rx_chars(struct uart_port * port,unsigned int status)236 static void mvebu_uart_rx_chars(struct uart_port *port, unsigned int status)
237 {
238 	struct tty_port *tport = &port->state->port;
239 	unsigned char ch = 0;
240 	char flag = 0;
241 
242 	do {
243 		if (status & STAT_RX_RDY(port)) {
244 			ch = readl(port->membase + UART_RBR(port));
245 			ch &= 0xff;
246 			flag = TTY_NORMAL;
247 			port->icount.rx++;
248 
249 			if (status & STAT_PAR_ERR)
250 				port->icount.parity++;
251 		}
252 
253 		if (status & STAT_BRK_DET) {
254 			port->icount.brk++;
255 			status &= ~(STAT_FRM_ERR | STAT_PAR_ERR);
256 			if (uart_handle_break(port))
257 				goto ignore_char;
258 		}
259 
260 		if (status & STAT_OVR_ERR)
261 			port->icount.overrun++;
262 
263 		if (status & STAT_FRM_ERR)
264 			port->icount.frame++;
265 
266 		if (uart_handle_sysrq_char(port, ch))
267 			goto ignore_char;
268 
269 		if (status & port->ignore_status_mask & STAT_PAR_ERR)
270 			status &= ~STAT_RX_RDY(port);
271 
272 		status &= port->read_status_mask;
273 
274 		if (status & STAT_PAR_ERR)
275 			flag = TTY_PARITY;
276 
277 		status &= ~port->ignore_status_mask;
278 
279 		if (status & STAT_RX_RDY(port))
280 			tty_insert_flip_char(tport, ch, flag);
281 
282 		if (status & STAT_BRK_DET)
283 			tty_insert_flip_char(tport, 0, TTY_BREAK);
284 
285 		if (status & STAT_FRM_ERR)
286 			tty_insert_flip_char(tport, 0, TTY_FRAME);
287 
288 		if (status & STAT_OVR_ERR)
289 			tty_insert_flip_char(tport, 0, TTY_OVERRUN);
290 
291 ignore_char:
292 		status = readl(port->membase + UART_STAT);
293 	} while (status & (STAT_RX_RDY(port) | STAT_BRK_DET));
294 
295 	tty_flip_buffer_push(tport);
296 }
297 
mvebu_uart_tx_chars(struct uart_port * port,unsigned int status)298 static void mvebu_uart_tx_chars(struct uart_port *port, unsigned int status)
299 {
300 	struct circ_buf *xmit = &port->state->xmit;
301 	unsigned int count;
302 	unsigned int st;
303 
304 	if (port->x_char) {
305 		writel(port->x_char, port->membase + UART_TSH(port));
306 		port->icount.tx++;
307 		port->x_char = 0;
308 		return;
309 	}
310 
311 	if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
312 		mvebu_uart_stop_tx(port);
313 		return;
314 	}
315 
316 	for (count = 0; count < port->fifosize; count++) {
317 		writel(xmit->buf[xmit->tail], port->membase + UART_TSH(port));
318 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
319 		port->icount.tx++;
320 
321 		if (uart_circ_empty(xmit))
322 			break;
323 
324 		st = readl(port->membase + UART_STAT);
325 		if (st & STAT_TX_FIFO_FUL)
326 			break;
327 	}
328 
329 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
330 		uart_write_wakeup(port);
331 
332 	if (uart_circ_empty(xmit))
333 		mvebu_uart_stop_tx(port);
334 }
335 
mvebu_uart_isr(int irq,void * dev_id)336 static irqreturn_t mvebu_uart_isr(int irq, void *dev_id)
337 {
338 	struct uart_port *port = (struct uart_port *)dev_id;
339 	unsigned int st = readl(port->membase + UART_STAT);
340 
341 	if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
342 		  STAT_BRK_DET))
343 		mvebu_uart_rx_chars(port, st);
344 
345 	if (st & STAT_TX_RDY(port))
346 		mvebu_uart_tx_chars(port, st);
347 
348 	return IRQ_HANDLED;
349 }
350 
mvebu_uart_rx_isr(int irq,void * dev_id)351 static irqreturn_t mvebu_uart_rx_isr(int irq, void *dev_id)
352 {
353 	struct uart_port *port = (struct uart_port *)dev_id;
354 	unsigned int st = readl(port->membase + UART_STAT);
355 
356 	if (st & (STAT_RX_RDY(port) | STAT_OVR_ERR | STAT_FRM_ERR |
357 			STAT_BRK_DET))
358 		mvebu_uart_rx_chars(port, st);
359 
360 	return IRQ_HANDLED;
361 }
362 
mvebu_uart_tx_isr(int irq,void * dev_id)363 static irqreturn_t mvebu_uart_tx_isr(int irq, void *dev_id)
364 {
365 	struct uart_port *port = (struct uart_port *)dev_id;
366 	unsigned int st = readl(port->membase + UART_STAT);
367 
368 	if (st & STAT_TX_RDY(port))
369 		mvebu_uart_tx_chars(port, st);
370 
371 	return IRQ_HANDLED;
372 }
373 
mvebu_uart_startup(struct uart_port * port)374 static int mvebu_uart_startup(struct uart_port *port)
375 {
376 	struct mvebu_uart *mvuart = to_mvuart(port);
377 	unsigned int ctl;
378 	int ret;
379 
380 	writel(CTRL_TXFIFO_RST | CTRL_RXFIFO_RST,
381 	       port->membase + UART_CTRL(port));
382 	udelay(1);
383 
384 	/* Clear the error bits of state register before IRQ request */
385 	ret = readl(port->membase + UART_STAT);
386 	ret |= STAT_BRK_ERR;
387 	writel(ret, port->membase + UART_STAT);
388 
389 	writel(CTRL_BRK_INT, port->membase + UART_CTRL(port));
390 
391 	ctl = readl(port->membase + UART_INTR(port));
392 	ctl |= CTRL_RX_RDY_INT(port);
393 	writel(ctl, port->membase + UART_INTR(port));
394 
395 	if (!mvuart->irq[UART_TX_IRQ]) {
396 		/* Old bindings with just one interrupt (UART0 only) */
397 		ret = devm_request_irq(port->dev, mvuart->irq[UART_IRQ_SUM],
398 				       mvebu_uart_isr, port->irqflags,
399 				       dev_name(port->dev), port);
400 		if (ret) {
401 			dev_err(port->dev, "unable to request IRQ %d\n",
402 				mvuart->irq[UART_IRQ_SUM]);
403 			return ret;
404 		}
405 	} else {
406 		/* New bindings with an IRQ for RX and TX (both UART) */
407 		ret = devm_request_irq(port->dev, mvuart->irq[UART_RX_IRQ],
408 				       mvebu_uart_rx_isr, port->irqflags,
409 				       dev_name(port->dev), port);
410 		if (ret) {
411 			dev_err(port->dev, "unable to request IRQ %d\n",
412 				mvuart->irq[UART_RX_IRQ]);
413 			return ret;
414 		}
415 
416 		ret = devm_request_irq(port->dev, mvuart->irq[UART_TX_IRQ],
417 				       mvebu_uart_tx_isr, port->irqflags,
418 				       dev_name(port->dev),
419 				       port);
420 		if (ret) {
421 			dev_err(port->dev, "unable to request IRQ %d\n",
422 				mvuart->irq[UART_TX_IRQ]);
423 			devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ],
424 				      port);
425 			return ret;
426 		}
427 	}
428 
429 	return 0;
430 }
431 
mvebu_uart_shutdown(struct uart_port * port)432 static void mvebu_uart_shutdown(struct uart_port *port)
433 {
434 	struct mvebu_uart *mvuart = to_mvuart(port);
435 
436 	writel(0, port->membase + UART_INTR(port));
437 
438 	if (!mvuart->irq[UART_TX_IRQ]) {
439 		devm_free_irq(port->dev, mvuart->irq[UART_IRQ_SUM], port);
440 	} else {
441 		devm_free_irq(port->dev, mvuart->irq[UART_RX_IRQ], port);
442 		devm_free_irq(port->dev, mvuart->irq[UART_TX_IRQ], port);
443 	}
444 }
445 
mvebu_uart_baud_rate_set(struct uart_port * port,unsigned int baud)446 static int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud)
447 {
448 	unsigned int d_divisor, m_divisor;
449 	u32 brdv, osamp;
450 
451 	if (!port->uartclk)
452 		return -EOPNOTSUPP;
453 
454 	/*
455 	 * The baudrate is derived from the UART clock thanks to two divisors:
456 	 *   > D ("baud generator"): can divide the clock from 2 to 2^10 - 1.
457 	 *   > M ("fractional divisor"): allows a better accuracy for
458 	 *     baudrates higher than 230400.
459 	 *
460 	 * As the derivation of M is rather complicated, the code sticks to its
461 	 * default value (x16) when all the prescalers are zeroed, and only
462 	 * makes use of D to configure the desired baudrate.
463 	 */
464 	m_divisor = OSAMP_DEFAULT_DIVISOR;
465 	d_divisor = DIV_ROUND_CLOSEST(port->uartclk, baud * m_divisor);
466 
467 	brdv = readl(port->membase + UART_BRDV);
468 	brdv &= ~BRDV_BAUD_MASK;
469 	brdv |= d_divisor;
470 	writel(brdv, port->membase + UART_BRDV);
471 
472 	osamp = readl(port->membase + UART_OSAMP);
473 	osamp &= ~OSAMP_DIVISORS_MASK;
474 	writel(osamp, port->membase + UART_OSAMP);
475 
476 	return 0;
477 }
478 
mvebu_uart_set_termios(struct uart_port * port,struct ktermios * termios,struct ktermios * old)479 static void mvebu_uart_set_termios(struct uart_port *port,
480 				   struct ktermios *termios,
481 				   struct ktermios *old)
482 {
483 	unsigned long flags;
484 	unsigned int baud, min_baud, max_baud;
485 
486 	spin_lock_irqsave(&port->lock, flags);
487 
488 	port->read_status_mask = STAT_RX_RDY(port) | STAT_OVR_ERR |
489 		STAT_TX_RDY(port) | STAT_TX_FIFO_FUL;
490 
491 	if (termios->c_iflag & INPCK)
492 		port->read_status_mask |= STAT_FRM_ERR | STAT_PAR_ERR;
493 
494 	port->ignore_status_mask = 0;
495 	if (termios->c_iflag & IGNPAR)
496 		port->ignore_status_mask |=
497 			STAT_FRM_ERR | STAT_PAR_ERR | STAT_OVR_ERR;
498 
499 	if ((termios->c_cflag & CREAD) == 0)
500 		port->ignore_status_mask |= STAT_RX_RDY(port) | STAT_BRK_ERR;
501 
502 	/*
503 	 * Maximal divisor is 1023 * 16 when using default (x16) scheme.
504 	 * Maximum achievable frequency with simple baudrate divisor is 230400.
505 	 * Since the error per bit frame would be of more than 15%, achieving
506 	 * higher frequencies would require to implement the fractional divisor
507 	 * feature.
508 	 */
509 	min_baud = DIV_ROUND_UP(port->uartclk, 1023 * 16);
510 	max_baud = 230400;
511 
512 	baud = uart_get_baud_rate(port, termios, old, min_baud, max_baud);
513 	if (mvebu_uart_baud_rate_set(port, baud)) {
514 		/* No clock available, baudrate cannot be changed */
515 		if (old)
516 			baud = uart_get_baud_rate(port, old, NULL,
517 						  min_baud, max_baud);
518 	} else {
519 		tty_termios_encode_baud_rate(termios, baud, baud);
520 		uart_update_timeout(port, termios->c_cflag, baud);
521 	}
522 
523 	/* Only the following flag changes are supported */
524 	if (old) {
525 		termios->c_iflag &= INPCK | IGNPAR;
526 		termios->c_iflag |= old->c_iflag & ~(INPCK | IGNPAR);
527 		termios->c_cflag &= CREAD | CBAUD;
528 		termios->c_cflag |= old->c_cflag & ~(CREAD | CBAUD);
529 		termios->c_cflag |= CS8;
530 	}
531 
532 	spin_unlock_irqrestore(&port->lock, flags);
533 }
534 
mvebu_uart_type(struct uart_port * port)535 static const char *mvebu_uart_type(struct uart_port *port)
536 {
537 	return MVEBU_UART_TYPE;
538 }
539 
mvebu_uart_release_port(struct uart_port * port)540 static void mvebu_uart_release_port(struct uart_port *port)
541 {
542 	/* Nothing to do here */
543 }
544 
mvebu_uart_request_port(struct uart_port * port)545 static int mvebu_uart_request_port(struct uart_port *port)
546 {
547 	return 0;
548 }
549 
550 #ifdef CONFIG_CONSOLE_POLL
mvebu_uart_get_poll_char(struct uart_port * port)551 static int mvebu_uart_get_poll_char(struct uart_port *port)
552 {
553 	unsigned int st = readl(port->membase + UART_STAT);
554 
555 	if (!(st & STAT_RX_RDY(port)))
556 		return NO_POLL_CHAR;
557 
558 	return readl(port->membase + UART_RBR(port));
559 }
560 
mvebu_uart_put_poll_char(struct uart_port * port,unsigned char c)561 static void mvebu_uart_put_poll_char(struct uart_port *port, unsigned char c)
562 {
563 	unsigned int st;
564 
565 	for (;;) {
566 		st = readl(port->membase + UART_STAT);
567 
568 		if (!(st & STAT_TX_FIFO_FUL))
569 			break;
570 
571 		udelay(1);
572 	}
573 
574 	writel(c, port->membase + UART_TSH(port));
575 }
576 #endif
577 
578 static const struct uart_ops mvebu_uart_ops = {
579 	.tx_empty	= mvebu_uart_tx_empty,
580 	.set_mctrl	= mvebu_uart_set_mctrl,
581 	.get_mctrl	= mvebu_uart_get_mctrl,
582 	.stop_tx	= mvebu_uart_stop_tx,
583 	.start_tx	= mvebu_uart_start_tx,
584 	.stop_rx	= mvebu_uart_stop_rx,
585 	.break_ctl	= mvebu_uart_break_ctl,
586 	.startup	= mvebu_uart_startup,
587 	.shutdown	= mvebu_uart_shutdown,
588 	.set_termios	= mvebu_uart_set_termios,
589 	.type		= mvebu_uart_type,
590 	.release_port	= mvebu_uart_release_port,
591 	.request_port	= mvebu_uart_request_port,
592 #ifdef CONFIG_CONSOLE_POLL
593 	.poll_get_char	= mvebu_uart_get_poll_char,
594 	.poll_put_char	= mvebu_uart_put_poll_char,
595 #endif
596 };
597 
598 /* Console Driver Operations  */
599 
600 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
601 /* Early Console */
mvebu_uart_putc(struct uart_port * port,int c)602 static void mvebu_uart_putc(struct uart_port *port, int c)
603 {
604 	unsigned int st;
605 
606 	for (;;) {
607 		st = readl(port->membase + UART_STAT);
608 		if (!(st & STAT_TX_FIFO_FUL))
609 			break;
610 	}
611 
612 	/* At early stage, DT is not parsed yet, only use UART0 */
613 	writel(c, port->membase + UART_STD_TSH);
614 
615 	for (;;) {
616 		st = readl(port->membase + UART_STAT);
617 		if (st & STAT_TX_FIFO_EMP)
618 			break;
619 	}
620 }
621 
mvebu_uart_putc_early_write(struct console * con,const char * s,unsigned n)622 static void mvebu_uart_putc_early_write(struct console *con,
623 					const char *s,
624 					unsigned n)
625 {
626 	struct earlycon_device *dev = con->data;
627 
628 	uart_console_write(&dev->port, s, n, mvebu_uart_putc);
629 }
630 
631 static int __init
mvebu_uart_early_console_setup(struct earlycon_device * device,const char * opt)632 mvebu_uart_early_console_setup(struct earlycon_device *device,
633 			       const char *opt)
634 {
635 	if (!device->port.membase)
636 		return -ENODEV;
637 
638 	device->con->write = mvebu_uart_putc_early_write;
639 
640 	return 0;
641 }
642 
643 EARLYCON_DECLARE(ar3700_uart, mvebu_uart_early_console_setup);
644 OF_EARLYCON_DECLARE(ar3700_uart, "marvell,armada-3700-uart",
645 		    mvebu_uart_early_console_setup);
646 
wait_for_xmitr(struct uart_port * port)647 static void wait_for_xmitr(struct uart_port *port)
648 {
649 	u32 val;
650 
651 	readl_poll_timeout_atomic(port->membase + UART_STAT, val,
652 				  (val & STAT_TX_RDY(port)), 1, 10000);
653 }
654 
wait_for_xmite(struct uart_port * port)655 static void wait_for_xmite(struct uart_port *port)
656 {
657 	u32 val;
658 
659 	readl_poll_timeout_atomic(port->membase + UART_STAT, val,
660 				  (val & STAT_TX_EMP), 1, 10000);
661 }
662 
mvebu_uart_console_putchar(struct uart_port * port,int ch)663 static void mvebu_uart_console_putchar(struct uart_port *port, int ch)
664 {
665 	wait_for_xmitr(port);
666 	writel(ch, port->membase + UART_TSH(port));
667 }
668 
mvebu_uart_console_write(struct console * co,const char * s,unsigned int count)669 static void mvebu_uart_console_write(struct console *co, const char *s,
670 				     unsigned int count)
671 {
672 	struct uart_port *port = &mvebu_uart_ports[co->index];
673 	unsigned long flags;
674 	unsigned int ier, intr, ctl;
675 	int locked = 1;
676 
677 	if (oops_in_progress)
678 		locked = spin_trylock_irqsave(&port->lock, flags);
679 	else
680 		spin_lock_irqsave(&port->lock, flags);
681 
682 	ier = readl(port->membase + UART_CTRL(port)) & CTRL_BRK_INT;
683 	intr = readl(port->membase + UART_INTR(port)) &
684 		(CTRL_RX_RDY_INT(port) | CTRL_TX_RDY_INT(port));
685 	writel(0, port->membase + UART_CTRL(port));
686 	writel(0, port->membase + UART_INTR(port));
687 
688 	uart_console_write(port, s, count, mvebu_uart_console_putchar);
689 
690 	wait_for_xmite(port);
691 
692 	if (ier)
693 		writel(ier, port->membase + UART_CTRL(port));
694 
695 	if (intr) {
696 		ctl = intr | readl(port->membase + UART_INTR(port));
697 		writel(ctl, port->membase + UART_INTR(port));
698 	}
699 
700 	if (locked)
701 		spin_unlock_irqrestore(&port->lock, flags);
702 }
703 
mvebu_uart_console_setup(struct console * co,char * options)704 static int mvebu_uart_console_setup(struct console *co, char *options)
705 {
706 	struct uart_port *port;
707 	int baud = 9600;
708 	int bits = 8;
709 	int parity = 'n';
710 	int flow = 'n';
711 
712 	if (co->index < 0 || co->index >= MVEBU_NR_UARTS)
713 		return -EINVAL;
714 
715 	port = &mvebu_uart_ports[co->index];
716 
717 	if (!port->mapbase || !port->membase) {
718 		pr_debug("console on ttyMV%i not present\n", co->index);
719 		return -ENODEV;
720 	}
721 
722 	if (options)
723 		uart_parse_options(options, &baud, &parity, &bits, &flow);
724 
725 	return uart_set_options(port, co, baud, parity, bits, flow);
726 }
727 
728 static struct uart_driver mvebu_uart_driver;
729 
730 static struct console mvebu_uart_console = {
731 	.name	= "ttyMV",
732 	.write	= mvebu_uart_console_write,
733 	.device	= uart_console_device,
734 	.setup	= mvebu_uart_console_setup,
735 	.flags	= CON_PRINTBUFFER,
736 	.index	= -1,
737 	.data	= &mvebu_uart_driver,
738 };
739 
mvebu_uart_console_init(void)740 static int __init mvebu_uart_console_init(void)
741 {
742 	register_console(&mvebu_uart_console);
743 	return 0;
744 }
745 
746 console_initcall(mvebu_uart_console_init);
747 
748 
749 #endif /* CONFIG_SERIAL_MVEBU_CONSOLE */
750 
751 static struct uart_driver mvebu_uart_driver = {
752 	.owner			= THIS_MODULE,
753 	.driver_name		= DRIVER_NAME,
754 	.dev_name		= "ttyMV",
755 	.nr			= MVEBU_NR_UARTS,
756 #ifdef CONFIG_SERIAL_MVEBU_CONSOLE
757 	.cons			= &mvebu_uart_console,
758 #endif
759 };
760 
761 #if defined(CONFIG_PM)
mvebu_uart_suspend(struct device * dev)762 static int mvebu_uart_suspend(struct device *dev)
763 {
764 	struct mvebu_uart *mvuart = dev_get_drvdata(dev);
765 	struct uart_port *port = mvuart->port;
766 
767 	uart_suspend_port(&mvebu_uart_driver, port);
768 
769 	mvuart->pm_regs.rbr = readl(port->membase + UART_RBR(port));
770 	mvuart->pm_regs.tsh = readl(port->membase + UART_TSH(port));
771 	mvuart->pm_regs.ctrl = readl(port->membase + UART_CTRL(port));
772 	mvuart->pm_regs.intr = readl(port->membase + UART_INTR(port));
773 	mvuart->pm_regs.stat = readl(port->membase + UART_STAT);
774 	mvuart->pm_regs.brdv = readl(port->membase + UART_BRDV);
775 	mvuart->pm_regs.osamp = readl(port->membase + UART_OSAMP);
776 
777 	device_set_wakeup_enable(dev, true);
778 
779 	return 0;
780 }
781 
mvebu_uart_resume(struct device * dev)782 static int mvebu_uart_resume(struct device *dev)
783 {
784 	struct mvebu_uart *mvuart = dev_get_drvdata(dev);
785 	struct uart_port *port = mvuart->port;
786 
787 	writel(mvuart->pm_regs.rbr, port->membase + UART_RBR(port));
788 	writel(mvuart->pm_regs.tsh, port->membase + UART_TSH(port));
789 	writel(mvuart->pm_regs.ctrl, port->membase + UART_CTRL(port));
790 	writel(mvuart->pm_regs.intr, port->membase + UART_INTR(port));
791 	writel(mvuart->pm_regs.stat, port->membase + UART_STAT);
792 	writel(mvuart->pm_regs.brdv, port->membase + UART_BRDV);
793 	writel(mvuart->pm_regs.osamp, port->membase + UART_OSAMP);
794 
795 	uart_resume_port(&mvebu_uart_driver, port);
796 
797 	return 0;
798 }
799 
800 static const struct dev_pm_ops mvebu_uart_pm_ops = {
801 	.suspend        = mvebu_uart_suspend,
802 	.resume         = mvebu_uart_resume,
803 };
804 #endif /* CONFIG_PM */
805 
806 static const struct of_device_id mvebu_uart_of_match[];
807 
808 /* Counter to keep track of each UART port id when not using CONFIG_OF */
809 static int uart_num_counter;
810 
mvebu_uart_probe(struct platform_device * pdev)811 static int mvebu_uart_probe(struct platform_device *pdev)
812 {
813 	struct resource *reg = platform_get_resource(pdev, IORESOURCE_MEM, 0);
814 	const struct of_device_id *match = of_match_device(mvebu_uart_of_match,
815 							   &pdev->dev);
816 	struct uart_port *port;
817 	struct mvebu_uart *mvuart;
818 	int id, irq;
819 
820 	if (!reg) {
821 		dev_err(&pdev->dev, "no registers defined\n");
822 		return -EINVAL;
823 	}
824 
825 	/* Assume that all UART ports have a DT alias or none has */
826 	id = of_alias_get_id(pdev->dev.of_node, "serial");
827 	if (!pdev->dev.of_node || id < 0)
828 		pdev->id = uart_num_counter++;
829 	else
830 		pdev->id = id;
831 
832 	if (pdev->id >= MVEBU_NR_UARTS) {
833 		dev_err(&pdev->dev, "cannot have more than %d UART ports\n",
834 			MVEBU_NR_UARTS);
835 		return -EINVAL;
836 	}
837 
838 	port = &mvebu_uart_ports[pdev->id];
839 
840 	spin_lock_init(&port->lock);
841 
842 	port->dev        = &pdev->dev;
843 	port->type       = PORT_MVEBU;
844 	port->ops        = &mvebu_uart_ops;
845 	port->regshift   = 0;
846 
847 	port->fifosize   = 32;
848 	port->iotype     = UPIO_MEM32;
849 	port->flags      = UPF_FIXED_PORT;
850 	port->line       = pdev->id;
851 
852 	/*
853 	 * IRQ number is not stored in this structure because we may have two of
854 	 * them per port (RX and TX). Instead, use the driver UART structure
855 	 * array so called ->irq[].
856 	 */
857 	port->irq        = 0;
858 	port->irqflags   = 0;
859 	port->mapbase    = reg->start;
860 
861 	port->membase = devm_ioremap_resource(&pdev->dev, reg);
862 	if (IS_ERR(port->membase))
863 		return PTR_ERR(port->membase);
864 
865 	mvuart = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_uart),
866 			      GFP_KERNEL);
867 	if (!mvuart)
868 		return -ENOMEM;
869 
870 	/* Get controller data depending on the compatible string */
871 	mvuart->data = (struct mvebu_uart_driver_data *)match->data;
872 	mvuart->port = port;
873 
874 	port->private_data = mvuart;
875 	platform_set_drvdata(pdev, mvuart);
876 
877 	/* Get fixed clock frequency */
878 	mvuart->clk = devm_clk_get(&pdev->dev, NULL);
879 	if (IS_ERR(mvuart->clk)) {
880 		if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER)
881 			return PTR_ERR(mvuart->clk);
882 
883 		if (IS_EXTENDED(port)) {
884 			dev_err(&pdev->dev, "unable to get UART clock\n");
885 			return PTR_ERR(mvuart->clk);
886 		}
887 	} else {
888 		if (!clk_prepare_enable(mvuart->clk))
889 			port->uartclk = clk_get_rate(mvuart->clk);
890 	}
891 
892 	/* Manage interrupts */
893 	if (platform_irq_count(pdev) == 1) {
894 		/* Old bindings: no name on the single unamed UART0 IRQ */
895 		irq = platform_get_irq(pdev, 0);
896 		if (irq < 0)
897 			return irq;
898 
899 		mvuart->irq[UART_IRQ_SUM] = irq;
900 	} else {
901 		/*
902 		 * New bindings: named interrupts (RX, TX) for both UARTS,
903 		 * only make use of uart-rx and uart-tx interrupts, do not use
904 		 * uart-sum of UART0 port.
905 		 */
906 		irq = platform_get_irq_byname(pdev, "uart-rx");
907 		if (irq < 0)
908 			return irq;
909 
910 		mvuart->irq[UART_RX_IRQ] = irq;
911 
912 		irq = platform_get_irq_byname(pdev, "uart-tx");
913 		if (irq < 0)
914 			return irq;
915 
916 		mvuart->irq[UART_TX_IRQ] = irq;
917 	}
918 
919 	/* UART Soft Reset*/
920 	writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port));
921 	udelay(1);
922 	writel(0, port->membase + UART_CTRL(port));
923 
924 	return uart_add_one_port(&mvebu_uart_driver, port);
925 }
926 
927 static struct mvebu_uart_driver_data uart_std_driver_data = {
928 	.is_ext = false,
929 	.regs.rbr = UART_STD_RBR,
930 	.regs.tsh = UART_STD_TSH,
931 	.regs.ctrl = UART_STD_CTRL1,
932 	.regs.intr = UART_STD_CTRL2,
933 	.flags.ctrl_tx_rdy_int = CTRL_STD_TX_RDY_INT,
934 	.flags.ctrl_rx_rdy_int = CTRL_STD_RX_RDY_INT,
935 	.flags.stat_tx_rdy = STAT_STD_TX_RDY,
936 	.flags.stat_rx_rdy = STAT_STD_RX_RDY,
937 };
938 
939 static struct mvebu_uart_driver_data uart_ext_driver_data = {
940 	.is_ext = true,
941 	.regs.rbr = UART_EXT_RBR,
942 	.regs.tsh = UART_EXT_TSH,
943 	.regs.ctrl = UART_EXT_CTRL1,
944 	.regs.intr = UART_EXT_CTRL2,
945 	.flags.ctrl_tx_rdy_int = CTRL_EXT_TX_RDY_INT,
946 	.flags.ctrl_rx_rdy_int = CTRL_EXT_RX_RDY_INT,
947 	.flags.stat_tx_rdy = STAT_EXT_TX_RDY,
948 	.flags.stat_rx_rdy = STAT_EXT_RX_RDY,
949 };
950 
951 /* Match table for of_platform binding */
952 static const struct of_device_id mvebu_uart_of_match[] = {
953 	{
954 		.compatible = "marvell,armada-3700-uart",
955 		.data = (void *)&uart_std_driver_data,
956 	},
957 	{
958 		.compatible = "marvell,armada-3700-uart-ext",
959 		.data = (void *)&uart_ext_driver_data,
960 	},
961 	{}
962 };
963 
964 static struct platform_driver mvebu_uart_platform_driver = {
965 	.probe	= mvebu_uart_probe,
966 	.driver	= {
967 		.name  = "mvebu-uart",
968 		.of_match_table = of_match_ptr(mvebu_uart_of_match),
969 		.suppress_bind_attrs = true,
970 #if defined(CONFIG_PM)
971 		.pm	= &mvebu_uart_pm_ops,
972 #endif /* CONFIG_PM */
973 	},
974 };
975 
mvebu_uart_init(void)976 static int __init mvebu_uart_init(void)
977 {
978 	int ret;
979 
980 	ret = uart_register_driver(&mvebu_uart_driver);
981 	if (ret)
982 		return ret;
983 
984 	ret = platform_driver_register(&mvebu_uart_platform_driver);
985 	if (ret)
986 		uart_unregister_driver(&mvebu_uart_driver);
987 
988 	return ret;
989 }
990 arch_initcall(mvebu_uart_init);
991