From patchwork Fri Aug 23 01:01:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagar Shrikant Kadam X-Patchwork-Id: 1151902 X-Patchwork-Delegate: uboot@andestech.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.b="UtGT2iTN"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 46F35w3wlMz9sN6 for ; Fri, 23 Aug 2019 11:02:28 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 2F10DC220EA; Fri, 23 Aug 2019 01:02:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 7240BC220A3; Fri, 23 Aug 2019 01:02:11 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 87055C22109; Fri, 23 Aug 2019 01:01:58 +0000 (UTC) Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) by lists.denx.de (Postfix) with ESMTPS id 9DE7CC220BB for ; Fri, 23 Aug 2019 01:01:54 +0000 (UTC) Received: by mail-pl1-f195.google.com with SMTP id c2so4488009plz.13 for ; Thu, 22 Aug 2019 18:01:54 -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=xPCj5/r9VoWftOIwteKSGxbDKxnFLrfUU4RSKIIzJ3w=; b=UtGT2iTNG3KjJRWAfxfO/AzoNJboJPye3WRDOkuh/+q0YZ4uZGRTwUTRfX5SAw2W+N zUOH1JmQyLl5qlmk6zyz3lu5BeoLY/aBqS1f2yzPWa/f4qZQj9DAm+c5y9XzwuLcc/vY s1rdj8nAxx40gSKdCYZNSDMXC7Yf9rPNSfSiFnRW0E0zzmXPN30RZ72tfZjSEBjJxp76 L7cLC94LE4Bb05ruttaGDkCceBFf+I3RGzIqp/3EgjuyvZizbdSolYVxIGkySzyU8nKW k3JBFlRvOtNZZYAPtnuE14AmarlmE+/fr6y1SuCC8QqCV+ZsRSgXyO61GuXqn2Vgrc1N E6BQ== 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=xPCj5/r9VoWftOIwteKSGxbDKxnFLrfUU4RSKIIzJ3w=; b=eTXH78aW3t42VF/J2B6ZIFyzAlCw7aKLvHxCGNoPQ1wyuPl9Gha5zvPdSTpN05TsR8 +W2JQJHMOj1JbqZ9vkFBDdjorNki1DpvbXMDNeVLcxX3Te3s5vGaW+yS9X9hpnpHiIVp cC66VOUkNS57fxQodRZzXOX8DaTrgHzAZBZ5LR+jo0DjHLeZe1tB8psOjzEa1C7qiDGz dNQE/KEDpn0q7P8Lo2rWOG6A7DxzUVm+0jVNo7fzzWu4yQR6Qvn5RkEsp7+VWcpzP2p8 nsWbmxLVFBd5NZpnN1R0Jq6IGNOPM+nXbEBHUQVYFN+6nYDu2yh8z5rpAejm3I0R0SoM d1ug== X-Gm-Message-State: APjAAAWCGrbJDHU7nGxoWiBJ9AwF1B6FHjU6K3yGDaGinlGiIsi+5Rlv 7CD7n95zukfRGB2f6nqRxGOODvdq2gY= X-Google-Smtp-Source: APXvYqwQe6gydilIGgtw9F2ezMnUizwv2m38DAUhtQAitZf9AfHrqESoMdgweD2N9fq0an681ffhGg== X-Received: by 2002:a17:902:650d:: with SMTP id b13mr1750608plk.90.1566522112617; Thu, 22 Aug 2019 18:01:52 -0700 (PDT) Received: from gamma07.internal.sifive.com ([64.62.193.194]) by smtp.googlemail.com with ESMTPSA id j5sm426208pgp.59.2019.08.22.18.01.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 22 Aug 2019 18:01:52 -0700 (PDT) From: Sagar Shrikant Kadam To: u-boot@lists.denx.de, rick@andestech.com, paul.walmsley@sifive.com, palmer@sifive.com, anup.patel@wdc.com, atish.patra@wdc.com Date: Thu, 22 Aug 2019 18:01:38 -0700 Message-Id: <1566522098-9254-2-git-send-email-sagar.kadam@sifive.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1566522098-9254-1-git-send-email-sagar.kadam@sifive.com> References: <1566522098-9254-1-git-send-email-sagar.kadam@sifive.com> Cc: wesley@sifive.com Subject: [U-Boot] [U-BOOT PATCH] gpio: fu540: add support for DM based gpio driver for FU540 SoC X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" This patch adds a DM based driver model for gpio controller present in FU540-C000 SoC on HiFive Unleashed A00 board. This SoC has one GPIO bank and 16 GPIO lines in total, out of which GPIO0 to GPIO9 and GPIO15 are routed to the J1 header on the board. This implementation is ported from linux based gpio driver submitted for review by Wesley W. Terpstra and/or Atish Patra . The linux driver can be referred here [1] [1]: https://lkml.org/lkml/2018/10/9/1103 Signed-off-by: Sagar Shrikant Kadam --- arch/riscv/include/asm/arch-generic/gpio.h | 35 +++++++ arch/riscv/include/asm/gpio.h | 6 ++ board/sifive/fu540/Kconfig | 3 + drivers/gpio/Kconfig | 8 ++ drivers/gpio/Makefile | 1 + drivers/gpio/fu540-gpio.c | 145 +++++++++++++++++++++++++++++ 6 files changed, 198 insertions(+) create mode 100644 arch/riscv/include/asm/arch-generic/gpio.h create mode 100644 arch/riscv/include/asm/gpio.h create mode 100644 drivers/gpio/fu540-gpio.c diff --git a/arch/riscv/include/asm/arch-generic/gpio.h b/arch/riscv/include/asm/arch-generic/gpio.h new file mode 100644 index 0000000..bedb8d8 --- /dev/null +++ b/arch/riscv/include/asm/arch-generic/gpio.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (C) 2019 SiFive, Inc. + */ + +#ifndef _GPIO_FU540_H +#define _GPIO_FU540_H + +#define GPIO_INPUT_VAL 0x00 +#define GPIO_INPUT_EN 0x04 +#define GPIO_OUTPUT_EN 0x08 +#define GPIO_OUTPUT_VAL 0x0C +#define GPIO_RISE_IE 0x18 +#define GPIO_RISE_IP 0x1C +#define GPIO_FALL_IE 0x20 +#define GPIO_FALL_IP 0x24 +#define GPIO_HIGH_IE 0x28 +#define GPIO_HIGH_IP 0x2C +#define GPIO_LOW_IE 0x30 +#define GPIO_LOW_IP 0x34 +#define GPIO_OUTPUT_XOR 0x40 + +#define NR_GPIOS 16 + +enum gpio_state { + LOW, + HIGH +}; + +/* Details about a GPIO bank */ +struct fu540_gpio_platdata { + u8 *base; /* address of registers in physical memory */ +}; + +#endif /* _GPIO_FU540_H */ diff --git a/arch/riscv/include/asm/gpio.h b/arch/riscv/include/asm/gpio.h new file mode 100644 index 0000000..008d756 --- /dev/null +++ b/arch/riscv/include/asm/gpio.h @@ -0,0 +1,6 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright 2018 SiFive, Inc. + */ + +#include diff --git a/board/sifive/fu540/Kconfig b/board/sifive/fu540/Kconfig index 5d65080..f939ed2 100644 --- a/board/sifive/fu540/Kconfig +++ b/board/sifive/fu540/Kconfig @@ -44,6 +44,9 @@ config BOARD_SPECIFIC_OPTIONS # dummy imply MMC_SPI imply MMC_BROKEN_CD imply CMD_MMC + imply DM_GPIO + imply FU540_GPIO + imply CMD_GPIO imply SMP endif diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 7d9c97f..b93092a 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -280,6 +280,14 @@ config STM32_GPIO usable on many stm32 families like stm32f4/f7/h7 and stm32mp1. Tested on STM32F7. +config FU540_GPIO + bool "FU540 GPIO Driver" + depends on DM_GPIO + help + Device model driver for GPIO controller present in FU540 SoC. This + driver enables GPIO interface on HiFive Unleashed A00 board a board + from SiFive Inc. having FU540-C000 SoC. + config MVEBU_GPIO bool "Marvell MVEBU GPIO driver" depends on DM_GPIO && ARCH_MVEBU diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index 4a8aa0f..238ad17 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -61,3 +61,4 @@ obj-$(CONFIG_$(SPL_)PCF8575_GPIO) += pcf8575_gpio.o obj-$(CONFIG_PM8916_GPIO) += pm8916_gpio.o obj-$(CONFIG_MT7621_GPIO) += mt7621_gpio.o obj-$(CONFIG_MSCC_SGPIO) += mscc_sgpio.o +obj-$(CONFIG_FU540_GPIO) += fu540-gpio.o diff --git a/drivers/gpio/fu540-gpio.c b/drivers/gpio/fu540-gpio.c new file mode 100644 index 0000000..7761689 --- /dev/null +++ b/drivers/gpio/fu540-gpio.c @@ -0,0 +1,145 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * SiFive GPIO driver + * + * Copyright (C) 2019 SiFive, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include + +static int fu540_gpio_probe(struct udevice *dev) +{ + struct fu540_gpio_platdata *plat = dev_get_platdata(dev); + struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); + char name[18], *str; + + sprintf(name, "gpio@%4p", plat->base); + str = strdup(name); + if (!str) + return -ENOMEM; + uc_priv->bank_name = str; + uc_priv->gpio_count = NR_GPIOS; + + return 0; +} + +static void fu540_update_gpio_reg(u8 *bptr, u32 offset, bool value) +{ + void __iomem *ptr = (void __iomem *)bptr; + + u32 bit = BIT(offset), old = readl(ptr); + + if (value) + writel(old | bit, ptr); + else + writel(old & ~bit, ptr); +} + +static int fu540_gpio_direction_input(struct udevice *dev, u32 offset) +{ + struct fu540_gpio_platdata *plat = dev_get_platdata(dev); + + if (offset > NR_GPIOS) + return -EINVAL; + + /* Configure GPIO direction as input. */ + fu540_update_gpio_reg(plat->base + GPIO_INPUT_EN, offset, true); + fu540_update_gpio_reg(plat->base + GPIO_OUTPUT_EN, offset, false); + + return 0; +} + +static int fu540_gpio_direction_output(struct udevice *dev, u32 offset, + int value) +{ + struct fu540_gpio_platdata *plat = dev_get_platdata(dev); + + if (offset > NR_GPIOS) + return -EINVAL; + + /* Configure GPIO direction as output. */ + fu540_update_gpio_reg(plat->base + GPIO_OUTPUT_EN, offset, true); + fu540_update_gpio_reg(plat->base + GPIO_INPUT_EN, offset, false); + + /* Set the Output state of the PIN */ + fu540_update_gpio_reg(plat->base + GPIO_OUTPUT_VAL, offset, value); + + return 0; +} + +static int fu540_gpio_get_value(struct udevice *dev, u32 offset) +{ + struct fu540_gpio_platdata *plat = dev_get_platdata(dev); + int val; + int dir; + + if (offset > NR_GPIOS) + return -EINVAL; + + /* Get direction of the pin OUTPUT=0 INPUT=1 */ + dir = !(readl(plat->base + GPIO_OUTPUT_EN) & BIT(offset)); + + if (dir) + val = readl(plat->base + GPIO_INPUT_VAL) & BIT(offset); + else + val = readl(plat->base + GPIO_OUTPUT_VAL) & BIT(offset); + + return val ? HIGH : LOW; +} + +static int fu540_gpio_set_value(struct udevice *dev, u32 offset, int value) +{ + struct fu540_gpio_platdata *plat = dev_get_platdata(dev); + + if (offset > NR_GPIOS) + return -EINVAL; + + fu540_update_gpio_reg(plat->base + GPIO_OUTPUT_VAL, offset, value); + + return 0; +} + +static const struct udevice_id fu540_gpio_match[] = { + { .compatible = "sifive,gpio0" }, + { } +}; + +static const struct dm_gpio_ops gpio_sifive_ops = { + .direction_input = fu540_gpio_direction_input, + .direction_output = fu540_gpio_direction_output, + .get_value = fu540_gpio_get_value, + .set_value = fu540_gpio_set_value, +}; + +static int fu540_gpio_ofdata_to_platdata(struct udevice *dev) +{ + struct fu540_gpio_platdata *plat = dev_get_platdata(dev); + fdt_addr_t addr; + + addr = devfdt_get_addr(dev); + if (addr == FDT_ADDR_T_NONE) + return -EINVAL; + + plat->base = (u8 *)addr; + return 0; +} + +U_BOOT_DRIVER(gpio_sifive) = { + .name = "gpio_sifive", + .id = UCLASS_GPIO, + .of_match = fu540_gpio_match, + .ofdata_to_platdata = of_match_ptr(fu540_gpio_ofdata_to_platdata), + .platdata_auto_alloc_size = sizeof(struct fu540_gpio_platdata), + .ops = &gpio_sifive_ops, + .probe = fu540_gpio_probe, + .priv_auto_alloc_size = sizeof(struct fu540_gpio_platdata), +};