From patchwork Thu Jul 1 07:39:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Green Wan X-Patchwork-Id: 1499458 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=2607:7c80:54:e::133; helo=bombadil.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=bombadil.20210309 header.b=l0SnqUc+; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=W/Vd93ea; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (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 4GFsfB5nQ0z9sWw for ; Thu, 1 Jul 2021 19:01:54 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: 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: List-Owner; bh=4Fl8uJ5CTbaA2gGjDoa8sUdb0KhvnVE1tpQY86Knyrw=; b=l0SnqUc+Hv4x9b IdOMnXOyVXgKe+Dw2d8XLDoTQkD3KUWz/saUxh+gR3+sckbR2HCB0px/0hNTOEkmNf28hQiJNwcfn 0ff3vLOu9AvADh5CfUanLFA16Rk5OBZuUl7JboSL4LKFnOJaXJ8UtPMQHzEsbH9BCuA6UvG5MZ8nH EdGKiINqfd7TVF2GMCu60sGnBy3W/hVd203fGLIZOyWW7Z3cXEgxDHymbHNZypCC306G2Cs0gMmPP wpQSGwGSI7zC+2pGlJ0BFH+u2JCxxLM/5TVXQ2t2ok+O/d79n1GdCMgr14W7KqBQFb3Esukms5WH9 /Te+o5lJLsV7JY0h1AXA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lysZy-00Gcpd-1Z; Thu, 01 Jul 2021 09:01:50 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1lyrJ3-00GRMo-DO for opensbi@lists.infradead.org; Thu, 01 Jul 2021 07:40:19 +0000 Received: by mail-pl1-x632.google.com with SMTP id v13so3164168ple.9 for ; Thu, 01 Jul 2021 00:40:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=a14sewlJ61LLDR22Rvf7q4NupsD7YSxTX/sG75UE4Vc=; b=W/Vd93ea9u3FsL/FxFQK8YgQ16CI32snMUrSI5dCXjKuurkhS56oFOCiUrPT78G3qU HoH8SaWttdTgrvFPUO3glUJXoeJN+1KztXCof5VVETXO95sIbYvtOt2G+nQO5S92lBxt bOf7QbgCJwnhZDPSCRgFc/BfVcobPHMdVQ1HdO1gfM58uGc3/NeIcFH7HO6S3gXUv330 hV0lBC0u5UHAiD9/t4Hzd7EF4BI6qzSdmV9Wv94wYYSqFih6dNEoN+a5LymZ/NBFyOq/ T4NRzmZzp5bYeyfnHAyMGknskzqpXpx6InSwU2pbG3KX2Ixn17RvsjkaIvZfho5ASuKr ELRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=a14sewlJ61LLDR22Rvf7q4NupsD7YSxTX/sG75UE4Vc=; b=f5TbsNpuGj6sltINa+LDvfl/qSHydmOHMyo+xnt9ML6/vCIGhyWGyF91afV4668zqI sV+sr0k9UDflCVxIjKop4kFK0UmQ50yfjcMBecklMXl0cF+2983123HAVsSmYqEDcnFc rSZ1R4TLwGuYkBEZFu97adHlPtkYAbxBM01xhYmkWJCyvHufKYZwtatB3vqX38pGKW+F 9pMybV3mHnS8lD37VW0oZ2QoN64IPQl8vFImItwGk1EWKYbOHmCwPMNqNmi00xgaJABL 0R6U2cw7JbKmNyZZsNkOMpP/fJHRbM8VwO+xBBsJWs2v6QVspxzC8g5cVFb78bd/YFHg hKbQ== X-Gm-Message-State: AOAM533rg/RsMoRnzppPFDEw521euyCbvccKmJK17GCogoILoJRDGlBe DPv18/jMtaPQUPtU25wGevhGv5FSv1SSPA== X-Google-Smtp-Source: ABdhPJzvMyu35aAVnXNaTPn9IuuJ7Tr0F24uNGb9ZUrEc3dm+MtvhJe4Rx+7+MvsTiUIvwCMrfxANw== X-Received: by 2002:a17:90a:ea88:: with SMTP id h8mr22766220pjz.147.1625125216429; Thu, 01 Jul 2021 00:40:16 -0700 (PDT) Received: from localhost.localdomain (36-228-106-236.dynamic-ip.hinet.net. [36.228.106.236]) by smtp.gmail.com with ESMTPSA id i6sm28125180pgg.50.2021.07.01.00.40.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jul 2021 00:40:16 -0700 (PDT) From: Green Wan To: opensbi@lists.infradead.org Cc: green.wan@sifive.com, david.abdurachmanov@sifive.com Subject: [RFC PATCH 3/4] lib: utils: add gpio generic interface Date: Thu, 1 Jul 2021 15:39:57 +0800 Message-Id: <20210701073958.15558-4-green.wan@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210701073958.15558-1-green.wan@sifive.com> References: <20210701073958.15558-1-green.wan@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210701_004017_495609_70426CD4 X-CRM114-Status: GOOD ( 21.22 ) X-Spam-Score: -0.2 (/) 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: Add generic interface for GPIO driver and add SiFive GPIO driver as an example. Currently, driver supports gpio output function only. lib/utils/gpio/fdt_gpio.c: - contains GPIO generic interfaces. lib/utils/gpio/sifive_gpio.c: - contains sifive GPIO driver. Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:632 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-Mailman-Approved-At: Thu, 01 Jul 2021 02:00:38 -0700 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: , MIME-Version: 1.0 Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Add generic interface for GPIO driver and add SiFive GPIO driver as an example. Currently, driver supports gpio output function only. lib/utils/gpio/fdt_gpio.c: - contains GPIO generic interfaces. lib/utils/gpio/sifive_gpio.c: - contains sifive GPIO driver. platform/generic/platform.c: - add entry of gpio init Signed-off-by: Green Wan --- include/sbi/sbi_gpio.h | 23 ++++++++++ include/sbi_utils/gpio/fdt_gpio.h | 32 ++++++++++++++ lib/utils/gpio/fdt_gpio.c | 55 ++++++++++++++++++++++++ lib/utils/gpio/objects.mk | 11 +++++ lib/utils/gpio/sifive_gpio.c | 70 +++++++++++++++++++++++++++++++ platform/generic/platform.c | 3 ++ 6 files changed, 194 insertions(+) create mode 100644 include/sbi/sbi_gpio.h create mode 100644 include/sbi_utils/gpio/fdt_gpio.h create mode 100644 lib/utils/gpio/fdt_gpio.c create mode 100644 lib/utils/gpio/objects.mk create mode 100644 lib/utils/gpio/sifive_gpio.c diff --git a/include/sbi/sbi_gpio.h b/include/sbi/sbi_gpio.h new file mode 100644 index 0000000..67ef9a9 --- /dev/null +++ b/include/sbi/sbi_gpio.h @@ -0,0 +1,23 @@ +/* + * SPDX-License-Identifier: GPL 2.0+ + * + * Copyright (c) 2021 SiFive. + * + * Authors: + * Green Wan + */ + +#ifndef __SBI_GPIO_H__ +#define __SBI_GPIO_H__ + +#include + +struct sbi_scratch; + +/* Initialize gpio */ +int sbi_gpio_init(struct sbi_scratch *scratch, bool cold_boot); + +/* Exit gpio */ +void sbi_gpio_exit(struct sbi_scratch *scratch); + +#endif diff --git a/include/sbi_utils/gpio/fdt_gpio.h b/include/sbi_utils/gpio/fdt_gpio.h new file mode 100644 index 0000000..c66380a --- /dev/null +++ b/include/sbi_utils/gpio/fdt_gpio.h @@ -0,0 +1,32 @@ +/* + * SPDX-License-Identifier: GPL 2.0+ + * + * Copyright (c) 2021 SiFive + * + * Authors: + * Green Wan + */ + +#ifndef __FDT_GPIO_H__ +#define __FDT_GPIO_H__ + +#include + +/** + * Make a GPIO an output, and set its value. + * + * @param gpio GPIO number + * @param value GPIO value (0 for low or 1 for high) + * @return 0 if ok, -1 on error + */ +int gpio_direction_output(unsigned int gpio, unsigned int value); + +struct fdt_gpio { + const struct fdt_match *match_table; + int (*init)(void *fdt, int nodeoff, const struct fdt_match *match); + int (*direction_output)(unsigned int gpio, unsigned int value); +}; + +int fdt_gpio_init(void); + +#endif diff --git a/lib/utils/gpio/fdt_gpio.c b/lib/utils/gpio/fdt_gpio.c new file mode 100644 index 0000000..732598b --- /dev/null +++ b/lib/utils/gpio/fdt_gpio.c @@ -0,0 +1,55 @@ +/* + * SPDX-License-Identifier: GPL 2.0+ + * + * Copyright (c) 2021 SiFive + * + * Authors: + * Green Wan + */ + +#include +#include +#include +#include + +extern struct fdt_gpio sifive_gpio; + +static struct fdt_gpio *gpio_drivers[] = { + &sifive_gpio +}; + +static struct fdt_gpio *current_driver; + +int gpio_direction_output(unsigned int gpio, unsigned int value) +{ + if (current_driver->direction_output) + return current_driver->direction_output(gpio, value); + + return 0; +} + +int fdt_gpio_init(void) +{ + int pos, noff, rc; + struct fdt_gpio *drv; + const struct fdt_match *match; + void *fdt = sbi_scratch_thishart_arg1_ptr(); + + for (pos = 0; pos < array_size(gpio_drivers); pos++) { + drv = gpio_drivers[pos]; + + noff = -1; + while ((noff = fdt_find_match(fdt, noff, + drv->match_table, + &match)) >= 0) { + if (drv->init) { + rc = drv->init(fdt, noff, match); + if (rc) + return rc; + } + current_driver = drv; + } + } + + return 0; +} diff --git a/lib/utils/gpio/objects.mk b/lib/utils/gpio/objects.mk new file mode 100644 index 0000000..727c567 --- /dev/null +++ b/lib/utils/gpio/objects.mk @@ -0,0 +1,11 @@ +# +# SPDX-License-Identifier: GPL 2.0+ +# +# Copyright (c) 2020 SiFive Inc. +# +# Authors: +# Green Wan +# + +libsbiutils-objs-y += gpio/fdt_gpio.o +libsbiutils-objs-y += gpio/sifive_gpio.o diff --git a/lib/utils/gpio/sifive_gpio.c b/lib/utils/gpio/sifive_gpio.c new file mode 100644 index 0000000..714e127 --- /dev/null +++ b/lib/utils/gpio/sifive_gpio.c @@ -0,0 +1,70 @@ +/* + * SPDX-License-Identifier: GPL 2.0+ + * + * Copyright (c) 2021 SiFive + * + * Authors: + * Green Wan + */ + +#include +#include +#include +#include +#include + +#define GPIO_OUTEN 0x8 +#define GPIO_OUTVAL 0xC +#define GPIO_BIT(b) (1UL << (b)) + +static struct platform_gpio_data gpio_data = { + .addr = 0x0 +}; + +static int sifive_gpio_init(void *fdt, int nodeoff, + const struct fdt_match *match) +{ + int rc; + + rc = fdt_parse_gpio_node(fdt, nodeoff, &gpio_data); + if (rc) + return rc; + + return 0; +} + +static int sifive_gpio_direction_output(unsigned int gpio, + unsigned int out) +{ + if (gpio_data.addr != 0) { + unsigned int val; + + val = readl((volatile void *)(gpio_data.addr + GPIO_OUTEN)); + val |= GPIO_BIT(gpio); + writel(val, (volatile void *)(gpio_data.addr + GPIO_OUTEN)); + + val = readl((volatile void *)(gpio_data.addr + GPIO_OUTVAL)); + + if (!out) + val &= ~GPIO_BIT(gpio); + else + val |= GPIO_BIT(gpio); + + writel(val, (volatile void *)(gpio_data.addr + GPIO_OUTVAL)); + + return 0; + } + + return SBI_EINVAL; +} + +static const struct fdt_match sifive_gpio_match[] = { + { .compatible = "sifive,gpio0" }, + { }, +}; + +struct fdt_gpio sifive_gpio = { + .match_table = sifive_gpio_match, + .init = sifive_gpio_init, + .direction_output = sifive_gpio_direction_output, +}; diff --git a/platform/generic/platform.c b/platform/generic/platform.c index da0c1af..39017f0 100644 --- a/platform/generic/platform.c +++ b/platform/generic/platform.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -128,6 +129,8 @@ static int generic_early_init(bool cold_boot) if (!cold_boot) return 0; + fdt_gpio_init(); + return fdt_reset_init(); }