From patchwork Wed Jun 19 05:46:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shan-Chun Hung X-Patchwork-Id: 13703416 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A1439C27C79 for ; Wed, 19 Jun 2024 05:47:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=kwhrrlH8XQCrhM9AqgjRF8LDCPYMq6uTj0z2U7cKwhA=; b=gLFXxKlud5YG6/bH0Ijc+nF86J 78XjtzljDOCqaFnhhE5n0ZRfbZ1NURT46kQ3uXbNh+dSGiWkRyX0oe6PlnrtJjUgY2d6jeYVvuk8b jTn+4YWSNqCKzlzw6rxoUyzVKCD/4/x60N8Hq6Mo9JHggtirmjDglsApsbEanZuU4tqGesOiypq7W PJzWjQe47DN/O0e51/hYG8IwTeialH3H8fsEHx1sRLbdexcUmWxPoqcpJ5V43KOVWhQNhSHh+yfU7 j3ZKnl203Ax05uSwubzq4o6u5OrXj9uSJhZRV5G+5t7OS2ZT8Bp+5UTRrlDg9R1QRrhHPlOAd3cN6 b9hfwKcg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sJoAO-0000000HYO2-1dVK; Wed, 19 Jun 2024 05:47:32 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sJoAG-0000000HYJn-2DlF for linux-arm-kernel@lists.infradead.org; Wed, 19 Jun 2024 05:47:25 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1f7274a453bso53826445ad.2 for ; Tue, 18 Jun 2024 22:47:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718776044; x=1719380844; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kwhrrlH8XQCrhM9AqgjRF8LDCPYMq6uTj0z2U7cKwhA=; b=gsWHAYuJTzhdftH+rwewKjPAJ5HO+Vi6bIbFOtjqIcLG/IvUfnUN8CsfhNQL7mL8KW MfvG2wl11x39gHOk75yYS1kwbQHP5dCzi4q7KxfX+ps3d8wSll0yZs8T3W5ktNtE7MyA i2gOe2CEybmvWh/cnpgMNGmzxxlpcHED8DI/cO0/lbkNbsaS3ciLGQLYblgiU5b/4e2D sfLKsSvn58ufpm4gpMQF9tDVvMjVtN0AqwECFcb1zbP1v093UIaa7tTagdtjxS7CQyyJ xLK5mC3NrUA4OE/xaS+ut64b3PVvUkPq0uDZC3EQY07PsD0Pu/9YsryeuXWNMvhBiZwm QAag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718776044; x=1719380844; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kwhrrlH8XQCrhM9AqgjRF8LDCPYMq6uTj0z2U7cKwhA=; b=MjZpRqsqwoKdD/Ymb71M4uI9Chz2QYWny/BKcgHMtAHxPr1NNhCAZAKUW3gbSGfJ9R XNS3WXUxlBEVV3qGT0F2dhoIuYbyXzoF3HiMq5aQXLLJJCR1jl2m2Oi8Tjx/5aMhaL9H nZQwMPWJlt2MfLodbDt+m3/iJ+7EGUh1NwvSDzSdJM8CkiEmoRM+t66h4t2jj5jyawA6 g5BNcT/YpO7ID4V4TCQFs0uJAO38I0rME2szUPK6Uk1xn4QUejeMW2bWy94mJalhejek Dg2scIOScP+0YvsAvFOFofWDM2Vc85YMdBJdmGJDaUXk6tjcI0DVlwHmYpCDdkd8RhuX laHQ== X-Forwarded-Encrypted: i=1; AJvYcCWYLP0+5rHu9FEZrImxxpS+U2VpSuZGZCjRolLH0utVxsYoTQ5Iodg94iFcKS+AhyrVeNJ1Ho14upnMokyI/uze0zvsZ1Lgtap+PURHEottEz7+FFA= X-Gm-Message-State: AOJu0Yw6Av1A9CNvu++/NcRd/JxPEu8LtVkDOlpNBlWBkqKy80/zZEky NVWuMoK5oqMlAwTe0zK5x+o8ftnSc6YUAyIGbKMHbQpjVnWWd2ht X-Google-Smtp-Source: AGHT+IGvAA+Cpd4oRIWGwaxBBPwcEZw+AodXI77z+3rAmXJWdZbtpV0WL0NfYV82Vs72JkvYODLNjw== X-Received: by 2002:a17:902:c945:b0:1f4:5278:5c19 with SMTP id d9443c01a7336-1f9aa4740femr15531445ad.49.1718776043615; Tue, 18 Jun 2024 22:47:23 -0700 (PDT) Received: from localhost.localdomain (60-250-192-107.hinet-ip.hinet.net. [60.250.192.107]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f598bfsm107487865ad.300.2024.06.18.22.47.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jun 2024 22:47:23 -0700 (PDT) From: Shan-Chun Hung To: ulf.hansson@linaro.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, adrian.hunter@intel.com, p.zabel@pengutronix.de, pbrobinson@gmail.com, serghox@gmail.com, mcgrof@kernel.org, prabhakar.mahadev-lad.rj@bp.renesas.com, forbidden405@outlook.com, tmaimon77@gmail.com, andy.shevchenko@gmail.com, linux-arm-kernel@lists.infradead.org, linux-mmc@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ychuang3@nuvoton.com, schung@nuvoton.com, Shan-Chun Hung Subject: [PATCH 1/2] dt-bindings: mmc: nuvoton,ma35d1-sdhci: Document MA35D1 SDHCI controller Date: Wed, 19 Jun 2024 13:46:40 +0800 Message-Id: <20240619054641.277062-2-shanchun1218@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240619054641.277062-1-shanchun1218@gmail.com> References: <20240619054641.277062-1-shanchun1218@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240618_224724_600553_D6938FB0 X-CRM114-Status: GOOD ( 13.25 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add binding for Nuvoton MA35D1 SDHCI controller. Signed-off-by: schung --- .../bindings/mmc/nuvoton,ma35d1-sdhci.yaml | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 Documentation/devicetree/bindings/mmc/nuvoton,ma35d1-sdhci.yaml -- 2.25.1 diff --git a/Documentation/devicetree/bindings/mmc/nuvoton,ma35d1-sdhci.yaml b/Documentation/devicetree/bindings/mmc/nuvoton,ma35d1-sdhci.yaml new file mode 100644 index 000000000000..173449360dea --- /dev/null +++ b/Documentation/devicetree/bindings/mmc/nuvoton,ma35d1-sdhci.yaml @@ -0,0 +1,106 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/mmc/nuvoton,ma35d1-sdhci.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Nuvoton MA35D1 SD/SDIO/MMC Controller + +maintainers: + - Shan-Chun Hung + +description: | + This controller on Nuvoton MA35 family SoCs provides an interface for MMC, SD, and + SDIO types of memory cards. + +properties: + compatible: + oneOf: + - enum: + - nuvoton,ma35d1-sdhci + reg: + maxItems: 1 + description: The SDHCI registers + + interrupts: + maxItems: 1 + + pinctrl-names: + description: + Should at least contain default and state_uhs. + minItems: 1 + items: + - const: default + - const: state_uhs + + pinctrl-0: + description: + Should contain default/high speed pin ctrl. + maxItems: 1 + + pinctrl-1: + description: + Should contain uhs mode pin ctrl. + maxItems: 1 + + clocks: + minItems: 1 + description: The SDHCI bus clock + + resets: + maxItems: 1 + description: + Phandle and reset specifier pair to softreset line of sdhci. + + nuvoton,sys: + $ref: /schemas/types.yaml#/definitions/phandle + description: + Phandle to the syscon that configure sdhci votage stable + +required: + - compatible + - reg + - interrupts + - clocks + - pinctrl-names + - pinctrl-0 + +unevaluatedProperties: false + +examples: + - | + #include + #include + #include + + soc { + #address-cells = <2>; + #size-cells = <2>; + sdhci0: sdhci@40180000 { + compatible = "nuvoton,ma35d1-sdhci"; + reg = <0x0 0x40180000 0x0 0x2000>; + interrupts = ; + clocks = <&clk SDH0_GATE>; + pinctrl-names = "default"; + pinctrl-0 = <&pinctrl_sdhci0>; + bus-width = <4>; + max-frequency = <100000000>; + no-1-8-v; + status = "disabled"; + }; + + sdhci1: sdhci@40190000 { + compatible = "nuvoton,ma35d1-sdhci"; + reg = <0x0 0x40190000 0x0 0x2000>; + interrupts = ; + clocks = <&clk SDH1_GATE>; + pinctrl-names = "default", "state_uhs"; + pinctrl-0 = <&pinctrl_sdhci1>; + pinctrl-1 = <&pinctrl_sdhci1_uhs>; + resets = <&sys MA35D1_RESET_SDH1>; + nuvoton,sys = <&sys>; + bus-width = <8>; + max-frequency = <200000000>; + status = "disabled"; + }; + }; From patchwork Wed Jun 19 05:46:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shan-Chun Hung X-Patchwork-Id: 13703415 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0CB0FC27C53 for ; Wed, 19 Jun 2024 05:47:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=YaqFTHj+TNawNCMcQbpSWxNdJ7LtFLM11UUPVkjfoaE=; b=vnsHTYTMmyZW3z1w0YiR00qO4w JRWf7NHqPPv6iBra7rzIuT4mfJ41WvMaeiAYm5AriHDlqta9dpu4UpbLfi8PvYgkmaUhhSCsTCyT3 XTp9o9sCUSMjdF2FjuuspUNjEHkRzoIsgmCMyLcrSAW0oWpvcGtGXhFdVgLP2jLckVf1iz/UvKhEl 6Z8uu29y6LUmKip9Q2xKLEuuFxj4lXu0LB23Ocw/fNjjleDcS96AMewkQO4rUQcFqAf9OF7QPoGoq /8CvczxzmiE31P2fK2LNCuIEfNJHLaQegNM21Bn90gMe8jcU2C0UGjeR/ekZZPT3Q784i1rJhTJUQ 96/DJ/CA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sJoAS-0000000HYPL-117s; Wed, 19 Jun 2024 05:47:36 +0000 Received: from mail-pf1-x429.google.com ([2607:f8b0:4864:20::429]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sJoAK-0000000HYLK-1l18 for linux-arm-kernel@lists.infradead.org; Wed, 19 Jun 2024 05:47:30 +0000 Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-705e9e193caso3126490b3a.2 for ; Tue, 18 Jun 2024 22:47:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718776047; x=1719380847; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YaqFTHj+TNawNCMcQbpSWxNdJ7LtFLM11UUPVkjfoaE=; b=BfdxNYo7JeUXsb1+nZPeHI7Zy3qhDhY7eUGS515fS0oatL9CNEAjBBGsBgfrWLn3u9 1TNCN6F/w6CngYDNLGcwjWujIXMx6/xukxZ76a8nU6JHQLPUnhBlN2aBeaNGbfjpGr6M vsyEBLa5ZkBTr9jXzhqJA7F8DgzxRd8xOymr8R1/1kaInDGDOf5C3Ja+3MRr1VPnvgoi w40X/GE6FmPFDYURcZu5IxC57Enuv8I9WwMshmEQF8e946tbQilXNuTzKrgGs+H3EBv7 UGBemZ4knjpZr790F8liPYt55SCxWuyi9uU6f/hj3sHc/FnJsRTM4bWwIkKb9tKFUmDS HaXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718776047; x=1719380847; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YaqFTHj+TNawNCMcQbpSWxNdJ7LtFLM11UUPVkjfoaE=; b=f8XGSKc1+CN6c4cNlU0KDJUw/hwmog7gcOufCBPnJoXiPM0ifjUy4fha3iCajkjeNS l0yPizSBUdvi9fwmC11YeVh8wEQeSuX3qSnUL7e+yKjwokA1v6Y2sVJ1JmhzQB1xdl23 sS4oweofVvQVTMKPs/bWIVqCRjtKHUDdaeX60khA8Jq5l1gxLLJSBxGGM6CAFQ+XBcZg W0+yRa3koTZ/9jOODxs3jhvDl7DenEApc0UVhP9VKMAtErthh0+/4WAe8ztCCgnaiPrw 0y7DRFYZnALmSor5i7nwI62+JdQxNpHl2FLA/l/6DL2ohXT/TpDquf6VlEfsCYyHZaZ7 CgFA== X-Forwarded-Encrypted: i=1; AJvYcCXxgq9+ba2ZZj+H65SL9MrOHv7n9FUpvF7L4Pu/LAu5RCWW1qRK14OTaCDlg1iUo2PLPXOiyqykw10JH0nBhrFizpUL3VmbWpov0qbx/xYc7yV+ny8= X-Gm-Message-State: AOJu0YyiHN7pbJ2+WOaK7uzhiMg5V11HDLfohcumHq+4GW7ELzsUS57N Stdv3j5NYFcqobnweYHZ3cjwnPet3Y2YwwF4/LI70MGQhWD3928Y X-Google-Smtp-Source: AGHT+IGfw8H9n9oYYnaXQbM7kC8bFdTUXDXgHrTKPzZTqrYob3kC+2upHCXWabvlgTBh873xdoqlvA== X-Received: by 2002:a05:6a20:3f8f:b0:1b8:622a:cf76 with SMTP id adf61e73a8af0-1bcbb65421bmr1487477637.48.1718776047318; Tue, 18 Jun 2024 22:47:27 -0700 (PDT) Received: from localhost.localdomain (60-250-192-107.hinet-ip.hinet.net. [60.250.192.107]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f598bfsm107487865ad.300.2024.06.18.22.47.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jun 2024 22:47:26 -0700 (PDT) From: Shan-Chun Hung To: ulf.hansson@linaro.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, adrian.hunter@intel.com, p.zabel@pengutronix.de, pbrobinson@gmail.com, serghox@gmail.com, mcgrof@kernel.org, prabhakar.mahadev-lad.rj@bp.renesas.com, forbidden405@outlook.com, tmaimon77@gmail.com, andy.shevchenko@gmail.com, linux-arm-kernel@lists.infradead.org, linux-mmc@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ychuang3@nuvoton.com, schung@nuvoton.com, Shan-Chun Hung Subject: [PATCH 2/2] mmc: sdhci-of-ma35d1: Add Novoton MA35D1 SDHCI driver Date: Wed, 19 Jun 2024 13:46:41 +0800 Message-Id: <20240619054641.277062-3-shanchun1218@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240619054641.277062-1-shanchun1218@gmail.com> References: <20240619054641.277062-1-shanchun1218@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240618_224729_102880_B130F38B X-CRM114-Status: GOOD ( 27.11 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This adds the SDHCI driver for the MA35 series SoC. It is based upon the SDHCI interface, but requires some extra initialization. Signed-off-by: schung --- drivers/mmc/host/Kconfig | 13 ++ drivers/mmc/host/Makefile | 1 + drivers/mmc/host/sdhci-of-ma35d1.c | 297 +++++++++++++++++++++++++++++ 3 files changed, 311 insertions(+) create mode 100644 drivers/mmc/host/sdhci-of-ma35d1.c -- 2.25.1 diff --git a/drivers/mmc/host/Kconfig b/drivers/mmc/host/Kconfig index bb0d4fb0892a..e993901ebedd 100644 --- a/drivers/mmc/host/Kconfig +++ b/drivers/mmc/host/Kconfig @@ -252,6 +252,19 @@ config MMC_SDHCI_OF_SPARX5 If unsure, say N. +config MMC_SDHCI_OF_MA35D1 + tristate "SDHCI OF support for the MA35D1 SDHCI controller" + depends on ARCH_A35 || COMPILE_TEST + depends on MMC_SDHCI_PLTFM + depends on OF && COMMON_CLK + help + This selects the MA35D1 Secure Digital Host Controller Interface. + + If you have a controller with this interface, say Y or M here. You + also need to enable an appropriate bus interface. + + If unsure, say N. + config MMC_SDHCI_CADENCE tristate "SDHCI support for the Cadence SD/SDIO/eMMC controller" depends on MMC_SDHCI_PLTFM diff --git a/drivers/mmc/host/Makefile b/drivers/mmc/host/Makefile index f53f86d200ac..3ccffebbe59b 100644 --- a/drivers/mmc/host/Makefile +++ b/drivers/mmc/host/Makefile @@ -88,6 +88,7 @@ obj-$(CONFIG_MMC_SDHCI_OF_ESDHC) += sdhci-of-esdhc.o obj-$(CONFIG_MMC_SDHCI_OF_HLWD) += sdhci-of-hlwd.o obj-$(CONFIG_MMC_SDHCI_OF_DWCMSHC) += sdhci-of-dwcmshc.o obj-$(CONFIG_MMC_SDHCI_OF_SPARX5) += sdhci-of-sparx5.o +obj-$(CONFIG_MMC_SDHCI_OF_MA35D1) += sdhci-of-ma35d1.o obj-$(CONFIG_MMC_SDHCI_BCM_KONA) += sdhci-bcm-kona.o obj-$(CONFIG_MMC_SDHCI_IPROC) += sdhci-iproc.o obj-$(CONFIG_MMC_SDHCI_NPCM) += sdhci-npcm.o diff --git a/drivers/mmc/host/sdhci-of-ma35d1.c b/drivers/mmc/host/sdhci-of-ma35d1.c new file mode 100644 index 000000000000..7714a5ab463d --- /dev/null +++ b/drivers/mmc/host/sdhci-of-ma35d1.c @@ -0,0 +1,297 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Nuvoton Technology Corp. + * + * Author: Shan-Chun Hung + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "sdhci-pltfm.h" + +#define MA35_SYS_MISCFCR0 0x070 +#define MA35_SDHCI_MSHCCTL 0x508 +#define MA35_SDHCI_MBIUCTL 0x510 + +#define MA35_SDHCI_CMD_CONFLICT_CHK BIT(0) +#define MA35_SDHCI_INCR_MSK GENMASK(3, 0) +#define MA35_SDHCI_INCR16 BIT(3) +#define MA35_SDHCI_INCR8 BIT(2) + +#define BOUNDARY_OK(addr, len) \ + ((addr | (SZ_128M - 1)) == ((addr + len - 1) | (SZ_128M - 1))) + +struct ma35_priv { + struct regmap *regmap; + struct reset_control *rst; + struct pinctrl *pinctrl; + struct pinctrl_state *pins_uhs; + struct pinctrl_state *pins_default; +}; + +struct ma35_restore_data { + u32 reg; + u32 width; +}; + +static const struct ma35_restore_data restore_data[] = { + { SDHCI_CLOCK_CONTROL, 32}, + { SDHCI_BLOCK_SIZE, 32}, + { SDHCI_INT_ENABLE, 32}, + { SDHCI_SIGNAL_ENABLE, 32}, + { SDHCI_AUTO_CMD_STATUS, 32}, + { SDHCI_HOST_CONTROL, 32}, + { SDHCI_TIMEOUT_CONTROL, 8}, + { MA35_SDHCI_MSHCCTL, 32}, + { MA35_SDHCI_MBIUCTL, 32}, +}; + +/* + * If DMA addr spans 128MB boundary, we split the DMA transfer into two + * so that each DMA transfer doesn't exceed the boundary. + */ +static void ma35_adma_write_desc(struct sdhci_host *host, void **desc, + dma_addr_t addr, int len, unsigned int cmd) +{ + int tmplen, offset; + + if (likely(!len || BOUNDARY_OK(addr, len))) { + sdhci_adma_write_desc(host, desc, addr, len, cmd); + return; + } + + offset = addr & (SZ_128M - 1); + tmplen = SZ_128M - offset; + sdhci_adma_write_desc(host, desc, addr, tmplen, cmd); + + addr += tmplen; + len -= tmplen; + sdhci_adma_write_desc(host, desc, addr, len, cmd); +} + +static void ma35_set_clock(struct sdhci_host *host, unsigned int clock) +{ + u32 ctl; + + /* If the clock frequency exceeds MMC_HIGH_52_MAX_DTR, + * disable command conflict check. + */ + ctl = sdhci_readw(host, MA35_SDHCI_MSHCCTL); + if (clock > MMC_HIGH_52_MAX_DTR) + ctl &= ~MA35_SDHCI_CMD_CONFLICT_CHK; + else + ctl |= MA35_SDHCI_CMD_CONFLICT_CHK; + sdhci_writew(host, ctl, MA35_SDHCI_MSHCCTL); + + sdhci_set_clock(host, clock); +} + +static int ma35_start_signal_voltage_switch(struct mmc_host *mmc, + struct mmc_ios *ios) +{ + struct sdhci_host *host = mmc_priv(mmc); + struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); + struct ma35_priv *priv = sdhci_pltfm_priv(pltfm_host); + + switch (ios->signal_voltage) { + case MMC_SIGNAL_VOLTAGE_180: + if (!IS_ERR(priv->pinctrl) && !IS_ERR(priv->pins_uhs)) + pinctrl_select_state(priv->pinctrl, priv->pins_uhs); + break; + case MMC_SIGNAL_VOLTAGE_330: + if (!IS_ERR(priv->pinctrl) && !IS_ERR(priv->pins_default)) + pinctrl_select_state(priv->pinctrl, priv->pins_default); + break; + default: + dev_err(mmc_dev(host->mmc), "Unsupported signal voltage!\n"); + return -EINVAL; + } + + return sdhci_start_signal_voltage_switch(mmc, ios); +} + +static void ma35_voltage_switch(struct sdhci_host *host) +{ + /* Wait for 5ms after set 1.8V signal enable bit */ + usleep_range(5000, 5500); +} + +static int ma35_execute_tuning(struct mmc_host *mmc, u32 opcode) +{ + struct sdhci_host *host = mmc_priv(mmc); + struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); + struct ma35_priv *priv = sdhci_pltfm_priv(pltfm_host); + + /* Limitations require a reset SD/eMMC before tuning. */ + if (!IS_ERR(priv->rst)) { + int idx; + u32 *val; + + val = kmalloc(ARRAY_SIZE(restore_data), GFP_KERNEL); + for (idx = 0; idx < ARRAY_SIZE(restore_data); idx++) { + if (restore_data[idx].width == 32) + val[idx] = sdhci_readl(host, restore_data[idx].reg); + else if (restore_data[idx].width == 8) + val[idx] = sdhci_readb(host, restore_data[idx].reg); + } + + reset_control_assert(priv->rst); + reset_control_deassert(priv->rst); + + for (idx = 0; idx < ARRAY_SIZE(restore_data); idx++) { + if (restore_data[idx].width == 32) + sdhci_writel(host, val[idx], restore_data[idx].reg); + else if (restore_data[idx].width == 8) + sdhci_writeb(host, val[idx], restore_data[idx].reg); + } + + kfree(val); + } + + return sdhci_execute_tuning(mmc, opcode); +} + +static const struct sdhci_ops sdhci_ma35_ops = { + .set_clock = ma35_set_clock, + .set_bus_width = sdhci_set_bus_width, + .set_uhs_signaling = sdhci_set_uhs_signaling, + .get_max_clock = sdhci_pltfm_clk_get_max_clock, + .reset = sdhci_reset, + .adma_write_desc = ma35_adma_write_desc, + .voltage_switch = ma35_voltage_switch, +}; + +static const struct sdhci_pltfm_data sdhci_ma35_pdata = { + .ops = &sdhci_ma35_ops, + .quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN, + .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | SDHCI_QUIRK2_BROKEN_DDR50 | + SDHCI_QUIRK2_ACMD23_BROKEN, +}; + +static int ma35_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct sdhci_pltfm_host *pltfm_host; + struct sdhci_host *host; + struct ma35_priv *priv; + int err; + u32 extra, ctl; + + host = sdhci_pltfm_init(pdev, &sdhci_ma35_pdata, + sizeof(struct ma35_priv)); + if (IS_ERR(host)) + return PTR_ERR(host); + + /* + * extra adma table cnt for cross 128M boundary handling. + */ + extra = DIV_ROUND_UP_ULL(dma_get_required_mask(&pdev->dev), SZ_128M); + if (extra > SDHCI_MAX_SEGS) + extra = SDHCI_MAX_SEGS; + host->adma_table_cnt += extra; + pltfm_host = sdhci_priv(host); + priv = sdhci_pltfm_priv(pltfm_host); + + if (dev->of_node) { + pltfm_host->clk = devm_clk_get(&pdev->dev, NULL); + if (IS_ERR(pltfm_host->clk)) { + err = PTR_ERR(pltfm_host->clk); + dev_err(&pdev->dev, "failed to get clk: %d\n", err); + goto free_pltfm; + } + err = clk_prepare_enable(pltfm_host->clk); + if (err) + goto free_pltfm; + } + + err = mmc_of_parse(host->mmc); + if (err) + goto err_clk; + + priv->rst = devm_reset_control_get(&pdev->dev, NULL); + + sdhci_get_of_property(pdev); + + priv->pinctrl = devm_pinctrl_get(&pdev->dev); + if (!IS_ERR(priv->pinctrl)) { + priv->pins_default = pinctrl_lookup_state(priv->pinctrl, "default"); + priv->pins_uhs = pinctrl_lookup_state(priv->pinctrl, "state_uhs"); + pinctrl_select_state(priv->pinctrl, priv->pins_default); + } + + if (!(host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)) { + u32 reg; + + priv->regmap = syscon_regmap_lookup_by_phandle( + pdev->dev.of_node, "nuvoton,sys"); + + if (!IS_ERR(priv->regmap)) { + /* Enable SDHCI voltage stable for 1.8V */ + regmap_read(priv->regmap, MA35_SYS_MISCFCR0, ®); + reg |= BIT(17); + regmap_write(priv->regmap, MA35_SYS_MISCFCR0, reg); + } + + host->mmc_host_ops.start_signal_voltage_switch = + ma35_start_signal_voltage_switch; + } + + host->mmc_host_ops.execute_tuning = ma35_execute_tuning; + + err = sdhci_add_host(host); + if (err) + goto err_clk; + + /* Enable INCR16 and INCR8 */ + ctl = sdhci_readw(host, MA35_SDHCI_MBIUCTL); + ctl &= ~MA35_SDHCI_INCR_MSK; + ctl |= MA35_SDHCI_INCR16|MA35_SDHCI_INCR8; + sdhci_writew(host, ctl, MA35_SDHCI_MBIUCTL); + + return 0; + +err_clk: + clk_disable_unprepare(pltfm_host->clk); + +free_pltfm: + sdhci_pltfm_free(pdev); + return err; +} + +static int ma35_remove(struct platform_device *pdev) +{ + struct sdhci_host *host = platform_get_drvdata(pdev); + struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); + + sdhci_remove_host(host, 0); + clk_disable_unprepare(pltfm_host->clk); + sdhci_pltfm_free(pdev); + + return 0; +} + +static const struct of_device_id sdhci_ma35_dt_ids[] = { + { .compatible = "nuvoton,ma35d1-sdhci" }, + {} +}; + +static struct platform_driver sdhci_ma35_driver = { + .driver = { + .name = "sdhci-ma35", + .of_match_table = sdhci_ma35_dt_ids, + }, + .probe = ma35_probe, + .remove = ma35_remove, +}; +module_platform_driver(sdhci_ma35_driver); + +MODULE_DESCRIPTION("SDHCI platform driver for Nuvoton ma35d1"); +MODULE_AUTHOR("shanchun1218@google.com"); +MODULE_LICENSE("GPL v2");