mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Lucas Stach <dev@lynxeye.de>
To: barebox@lists.infradead.org
Subject: [PATCH 2/3] serial: cadence: add lowlevel init and putc functions
Date: Mon, 13 Jan 2020 20:37:15 +0100	[thread overview]
Message-ID: <20200113193716.133305-2-dev@lynxeye.de> (raw)
In-Reply-To: <20200113193716.133305-1-dev@lynxeye.de>

This allows to use the Cadence serial as a PBL console.

Signed-off-by: Lucas Stach <dev@lynxeye.de>
---
 include/serial/cadence.h | 55 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/include/serial/cadence.h b/include/serial/cadence.h
index 014fb01203a9..f08b5b0cba4a 100644
--- a/include/serial/cadence.h
+++ b/include/serial/cadence.h
@@ -34,4 +34,59 @@
 #define CADENCE_STS_TEMPTY		(1 << 3)
 #define CADENCE_STS_TFUL		(1 << 4)
 
+static inline void cadence_uart_init(void __iomem *uartbase)
+{
+	int baudrate = CONFIG_BAUDRATE;
+	unsigned int clk = 49999995;
+	unsigned int gen, div;
+
+	/* disable transmitter and receiver */
+	writel(0, uartbase + CADENCE_UART_CONTROL);
+
+	/* calculate and set baud clock generator parameters */
+	for (div = 4; div < 256; div++) {
+		int calc_rate, error;
+
+		gen = clk / (baudrate * (div + 1));
+
+		if (gen < 1 || gen > 65535)
+			continue;
+
+		calc_rate = clk / (gen * (div + 1));
+		error = baudrate - calc_rate;
+		if (error < 0)
+			error *= -1;
+		if (((error * 100) / baudrate) < 3)
+			break;
+	}
+
+	writel(gen, uartbase + CADENCE_UART_BAUD_GEN);
+	writel(div, uartbase + CADENCE_UART_BAUD_DIV);
+
+	/* soft-reset tx/rx paths */
+	writel(CADENCE_CTRL_RXRES | CADENCE_CTRL_TXRES,
+	       uartbase + CADENCE_UART_CONTROL);
+
+	while (readl(uartbase + CADENCE_UART_CONTROL) &
+		(CADENCE_CTRL_RXRES | CADENCE_CTRL_TXRES))
+		;
+
+	/* enable UART */
+	writel(CADENCE_MODE_CLK_REF | CADENCE_MODE_CHRL_8 |
+	       CADENCE_MODE_PAR_NONE, uartbase + CADENCE_UART_MODE);
+	writel(CADENCE_CTRL_RXEN | CADENCE_CTRL_TXEN,
+	       uartbase + CADENCE_UART_CONTROL);
+}
+
+static inline void cadence_uart_putc(void *base, int c)
+{
+	if (!(readl(base + CADENCE_UART_CONTROL) & CADENCE_CTRL_TXEN))
+		return;
+
+	while ((readl(base + CADENCE_UART_CHANNEL_STS) & CADENCE_STS_TFUL))
+		;
+
+	writel(c, base + CADENCE_UART_RXTXFIFO);
+}
+
 #endif /* __CADENCE_UART_H__ */
-- 
2.24.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2020-01-13 19:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-13 19:37 [PATCH 1/3] serial: cadence: move register definitions into header file Lucas Stach
2020-01-13 19:37 ` Lucas Stach [this message]
2020-01-13 19:37 ` [PATCH 3/3] ARM: zynq: zedboard: add PBL console support Lucas Stach
2020-01-14 11:07 ` [PATCH 1/3] serial: cadence: move register definitions into header file Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200113193716.133305-2-dev@lynxeye.de \
    --to=dev@lynxeye.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox