From patchwork Sun May 21 03:43:11 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryder Lee X-Patchwork-Id: 9738875 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 24BDB60328 for ; Sun, 21 May 2017 03:44:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0FA602853A for ; Sun, 21 May 2017 03:44:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 02D2828692; Sun, 21 May 2017 03:44:24 +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=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,UNPARSEABLE_RELAY 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 389A82853A for ; Sun, 21 May 2017 03:44:23 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=DdKylbqW98O4YAC9t0BuvA6SmteDhKrmIWEj34pcZVQ=; b=PA/MDdOdKVykGU GwdkBGzpIeoqj0vOCUyOeyFmt64rXERunLzRrFA+0wOWyEfIGG+8yvEGEyA59KZnwdEv4tyA7Yy86 ffBR2uaCb2IjcuvOodDJ29ZR1EMJ8LPd9d2BgHm/uH1K+vB6dnUDnAVik3kYEavEYIQA+4hJJm+o4 ZKoW94x1sjeGPJOF8lsszEUcSnGa5gmjoMV7EEHKIQp2IHtQlgXFgrRdYhrpAnsM2kelaADB6UiuM OYCG2pcxcILqDvmJ5z4OxbenYa/8CCXCf1TXjdG27N5A0C3bSPBW/HtUd2Um694T/V9xGepiE9ENY YGODNeT1bwYc9E2jm0ug==; 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 1dCHn4-0005eU-Ap; Sun, 21 May 2017 03:44:22 +0000 Received: from [210.61.82.184] (helo=mailgw02.mediatek.com) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dCHmz-0005E2-Tn; Sun, 21 May 2017 03:44:20 +0000 Received: from mtkhts07.mediatek.inc [(172.21.101.69)] by mailgw02.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 1302674898; Sun, 21 May 2017 11:43:51 +0800 Received: from mtkslt306.mediatek.inc (10.21.14.136) by mtkhts07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 14.3.266.1; Sun, 21 May 2017 11:43:49 +0800 From: Ryder Lee To: Kishon Vijay Abraham I , Rob Herring Subject: [PATCH v2 1/2] phy: add PCIe PHY driver for mt7623 SoCs families Date: Sun, 21 May 2017 11:43:11 +0800 Message-ID: <1495338192-9423-2-git-send-email-ryder.lee@mediatek.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1495338192-9423-1-git-send-email-ryder.lee@mediatek.com> References: <1495338192-9423-1-git-send-email-ryder.lee@mediatek.com> MIME-Version: 1.0 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170520_204418_316345_95F1F2AD X-CRM114-Status: GOOD ( 22.86 ) 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: , Cc: devicetree@vger.kernel.org, Ryder Lee , linux-mediatek@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org 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 support PCIe PHY of MT7623 SoCs families Signed-off-by: Ryder Lee --- drivers/phy/Kconfig | 8 ++ drivers/phy/Makefile | 1 + drivers/phy/phy-mt7623-pcie.c | 290 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 299 insertions(+) create mode 100644 drivers/phy/phy-mt7623-pcie.c diff --git a/drivers/phy/Kconfig b/drivers/phy/Kconfig index afaf7b6..94b3e61 100644 --- a/drivers/phy/Kconfig +++ b/drivers/phy/Kconfig @@ -530,4 +530,12 @@ config PHY_MESON8B_USB2 and GXBB SoCs. If unsure, say N. +config PHY_MT7623_PCIE + tristate "Mediatek PCIe PHY driver for MT7623 SoC families" + depends on ARCH_MEDIATEK && OF + select GENERIC_PHY + select MFD_SYSCON + help + Say 'Y' here to add support for Mediatek PCIe PHY driver which + can be found on the MT7623 SoC families. endmenu diff --git a/drivers/phy/Makefile b/drivers/phy/Makefile index f8047b4..0185f6a 100644 --- a/drivers/phy/Makefile +++ b/drivers/phy/Makefile @@ -64,3 +64,4 @@ obj-$(CONFIG_PHY_CYGNUS_PCIE) += phy-bcm-cygnus-pcie.o obj-$(CONFIG_ARCH_TEGRA) += tegra/ obj-$(CONFIG_PHY_NS2_PCIE) += phy-bcm-ns2-pcie.o obj-$(CONFIG_PHY_MESON8B_USB2) += phy-meson8b-usb2.o +obj-$(CONFIG_PHY_MT7623_PCIE) += phy-mt7623-pcie.o diff --git a/drivers/phy/phy-mt7623-pcie.c b/drivers/phy/phy-mt7623-pcie.c new file mode 100644 index 0000000..9c32601 --- /dev/null +++ b/drivers/phy/phy-mt7623-pcie.c @@ -0,0 +1,290 @@ +/* + * Copyright (c) 2017 MediaTek Inc. + * Author: Ryder Lee + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Offsets of sub-segment in each port registers */ +#define PCIE_SIFSLV_PHYD_BANK2_BASE 0xa00 +#define SSUSB_SIFSLV_PHYA_BASE 0xb00 +#define SSUSB_SIFSLV_PHYA_DA_BASE 0xc00 + +/* + * RX detection stable - 1 scale represent 8 reference cycles + * cover reference clock from 1M~100MHz, 7us~40us + */ +#define B2_PHYD_RXDET1 (PCIE_SIFSLV_PHYD_BANK2_BASE + 0x28) +#define RG_SSUSB_RXDET_STB2 GENMASK(17, 9) +#define RG_SSUSB_RXDET_STB2_VAL(x) ((0x1ff & (x)) << 9) + +#define B2_PHYD_RXDET2 (PCIE_SIFSLV_PHYD_BANK2_BASE + 0x2c) +#define RG_SSUSB_RXDET_STB2_P3 GENMASK(8, 0) +#define RG_SSUSB_RXDET_STB2_P3_VAL(x) (0x1ff & (x)) + +#define U3_PHYA_REG0 (SSUSB_SIFSLV_PHYA_BASE + 0x00) +#define RG_PCIE_CLKDRV_OFFSET GENMASK(3, 1) +#define RG_PCIE_CLKDRV_OFFSET_VAL(x) ((0x3 & (x)) << 2) + +#define U3_PHYA_REG1 (SSUSB_SIFSLV_PHYA_BASE + 0x04) +#define RG_PCIE_CLKDRV_AMP GENMASK(31, 29) +#define RG_PCIE_CLKDRV_AMP_VAL(x) ((0x7 & (x)) << 29) + +#define DA_SSUSB_CDR_REFCK_SEL (SSUSB_SIFSLV_PHYA_DA_BASE + 0x00) +#define RG_SSUSB_XTAL_EXT_PE1H GENMASK(13, 12) +#define RG_SSUSB_XTAL_EXT_PE1H_VAL(x) ((0x3 & (x)) << 12) +#define RG_SSUSB_XTAL_EXT_PE2H GENMASK(17, 16) +#define RG_SSUSB_XTAL_EXT_PE2H_VAL(x) ((0x3 & (x)) << 16) + +#define DA_SSUSB_PLL_IC (SSUSB_SIFSLV_PHYA_DA_BASE + 0x0c) +#define RG_SSUSB_PLL_IC_PE2H GENMASK(15, 12) +#define RG_SSUSB_PLL_IC_PE2H_VAL(x) ((0xf & (x)) << 12) +#define RG_SSUSB_PLL_BR_PE2H GENMASK(29, 28) +#define RG_SSUSB_PLL_BR_PE2H_VAL(x) ((0x3 & (x)) << 28) + +#define DA_SSUSB_PLL_BC (SSUSB_SIFSLV_PHYA_DA_BASE + 0x08) +#define RG_SSUSB_PLL_DIVEN_PE2H GENMASK(21, 19) +#define RG_SSUSB_PLL_BC_PE2H GENMASK(7, 6) +#define RG_SSUSB_PLL_BC_PE2H_VAL(x) ((0x3 & (x)) << 6) + +#define DA_SSUSB_PLL_IR (SSUSB_SIFSLV_PHYA_DA_BASE + 0x10) +#define RG_SSUSB_PLL_IR_PE2H GENMASK(19, 16) +#define RG_SSUSB_PLL_IR_PE2H_VAL(x) ((0xf & (x)) << 16) + +#define DA_SSUSB_PLL_BP (SSUSB_SIFSLV_PHYA_DA_BASE + 0x14) +#define RG_SSUSB_PLL_BP_PE2H GENMASK(19, 16) +#define RG_SSUSB_PLL_BP_PE2H_VAL(x) ((0xf & (x)) << 16) + +#define DA_SSUSB_PLL_SSC_DELTA1_REG20 (SSUSB_SIFSLV_PHYA_DA_BASE + 0x3c) +#define RG_SSUSB_PLL_SSC_DELTA1_PE2H GENMASK(31, 16) +#define RG_SSUSB_PLL_SSC_DELTA1_PE2H_VAL(x) ((0xffff & (x)) << 16) + +#define DA_SSUSB_PLL_SSC_DELTA_REG25 (SSUSB_SIFSLV_PHYA_DA_BASE + 0x48) +#define RG_SSUSB_PLL_SSC_DELTA_PE2H GENMASK(15, 0) +#define RG_SSUSB_PLL_SSC_DELTA_PE2H_VAL(x) (0xffff & (x)) + +#define HIF_SYSCFG1 0x14 +#define HIF_SYSCFG1_PHY2_MASK (0x3 << 20) + +struct mtk_pcie_phy { + struct device *dev; + void __iomem *base; + struct regmap *hif; + struct clk *phya_ref; + struct phy *phy; +}; + +static inline u32 phy_read(struct mtk_pcie_phy *phy, u32 reg) +{ + return readl(phy->base + reg); +} + +static inline void phy_write(struct mtk_pcie_phy *phy, u32 val, u32 reg) +{ + writel(val, phy->base + reg); +} + +static int mtk_pcie_phy_power_on(struct phy *phy) +{ + struct mtk_pcie_phy *mtk_phy = phy_get_drvdata(phy); + int err; + u32 val; + + /* enable PCIe mode */ + if (mtk_phy->hif) + regmap_update_bits(mtk_phy->hif, HIF_SYSCFG1, + HIF_SYSCFG1_PHY2_MASK, 0); + + err = clk_prepare_enable(mtk_phy->phya_ref); + if (err) { + dev_err(mtk_phy->dev, "failed to enable PCIe phy clock\n"); + return err; + } + + val = phy_read(mtk_phy, DA_SSUSB_CDR_REFCK_SEL); + val &= ~(RG_SSUSB_XTAL_EXT_PE1H | RG_SSUSB_XTAL_EXT_PE2H); + val |= RG_SSUSB_XTAL_EXT_PE1H_VAL(0x2) | + RG_SSUSB_XTAL_EXT_PE2H_VAL(0x2); + phy_write(mtk_phy, val, DA_SSUSB_CDR_REFCK_SEL); + + /* ref clk drive */ + val = phy_read(mtk_phy, U3_PHYA_REG1); + val &= ~RG_PCIE_CLKDRV_AMP; + val |= RG_PCIE_CLKDRV_AMP_VAL(0x4); + phy_write(mtk_phy, val, U3_PHYA_REG1); + + val = phy_read(mtk_phy, U3_PHYA_REG0); + val &= ~RG_PCIE_CLKDRV_OFFSET; + val |= RG_PCIE_CLKDRV_OFFSET_VAL(0x1); + phy_write(mtk_phy, val, U3_PHYA_REG0); + + /* SSC delta -5000ppm */ + val = phy_read(mtk_phy, DA_SSUSB_PLL_SSC_DELTA1_REG20); + val &= ~RG_SSUSB_PLL_SSC_DELTA1_PE2H; + val |= RG_SSUSB_PLL_SSC_DELTA1_PE2H_VAL(0x3c); + phy_write(mtk_phy, val, DA_SSUSB_PLL_SSC_DELTA1_REG20); + + val = phy_read(mtk_phy, DA_SSUSB_PLL_SSC_DELTA_REG25); + val &= ~RG_SSUSB_PLL_SSC_DELTA_PE2H; + val |= RG_SSUSB_PLL_SSC_DELTA_PE2H_VAL(0x36); + phy_write(mtk_phy, val, DA_SSUSB_PLL_SSC_DELTA_REG25); + + /* change pll BW 0.6M */ + val = phy_read(mtk_phy, DA_SSUSB_PLL_IC); + val &= ~RG_SSUSB_PLL_BR_PE2H; + val |= RG_SSUSB_PLL_BR_PE2H_VAL(0x1); + phy_write(mtk_phy, val, DA_SSUSB_PLL_IC); + + val = phy_read(mtk_phy, DA_SSUSB_PLL_BC); + val &= ~(RG_SSUSB_PLL_DIVEN_PE2H | RG_SSUSB_PLL_BC_PE2H); + val |= RG_SSUSB_PLL_BC_PE2H_VAL(0x3); + phy_write(mtk_phy, val, DA_SSUSB_PLL_BC); + + val = phy_read(mtk_phy, DA_SSUSB_PLL_IR); + val &= ~RG_SSUSB_PLL_IR_PE2H; + val |= RG_SSUSB_PLL_IR_PE2H_VAL(0x2); + phy_write(mtk_phy, val, DA_SSUSB_PLL_IR); + + val = phy_read(mtk_phy, DA_SSUSB_PLL_IC); + val &= ~RG_SSUSB_PLL_IC_PE2H; + val |= RG_SSUSB_PLL_IC_PE2H_VAL(0x1); + phy_write(mtk_phy, val, DA_SSUSB_PLL_IC); + + val = phy_read(mtk_phy, DA_SSUSB_PLL_BP); + val &= ~RG_SSUSB_PLL_BP_PE2H; + val |= RG_SSUSB_PLL_BP_PE2H_VAL(0xa); + phy_write(mtk_phy, val, DA_SSUSB_PLL_BP); + + /* Tx Detect Rx Timing: 10us -> 5us */ + val = phy_read(mtk_phy, B2_PHYD_RXDET1); + val &= ~RG_SSUSB_RXDET_STB2; + val |= RG_SSUSB_RXDET_STB2_VAL(0x10); + phy_write(mtk_phy, val, B2_PHYD_RXDET1); + + val = phy_read(mtk_phy, B2_PHYD_RXDET2); + val &= ~RG_SSUSB_RXDET_STB2_P3; + val |= RG_SSUSB_RXDET_STB2_P3_VAL(0x10); + phy_write(mtk_phy, val, B2_PHYD_RXDET2); + + /* wait for PCIe subsys register to active */ + usleep_range(2500, 3000); + + return 0; +} + +static int mtk_pcie_phy_power_off(struct phy *phy) +{ + struct mtk_pcie_phy *mtk_phy = phy_get_drvdata(phy); + + clk_disable_unprepare(mtk_phy->phya_ref); + + return 0; +} + +static struct phy_ops mtk_pcie_phy_ops = { + .power_on = mtk_pcie_phy_power_on, + .power_off = mtk_pcie_phy_power_off, + .owner = THIS_MODULE, +}; + +static const struct of_device_id mtk_pcie_phy_of_match[]; + +static int mtk_pcie_phy_probe(struct platform_device *pdev) +{ + struct device_node *np = pdev->dev.of_node; + const struct of_device_id *match; + struct phy_provider *phy_provider; + struct mtk_pcie_phy *mtk_phy; + struct resource *res; + struct phy *phy; + + match = of_match_device(mtk_pcie_phy_of_match, &pdev->dev); + if (!match) + return -ENODEV; + + mtk_phy = devm_kzalloc(&pdev->dev, sizeof(*mtk_phy), GFP_KERNEL); + if (!mtk_phy) + return -ENOMEM; + + mtk_phy->dev = &pdev->dev; + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + mtk_phy->base = devm_ioremap_resource(&pdev->dev, res); + if (IS_ERR(mtk_phy->base)) { + dev_err(&pdev->dev, "failed to get phy base\n"); + return PTR_ERR(mtk_phy->base); + } + + mtk_phy->phya_ref = devm_clk_get(&pdev->dev, "pciephya_ref"); + if (IS_ERR(mtk_phy->phya_ref)) { + dev_err(&pdev->dev, "error to get pciephya_ref\n"); + return PTR_ERR(mtk_phy->phya_ref); + } + + if (of_find_property(np, "mediatek,phy-switch", NULL)) { + mtk_phy->hif = syscon_regmap_lookup_by_phandle( + np, "mediatek,phy-switch"); + if (IS_ERR(mtk_phy->hif)) { + dev_err(&pdev->dev, "missing \"mediatek,phy-switch\" phandle\n"); + return PTR_ERR(mtk_phy->hif); + } + } + + platform_set_drvdata(pdev, mtk_phy); + phy = devm_phy_create(&pdev->dev, NULL, &mtk_pcie_phy_ops); + if (IS_ERR(phy)) { + dev_err(&pdev->dev, "failed to create phy device\n"); + return PTR_ERR(phy); + } + + mtk_phy->phy = phy; + phy_set_drvdata(phy, mtk_phy); + + phy_provider = devm_of_phy_provider_register(&pdev->dev, + of_phy_simple_xlate); + if (IS_ERR(phy_provider)) { + dev_err(&pdev->dev, "failed to register phy provider\n"); + return PTR_ERR(phy_provider); + } + + return 0; +} + +static const struct of_device_id mtk_pcie_phy_of_match[] = { + { .compatible = "mediatek,mt7623-pcie-phy"}, + { .compatible = "mediatek,mt2701-pcie-phy"}, + {}, +}; +MODULE_DEVICE_TABLE(of, mtk_pcie_phy_of_match); + +static struct platform_driver mtk_pcie_phy_driver = { + .probe = mtk_pcie_phy_probe, + .driver = { + .name = "mtk-pcie-phy", + .of_match_table = mtk_pcie_phy_of_match, + } +}; +module_platform_driver(mtk_pcie_phy_driver); + +MODULE_AUTHOR("Ryder Lee "); +MODULE_DESCRIPTION("Mediatek PCIe PHY driver"); +MODULE_LICENSE("GPL v2");