From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0E262C433F5 for ; Wed, 15 Dec 2021 03:02:29 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6343C8303E; Wed, 15 Dec 2021 04:01:54 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="BPHeWt4r"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B574F82F8E; Wed, 15 Dec 2021 03:59:07 +0100 (CET) Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id D94F28069D for ; Wed, 15 Dec 2021 03:59:00 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=stanley.chuys@gmail.com Received: by mail-pj1-x102a.google.com with SMTP id nh10-20020a17090b364a00b001a69adad5ebso17842858pjb.2 for ; Tue, 14 Dec 2021 18:59:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=FiKZQNp/n3JLaAnf3GfCrHDqkihraGa70G5NZdEjQFQ=; b=BPHeWt4rdFRRW9TNBrkNwR0Imm4Uj6QE5ck3zfyEzEd8AfFuaNz14af1NqFBTvBslM uAngdv/jfx6NevDqOvTT7+GFmQFzX38W1RwKQ+1mj2YUdvgE6dgvfRcIz77Fno/LVuZy uOuPJeOLw9w5L7DLF5or6taBIhxukE6VsgRyzN8ruBLUi0cBD2V1KVLqhouedFSCG2y4 6ifmO/cmghrc6+VNmAW//RwTFJvkmC3+OKfvQ/jjNFmdxeXLg8B61daJYuvGqCFhaPlG E280QD62j3dSrP50+8TD5xsT3hneiz0BpsF5QBeQRSWP2VKHbeac0Zg8Z/W2PnLNURHS LG4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=FiKZQNp/n3JLaAnf3GfCrHDqkihraGa70G5NZdEjQFQ=; b=A7B2W9RbVMb2O9kIRv9+LOGMZPM5Ti7yyN1qlAjqb/xVDGk79uqRuEM7BAPO7LMORC /CuHJ5e6wc1sXmNofCCptJVZdwMgxV6EYiZEy2dvgBXCkag3Yz7Gj3szfVidYSfhLbka j5K8PnNgYPrq85ZGMZYHtRFY/2IuzB75D4aEwF6KTnnd8P2GbNMygA7Srclo1XWzA1Gc mI8IDGUg4nIS8/uPttCen1oFvSCPJIBdwudInVZH7ezOX7JKbGuctHP0c5Ae/iA23o/a 0baVtHexSKYsRWJcuezCQlr68GbB2GNMIXuETbZhh6LdhJU+daAgOMYeshtluQjirZQ6 wvpw== X-Gm-Message-State: AOAM532Xnjhbnu1bfrODjAaAOVwyMyTTMkfcaYox1uUmp80L+4T5tPaw SFPWWvYa10riaUFa2a6YItk= X-Google-Smtp-Source: ABdhPJzeKbAEA76WsmU+kAdlvM0GQzsML7rT9UnjXQKAMf7SBOKKvttn6EcIEG+7UPXG9Om4R4txtA== X-Received: by 2002:a17:90a:b88d:: with SMTP id o13mr9419691pjr.39.1639537139315; Tue, 14 Dec 2021 18:58:59 -0800 (PST) Received: from cs20-buildserver.lan ([180.217.149.17]) by smtp.gmail.com with ESMTPSA id q18sm437413pfn.83.2021.12.14.18.58.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Dec 2021 18:58:58 -0800 (PST) From: Stanley Chu X-Google-Original-From: Stanley Chu To: lukma@denx.de, jagan@amarulasolutions.com, andre.przywara@arm.com, festevam@denx.de, narmstrong@baylibre.com, pbrobinson@gmail.com, tharvey@gateworks.com, christianshewitt@gmail.com, lokeshvutla@ti.com, sjg@chromium.org, sr@denx.de, michal.simek@xilinx.com, hs@denx.de, weijie.gao@mediatek.com, hannes.schmelzer@br-automation.com, harm.berntsen@nedap.com, sebastian.reichel@collabora.com, stephan@gerhold.net, fangyuanseu@gmail.com, kettenis@openbsd.org, seanga2@gmail.com, dsankouski@gmail.com, vabhav.sharma@nxp.com, bmeng.cn@gmail.com, patrick@blueri.se, samuel@sholland.org, giulio.benetti@benettiengineering.com, mr.bossman075@gmail.com, yschu@nuvoton.com, kwliu@nuvoton.com, ctcchien@nuvoton.com, avifishman70@gmail.com, tmaimon77@gmail.com Cc: u-boot@lists.denx.de, openbmc@lists.ozlabs.org Subject: [PATCH v1 5/9] gpio: npcm: Add support for Nuvoton NPCM SoCs Date: Wed, 15 Dec 2021 10:57:56 +0800 Message-Id: <20211215025800.26918-6-yschu@nuvoton.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211215025800.26918-1-yschu@nuvoton.com> References: <20211215025800.26918-1-yschu@nuvoton.com> X-Mailman-Approved-At: Wed, 15 Dec 2021 04:01:29 +0100 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.38 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Add Nuvoton BMC NPCM7xx/NPCM8xx gpio driver Signed-off-by: Stanley Chu --- drivers/gpio/Kconfig | 7 +++ drivers/gpio/Makefile | 1 + drivers/gpio/npcm_gpio.c | 133 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 drivers/gpio/npcm_gpio.c diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 40abc33772..2aed8fdae3 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -523,4 +523,11 @@ config NOMADIK_GPIO into a number of banks each with 32 GPIOs. The GPIOs for a device are defined in the device tree with one node for each bank. +config NPCM_GPIO + bool "NuvoTon NPCM GPIO driver" + depends on DM_GPIO + help + Support GPIO controllers on NuvoTon NPCM SoCs. + It contains eight GPIO modules with total 256 pins. + endif diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index 3c851b38c7..d0206580c4 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -69,3 +69,4 @@ obj-$(CONFIG_NX_GPIO) += nx_gpio.o obj-$(CONFIG_SIFIVE_GPIO) += sifive-gpio.o obj-$(CONFIG_NOMADIK_GPIO) += nmk_gpio.o obj-$(CONFIG_MAX7320_GPIO) += max7320_gpio.o +obj-$(CONFIG_NPCM_GPIO) += npcm_gpio.o diff --git a/drivers/gpio/npcm_gpio.c b/drivers/gpio/npcm_gpio.c new file mode 100644 index 0000000000..703d2e4815 --- /dev/null +++ b/drivers/gpio/npcm_gpio.c @@ -0,0 +1,133 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2021 Nuvoton Technology. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define NPCM_GPIO_PORTS_PER_BANK 32 + +#define NPCM_GPIO_REG_DIN 0x04 /* RO - Data In */ +#define NPCM_GPIO_REG_DOUT 0x0C /* RW - Data Out */ +#define NPCM_GPIO_REG_IEM 0x58 /* RW - Input Enable Mask */ +#define NPCM_GPIO_REG_OE 0x10 /* RW - Output Enable */ +#define NPCM_GPIO_REG_OES 0x70 /* WO - Output Enable Register Set */ +#define NPCM_GPIO_REG_OEC 0x74 /* WO - Output Enable Register Clear */ + +struct npcm_gpio_priv { + void __iomem *base; +}; + +static void npcm_gpio_offset_write(struct udevice *dev, unsigned int offset, + unsigned int reg, int value) +{ + struct npcm_gpio_priv *priv = dev_get_priv(dev); + u32 tmp; + + tmp = readl(priv->base + reg); + + if (value) + tmp |= BIT(offset); + else + tmp &= ~BIT(offset); + + writel(tmp, priv->base + reg); +} + +static int npcm_gpio_offset_read(struct udevice *dev, unsigned int offset, + unsigned int reg) +{ + struct npcm_gpio_priv *priv = dev_get_priv(dev); + + return !!(readl(priv->base + reg) & BIT(offset)); +} + +static int npcm_gpio_direction_input(struct udevice *dev, unsigned int offset) +{ + npcm_gpio_offset_write(dev, offset, NPCM_GPIO_REG_OEC, 1); + npcm_gpio_offset_write(dev, offset, NPCM_GPIO_REG_IEM, 1); + + return 0; +} + +static int npcm_gpio_direction_output(struct udevice *dev, unsigned int offset, + int value) +{ + npcm_gpio_offset_write(dev, offset, NPCM_GPIO_REG_IEM, 0); + npcm_gpio_offset_write(dev, offset, NPCM_GPIO_REG_OES, 1); + npcm_gpio_offset_write(dev, offset, NPCM_GPIO_REG_DOUT, value); + + return 0; +} + +static int npcm_gpio_get_value(struct udevice *dev, unsigned int offset) +{ + if (npcm_gpio_offset_read(dev, offset, NPCM_GPIO_REG_IEM)) + return npcm_gpio_offset_read(dev, offset, NPCM_GPIO_REG_DIN); + + if (npcm_gpio_offset_read(dev, offset, NPCM_GPIO_REG_OE)) + return npcm_gpio_offset_read(dev, offset, NPCM_GPIO_REG_DOUT); + + return -EINVAL; +} + +static int npcm_gpio_set_value(struct udevice *dev, unsigned int offset, + int value) +{ + npcm_gpio_offset_write(dev, offset, NPCM_GPIO_REG_DOUT, value); + + return 0; +} + +static int npcm_gpio_get_function(struct udevice *dev, unsigned int offset) +{ + if (npcm_gpio_offset_read(dev, offset, NPCM_GPIO_REG_IEM)) + return GPIOF_INPUT; + + if (npcm_gpio_offset_read(dev, offset, NPCM_GPIO_REG_OE)) + return GPIOF_OUTPUT; + + return GPIOF_FUNC; +} + +static const struct dm_gpio_ops npcm_gpio_ops = { + .direction_input = npcm_gpio_direction_input, + .direction_output = npcm_gpio_direction_output, + .get_value = npcm_gpio_get_value, + .set_value = npcm_gpio_set_value, + .get_function = npcm_gpio_get_function, +}; + +static int npcm_gpio_probe(struct udevice *dev) +{ + struct npcm_gpio_priv *priv = dev_get_priv(dev); + struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); + + priv->base = dev_read_addr_ptr(dev); + uc_priv->gpio_count = NPCM_GPIO_PORTS_PER_BANK; + uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name"); + + return 0; +} + +static const struct udevice_id npcm_gpio_match[] = { + { .compatible = "nuvoton,npcm845-gpio" }, + { .compatible = "nuvoton,npcm750-gpio" }, + { } +}; + +U_BOOT_DRIVER(npcm_gpio) = { + .name = "npcm_gpio", + .id = UCLASS_GPIO, + .of_match = npcm_gpio_match, + .probe = npcm_gpio_probe, + .priv_auto = sizeof(struct npcm_gpio_priv), + .ops = &npcm_gpio_ops, +}; -- 2.17.1