From patchwork Fri Apr 30 06:35:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Cederman X-Patchwork-Id: 1471981 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2001:8b0:10b:1:d65d:64ff:fe57:4e05; helo=desiato.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=desiato.20200630 header.b=Bvtw+5cr; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=doQw3gye; dkim-atps=neutral Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FWjMX3D8mz9sjD for ; Fri, 30 Apr 2021 16:36:56 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=khSKU2kDly0uMEEqhzFr6nv5Xa68sOqApcv3TzGe+sc=; b=Bvtw+5crEL8XFN73bcgl3SADBu WjCJQ9bMqBARxdkuJhWohhmmrTrpFttPWgiiE1vPSBvLmVHsgWYtLcwGddC0Bq+4MuU/M9qbp7jiw qaJZvjyVBQUay4XBvNgaqn70bhuzj8dRzPN51cJiuL2z4DGTEByt0fvnMstI+657bnt9YGHbISKOn 2+zZ2E/+ufZ7JGqzXlNY0GemAKBgPN4w3CrD/k+edBvBoBy1IwB6HsrBWhhbPWa8dzAkYvbZR8PnC PvRnIms2JC2y6/6xlTT083UVQ2W3xEegKrTH4TpzanhhWH1hgSBDzeQCYgIOOpfqiT/qdQx6QzY/l JM0j/pBQ==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lcMld-007K2G-6k; Fri, 30 Apr 2021 06:36:49 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lcMlZ-007K27-OH for opensbi@desiato.infradead.org; Fri, 30 Apr 2021 06:36:45 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:In-Reply-To:References; bh=MPPjgHM4TvOGJmEfL15zwpstEoMYzbod5UOImQA8uT0=; b=doQw3gye7jZgo4r/PW7T3ebNKT HL1bNnP62ktUcfDeULmVbGIVXILS57mIkeff3FIxPJjSFgREEaOzz5guhoS32cQKQom6vc6R2q6sx ZWuXQieZuh350YYihDInqOBBMeOp4GNrQKsTqnlSJ0YpplGzZp9rK8eTl++mr4Q6g6gRovvK7hnyp Zp78zpoYQFT1cCaMZObuD3sbTXyWXf4hyCnFCcq/ELkY1N4o7k/TI0HHkg7UZSTOBEpxaOxsdnsia /vBw1qn9dIqlH/uU5e3Jewgi9neKP65oi3HYW99bbHiKzg0RJ0KyHhv0Op/d9bhbiXqmJHnKhl3QC q8N22DXg==; Received: from pio-ftg-msa1.bahnhof.se ([79.136.2.47]) by bombadil.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lcMlV-001Ajm-QP for opensbi@lists.infradead.org; Fri, 30 Apr 2021 06:36:44 +0000 Received: from localhost (localhost [127.0.0.1]) by pio-ftg-msa1.bahnhof.se (Postfix) with ESMTP id 892273F437; Fri, 30 Apr 2021 08:36:33 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at bahnhof.se X-Spam-Flag: NO X-Spam-Score: -1.9 X-Spam-Level: X-Spam-Status: No, score=-1.9 tagged_above=-999 required=6.31 tests=[BAYES_00=-1.9, NO_RELAYS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no Received: from pio-ftg-msa1.bahnhof.se ([127.0.0.1]) by localhost (pio-ftg-msa1.bahnhof.se [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id AnPQApsx0Ews; Fri, 30 Apr 2021 08:36:20 +0200 (CEST) Received: by pio-ftg-msa1.bahnhof.se (Postfix) with ESMTPA id D58043F415; Fri, 30 Apr 2021 08:36:19 +0200 (CEST) From: Daniel Cederman To: opensbi@lists.infradead.org Cc: daniel@gaisler.com Subject: [PATCH 1/1] lib: utils/serial: Add support for Gaisler APBUART Date: Fri, 30 Apr 2021 08:35:56 +0200 Message-Id: <20210430063556.4153719-1-cederman@gaisler.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210429_233642_203270_CD44EB27 X-CRM114-Status: GOOD ( 22.62 ) X-Spam-Score: 0.0 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This patch adds support for the UART used by the NOEL-V processor. Cobham Gaisler's NOEL-V RISC-V processor IP is available under GPL and commercial license and is described in more detail at https://www.gaisler.com/noelv. Content analysis details: (0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org This patch adds support for the UART used by the NOEL-V processor. Cobham Gaisler's NOEL-V RISC-V processor IP is available under GPL and commercial license and is described in more detail at https://www.gaisler.com/noelv. Signed-off-by: Daniel Cederman Reviewed-by: Anup Patel Reviewed-by: Bin Meng --- include/sbi_utils/fdt/fdt_helper.h | 3 + include/sbi_utils/serial/gaisler-uart.h | 17 +++++ lib/utils/fdt/fdt_helper.c | 40 ++++++++++++ lib/utils/serial/fdt_serial.c | 2 + lib/utils/serial/fdt_serial_gaisler.c | 35 ++++++++++ lib/utils/serial/gaisler-uart.c | 86 +++++++++++++++++++++++++ lib/utils/serial/objects.mk | 2 + 7 files changed, 185 insertions(+) create mode 100644 include/sbi_utils/serial/gaisler-uart.h create mode 100644 lib/utils/serial/fdt_serial_gaisler.c create mode 100644 lib/utils/serial/gaisler-uart.c diff --git a/include/sbi_utils/fdt/fdt_helper.h b/include/sbi_utils/fdt/fdt_helper.h index f5222de..c89f2e6 100644 --- a/include/sbi_utils/fdt/fdt_helper.h +++ b/include/sbi_utils/fdt/fdt_helper.h @@ -39,6 +39,9 @@ int fdt_parse_hart_id(void *fdt, int cpu_offset, u32 *hartid); int fdt_parse_max_hart_id(void *fdt, u32 *max_hartid); +int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset, + struct platform_uart_data *uart); + int fdt_parse_shakti_uart_node(void *fdt, int nodeoffset, struct platform_uart_data *uart); diff --git a/include/sbi_utils/serial/gaisler-uart.h b/include/sbi_utils/serial/gaisler-uart.h new file mode 100644 index 0000000..11d1f20 --- /dev/null +++ b/include/sbi_utils/serial/gaisler-uart.h @@ -0,0 +1,17 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2021 Cobham Gaisler AB. + * + * Authors: + * Daniel Cederman + */ + +#ifndef __SERIAL_GAISLER_APBUART_H__ +#define __SERIAL_GAISLER_APBUART_H__ + +#include + +int gaisler_uart_init(unsigned long base, u32 in_freq, u32 baudrate); + +#endif diff --git a/lib/utils/fdt/fdt_helper.c b/lib/utils/fdt/fdt_helper.c index 909de01..9143e44 100644 --- a/lib/utils/fdt/fdt_helper.c +++ b/lib/utils/fdt/fdt_helper.c @@ -26,6 +26,8 @@ #define DEFAULT_SIFIVE_UART_REG_SHIFT 0 #define DEFAULT_SIFIVE_UART_REG_IO_WIDTH 4 +#define DEFAULT_GAISLER_UART_REG_IO_WIDTH 4 + #define DEFAULT_SHAKTI_UART_FREQ 50000000 #define DEFAULT_SHAKTI_UART_BAUD 115200 @@ -213,6 +215,44 @@ int fdt_parse_max_hart_id(void *fdt, u32 *max_hartid) return 0; } +int fdt_parse_gaisler_uart_node(void *fdt, int nodeoffset, + struct platform_uart_data *uart) +{ + int len, rc; + const fdt32_t *val; + unsigned long reg_addr, reg_size; + + if (nodeoffset < 0 || !uart || !fdt) + return SBI_ENODEV; + + rc = fdt_get_node_addr_size(fdt, nodeoffset, ®_addr, ®_size); + if (rc < 0 || !reg_addr || !reg_size) + return SBI_ENODEV; + uart->addr = reg_addr; + + /** + * UART address is mandatory. clock-frequency and current-speed + * may not be present. Don't return error. + */ + val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "clock-frequency", &len); + if (len > 0 && val) + uart->freq = fdt32_to_cpu(*val); + else + uart->freq = DEFAULT_UART_FREQ; + + val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "current-speed", &len); + if (len > 0 && val) + uart->baud = fdt32_to_cpu(*val); + else + uart->baud = DEFAULT_UART_BAUD; + + /* For Gaisler APBUART, the reg-shift and reg-io-width are fixed .*/ + uart->reg_shift = DEFAULT_UART_REG_SHIFT; + uart->reg_io_width = DEFAULT_GAISLER_UART_REG_IO_WIDTH; + + return 0; +} + int fdt_parse_shakti_uart_node(void *fdt, int nodeoffset, struct platform_uart_data *uart) { diff --git a/lib/utils/serial/fdt_serial.c b/lib/utils/serial/fdt_serial.c index 43c55e8..bad3387 100644 --- a/lib/utils/serial/fdt_serial.c +++ b/lib/utils/serial/fdt_serial.c @@ -16,12 +16,14 @@ extern struct fdt_serial fdt_serial_uart8250; extern struct fdt_serial fdt_serial_sifive; extern struct fdt_serial fdt_serial_htif; extern struct fdt_serial fdt_serial_shakti; +extern struct fdt_serial fdt_serial_gaisler; static struct fdt_serial *serial_drivers[] = { &fdt_serial_uart8250, &fdt_serial_sifive, &fdt_serial_htif, &fdt_serial_shakti, + &fdt_serial_gaisler }; static struct fdt_serial dummy = { diff --git a/lib/utils/serial/fdt_serial_gaisler.c b/lib/utils/serial/fdt_serial_gaisler.c new file mode 100644 index 0000000..9a9f9a8 --- /dev/null +++ b/lib/utils/serial/fdt_serial_gaisler.c @@ -0,0 +1,35 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2021 Cobham Gaisler AB. + * + * Authors: + * Daniel Cederman + */ + +#include +#include +#include + +static int serial_gaisler_init(void *fdt, int nodeoff, + const struct fdt_match *match) +{ + int rc; + struct platform_uart_data uart; + + rc = fdt_parse_gaisler_uart_node(fdt, nodeoff, &uart); + if (rc) + return rc; + + return gaisler_uart_init(uart.addr, uart.freq, uart.baud); +} + +static const struct fdt_match serial_gaisler_match[] = { + { .compatible = "gaisler,apbuart" }, + {}, +}; + +struct fdt_serial fdt_serial_gaisler = { + .match_table = serial_gaisler_match, + .init = serial_gaisler_init +}; diff --git a/lib/utils/serial/gaisler-uart.c b/lib/utils/serial/gaisler-uart.c new file mode 100644 index 0000000..49298e9 --- /dev/null +++ b/lib/utils/serial/gaisler-uart.c @@ -0,0 +1,86 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2021 Cobham Gaisler AB. + * + * Authors: + * Daniel Cederman + */ + +#include +#include +#include + +/* clang-format off */ + +#define UART_REG_DATA 0 +#define UART_REG_STATUS 1 +#define UART_REG_CTRL 2 +#define UART_REG_SCALER 3 + +#define UART_DATA_DATA 0x000000ff +#define UART_STATUS_FIFOFULL 0x00000200 +#define UART_STATUS_DATAREADY 0x00000001 + +#define UART_CTRL_DB (1<<11) +#define UART_CTRL_FL (1<<6) +#define UART_CTRL_TE (1<<1) +#define UART_CTRL_RE (1<<0) + +/* clang-format on */ + +static volatile void *uart_base; + +static u32 get_reg(u32 num) +{ + return readl(uart_base + (num * 0x4)); +} + +static void set_reg(u32 num, u32 val) +{ + writel(val, uart_base + (num * 0x4)); +} + +static void gaisler_uart_putc(char ch) +{ + while (get_reg(UART_REG_STATUS) & UART_STATUS_FIFOFULL) + ; + + set_reg(UART_REG_DATA, ch); +} + +static int gaisler_uart_getc(void) +{ + u32 ret = get_reg(UART_REG_STATUS); + if (!(ret & UART_STATUS_DATAREADY)) + return -1; + return get_reg(UART_REG_DATA) & UART_DATA_DATA; +} + +static struct sbi_console_device gaisler_console = { + .name = "gaisler_uart", + .console_putc = gaisler_uart_putc, + .console_getc = gaisler_uart_getc +}; + +int gaisler_uart_init(unsigned long base, u32 in_freq, u32 baudrate) +{ + u32 ctrl; + + uart_base = (volatile void *)base; + + /* Configure baudrate */ + if (in_freq) + set_reg(UART_REG_SCALER, in_freq / (baudrate * 8 + 7)); + + ctrl = get_reg(UART_REG_CTRL); + /* Preserve debug mode and flow control */ + ctrl &= (UART_CTRL_DB | UART_CTRL_FL); + /* Enable TX and RX */ + ctrl |= UART_CTRL_TE | UART_CTRL_RE; + set_reg(UART_REG_CTRL, ctrl); + + sbi_console_set_device(&gaisler_console); + + return 0; +} diff --git a/lib/utils/serial/objects.mk b/lib/utils/serial/objects.mk index c0746f0..9fb0902 100644 --- a/lib/utils/serial/objects.mk +++ b/lib/utils/serial/objects.mk @@ -8,10 +8,12 @@ # libsbiutils-objs-y += serial/fdt_serial.o +libsbiutils-objs-y += serial/fdt_serial_gaisler.o libsbiutils-objs-y += serial/fdt_serial_htif.o libsbiutils-objs-y += serial/fdt_serial_shakti.o libsbiutils-objs-y += serial/fdt_serial_sifive.o libsbiutils-objs-y += serial/fdt_serial_uart8250.o +libsbiutils-objs-y += serial/gaisler-uart.o libsbiutils-objs-y += serial/shakti-uart.o libsbiutils-objs-y += serial/sifive-uart.o libsbiutils-objs-y += serial/uart8250.o