Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 *  Based on meson_uart.c, by AMLOGIC, INC.
  4 *
  5 * Copyright (C) 2014 Carlo Caione <carlo@caione.org>
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/clk.h>
  9#include <linux/console.h>
 10#include <linux/delay.h>
 11#include <linux/init.h>
 12#include <linux/io.h>
 13#include <linux/iopoll.h>
 14#include <linux/module.h>
 15#include <linux/kernel.h>
 16#include <linux/of.h>
 17#include <linux/platform_device.h>
 18#include <linux/serial.h>
 19#include <linux/serial_core.h>
 20#include <linux/tty.h>
 21#include <linux/tty_flip.h>
 22
 23/* Register offsets */
 24#define AML_UART_WFIFO			0x00
 25#define AML_UART_RFIFO			0x04
 26#define AML_UART_CONTROL		0x08
 27#define AML_UART_STATUS			0x0c
 28#define AML_UART_MISC			0x10
 29#define AML_UART_REG5			0x14
 30
 31/* AML_UART_CONTROL bits */
 32#define AML_UART_TX_EN			BIT(12)
 33#define AML_UART_RX_EN			BIT(13)
 34#define AML_UART_TWO_WIRE_EN		BIT(15)
 35#define AML_UART_STOP_BIT_LEN_MASK	(0x03 << 16)
 36#define AML_UART_STOP_BIT_1SB		(0x00 << 16)
 37#define AML_UART_STOP_BIT_2SB		(0x01 << 16)
 38#define AML_UART_PARITY_TYPE		BIT(18)
 39#define AML_UART_PARITY_EN		BIT(19)
 40#define AML_UART_TX_RST			BIT(22)
 41#define AML_UART_RX_RST			BIT(23)
 42#define AML_UART_CLEAR_ERR		BIT(24)
 43#define AML_UART_RX_INT_EN		BIT(27)
 44#define AML_UART_TX_INT_EN		BIT(28)
 45#define AML_UART_DATA_LEN_MASK		(0x03 << 20)
 46#define AML_UART_DATA_LEN_8BIT		(0x00 << 20)
 47#define AML_UART_DATA_LEN_7BIT		(0x01 << 20)
 48#define AML_UART_DATA_LEN_6BIT		(0x02 << 20)
 49#define AML_UART_DATA_LEN_5BIT		(0x03 << 20)
 50
 51/* AML_UART_STATUS bits */
 52#define AML_UART_PARITY_ERR		BIT(16)
 53#define AML_UART_FRAME_ERR		BIT(17)
 54#define AML_UART_TX_FIFO_WERR		BIT(18)
 55#define AML_UART_RX_EMPTY		BIT(20)
 56#define AML_UART_TX_FULL		BIT(21)
 57#define AML_UART_TX_EMPTY		BIT(22)
 58#define AML_UART_XMIT_BUSY		BIT(25)
 59#define AML_UART_ERR			(AML_UART_PARITY_ERR | \
 60					 AML_UART_FRAME_ERR  | \
 61					 AML_UART_TX_FIFO_WERR)
 62
 
 
 
 
 
 
 
 
 
 63/* AML_UART_MISC bits */
 64#define AML_UART_XMIT_IRQ(c)		(((c) & 0xff) << 8)
 65#define AML_UART_RECV_IRQ(c)		((c) & 0xff)
 66
 67/* AML_UART_REG5 bits */
 68#define AML_UART_BAUD_MASK		0x7fffff
 69#define AML_UART_BAUD_USE		BIT(23)
 70#define AML_UART_BAUD_XTAL		BIT(24)
 71#define AML_UART_BAUD_XTAL_DIV2		BIT(27)
 72
 73#define AML_UART_PORT_NUM		12
 74#define AML_UART_PORT_OFFSET		6
 75
 76#define AML_UART_POLL_USEC		5
 77#define AML_UART_TIMEOUT_USEC		10000
 78
 79static struct uart_driver meson_uart_driver_ttyAML;
 80static struct uart_driver meson_uart_driver_ttyS;
 81
 82static struct uart_port *meson_ports[AML_UART_PORT_NUM];
 83
 84struct meson_uart_data {
 85	struct uart_driver *uart_driver;
 86	bool has_xtal_div2;
 87};
 88
 89static void meson_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
 90{
 91}
 92
 93static unsigned int meson_uart_get_mctrl(struct uart_port *port)
 94{
 95	return TIOCM_CTS;
 96}
 97
 98static unsigned int meson_uart_tx_empty(struct uart_port *port)
 99{
100	u32 val;
101
102	val = readl(port->membase + AML_UART_STATUS);
103	val &= (AML_UART_TX_EMPTY | AML_UART_XMIT_BUSY);
104	return (val == AML_UART_TX_EMPTY) ? TIOCSER_TEMT : 0;
105}
106
107static void meson_uart_stop_tx(struct uart_port *port)
108{
109	u32 val;
110
111	val = readl(port->membase + AML_UART_CONTROL);
112	val &= ~AML_UART_TX_INT_EN;
113	writel(val, port->membase + AML_UART_CONTROL);
114}
115
116static void meson_uart_stop_rx(struct uart_port *port)
117{
118	u32 val;
119
120	val = readl(port->membase + AML_UART_CONTROL);
121	val &= ~AML_UART_RX_EN;
122	writel(val, port->membase + AML_UART_CONTROL);
123}
124
125static void meson_uart_shutdown(struct uart_port *port)
126{
127	unsigned long flags;
128	u32 val;
129
130	free_irq(port->irq, port);
131
132	uart_port_lock_irqsave(port, &flags);
133
134	val = readl(port->membase + AML_UART_CONTROL);
135	val &= ~AML_UART_RX_EN;
136	val &= ~(AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
137	writel(val, port->membase + AML_UART_CONTROL);
138
139	uart_port_unlock_irqrestore(port, flags);
140}
141
142static void meson_uart_start_tx(struct uart_port *port)
143{
144	struct circ_buf *xmit = &port->state->xmit;
145	unsigned int ch;
146	u32 val;
147
148	if (uart_tx_stopped(port)) {
149		meson_uart_stop_tx(port);
150		return;
151	}
152
153	while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
154		if (port->x_char) {
155			writel(port->x_char, port->membase + AML_UART_WFIFO);
156			port->icount.tx++;
157			port->x_char = 0;
158			continue;
159		}
160
161		if (uart_circ_empty(xmit))
162			break;
163
164		ch = xmit->buf[xmit->tail];
165		writel(ch, port->membase + AML_UART_WFIFO);
166		uart_xmit_advance(port, 1);
 
167	}
168
169	if (!uart_circ_empty(xmit)) {
170		val = readl(port->membase + AML_UART_CONTROL);
171		val |= AML_UART_TX_INT_EN;
172		writel(val, port->membase + AML_UART_CONTROL);
173	}
174
175	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
176		uart_write_wakeup(port);
177}
178
179static void meson_receive_chars(struct uart_port *port)
180{
181	struct tty_port *tport = &port->state->port;
182	char flag;
183	u32 ostatus, status, ch, mode;
184
185	do {
186		flag = TTY_NORMAL;
187		port->icount.rx++;
188		ostatus = status = readl(port->membase + AML_UART_STATUS);
189
190		if (status & AML_UART_ERR) {
191			if (status & AML_UART_TX_FIFO_WERR)
192				port->icount.overrun++;
193			else if (status & AML_UART_FRAME_ERR)
194				port->icount.frame++;
195			else if (status & AML_UART_PARITY_ERR)
196				port->icount.frame++;
197
198			mode = readl(port->membase + AML_UART_CONTROL);
199			mode |= AML_UART_CLEAR_ERR;
200			writel(mode, port->membase + AML_UART_CONTROL);
201
202			/* It doesn't clear to 0 automatically */
203			mode &= ~AML_UART_CLEAR_ERR;
204			writel(mode, port->membase + AML_UART_CONTROL);
205
206			status &= port->read_status_mask;
207			if (status & AML_UART_FRAME_ERR)
208				flag = TTY_FRAME;
209			else if (status & AML_UART_PARITY_ERR)
210				flag = TTY_PARITY;
211		}
212
213		ch = readl(port->membase + AML_UART_RFIFO);
214		ch &= 0xff;
215
216		if ((ostatus & AML_UART_FRAME_ERR) && (ch == 0)) {
217			port->icount.brk++;
218			flag = TTY_BREAK;
219			if (uart_handle_break(port))
220				continue;
221		}
222
223		if (uart_handle_sysrq_char(port, ch))
224			continue;
225
226		if ((status & port->ignore_status_mask) == 0)
227			tty_insert_flip_char(tport, ch, flag);
228
229		if (status & AML_UART_TX_FIFO_WERR)
230			tty_insert_flip_char(tport, 0, TTY_OVERRUN);
231
232	} while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY));
233
 
234	tty_flip_buffer_push(tport);
 
235}
236
237static irqreturn_t meson_uart_interrupt(int irq, void *dev_id)
238{
239	struct uart_port *port = (struct uart_port *)dev_id;
240
241	uart_port_lock(port);
242
243	if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY))
244		meson_receive_chars(port);
245
246	if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
247		if (readl(port->membase + AML_UART_CONTROL) & AML_UART_TX_INT_EN)
248			meson_uart_start_tx(port);
249	}
250
251	uart_port_unlock(port);
252
253	return IRQ_HANDLED;
254}
255
256static const char *meson_uart_type(struct uart_port *port)
257{
258	return (port->type == PORT_MESON) ? "meson_uart" : NULL;
259}
260
261/*
262 * This function is called only from probe() using a temporary io mapping
263 * in order to perform a reset before setting up the device. Since the
264 * temporarily mapped region was successfully requested, there can be no
265 * console on this port at this time. Hence it is not necessary for this
266 * function to acquire the port->lock. (Since there is no console on this
267 * port at this time, the port->lock is not initialized yet.)
268 */
269static void meson_uart_reset(struct uart_port *port)
270{
271	u32 val;
272
273	val = readl(port->membase + AML_UART_CONTROL);
274	val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLEAR_ERR);
275	writel(val, port->membase + AML_UART_CONTROL);
276
277	val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLEAR_ERR);
278	writel(val, port->membase + AML_UART_CONTROL);
279}
280
281static int meson_uart_startup(struct uart_port *port)
282{
283	unsigned long flags;
284	u32 val;
285	int ret = 0;
286
287	uart_port_lock_irqsave(port, &flags);
288
289	val = readl(port->membase + AML_UART_CONTROL);
290	val |= AML_UART_CLEAR_ERR;
291	writel(val, port->membase + AML_UART_CONTROL);
292	val &= ~AML_UART_CLEAR_ERR;
293	writel(val, port->membase + AML_UART_CONTROL);
294
295	val |= (AML_UART_RX_EN | AML_UART_TX_EN);
296	writel(val, port->membase + AML_UART_CONTROL);
297
298	val |= (AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
299	writel(val, port->membase + AML_UART_CONTROL);
300
301	val = (AML_UART_RECV_IRQ(1) | AML_UART_XMIT_IRQ(port->fifosize / 2));
302	writel(val, port->membase + AML_UART_MISC);
303
304	uart_port_unlock_irqrestore(port, flags);
305
306	ret = request_irq(port->irq, meson_uart_interrupt, 0,
307			  port->name, port);
308
309	return ret;
310}
311
312static void meson_uart_change_speed(struct uart_port *port, unsigned long baud)
313{
314	const struct meson_uart_data *private_data = port->private_data;
315	u32 val = 0;
316
317	while (!meson_uart_tx_empty(port))
318		cpu_relax();
319
 
 
320	if (port->uartclk == 24000000) {
321		unsigned int xtal_div = 3;
322
323		if (private_data && private_data->has_xtal_div2) {
324			xtal_div = 2;
325			val |= AML_UART_BAUD_XTAL_DIV2;
326		}
327		val |= DIV_ROUND_CLOSEST(port->uartclk / xtal_div, baud) - 1;
328		val |= AML_UART_BAUD_XTAL;
329	} else {
330		val =  DIV_ROUND_CLOSEST(port->uartclk / 4, baud) - 1;
331	}
332	val |= AML_UART_BAUD_USE;
333	writel(val, port->membase + AML_UART_REG5);
334}
335
336static void meson_uart_set_termios(struct uart_port *port,
337				   struct ktermios *termios,
338				   const struct ktermios *old)
339{
340	unsigned int cflags, iflags, baud;
341	unsigned long flags;
342	u32 val;
343
344	uart_port_lock_irqsave(port, &flags);
345
346	cflags = termios->c_cflag;
347	iflags = termios->c_iflag;
348
349	val = readl(port->membase + AML_UART_CONTROL);
350
351	val &= ~AML_UART_DATA_LEN_MASK;
352	switch (cflags & CSIZE) {
353	case CS8:
354		val |= AML_UART_DATA_LEN_8BIT;
355		break;
356	case CS7:
357		val |= AML_UART_DATA_LEN_7BIT;
358		break;
359	case CS6:
360		val |= AML_UART_DATA_LEN_6BIT;
361		break;
362	case CS5:
363		val |= AML_UART_DATA_LEN_5BIT;
364		break;
365	}
366
367	if (cflags & PARENB)
368		val |= AML_UART_PARITY_EN;
369	else
370		val &= ~AML_UART_PARITY_EN;
371
372	if (cflags & PARODD)
373		val |= AML_UART_PARITY_TYPE;
374	else
375		val &= ~AML_UART_PARITY_TYPE;
376
377	val &= ~AML_UART_STOP_BIT_LEN_MASK;
378	if (cflags & CSTOPB)
379		val |= AML_UART_STOP_BIT_2SB;
380	else
381		val |= AML_UART_STOP_BIT_1SB;
382
383	if (cflags & CRTSCTS) {
384		if (port->flags & UPF_HARD_FLOW)
385			val &= ~AML_UART_TWO_WIRE_EN;
386		else
387			termios->c_cflag &= ~CRTSCTS;
388	} else {
389		val |= AML_UART_TWO_WIRE_EN;
390	}
391
392	writel(val, port->membase + AML_UART_CONTROL);
393
394	baud = uart_get_baud_rate(port, termios, old, 50, 4000000);
395	meson_uart_change_speed(port, baud);
396
397	port->read_status_mask = AML_UART_TX_FIFO_WERR;
398	if (iflags & INPCK)
399		port->read_status_mask |= AML_UART_PARITY_ERR |
400					  AML_UART_FRAME_ERR;
401
402	port->ignore_status_mask = 0;
403	if (iflags & IGNPAR)
404		port->ignore_status_mask |= AML_UART_PARITY_ERR |
405					    AML_UART_FRAME_ERR;
406
407	uart_update_timeout(port, termios->c_cflag, baud);
408	uart_port_unlock_irqrestore(port, flags);
409}
410
411static int meson_uart_verify_port(struct uart_port *port,
412				  struct serial_struct *ser)
413{
414	int ret = 0;
415
416	if (port->type != PORT_MESON)
417		ret = -EINVAL;
418	if (port->irq != ser->irq)
419		ret = -EINVAL;
420	if (ser->baud_base < 9600)
421		ret = -EINVAL;
422	return ret;
423}
424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
425static void meson_uart_release_port(struct uart_port *port)
426{
427	devm_iounmap(port->dev, port->membase);
428	port->membase = NULL;
429	devm_release_mem_region(port->dev, port->mapbase, port->mapsize);
 
 
 
 
430}
431
432static int meson_uart_request_port(struct uart_port *port)
433{
434	if (!devm_request_mem_region(port->dev, port->mapbase, port->mapsize,
 
 
 
 
 
435				     dev_name(port->dev))) {
436		dev_err(port->dev, "Memory region busy\n");
437		return -EBUSY;
438	}
439
440	port->membase = devm_ioremap(port->dev, port->mapbase,
441					     port->mapsize);
442	if (!port->membase)
443		return -ENOMEM;
 
 
 
444
445	return 0;
446}
447
448static void meson_uart_config_port(struct uart_port *port, int flags)
449{
450	if (flags & UART_CONFIG_TYPE) {
451		port->type = PORT_MESON;
452		meson_uart_request_port(port);
453	}
454}
455
456#ifdef CONFIG_CONSOLE_POLL
457/*
458 * Console polling routines for writing and reading from the uart while
459 * in an interrupt or debug context (i.e. kgdb).
460 */
461
462static int meson_uart_poll_get_char(struct uart_port *port)
463{
464	u32 c;
465	unsigned long flags;
466
467	uart_port_lock_irqsave(port, &flags);
468
469	if (readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY)
470		c = NO_POLL_CHAR;
471	else
472		c = readl(port->membase + AML_UART_RFIFO);
473
474	uart_port_unlock_irqrestore(port, flags);
475
476	return c;
477}
478
479static void meson_uart_poll_put_char(struct uart_port *port, unsigned char c)
480{
481	unsigned long flags;
482	u32 reg;
483	int ret;
484
485	uart_port_lock_irqsave(port, &flags);
486
487	/* Wait until FIFO is empty or timeout */
488	ret = readl_poll_timeout_atomic(port->membase + AML_UART_STATUS, reg,
489					reg & AML_UART_TX_EMPTY,
490					AML_UART_POLL_USEC,
491					AML_UART_TIMEOUT_USEC);
492	if (ret == -ETIMEDOUT) {
493		dev_err(port->dev, "Timeout waiting for UART TX EMPTY\n");
494		goto out;
495	}
496
497	/* Write the character */
498	writel(c, port->membase + AML_UART_WFIFO);
499
500	/* Wait until FIFO is empty or timeout */
501	ret = readl_poll_timeout_atomic(port->membase + AML_UART_STATUS, reg,
502					reg & AML_UART_TX_EMPTY,
503					AML_UART_POLL_USEC,
504					AML_UART_TIMEOUT_USEC);
505	if (ret == -ETIMEDOUT)
506		dev_err(port->dev, "Timeout waiting for UART TX EMPTY\n");
507
508out:
509	uart_port_unlock_irqrestore(port, flags);
510}
511
512#endif /* CONFIG_CONSOLE_POLL */
513
514static const struct uart_ops meson_uart_ops = {
515	.set_mctrl      = meson_uart_set_mctrl,
516	.get_mctrl      = meson_uart_get_mctrl,
517	.tx_empty	= meson_uart_tx_empty,
518	.start_tx	= meson_uart_start_tx,
519	.stop_tx	= meson_uart_stop_tx,
520	.stop_rx	= meson_uart_stop_rx,
521	.startup	= meson_uart_startup,
522	.shutdown	= meson_uart_shutdown,
523	.set_termios	= meson_uart_set_termios,
524	.type		= meson_uart_type,
525	.config_port	= meson_uart_config_port,
526	.request_port	= meson_uart_request_port,
527	.release_port	= meson_uart_release_port,
528	.verify_port	= meson_uart_verify_port,
529#ifdef CONFIG_CONSOLE_POLL
530	.poll_get_char	= meson_uart_poll_get_char,
531	.poll_put_char	= meson_uart_poll_put_char,
532#endif
533};
534
535#ifdef CONFIG_SERIAL_MESON_CONSOLE
536static void meson_uart_enable_tx_engine(struct uart_port *port)
537{
538	u32 val;
539
540	val = readl(port->membase + AML_UART_CONTROL);
541	val |= AML_UART_TX_EN;
542	writel(val, port->membase + AML_UART_CONTROL);
543}
544
545static void meson_console_putchar(struct uart_port *port, unsigned char ch)
546{
547	if (!port->membase)
548		return;
549
550	while (readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)
551		cpu_relax();
552	writel(ch, port->membase + AML_UART_WFIFO);
553}
554
555static void meson_serial_port_write(struct uart_port *port, const char *s,
556				    u_int count)
557{
558	unsigned long flags;
559	int locked;
560	u32 val, tmp;
561
562	local_irq_save(flags);
563	if (port->sysrq) {
564		locked = 0;
565	} else if (oops_in_progress) {
566		locked = uart_port_trylock(port);
567	} else {
568		uart_port_lock(port);
569		locked = 1;
570	}
571
572	val = readl(port->membase + AML_UART_CONTROL);
 
573	tmp = val & ~(AML_UART_TX_INT_EN | AML_UART_RX_INT_EN);
574	writel(tmp, port->membase + AML_UART_CONTROL);
575
576	uart_console_write(port, s, count, meson_console_putchar);
577	writel(val, port->membase + AML_UART_CONTROL);
578
579	if (locked)
580		uart_port_unlock(port);
581	local_irq_restore(flags);
582}
583
584static void meson_serial_console_write(struct console *co, const char *s,
585				       u_int count)
586{
587	struct uart_port *port;
588
589	port = meson_ports[co->index];
590	if (!port)
591		return;
592
593	meson_serial_port_write(port, s, count);
594}
595
596static int meson_serial_console_setup(struct console *co, char *options)
597{
598	struct uart_port *port;
599	int baud = 115200;
600	int bits = 8;
601	int parity = 'n';
602	int flow = 'n';
603
604	if (co->index < 0 || co->index >= AML_UART_PORT_NUM)
605		return -EINVAL;
606
607	port = meson_ports[co->index];
608	if (!port || !port->membase)
609		return -ENODEV;
610
611	meson_uart_enable_tx_engine(port);
612
613	if (options)
614		uart_parse_options(options, &baud, &parity, &bits, &flow);
615
616	return uart_set_options(port, co, baud, parity, bits, flow);
617}
618
619#define MESON_SERIAL_CONSOLE(_devname)					\
620	static struct console meson_serial_console_##_devname = {	\
621		.name		= __stringify(_devname),		\
622		.write		= meson_serial_console_write,		\
623		.device		= uart_console_device,			\
624		.setup		= meson_serial_console_setup,		\
625		.flags		= CON_PRINTBUFFER,			\
626		.index		= -1,					\
627		.data		= &meson_uart_driver_##_devname,	\
628	}
629
630MESON_SERIAL_CONSOLE(ttyAML);
631MESON_SERIAL_CONSOLE(ttyS);
 
 
 
 
632
633static void meson_serial_early_console_write(struct console *co,
634					     const char *s,
635					     u_int count)
636{
637	struct earlycon_device *dev = co->data;
638
639	meson_serial_port_write(&dev->port, s, count);
640}
641
642static int __init
643meson_serial_early_console_setup(struct earlycon_device *device, const char *opt)
644{
645	if (!device->port.membase)
646		return -ENODEV;
647
648	meson_uart_enable_tx_engine(&device->port);
649	device->con->write = meson_serial_early_console_write;
650	return 0;
651}
 
 
652
653OF_EARLYCON_DECLARE(meson, "amlogic,meson-ao-uart", meson_serial_early_console_setup);
654OF_EARLYCON_DECLARE(meson, "amlogic,meson-s4-uart", meson_serial_early_console_setup);
655
656#define MESON_SERIAL_CONSOLE_PTR(_devname) (&meson_serial_console_##_devname)
657#else
658#define MESON_SERIAL_CONSOLE_PTR(_devname) (NULL)
659#endif
660
661#define MESON_UART_DRIVER(_devname)					\
662	static struct uart_driver meson_uart_driver_##_devname = {	\
663		.owner		= THIS_MODULE,				\
664		.driver_name	= "meson_uart",				\
665		.dev_name	= __stringify(_devname),		\
666		.nr		= AML_UART_PORT_NUM,			\
667		.cons		= MESON_SERIAL_CONSOLE_PTR(_devname),	\
668	}
669
670MESON_UART_DRIVER(ttyAML);
671MESON_UART_DRIVER(ttyS);
672
673static int meson_uart_probe_clocks(struct platform_device *pdev,
674				   struct uart_port *port)
675{
676	struct clk *clk_xtal = NULL;
677	struct clk *clk_pclk = NULL;
678	struct clk *clk_baud = NULL;
679
680	clk_pclk = devm_clk_get_enabled(&pdev->dev, "pclk");
681	if (IS_ERR(clk_pclk))
682		return PTR_ERR(clk_pclk);
683
684	clk_xtal = devm_clk_get_enabled(&pdev->dev, "xtal");
685	if (IS_ERR(clk_xtal))
686		return PTR_ERR(clk_xtal);
687
688	clk_baud = devm_clk_get_enabled(&pdev->dev, "baud");
689	if (IS_ERR(clk_baud))
690		return PTR_ERR(clk_baud);
691
692	port->uartclk = clk_get_rate(clk_baud);
693
694	return 0;
695}
696
697static struct uart_driver *meson_uart_current(const struct meson_uart_data *pd)
698{
699	return (pd && pd->uart_driver) ?
700		pd->uart_driver : &meson_uart_driver_ttyAML;
701}
702
703static int meson_uart_probe(struct platform_device *pdev)
704{
705	const struct meson_uart_data *priv_data;
706	struct uart_driver *uart_driver;
707	struct resource *res_mem;
708	struct uart_port *port;
709	u32 fifosize = 64; /* Default is 64, 128 for EE UART_0 */
710	int ret = 0;
711	int irq;
712	bool has_rtscts;
713
714	if (pdev->dev.of_node)
715		pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
716
717	if (pdev->id < 0) {
718		int id;
719
720		for (id = AML_UART_PORT_OFFSET; id < AML_UART_PORT_NUM; id++) {
721			if (!meson_ports[id]) {
722				pdev->id = id;
723				break;
724			}
725		}
726	}
727
728	if (pdev->id < 0 || pdev->id >= AML_UART_PORT_NUM)
729		return -EINVAL;
730
731	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
732	if (!res_mem)
733		return -ENODEV;
734
735	irq = platform_get_irq(pdev, 0);
736	if (irq < 0)
737		return irq;
738
739	of_property_read_u32(pdev->dev.of_node, "fifo-size", &fifosize);
740	has_rtscts = of_property_read_bool(pdev->dev.of_node, "uart-has-rtscts");
741
742	if (meson_ports[pdev->id]) {
743		return dev_err_probe(&pdev->dev, -EBUSY,
744				     "port %d already allocated\n", pdev->id);
745	}
746
747	port = devm_kzalloc(&pdev->dev, sizeof(struct uart_port), GFP_KERNEL);
748	if (!port)
749		return -ENOMEM;
750
751	ret = meson_uart_probe_clocks(pdev, port);
752	if (ret)
753		return ret;
754
755	priv_data = device_get_match_data(&pdev->dev);
756
757	uart_driver = meson_uart_current(priv_data);
758
759	if (!uart_driver->state) {
760		ret = uart_register_driver(uart_driver);
761		if (ret)
762			return dev_err_probe(&pdev->dev, ret,
763					     "can't register uart driver\n");
764	}
765
 
766	port->iotype = UPIO_MEM;
767	port->mapbase = res_mem->start;
768	port->mapsize = resource_size(res_mem);
769	port->irq = irq;
770	port->flags = UPF_BOOT_AUTOCONF | UPF_LOW_LATENCY;
771	if (has_rtscts)
772		port->flags |= UPF_HARD_FLOW;
773	port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MESON_CONSOLE);
774	port->dev = &pdev->dev;
775	port->line = pdev->id;
776	port->type = PORT_MESON;
777	port->x_char = 0;
778	port->ops = &meson_uart_ops;
779	port->fifosize = fifosize;
780	port->private_data = (void *)priv_data;
781
782	meson_ports[pdev->id] = port;
783	platform_set_drvdata(pdev, port);
784
785	/* reset port before registering (and possibly registering console) */
786	if (meson_uart_request_port(port) >= 0) {
787		meson_uart_reset(port);
788		meson_uart_release_port(port);
789	}
790
791	ret = uart_add_one_port(uart_driver, port);
792	if (ret)
793		meson_ports[pdev->id] = NULL;
794
795	return ret;
796}
797
798static void meson_uart_remove(struct platform_device *pdev)
799{
800	struct uart_driver *uart_driver;
801	struct uart_port *port;
802
803	port = platform_get_drvdata(pdev);
804	uart_driver = meson_uart_current(port->private_data);
805	uart_remove_one_port(uart_driver, port);
806	meson_ports[pdev->id] = NULL;
807
808	for (int id = 0; id < AML_UART_PORT_NUM; id++)
809		if (meson_ports[id])
810			return;
811
812	/* No more available uart ports, unregister uart driver */
813	uart_unregister_driver(uart_driver);
814}
815
816static struct meson_uart_data meson_g12a_uart_data = {
817	.has_xtal_div2 = true,
818};
819
820static struct meson_uart_data meson_a1_uart_data = {
821	.uart_driver = &meson_uart_driver_ttyS,
822	.has_xtal_div2 = false,
823};
824
825static struct meson_uart_data meson_s4_uart_data = {
826	.uart_driver = &meson_uart_driver_ttyS,
827	.has_xtal_div2 = true,
828};
829
830static const struct of_device_id meson_uart_dt_match[] = {
831	{ .compatible = "amlogic,meson6-uart" },
832	{ .compatible = "amlogic,meson8-uart" },
833	{ .compatible = "amlogic,meson8b-uart" },
834	{ .compatible = "amlogic,meson-gx-uart" },
835	{
836		.compatible = "amlogic,meson-g12a-uart",
837		.data = (void *)&meson_g12a_uart_data,
838	},
839	{
840		.compatible = "amlogic,meson-s4-uart",
841		.data = (void *)&meson_s4_uart_data,
842	},
843	{
844		.compatible = "amlogic,meson-a1-uart",
845		.data = (void *)&meson_a1_uart_data,
846	},
847	{ /* sentinel */ },
848};
849MODULE_DEVICE_TABLE(of, meson_uart_dt_match);
850
851static  struct platform_driver meson_uart_platform_driver = {
852	.probe		= meson_uart_probe,
853	.remove_new	= meson_uart_remove,
854	.driver		= {
855		.name		= "meson_uart",
856		.of_match_table	= meson_uart_dt_match,
857	},
858};
859
860module_platform_driver(meson_uart_platform_driver);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
861
862MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
863MODULE_DESCRIPTION("Amlogic Meson serial port driver");
864MODULE_LICENSE("GPL v2");
v4.10.11
 
  1/*
  2 *  Based on meson_uart.c, by AMLOGIC, INC.
  3 *
  4 * Copyright (C) 2014 Carlo Caione <carlo@caione.org>
  5 *
  6 * This program is free software; you can redistribute it and/or modify it
  7 * under the terms of the GNU General Public License version 2 as published
  8 * by the Free Software Foundation.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 */
 16
 17#include <linux/clk.h>
 18#include <linux/console.h>
 19#include <linux/delay.h>
 20#include <linux/init.h>
 21#include <linux/io.h>
 
 22#include <linux/module.h>
 23#include <linux/kernel.h>
 24#include <linux/of.h>
 25#include <linux/platform_device.h>
 26#include <linux/serial.h>
 27#include <linux/serial_core.h>
 28#include <linux/tty.h>
 29#include <linux/tty_flip.h>
 30
 31/* Register offsets */
 32#define AML_UART_WFIFO			0x00
 33#define AML_UART_RFIFO			0x04
 34#define AML_UART_CONTROL		0x08
 35#define AML_UART_STATUS			0x0c
 36#define AML_UART_MISC			0x10
 37#define AML_UART_REG5			0x14
 38
 39/* AML_UART_CONTROL bits */
 40#define AML_UART_TX_EN			BIT(12)
 41#define AML_UART_RX_EN			BIT(13)
 
 
 
 
 
 
 42#define AML_UART_TX_RST			BIT(22)
 43#define AML_UART_RX_RST			BIT(23)
 44#define AML_UART_CLR_ERR		BIT(24)
 45#define AML_UART_RX_INT_EN		BIT(27)
 46#define AML_UART_TX_INT_EN		BIT(28)
 47#define AML_UART_DATA_LEN_MASK		(0x03 << 20)
 48#define AML_UART_DATA_LEN_8BIT		(0x00 << 20)
 49#define AML_UART_DATA_LEN_7BIT		(0x01 << 20)
 50#define AML_UART_DATA_LEN_6BIT		(0x02 << 20)
 51#define AML_UART_DATA_LEN_5BIT		(0x03 << 20)
 52
 53/* AML_UART_STATUS bits */
 54#define AML_UART_PARITY_ERR		BIT(16)
 55#define AML_UART_FRAME_ERR		BIT(17)
 56#define AML_UART_TX_FIFO_WERR		BIT(18)
 57#define AML_UART_RX_EMPTY		BIT(20)
 58#define AML_UART_TX_FULL		BIT(21)
 59#define AML_UART_TX_EMPTY		BIT(22)
 60#define AML_UART_XMIT_BUSY		BIT(25)
 61#define AML_UART_ERR			(AML_UART_PARITY_ERR | \
 62					 AML_UART_FRAME_ERR  | \
 63					 AML_UART_TX_FIFO_WERR)
 64
 65/* AML_UART_CONTROL bits */
 66#define AML_UART_TWO_WIRE_EN		BIT(15)
 67#define AML_UART_PARITY_TYPE		BIT(18)
 68#define AML_UART_PARITY_EN		BIT(19)
 69#define AML_UART_CLEAR_ERR		BIT(24)
 70#define AML_UART_STOP_BIN_LEN_MASK	(0x03 << 16)
 71#define AML_UART_STOP_BIN_1SB		(0x00 << 16)
 72#define AML_UART_STOP_BIN_2SB		(0x01 << 16)
 73
 74/* AML_UART_MISC bits */
 75#define AML_UART_XMIT_IRQ(c)		(((c) & 0xff) << 8)
 76#define AML_UART_RECV_IRQ(c)		((c) & 0xff)
 77
 78/* AML_UART_REG5 bits */
 79#define AML_UART_BAUD_MASK		0x7fffff
 80#define AML_UART_BAUD_USE		BIT(23)
 81#define AML_UART_BAUD_XTAL		BIT(24)
 
 82
 83#define AML_UART_PORT_NUM		6
 84#define AML_UART_DEV_NAME		"ttyAML"
 85
 
 
 86
 87static struct uart_driver meson_uart_driver;
 
 88
 89static struct uart_port *meson_ports[AML_UART_PORT_NUM];
 90
 
 
 
 
 
 91static void meson_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
 92{
 93}
 94
 95static unsigned int meson_uart_get_mctrl(struct uart_port *port)
 96{
 97	return TIOCM_CTS;
 98}
 99
100static unsigned int meson_uart_tx_empty(struct uart_port *port)
101{
102	u32 val;
103
104	val = readl(port->membase + AML_UART_STATUS);
105	val &= (AML_UART_TX_EMPTY | AML_UART_XMIT_BUSY);
106	return (val == AML_UART_TX_EMPTY) ? TIOCSER_TEMT : 0;
107}
108
109static void meson_uart_stop_tx(struct uart_port *port)
110{
111	u32 val;
112
113	val = readl(port->membase + AML_UART_CONTROL);
114	val &= ~AML_UART_TX_INT_EN;
115	writel(val, port->membase + AML_UART_CONTROL);
116}
117
118static void meson_uart_stop_rx(struct uart_port *port)
119{
120	u32 val;
121
122	val = readl(port->membase + AML_UART_CONTROL);
123	val &= ~AML_UART_RX_EN;
124	writel(val, port->membase + AML_UART_CONTROL);
125}
126
127static void meson_uart_shutdown(struct uart_port *port)
128{
129	unsigned long flags;
130	u32 val;
131
132	free_irq(port->irq, port);
133
134	spin_lock_irqsave(&port->lock, flags);
135
136	val = readl(port->membase + AML_UART_CONTROL);
137	val &= ~AML_UART_RX_EN;
138	val &= ~(AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
139	writel(val, port->membase + AML_UART_CONTROL);
140
141	spin_unlock_irqrestore(&port->lock, flags);
142}
143
144static void meson_uart_start_tx(struct uart_port *port)
145{
146	struct circ_buf *xmit = &port->state->xmit;
147	unsigned int ch;
148	u32 val;
149
150	if (uart_tx_stopped(port)) {
151		meson_uart_stop_tx(port);
152		return;
153	}
154
155	while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
156		if (port->x_char) {
157			writel(port->x_char, port->membase + AML_UART_WFIFO);
158			port->icount.tx++;
159			port->x_char = 0;
160			continue;
161		}
162
163		if (uart_circ_empty(xmit))
164			break;
165
166		ch = xmit->buf[xmit->tail];
167		writel(ch, port->membase + AML_UART_WFIFO);
168		xmit->tail = (xmit->tail+1) & (SERIAL_XMIT_SIZE - 1);
169		port->icount.tx++;
170	}
171
172	if (!uart_circ_empty(xmit)) {
173		val = readl(port->membase + AML_UART_CONTROL);
174		val |= AML_UART_TX_INT_EN;
175		writel(val, port->membase + AML_UART_CONTROL);
176	}
177
178	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
179		uart_write_wakeup(port);
180}
181
182static void meson_receive_chars(struct uart_port *port)
183{
184	struct tty_port *tport = &port->state->port;
185	char flag;
186	u32 status, ch, mode;
187
188	do {
189		flag = TTY_NORMAL;
190		port->icount.rx++;
191		status = readl(port->membase + AML_UART_STATUS);
192
193		if (status & AML_UART_ERR) {
194			if (status & AML_UART_TX_FIFO_WERR)
195				port->icount.overrun++;
196			else if (status & AML_UART_FRAME_ERR)
197				port->icount.frame++;
198			else if (status & AML_UART_PARITY_ERR)
199				port->icount.frame++;
200
201			mode = readl(port->membase + AML_UART_CONTROL);
202			mode |= AML_UART_CLEAR_ERR;
203			writel(mode, port->membase + AML_UART_CONTROL);
204
205			/* It doesn't clear to 0 automatically */
206			mode &= ~AML_UART_CLEAR_ERR;
207			writel(mode, port->membase + AML_UART_CONTROL);
208
209			status &= port->read_status_mask;
210			if (status & AML_UART_FRAME_ERR)
211				flag = TTY_FRAME;
212			else if (status & AML_UART_PARITY_ERR)
213				flag = TTY_PARITY;
214		}
215
216		ch = readl(port->membase + AML_UART_RFIFO);
217		ch &= 0xff;
218
 
 
 
 
 
 
 
 
 
 
219		if ((status & port->ignore_status_mask) == 0)
220			tty_insert_flip_char(tport, ch, flag);
221
222		if (status & AML_UART_TX_FIFO_WERR)
223			tty_insert_flip_char(tport, 0, TTY_OVERRUN);
224
225	} while (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY));
226
227	spin_unlock(&port->lock);
228	tty_flip_buffer_push(tport);
229	spin_lock(&port->lock);
230}
231
232static irqreturn_t meson_uart_interrupt(int irq, void *dev_id)
233{
234	struct uart_port *port = (struct uart_port *)dev_id;
235
236	spin_lock(&port->lock);
237
238	if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_RX_EMPTY))
239		meson_receive_chars(port);
240
241	if (!(readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)) {
242		if (readl(port->membase + AML_UART_CONTROL) & AML_UART_TX_INT_EN)
243			meson_uart_start_tx(port);
244	}
245
246	spin_unlock(&port->lock);
247
248	return IRQ_HANDLED;
249}
250
251static const char *meson_uart_type(struct uart_port *port)
252{
253	return (port->type == PORT_MESON) ? "meson_uart" : NULL;
254}
255
 
 
 
 
 
 
 
 
256static void meson_uart_reset(struct uart_port *port)
257{
258	u32 val;
259
260	val = readl(port->membase + AML_UART_CONTROL);
261	val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
262	writel(val, port->membase + AML_UART_CONTROL);
263
264	val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
265	writel(val, port->membase + AML_UART_CONTROL);
266}
267
268static int meson_uart_startup(struct uart_port *port)
269{
 
270	u32 val;
271	int ret = 0;
272
 
 
273	val = readl(port->membase + AML_UART_CONTROL);
274	val |= AML_UART_CLR_ERR;
275	writel(val, port->membase + AML_UART_CONTROL);
276	val &= ~AML_UART_CLR_ERR;
277	writel(val, port->membase + AML_UART_CONTROL);
278
279	val |= (AML_UART_RX_EN | AML_UART_TX_EN);
280	writel(val, port->membase + AML_UART_CONTROL);
281
282	val |= (AML_UART_RX_INT_EN | AML_UART_TX_INT_EN);
283	writel(val, port->membase + AML_UART_CONTROL);
284
285	val = (AML_UART_RECV_IRQ(1) | AML_UART_XMIT_IRQ(port->fifosize / 2));
286	writel(val, port->membase + AML_UART_MISC);
287
 
 
288	ret = request_irq(port->irq, meson_uart_interrupt, 0,
289			  meson_uart_type(port), port);
290
291	return ret;
292}
293
294static void meson_uart_change_speed(struct uart_port *port, unsigned long baud)
295{
296	u32 val;
 
297
298	while (!meson_uart_tx_empty(port))
299		cpu_relax();
300
301	val = readl(port->membase + AML_UART_REG5);
302	val &= ~AML_UART_BAUD_MASK;
303	if (port->uartclk == 24000000) {
304		val = ((port->uartclk / 3) / baud) - 1;
 
 
 
 
 
 
305		val |= AML_UART_BAUD_XTAL;
306	} else {
307		val = ((port->uartclk * 10 / (baud * 4) + 5) / 10) - 1;
308	}
309	val |= AML_UART_BAUD_USE;
310	writel(val, port->membase + AML_UART_REG5);
311}
312
313static void meson_uart_set_termios(struct uart_port *port,
314				   struct ktermios *termios,
315				   struct ktermios *old)
316{
317	unsigned int cflags, iflags, baud;
318	unsigned long flags;
319	u32 val;
320
321	spin_lock_irqsave(&port->lock, flags);
322
323	cflags = termios->c_cflag;
324	iflags = termios->c_iflag;
325
326	val = readl(port->membase + AML_UART_CONTROL);
327
328	val &= ~AML_UART_DATA_LEN_MASK;
329	switch (cflags & CSIZE) {
330	case CS8:
331		val |= AML_UART_DATA_LEN_8BIT;
332		break;
333	case CS7:
334		val |= AML_UART_DATA_LEN_7BIT;
335		break;
336	case CS6:
337		val |= AML_UART_DATA_LEN_6BIT;
338		break;
339	case CS5:
340		val |= AML_UART_DATA_LEN_5BIT;
341		break;
342	}
343
344	if (cflags & PARENB)
345		val |= AML_UART_PARITY_EN;
346	else
347		val &= ~AML_UART_PARITY_EN;
348
349	if (cflags & PARODD)
350		val |= AML_UART_PARITY_TYPE;
351	else
352		val &= ~AML_UART_PARITY_TYPE;
353
354	val &= ~AML_UART_STOP_BIN_LEN_MASK;
355	if (cflags & CSTOPB)
356		val |= AML_UART_STOP_BIN_2SB;
357	else
358		val &= ~AML_UART_STOP_BIN_1SB;
359
360	if (cflags & CRTSCTS)
361		val &= ~AML_UART_TWO_WIRE_EN;
362	else
 
 
 
363		val |= AML_UART_TWO_WIRE_EN;
 
364
365	writel(val, port->membase + AML_UART_CONTROL);
366
367	baud = uart_get_baud_rate(port, termios, old, 9600, 115200);
368	meson_uart_change_speed(port, baud);
369
370	port->read_status_mask = AML_UART_TX_FIFO_WERR;
371	if (iflags & INPCK)
372		port->read_status_mask |= AML_UART_PARITY_ERR |
373					  AML_UART_FRAME_ERR;
374
375	port->ignore_status_mask = 0;
376	if (iflags & IGNPAR)
377		port->ignore_status_mask |= AML_UART_PARITY_ERR |
378					    AML_UART_FRAME_ERR;
379
380	uart_update_timeout(port, termios->c_cflag, baud);
381	spin_unlock_irqrestore(&port->lock, flags);
382}
383
384static int meson_uart_verify_port(struct uart_port *port,
385				  struct serial_struct *ser)
386{
387	int ret = 0;
388
389	if (port->type != PORT_MESON)
390		ret = -EINVAL;
391	if (port->irq != ser->irq)
392		ret = -EINVAL;
393	if (ser->baud_base < 9600)
394		ret = -EINVAL;
395	return ret;
396}
397
398static int meson_uart_res_size(struct uart_port *port)
399{
400	struct platform_device *pdev = to_platform_device(port->dev);
401	struct resource *res;
402
403	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
404	if (!res) {
405		dev_err(port->dev, "cannot obtain I/O memory region");
406		return -ENODEV;
407	}
408
409	return resource_size(res);
410}
411
412static void meson_uart_release_port(struct uart_port *port)
413{
414	int size = meson_uart_res_size(port);
415
416	if (port->flags & UPF_IOREMAP) {
417		devm_release_mem_region(port->dev, port->mapbase, size);
418		devm_iounmap(port->dev, port->membase);
419		port->membase = NULL;
420	}
421}
422
423static int meson_uart_request_port(struct uart_port *port)
424{
425	int size = meson_uart_res_size(port);
426
427	if (size < 0)
428		return size;
429
430	if (!devm_request_mem_region(port->dev, port->mapbase, size,
431				     dev_name(port->dev))) {
432		dev_err(port->dev, "Memory region busy\n");
433		return -EBUSY;
434	}
435
436	if (port->flags & UPF_IOREMAP) {
437		port->membase = devm_ioremap_nocache(port->dev,
438						     port->mapbase,
439						     size);
440		if (port->membase == NULL)
441			return -ENOMEM;
442	}
443
444	return 0;
445}
446
447static void meson_uart_config_port(struct uart_port *port, int flags)
448{
449	if (flags & UART_CONFIG_TYPE) {
450		port->type = PORT_MESON;
451		meson_uart_request_port(port);
452	}
453}
454
455static struct uart_ops meson_uart_ops = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
456	.set_mctrl      = meson_uart_set_mctrl,
457	.get_mctrl      = meson_uart_get_mctrl,
458	.tx_empty	= meson_uart_tx_empty,
459	.start_tx	= meson_uart_start_tx,
460	.stop_tx	= meson_uart_stop_tx,
461	.stop_rx	= meson_uart_stop_rx,
462	.startup	= meson_uart_startup,
463	.shutdown	= meson_uart_shutdown,
464	.set_termios	= meson_uart_set_termios,
465	.type		= meson_uart_type,
466	.config_port	= meson_uart_config_port,
467	.request_port	= meson_uart_request_port,
468	.release_port	= meson_uart_release_port,
469	.verify_port	= meson_uart_verify_port,
 
 
 
 
470};
471
472#ifdef CONFIG_SERIAL_MESON_CONSOLE
 
 
 
 
 
 
 
 
473
474static void meson_console_putchar(struct uart_port *port, int ch)
475{
476	if (!port->membase)
477		return;
478
479	while (readl(port->membase + AML_UART_STATUS) & AML_UART_TX_FULL)
480		cpu_relax();
481	writel(ch, port->membase + AML_UART_WFIFO);
482}
483
484static void meson_serial_port_write(struct uart_port *port, const char *s,
485				    u_int count)
486{
487	unsigned long flags;
488	int locked;
489	u32 val, tmp;
490
491	local_irq_save(flags);
492	if (port->sysrq) {
493		locked = 0;
494	} else if (oops_in_progress) {
495		locked = spin_trylock(&port->lock);
496	} else {
497		spin_lock(&port->lock);
498		locked = 1;
499	}
500
501	val = readl(port->membase + AML_UART_CONTROL);
502	val |= AML_UART_TX_EN;
503	tmp = val & ~(AML_UART_TX_INT_EN | AML_UART_RX_INT_EN);
504	writel(tmp, port->membase + AML_UART_CONTROL);
505
506	uart_console_write(port, s, count, meson_console_putchar);
507	writel(val, port->membase + AML_UART_CONTROL);
508
509	if (locked)
510		spin_unlock(&port->lock);
511	local_irq_restore(flags);
512}
513
514static void meson_serial_console_write(struct console *co, const char *s,
515				       u_int count)
516{
517	struct uart_port *port;
518
519	port = meson_ports[co->index];
520	if (!port)
521		return;
522
523	meson_serial_port_write(port, s, count);
524}
525
526static int meson_serial_console_setup(struct console *co, char *options)
527{
528	struct uart_port *port;
529	int baud = 115200;
530	int bits = 8;
531	int parity = 'n';
532	int flow = 'n';
533
534	if (co->index < 0 || co->index >= AML_UART_PORT_NUM)
535		return -EINVAL;
536
537	port = meson_ports[co->index];
538	if (!port || !port->membase)
539		return -ENODEV;
540
 
 
541	if (options)
542		uart_parse_options(options, &baud, &parity, &bits, &flow);
543
544	return uart_set_options(port, co, baud, parity, bits, flow);
545}
546
547static struct console meson_serial_console = {
548	.name		= AML_UART_DEV_NAME,
549	.write		= meson_serial_console_write,
550	.device		= uart_console_device,
551	.setup		= meson_serial_console_setup,
552	.flags		= CON_PRINTBUFFER,
553	.index		= -1,
554	.data		= &meson_uart_driver,
555};
 
556
557static int __init meson_serial_console_init(void)
558{
559	register_console(&meson_serial_console);
560	return 0;
561}
562console_initcall(meson_serial_console_init);
563
564static void meson_serial_early_console_write(struct console *co,
565					     const char *s,
566					     u_int count)
567{
568	struct earlycon_device *dev = co->data;
569
570	meson_serial_port_write(&dev->port, s, count);
571}
572
573static int __init
574meson_serial_early_console_setup(struct earlycon_device *device, const char *opt)
575{
576	if (!device->port.membase)
577		return -ENODEV;
578
 
579	device->con->write = meson_serial_early_console_write;
580	return 0;
581}
582OF_EARLYCON_DECLARE(meson, "amlogic,meson-uart",
583		    meson_serial_early_console_setup);
584
585#define MESON_SERIAL_CONSOLE	(&meson_serial_console)
 
 
 
586#else
587#define MESON_SERIAL_CONSOLE	NULL
588#endif
589
590static struct uart_driver meson_uart_driver = {
591	.owner		= THIS_MODULE,
592	.driver_name	= "meson_uart",
593	.dev_name	= AML_UART_DEV_NAME,
594	.nr		= AML_UART_PORT_NUM,
595	.cons		= MESON_SERIAL_CONSOLE,
596};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597
598static int meson_uart_probe(struct platform_device *pdev)
599{
600	struct resource *res_mem, *res_irq;
 
 
601	struct uart_port *port;
602	struct clk *clk;
603	int ret = 0;
 
 
604
605	if (pdev->dev.of_node)
606		pdev->id = of_alias_get_id(pdev->dev.of_node, "serial");
607
 
 
 
 
 
 
 
 
 
 
 
608	if (pdev->id < 0 || pdev->id >= AML_UART_PORT_NUM)
609		return -EINVAL;
610
611	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
612	if (!res_mem)
613		return -ENODEV;
614
615	res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
616	if (!res_irq)
617		return -ENODEV;
 
 
 
618
619	if (meson_ports[pdev->id]) {
620		dev_err(&pdev->dev, "port %d already allocated\n", pdev->id);
621		return -EBUSY;
622	}
623
624	port = devm_kzalloc(&pdev->dev, sizeof(struct uart_port), GFP_KERNEL);
625	if (!port)
626		return -ENOMEM;
627
628	clk = clk_get(&pdev->dev, NULL);
629	if (IS_ERR(clk))
630		return PTR_ERR(clk);
 
 
 
 
 
 
 
 
 
 
 
631
632	port->uartclk = clk_get_rate(clk);
633	port->iotype = UPIO_MEM;
634	port->mapbase = res_mem->start;
635	port->irq = res_irq->start;
636	port->flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP | UPF_LOW_LATENCY;
 
 
 
 
637	port->dev = &pdev->dev;
638	port->line = pdev->id;
639	port->type = PORT_MESON;
640	port->x_char = 0;
641	port->ops = &meson_uart_ops;
642	port->fifosize = 64;
 
643
644	meson_ports[pdev->id] = port;
645	platform_set_drvdata(pdev, port);
646
647	/* reset port before registering (and possibly registering console) */
648	if (meson_uart_request_port(port) >= 0) {
649		meson_uart_reset(port);
650		meson_uart_release_port(port);
651	}
652
653	ret = uart_add_one_port(&meson_uart_driver, port);
654	if (ret)
655		meson_ports[pdev->id] = NULL;
656
657	return ret;
658}
659
660static int meson_uart_remove(struct platform_device *pdev)
661{
 
662	struct uart_port *port;
663
664	port = platform_get_drvdata(pdev);
665	uart_remove_one_port(&meson_uart_driver, port);
 
666	meson_ports[pdev->id] = NULL;
667
668	return 0;
 
 
 
 
 
669}
670
 
 
 
 
 
 
 
 
 
 
 
 
 
671
672static const struct of_device_id meson_uart_dt_match[] = {
673	{ .compatible = "amlogic,meson-uart" },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
674	{ /* sentinel */ },
675};
676MODULE_DEVICE_TABLE(of, meson_uart_dt_match);
677
678static  struct platform_driver meson_uart_platform_driver = {
679	.probe		= meson_uart_probe,
680	.remove		= meson_uart_remove,
681	.driver		= {
682		.name		= "meson_uart",
683		.of_match_table	= meson_uart_dt_match,
684	},
685};
686
687static int __init meson_uart_init(void)
688{
689	int ret;
690
691	ret = uart_register_driver(&meson_uart_driver);
692	if (ret)
693		return ret;
694
695	ret = platform_driver_register(&meson_uart_platform_driver);
696	if (ret)
697		uart_unregister_driver(&meson_uart_driver);
698
699	return ret;
700}
701
702static void __exit meson_uart_exit(void)
703{
704	platform_driver_unregister(&meson_uart_platform_driver);
705	uart_unregister_driver(&meson_uart_driver);
706}
707
708module_init(meson_uart_init);
709module_exit(meson_uart_exit);
710
711MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
712MODULE_DESCRIPTION("Amlogic Meson serial port driver");
713MODULE_LICENSE("GPL v2");