From patchwork Thu Aug 24 08:49:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oza Pawandeep X-Patchwork-Id: 9919561 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 27DCE60353 for ; Thu, 24 Aug 2017 08:50:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 17CD328B7A for ; Thu, 24 Aug 2017 08:50:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0C45528BBA; Thu, 24 Aug 2017 08:50:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5183728B7A for ; Thu, 24 Aug 2017 08:50:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; 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:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=0fbltV6xXvq3/xAypJYnHC8ipcyOUyU4Uk3ObnxUGhg=; b=BV381MQRo3Fpzs zQJ2BIJTTITNE4pTi3+QjGRDdobIN/+qWhaD4BGQZH4sutPatTrs3woHNlJ5SIjvmtvJCmtbFM6zg hfqsNIdRRJQZuALN733YnVhTrpQj6UkkY0F3SjMB7xJTlnpXJNFSRV/1dXqBzp2RsqSV/OX50vqeE b+e5XccmL+pO7041BXBPWQ1Tc1z6JDxnSCo6fjkwZdGq5VD/xh1cCBC+QqmXJQ5SQfWJ0B90LW+bM jgLHka9CIHbFuFY0TB5kR72SypGTaQ4d3VviPChqCXqFd+8+vqOpKKCaze3udqMxQfljFDwO++x75 zRrNqkDFQ1NcXG9iugRQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dknqN-0004XI-HU; Thu, 24 Aug 2017 08:50:27 +0000 Received: from mail-wm0-x230.google.com ([2a00:1450:400c:c09::230]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dknpd-0002VY-An for linux-arm-kernel@lists.infradead.org; Thu, 24 Aug 2017 08:49:44 +0000 Received: by mail-wm0-x230.google.com with SMTP id i17so5260193wmb.0 for ; Thu, 24 Aug 2017 01:49:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:subject:date:message-id:in-reply-to:references; bh=Qkqac/xSjoGzYlY8TAEOWbTQ0D2VPUtbms+TJgl1Y+Y=; b=Ppe9Hy7+LszU3d2RPg2wPi6fYoVpkF6AHDPfagQJ6GW603F6SDledvTcgxWzhFWqMZ haUYvvNPNaqFLn2Z/yHKvv9TGGIe6cn0xnH/hhnp9M1kLv01CjtbHycnshnWL/gzo+qq a0VS8YftYRItClB8lP9g1rZ/z1RpU4eFeVHww= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=Qkqac/xSjoGzYlY8TAEOWbTQ0D2VPUtbms+TJgl1Y+Y=; b=VC4T5dhla15kg95fKhGbaTGVB6sCQA2jBhEuFRku/lEUF3j7G/+kErqFRCwUVFMaWk YEA++S1RN7X1q3cjahQVrKMRCCKZH2n8IdGuqNLBeFh3yqRii/jw9PXX9rxv+oJP40xR WtraGd6i3PgaPediAFVuEUOJaALA9E0GVI49OigOXZlECOZnWOLhUH2ufm0q3HGVpE7c MSYrswlFwV7c0/PCezdZ/at/MhV6pEHvjS08uvaNPCYNcALFXT2MWyDyqwAZIDYzgo3J xBIwCuMFtHBXK94cGpgePq40/Ebihd0leVXDe/shUo2mbzTGZtdz1+Jbly7kvKyhrgET QFXQ== X-Gm-Message-State: AHYfb5g2rWiE/3rpWCpudGc8nuxe7O2VI30RxV1kDAxuPlu77+vZ6y+0 9in17hDScMG6z7mS X-Received: by 10.28.28.134 with SMTP id c128mr1055058wmc.93.1503564559326; Thu, 24 Aug 2017 01:49:19 -0700 (PDT) Received: from anjanavk-OptiPlex-7010.dhcp.avagotech.net ([192.19.237.250]) by smtp.gmail.com with ESMTPSA id g133sm6592832wmd.48.2017.08.24.01.49.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 24 Aug 2017 01:49:18 -0700 (PDT) From: Oza Pawandeep To: Bjorn Helgaas , , Rob Herring , Mark Rutland , Ray Jui , Scott Branden , Jon Mason , bcm-kernel-feedback-list@broadcom.com, Oza Pawandeep , Andy Gospodarek , linux-pci@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Oza Pawandeep Subject: [PATCH v4 1/3] PCI: iproc: implement PCI hotplug support Date: Thu, 24 Aug 2017 14:19:06 +0530 Message-Id: <1503564548-1834-2-git-send-email-oza.oza@broadcom.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1503564548-1834-1-git-send-email-oza.oza@broadcom.com> References: <1503564548-1834-1-git-send-email-oza.oza@broadcom.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170824_014941_794686_8EBC0D7B X-CRM114-Status: GOOD ( 22.76 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP This patch implements PCI hotplug support for iproc family chipsets. iproc based SOC (e.g. Stingray) does not have hotplug controller integrated. Hence, standard PCI hotplug framework hooks can-not be used. e.g. controlled power up/down of slot. The mechanism, for e.g. Stingray has adopted for PCI hotplug is as follows: PCI present lines are input to GPIOs depending on the type of connector (x2, x4, x8). GPIO array needs to be present if hotplug is supported. HW implementation is SOC/Board specific, and also it depends on how add-in card is designed (e.g. how many present pins are implemented). If x8 card is connected, then it might be possible that all the 3 present pins could go low, or at least one pin goes low. If x4 card is connected, then it might be possible that 2 present pins go low, or at least one pin goes low. The implementation essentially takes care of following: > Initializing hotplug irq thread. > Detecting the endpoint device based on link state. > Handling PERST and detecting the plugged devices. > Ordered Hot plug-out, where User is expected to write 1 to /sys/bus/pci/devices//remove > Handling spurious interrupt > Handling multiple interrupts and makes sure that card is enumerated only once. Signed-off-by: Oza Pawandeep Reviewed-by: Ray Jui diff --git a/drivers/pci/host/pcie-iproc-platform.c b/drivers/pci/host/pcie-iproc-platform.c index a5073a9..6287a43 100644 --- a/drivers/pci/host/pcie-iproc-platform.c +++ b/drivers/pci/host/pcie-iproc-platform.c @@ -92,6 +92,9 @@ static int iproc_pcie_pltfm_probe(struct platform_device *pdev) pcie->need_ob_cfg = true; } + if (of_property_read_bool(np, "slot-pluggable")) + pcie->enable_hotplug = true; + /* PHY use is optional */ pcie->phy = devm_phy_get(dev, "pcie-phy"); if (IS_ERR(pcie->phy)) { diff --git a/drivers/pci/host/pcie-iproc.c b/drivers/pci/host/pcie-iproc.c index cbdabe8..0bf71f3 100644 --- a/drivers/pci/host/pcie-iproc.c +++ b/drivers/pci/host/pcie-iproc.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "pcie-iproc.h" @@ -65,6 +66,17 @@ #define PCIE_DL_ACTIVE_SHIFT 2 #define PCIE_DL_ACTIVE BIT(PCIE_DL_ACTIVE_SHIFT) +#define CFG_RC_LTSSM 0x1cf8 +#define CFG_RC_PHY_CTL 0x1804 +#define CFG_RC_LTSSM_TIMEOUT 1000 +#define CFG_RC_LTSSM_STATE_MASK 0xff +#define CFG_RC_LTSSM_STATE_L1 0x1 + +#define CFG_RC_CLR_LTSSM_HIST_SHIFT 29 +#define CFG_RC_CLR_LTSSM_HIST_MASK BIT(CFG_RC_CLR_LTSSM_HIST_SHIFT) +#define CFG_RC_CLR_RECOV_HIST_SHIFT 31 +#define CFG_RC_CLR_RECOV_HIST_MASK BIT(CFG_RC_CLR_RECOV_HIST_SHIFT) + #define APB_ERR_EN_SHIFT 0 #define APB_ERR_EN BIT(APB_ERR_EN_SHIFT) @@ -1340,13 +1352,107 @@ static int iproc_pcie_rev_init(struct iproc_pcie *pcie) return 0; } +static bool iproc_pci_hp_check_ltssm(struct iproc_pcie *pcie) +{ + struct pci_bus *bus = pcie->root_bus; + u32 val, timeout = CFG_RC_LTSSM_TIMEOUT; + + /* Clear LTSSM history. */ + pci_bus_read_config_dword(pcie->root_bus, 0, + CFG_RC_PHY_CTL, &val); + pci_bus_write_config_dword(bus, 0, CFG_RC_PHY_CTL, + val | CFG_RC_CLR_RECOV_HIST_MASK | + CFG_RC_CLR_LTSSM_HIST_MASK); + /* write back the origional value. */ + pci_bus_write_config_dword(bus, 0, CFG_RC_PHY_CTL, val); + + do { + pci_bus_read_config_dword(pcie->root_bus, 0, + CFG_RC_LTSSM, &val); + /* check link state to see if link moved to L1 state. */ + if ((val & CFG_RC_LTSSM_STATE_MASK) == + CFG_RC_LTSSM_STATE_L1) + return true; + timeout--; + usleep_range(500, 1000); + } while (timeout); + + return false; +} + +static irqreturn_t iproc_pci_hotplug_thread(int irq, void *data) +{ + struct iproc_pcie *pcie = data; + struct pci_bus *bus = pcie->root_bus, *child; + bool link_status; + + iproc_pcie_perst_ctrl(pcie, true); + iproc_pcie_perst_ctrl(pcie, false); + + link_status = iproc_pci_hp_check_ltssm(pcie); + + if (link_status && + !iproc_pcie_check_link(pcie) && + !pcie->ep_is_present) { + pci_rescan_bus(bus); + list_for_each_entry(child, &bus->children, node) + pcie_bus_configure_settings(child); + pcie->ep_is_present = true; + dev_info(pcie->dev, + "PCI Hotplug: \n"); + } else if (link_status && pcie->ep_is_present) + /* + * ep_is_present makes sure, enumuration done only once. + * So it can handle spurious intrrupts, and also if we + * get multiple interrupts for all the implemented pins, + * we handle it only once. + */ + dev_info(pcie->dev, + "PCI Hotplug: \n"); + else { + iproc_pcie_perst_ctrl(pcie, true); + pcie->ep_is_present = false; + dev_info(pcie->dev, + "PCI Hotplug: \n"); + } + return IRQ_HANDLED; +} + +static int iproc_pci_hp_gpio_irq_get(struct iproc_pcie *pcie) +{ + struct gpio_descs *hp_gpiod; + struct device *dev = pcie->dev; + int i; + + hp_gpiod = devm_gpiod_get_array(dev, "prsnt", GPIOD_IN); + if (PTR_ERR(hp_gpiod) == -EPROBE_DEFER) + return -EPROBE_DEFER; + + if (!IS_ERR(hp_gpiod) && (hp_gpiod->ndescs > 0)) { + for (i = 0; i < hp_gpiod->ndescs; ++i) { + gpiod_direction_input(hp_gpiod->desc[i]); + if (request_threaded_irq(gpiod_to_irq + (hp_gpiod->desc[i]), + NULL, iproc_pci_hotplug_thread, + IRQF_TRIGGER_FALLING, + "PCI-hotplug", pcie)) + dev_err(dev, + "PCI hotplug prsnt: request irq failed\n"); + } + } + pcie->ep_is_present = false; + + return 0; +} + int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res) { struct device *dev; int ret; void *sysdata; - struct pci_bus *child; + struct pci_bus *bus, *child; struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie); + bool link_not_active; dev = pcie->dev; @@ -1372,6 +1478,12 @@ int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res) goto err_exit_phy; } + if (pcie->enable_hotplug) { + ret = iproc_pci_hp_gpio_irq_get(pcie); + if (ret < 0) + return ret; + } + iproc_pcie_perst_ctrl(pcie, true); iproc_pcie_perst_ctrl(pcie, false); @@ -1394,8 +1506,16 @@ int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res) sysdata = pcie; #endif - ret = iproc_pcie_check_link(pcie); - if (ret) { + link_not_active = iproc_pcie_check_link(pcie); + if (link_not_active && pcie->enable_hotplug) { + /* + * When link is not active and PCI hotplug + * is supported, do not turn off phy, let probe + * go ahead. + */ + dev_err(dev, "no PCIe EP device detected\n"); + iproc_pcie_perst_ctrl(pcie, true); + } else if (link_not_active) { dev_err(dev, "no PCIe EP device detected\n"); goto err_power_off_phy; } @@ -1406,24 +1526,34 @@ int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res) if (iproc_pcie_msi_enable(pcie)) dev_info(dev, "not using iProc MSI\n"); - list_splice_init(res, &host->windows); - host->busnr = 0; - host->dev.parent = dev; - host->ops = &iproc_pcie_ops; - host->sysdata = sysdata; - host->map_irq = pcie->map_irq; - host->swizzle_irq = pci_common_swizzle; + if (!link_not_active) { + list_splice_init(res, &host->windows); + host->busnr = 0; + host->dev.parent = dev; + host->ops = &iproc_pcie_ops; + host->sysdata = sysdata; + host->map_irq = pcie->map_irq; + host->swizzle_irq = pci_common_swizzle; + + ret = pci_scan_root_bus_bridge(host); + if (ret < 0) { + dev_err(dev, "failed to scan host: %d\n", ret); + goto err_power_off_phy; + } - ret = pci_scan_root_bus_bridge(host); - if (ret < 0) { - dev_err(dev, "failed to scan host: %d\n", ret); - goto err_power_off_phy; + pci_assign_unassigned_bus_resources(host->bus); + pcie->root_bus = host->bus; + } else { + bus = pci_create_root_bus(dev, 0, + &iproc_pcie_ops, sysdata, res); + if (!bus) { + dev_err(dev, "unable to create PCI root bus\n"); + ret = -ENOMEM; + goto err_power_off_phy; + } + pcie->root_bus = bus; } - pci_assign_unassigned_bus_resources(host->bus); - - pcie->root_bus = host->bus; - list_for_each_entry(child, &host->bus->children, node) pcie_bus_configure_settings(child); diff --git a/drivers/pci/host/pcie-iproc.h b/drivers/pci/host/pcie-iproc.h index a6b55ce..e5d0cd4 100644 --- a/drivers/pci/host/pcie-iproc.h +++ b/drivers/pci/host/pcie-iproc.h @@ -77,6 +77,10 @@ struct iproc_pcie_ib { * @ib: inbound mapping related parameters * @ib_map: outbound mapping region related parameters * + * @enable_hotplug: indicates PCI hotplug feature is enabled + * @ep_is_present: when PCIe hotplug is enabled, this flag is used to + * indicate whether or not the endpoint device is present + * * @need_msi_steer: indicates additional configuration of the iProc PCIe * controller is required to steer MSI writes to external interrupt controller * @msi: MSI data @@ -104,6 +108,9 @@ struct iproc_pcie { struct iproc_pcie_ib ib; const struct iproc_pcie_ib_map *ib_map; + bool enable_hotplug; + bool ep_is_present; + bool need_msi_steer; struct iproc_msi *msi; };