Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp4012253ybg; Fri, 25 Oct 2019 12:05:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqwiEZE8qsvrW99G+CZKlDDJDWy/52NeUqLSQtdfbsNZ1VfDIbRw6j+dzhPthi1Y5bixqKTv X-Received: by 2002:a17:906:474b:: with SMTP id j11mr5015987ejs.325.1572030302571; Fri, 25 Oct 2019 12:05:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572030302; cv=none; d=google.com; s=arc-20160816; b=Qj68Di0lf/b18eRiOEmEcQGwy5URWAlegO6hcCJO8aGpCQjE3qYuZyYv911yew+k6/ ITXs3ctA+IuZutCi1h5eSCVv6hf87cc6xHaSyO1WVrBv4SKxSrHPe0l7TrIDdsP2EW/+ Y7hhkkAEOFJvxpR0tULxno0xxTAl3oN8bl6PeMiloeRTPw5BhYhEhl2TJ7iFLv63PkvO b7dKXmRvDXouqFm6BReeFFxdMPn7F7Lb7Skvzf+V/nhyB3RWJ2OHOq4fBMTr4Cw9OmdH PAI8g1yMRLh+b/sfUBFTYjsk7aVcqUCSxCVo8MaV+QpGbb1ZUyP2IYxi5R+VgusUomNO o1Cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=C3M8+LHd/TwZW1ATfg+ZMsypot9ZMhn+VzFnJCneSGk=; b=Kond2rCgT+aANaCQN0IkD6SruVHEfneJssKPpg7pPnYoTQC6/Jzk4nA58lEZyBPX1e pQIUmq6T0GEJOJeM1MswSt7enEXq+ceMZbvyO+oYWEU9X4JeV0cBesBgGKTj+WATAyBi Zk+UaOrHbDiQry4ebGteSJUCy/FzCks1T+J8wn22sQXD7HyNkXMFCdpxh7CexlF83QEO 5uBMbkf1bl1JLWNIdWdfh7M/DvCsYXMTVjO7+xjB92WOvcR17YG9ylPDEfhMVbSC9pnq 5Z/D0d+Si+dEQpkiCrVWyMgNFfNjVVTlbzQTrbgfFuL4tQBOBJgOrokjTr13XBy+OOKF wNSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alliedtelesis.co.nz header.s=mail181024 header.b=YGKdFPhj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alliedtelesis.co.nz Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j10si1640826ejv.339.2019.10.25.12.04.38; Fri, 25 Oct 2019 12:05:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@alliedtelesis.co.nz header.s=mail181024 header.b=YGKdFPhj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alliedtelesis.co.nz Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727643AbfJXU1V (ORCPT + 99 others); Thu, 24 Oct 2019 16:27:21 -0400 Received: from gate2.alliedtelesis.co.nz ([202.36.163.20]:59811 "EHLO gate2.alliedtelesis.co.nz" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727561AbfJXU1O (ORCPT ); Thu, 24 Oct 2019 16:27:14 -0400 Received: from mmarshal3.atlnz.lc (mmarshal3.atlnz.lc [10.32.18.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by gate2.alliedtelesis.co.nz (Postfix) with ESMTPS id C0952891AB; Fri, 25 Oct 2019 09:27:09 +1300 (NZDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alliedtelesis.co.nz; s=mail181024; t=1571948829; bh=C3M8+LHd/TwZW1ATfg+ZMsypot9ZMhn+VzFnJCneSGk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=YGKdFPhj0VI3k//IWGSwhNGcMAd6EbCR/Zlz2Aou8MJohx+iTiX/xyZ+lJeDNFQcc skNCCzajriuNaNZyJS97YjmXAzllW+FlI1cX/AYrjeYGqXeAzTwOv3Mg4bFEbFmKj/ xOGDE5OL1I6qOPLilV8MDd7hnLEFGy+X8AJfYI1p652Q7bORkDkML6L1lrgilKRJ+p CJ0CskrRxgCpE2Y/Xw0WmlfVmRMi9vdOUwPWbV0G/HIfOXItWwAWSh0KzPjnuqk4gE YSbmdd8e0lCy73dsYPbu1Q9RovcSE6AXqCrSEgE5k5PkMH8Ud73Ru/IOTKvIf5nHWW f8K4jShS1FVVg== Received: from smtp (Not Verified[10.32.16.33]) by mmarshal3.atlnz.lc with Trustwave SEG (v7,5,8,10121) id ; Fri, 25 Oct 2019 09:27:08 +1300 Received: from chrisp-dl.ws.atlnz.lc (chrisp-dl.ws.atlnz.lc [10.33.22.20]) by smtp (Postfix) with ESMTP id 5EFC313EF97; Fri, 25 Oct 2019 09:27:10 +1300 (NZDT) Received: by chrisp-dl.ws.atlnz.lc (Postfix, from userid 1030) id 296FF28005C; Fri, 25 Oct 2019 09:27:06 +1300 (NZDT) From: Chris Packham To: linus.walleij@linaro.org, bgolaszewski@baylibre.com, robh+dt@kernel.org, mark.rutland@arm.com, rjui@broadcom.com, sbranden@broadcom.com, bcm-kernel-feedback-list@broadcom.com Cc: linux-gpio@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Chris Packham Subject: [PATCH v4 2/2] gpio: Add xgs-iproc driver Date: Fri, 25 Oct 2019 09:27:03 +1300 Message-Id: <20191024202703.8017-3-chris.packham@alliedtelesis.co.nz> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191024202703.8017-1-chris.packham@alliedtelesis.co.nz> References: <20191024202703.8017-1-chris.packham@alliedtelesis.co.nz> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable x-atlnz-ls: pat Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This driver supports the Chip Common A GPIO controller present on a number of Broadcom switch ASICs with integrated SoCs. The controller is similar to the pinctrl-nsp-gpio and pinctrl-iproc-gpio blocks but different enough that a separate driver is required. This has been ported from Broadcom's XLDK 5.0.3 retaining only the CCA support (pinctrl-iproc-gpio covers CCB). Signed-off-by: Chris Packham --- Notes: Changes in v4: - rename the config option to GPIO_BCM_XGS_IPROC and place alphabetic= ally - sort #includes alphabetically =20 Changes in v3: - prefix local #defines with 'IPROC' - use {readl,writel}_relaxed - remove unnecessary headers - actually use spinlock to guard hardware accesses =20 Changes in v2: - use more of the generic infrastructure for gpio chips - handling the root interrupt is still done manually due to sharing w= ith uart0. drivers/gpio/Kconfig | 9 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-xgs-iproc.c | 321 ++++++++++++++++++++++++++++++++++ 3 files changed, 331 insertions(+) create mode 100644 drivers/gpio/gpio-xgs-iproc.c diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 38e096e6925f..04396787fbb0 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -147,6 +147,15 @@ config GPIO_BCM_KONA help Turn on GPIO support for Broadcom "Kona" chips. =20 +config GPIO_BCM_XGS_IPROC + tristate "BRCM XGS iProc GPIO support" + depends on OF_GPIO && (ARCH_BCM_IPROC || COMPILE_TEST) + select GPIO_GENERIC + select GPIOLIB_IRQCHIP + default ARCH_BCM_IPROC + help + Say yes here to enable GPIO support for Broadcom XGS iProc SoCs. + config GPIO_BRCMSTB tristate "BRCMSTB GPIO support" default y if (ARCH_BRCMSTB || BMIPS_GENERIC) diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index d2fd19c15bae..8725d158a964 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -34,6 +34,7 @@ obj-$(CONFIG_GPIO_ARIZONA) +=3D gpio-arizona.o obj-$(CONFIG_GPIO_ASPEED) +=3D gpio-aspeed.o obj-$(CONFIG_GPIO_ATH79) +=3D gpio-ath79.o obj-$(CONFIG_GPIO_BCM_KONA) +=3D gpio-bcm-kona.o +obj-$(CONFIG_GPIO_BCM_XGS_IPROC) +=3D gpio-xgs-iproc.o obj-$(CONFIG_GPIO_BD70528) +=3D gpio-bd70528.o obj-$(CONFIG_GPIO_BD9571MWV) +=3D gpio-bd9571mwv.o obj-$(CONFIG_GPIO_BRCMSTB) +=3D gpio-brcmstb.o diff --git a/drivers/gpio/gpio-xgs-iproc.c b/drivers/gpio/gpio-xgs-iproc.= c new file mode 100644 index 000000000000..a3fdd95cc9e6 --- /dev/null +++ b/drivers/gpio/gpio-xgs-iproc.c @@ -0,0 +1,321 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2017 Broadcom + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define IPROC_CCA_INT_F_GPIOINT BIT(0) +#define IPROC_CCA_INT_STS 0x20 +#define IPROC_CCA_INT_MASK 0x24 + +#define IPROC_GPIO_CCA_DIN 0x0 +#define IPROC_GPIO_CCA_DOUT 0x4 +#define IPROC_GPIO_CCA_OUT_EN 0x8 +#define IPROC_GPIO_CCA_INT_LEVEL 0x10 +#define IPROC_GPIO_CCA_INT_LEVEL_MASK 0x14 +#define IPROC_GPIO_CCA_INT_EVENT 0x18 +#define IPROC_GPIO_CCA_INT_EVENT_MASK 0x1C +#define IPROC_GPIO_CCA_INT_EDGE 0x24 + +struct iproc_gpio_chip { + struct irq_chip irqchip; + struct gpio_chip gc; + spinlock_t lock; + struct device *dev; + void __iomem *base; + void __iomem *intr; +}; + +static inline struct iproc_gpio_chip * +to_iproc_gpio(struct gpio_chip *gc) +{ + return container_of(gc, struct iproc_gpio_chip, gc); +} + +static void iproc_gpio_irq_ack(struct irq_data *d) +{ + struct gpio_chip *gc =3D irq_data_get_irq_chip_data(d); + struct iproc_gpio_chip *chip =3D to_iproc_gpio(gc); + int pin =3D d->hwirq; + unsigned long flags; + u32 irq =3D d->irq; + u32 irq_type, event_status =3D 0; + + spin_lock_irqsave(&chip->lock, flags); + irq_type =3D irq_get_trigger_type(irq); + if (irq_type & IRQ_TYPE_EDGE_BOTH) { + event_status |=3D BIT(pin); + writel_relaxed(event_status, + chip->base + IPROC_GPIO_CCA_INT_EVENT); + } + spin_unlock_irqrestore(&chip->lock, flags); +} + +static void iproc_gpio_irq_unmask(struct irq_data *d) +{ + struct gpio_chip *gc =3D irq_data_get_irq_chip_data(d); + struct iproc_gpio_chip *chip =3D to_iproc_gpio(gc); + int pin =3D d->hwirq; + unsigned long flags; + u32 irq =3D d->irq; + u32 int_mask, irq_type, event_mask; + + spin_lock_irqsave(&chip->lock, flags); + irq_type =3D irq_get_trigger_type(irq); + event_mask =3D readl_relaxed(chip->base + IPROC_GPIO_CCA_INT_EVENT_MASK= ); + int_mask =3D readl_relaxed(chip->base + IPROC_GPIO_CCA_INT_LEVEL_MASK); + + if (irq_type & IRQ_TYPE_EDGE_BOTH) { + event_mask |=3D 1 << pin; + writel_relaxed(event_mask, + chip->base + IPROC_GPIO_CCA_INT_EVENT_MASK); + } else { + int_mask |=3D 1 << pin; + writel_relaxed(int_mask, + chip->base + IPROC_GPIO_CCA_INT_LEVEL_MASK); + } + spin_unlock_irqrestore(&chip->lock, flags); +} + +static void iproc_gpio_irq_mask(struct irq_data *d) +{ + struct gpio_chip *gc =3D irq_data_get_irq_chip_data(d); + struct iproc_gpio_chip *chip =3D to_iproc_gpio(gc); + int pin =3D d->hwirq; + unsigned long flags; + u32 irq =3D d->irq; + u32 irq_type, int_mask, event_mask; + + spin_lock_irqsave(&chip->lock, flags); + irq_type =3D irq_get_trigger_type(irq); + event_mask =3D readl_relaxed(chip->base + IPROC_GPIO_CCA_INT_EVENT_MASK= ); + int_mask =3D readl_relaxed(chip->base + IPROC_GPIO_CCA_INT_LEVEL_MASK); + + if (irq_type & IRQ_TYPE_EDGE_BOTH) { + event_mask &=3D ~BIT(pin); + writel_relaxed(event_mask, + chip->base + IPROC_GPIO_CCA_INT_EVENT_MASK); + } else { + int_mask &=3D ~BIT(pin); + writel_relaxed(int_mask, + chip->base + IPROC_GPIO_CCA_INT_LEVEL_MASK); + } + spin_unlock_irqrestore(&chip->lock, flags); +} + +static int iproc_gpio_irq_set_type(struct irq_data *d, u32 type) +{ + struct gpio_chip *gc =3D irq_data_get_irq_chip_data(d); + struct iproc_gpio_chip *chip =3D to_iproc_gpio(gc); + int pin =3D d->hwirq; + unsigned long flags; + u32 irq =3D d->irq; + u32 event_pol, int_pol; + int ret =3D 0; + + spin_lock_irqsave(&chip->lock, flags); + switch (type & IRQ_TYPE_SENSE_MASK) { + case IRQ_TYPE_EDGE_RISING: + event_pol =3D readl_relaxed(chip->base + IPROC_GPIO_CCA_INT_EDGE); + event_pol &=3D ~BIT(pin); + writel_relaxed(event_pol, chip->base + IPROC_GPIO_CCA_INT_EDGE); + break; + case IRQ_TYPE_EDGE_FALLING: + event_pol =3D readl_relaxed(chip->base + IPROC_GPIO_CCA_INT_EDGE); + event_pol |=3D BIT(pin); + writel_relaxed(event_pol, chip->base + IPROC_GPIO_CCA_INT_EDGE); + break; + case IRQ_TYPE_LEVEL_HIGH: + int_pol =3D readl_relaxed(chip->base + IPROC_GPIO_CCA_INT_LEVEL); + int_pol &=3D ~BIT(pin); + writel_relaxed(int_pol, chip->base + IPROC_GPIO_CCA_INT_LEVEL); + break; + case IRQ_TYPE_LEVEL_LOW: + int_pol =3D readl_relaxed(chip->base + IPROC_GPIO_CCA_INT_LEVEL); + int_pol |=3D BIT(pin); + writel_relaxed(int_pol, chip->base + IPROC_GPIO_CCA_INT_LEVEL); + break; + default: + /* should not come here */ + ret =3D -EINVAL; + goto out_unlock; + } + + if (type & IRQ_TYPE_LEVEL_MASK) + irq_set_handler_locked(irq_get_irq_data(irq), handle_level_irq); + else if (type & IRQ_TYPE_EDGE_BOTH) + irq_set_handler_locked(irq_get_irq_data(irq), handle_edge_irq); + +out_unlock: + spin_unlock_irqrestore(&chip->lock, flags); + + return ret; +} + +static irqreturn_t iproc_gpio_irq_handler(int irq, void *data) +{ + struct gpio_chip *gc =3D (struct gpio_chip *)data; + struct iproc_gpio_chip *chip =3D to_iproc_gpio(gc); + int bit; + unsigned long int_bits =3D 0; + u32 int_status; + + /* go through the entire GPIOs and handle all interrupts */ + int_status =3D readl_relaxed(chip->intr + IPROC_CCA_INT_STS); + if (int_status & IPROC_CCA_INT_F_GPIOINT) { + u32 event, level; + + /* Get level and edge interrupts */ + event =3D + readl_relaxed(chip->base + IPROC_GPIO_CCA_INT_EVENT_MASK); + event &=3D readl_relaxed(chip->base + IPROC_GPIO_CCA_INT_EVENT); + level =3D readl_relaxed(chip->base + IPROC_GPIO_CCA_DIN); + level ^=3D readl_relaxed(chip->base + IPROC_GPIO_CCA_INT_LEVEL); + level &=3D + readl_relaxed(chip->base + IPROC_GPIO_CCA_INT_LEVEL_MASK); + int_bits =3D level | event; + + for_each_set_bit(bit, &int_bits, gc->ngpio) + generic_handle_irq(irq_linear_revmap(gc->irq.domain, bit)); + } + + return int_bits ? IRQ_HANDLED : IRQ_NONE; +} + +static int iproc_gpio_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct device_node *dn =3D pdev->dev.of_node; + struct iproc_gpio_chip *chip; + u32 num_gpios; + int irq, ret; + + chip =3D devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); + if (!chip) + return -ENOMEM; + + chip->dev =3D dev; + platform_set_drvdata(pdev, chip); + spin_lock_init(&chip->lock); + + chip->base =3D devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(chip->base)) + return PTR_ERR(chip->base); + + ret =3D bgpio_init(&chip->gc, dev, 4, + chip->base + IPROC_GPIO_CCA_DIN, + chip->base + IPROC_GPIO_CCA_DOUT, + NULL, + chip->base + IPROC_GPIO_CCA_OUT_EN, + NULL, + 0); + if (ret) { + dev_err(dev, "unable to init GPIO chip\n"); + return ret; + } + + chip->gc.label =3D dev_name(dev); + if (of_property_read_u32(dn, "ngpios", &num_gpios)) + chip->gc.ngpio =3D num_gpios; + + irq =3D platform_get_irq(pdev, 0); + if (irq > 0) { + struct gpio_irq_chip *girq; + struct irq_chip *irqc; + u32 val; + + irqc =3D &chip->irqchip; + irqc->name =3D dev_name(dev); + irqc->irq_ack =3D iproc_gpio_irq_ack; + irqc->irq_mask =3D iproc_gpio_irq_mask; + irqc->irq_unmask =3D iproc_gpio_irq_unmask; + irqc->irq_set_type =3D iproc_gpio_irq_set_type; + + chip->intr =3D devm_platform_ioremap_resource(pdev, 1); + if (IS_ERR(chip->intr)) + return PTR_ERR(chip->intr); + + /* Enable GPIO interrupts for CCA GPIO */ + val =3D readl_relaxed(chip->intr + IPROC_CCA_INT_MASK); + val |=3D IPROC_CCA_INT_F_GPIOINT; + writel_relaxed(val, chip->intr + IPROC_CCA_INT_MASK); + + /* + * Directly request the irq here instead of passing + * a flow-handler to gpiochip_set_chained_irqchip, + * because the irq is shared. + */ + ret =3D devm_request_irq(dev, irq, iproc_gpio_irq_handler, + IRQF_SHARED, chip->gc.label, &chip->gc); + if (ret) { + dev_err(dev, "Fail to request IRQ%d: %d\n", irq, ret); + return ret; + } + + girq =3D &chip->gc.irq; + girq->chip =3D irqc; + /* This will let us handle the parent IRQ in the driver */ + girq->parent_handler =3D NULL; + girq->num_parents =3D 0; + girq->parents =3D NULL; + girq->default_type =3D IRQ_TYPE_NONE; + girq->handler =3D handle_simple_irq; + } + + ret =3D devm_gpiochip_add_data(dev, &chip->gc, chip); + if (ret) { + dev_err(dev, "unable to add GPIO chip\n"); + return ret; + } + + return 0; +} + +static int __exit iproc_gpio_remove(struct platform_device *pdev) +{ + struct iproc_gpio_chip *chip; + + chip =3D platform_get_drvdata(pdev); + if (!chip) + return -ENODEV; + + if (chip->intr) { + u32 val; + + val =3D readl_relaxed(chip->intr + IPROC_CCA_INT_MASK); + val &=3D ~IPROC_CCA_INT_F_GPIOINT; + writel_relaxed(val, chip->intr + IPROC_CCA_INT_MASK); + } + + return 0; +} + +static const struct of_device_id bcm_iproc_gpio_of_match[] __initconst =3D= { + { .compatible =3D "brcm,iproc-gpio-cca" }, + {} +}; +MODULE_DEVICE_TABLE(of, bcm_iproc_gpio_of_match); + +static struct platform_driver bcm_iproc_gpio_driver =3D { + .driver =3D { + .name =3D "iproc-xgs-gpio", + .owner =3D THIS_MODULE, + .of_match_table =3D bcm_iproc_gpio_of_match, + }, + .probe =3D iproc_gpio_probe, + .remove =3D iproc_gpio_remove, +}; + +module_platform_driver(bcm_iproc_gpio_driver); + +MODULE_DESCRIPTION("XGS IPROC GPIO driver"); +MODULE_LICENSE("GPL v2"); --=20 2.23.0