Message ID | 20221205085351.27566-3-tmaimon77@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2140001wrr; Mon, 5 Dec 2022 01:04:29 -0800 (PST) X-Google-Smtp-Source: AA0mqf4lBcUSCAo3nBuvL3u24YW14EYDq7/RnWm4Cvrd4IUsUBfE6Kmy5xqCSBlKqUK9ACLroRV4 X-Received: by 2002:a17:906:660d:b0:7ba:5085:864 with SMTP id b13-20020a170906660d00b007ba50850864mr45480393ejp.357.1670231068824; Mon, 05 Dec 2022 01:04:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670231068; cv=none; d=google.com; s=arc-20160816; b=o07yYb/oLtT7CMLwl/iL/Z9M+9FrtwM8V0umxAxPSdH2GM6YUBd0x/a3P4tr3yUiZn NFMpGuRhDF8SqruJok8yE07eVCCoGSV/FijlAfXNxC/rbjLVZfehHCdqI2Jb2VPJx3eg tZZ5Gg8ylyei5bAHf4MI4fbLF9E4TETRNvB5ptvmw/Vuippfy/Jz2dfdVJ+e3TLxTCFR c3Xheb/G5EnbDqal2x+pyVVxWsoiCLs2YRhAjTyvHFmkTfJarINz5Gvr/9JIVpvNutux GhzH6Org71P8IdTKyPkm0ty52esxXDOaCMzNTcC2y1SNXLc/AdNdATQ3v9AsjhCXPpak Wj2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ES8KQ8xrF4wsiCaZbXK1hceH5iqoOcxXGT1MBi8AFYY=; b=EiT9ujp4Eof4Kz/Ukw/J/XHCw7WjsNOZh6IdbqUIjua6CrbKlgy0IJh1rOHItxAySK uf7HZsApMzekZrxowM3DT5NKDVjPfz1keazK/Djy9Zxc9u7UDR8uXDH4UrwD1O5MTulg 7wA+mH3NctJdNqdel5vHAwSbTJmxUvanjRkBBz7Kh/k7uC34FORf5h9yw+0ep7ZEbCpg qNrogFaCgde+I+7jIe35o73e/woHt0GXEZg/8gdPJpGHXtbrRgr0UwhSjKl7pKeUWqJW gKdDEPRuWD9qRfPs+5Yy6t8wAHXsw9B5AN3JidtePCdtFUDvcIC2nT9lIm9vJv4sYdJw YKKA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id rh16-20020a17090720f000b007ae4ed48293si8949836ejb.281.2022.12.05.01.04.05; Mon, 05 Dec 2022 01:04:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232430AbiLEIz3 (ORCPT <rfc822;jaysivo@gmail.com> + 99 others); Mon, 5 Dec 2022 03:55:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232385AbiLEIyn (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 5 Dec 2022 03:54:43 -0500 Received: from maillog.nuvoton.com (maillog.nuvoton.com [202.39.227.15]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C3A92BBF; Mon, 5 Dec 2022 00:54:01 -0800 (PST) Received: from NTHCCAS01.nuvoton.com (NTHCCAS01.nuvoton.com [10.1.8.28]) by maillog.nuvoton.com (Postfix) with ESMTP id 32ECE1C81291; Mon, 5 Dec 2022 16:54:00 +0800 (CST) Received: from NTHCCAS03.nuvoton.com (10.1.20.28) by NTHCCAS01.nuvoton.com (10.1.8.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.7; Mon, 5 Dec 2022 16:54:00 +0800 Received: from NTHCCAS01.nuvoton.com (10.1.8.28) by NTHCCAS03.nuvoton.com (10.1.20.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1847.3; Mon, 5 Dec 2022 16:53:59 +0800 Received: from taln60.nuvoton.co.il (10.191.1.180) by NTHCCAS01.nuvoton.com (10.1.12.25) with Microsoft SMTP Server id 15.1.2375.7 via Frontend Transport; Mon, 5 Dec 2022 16:53:59 +0800 Received: by taln60.nuvoton.co.il (Postfix, from userid 10070) id 9A08563A6B; Mon, 5 Dec 2022 10:53:58 +0200 (IST) From: Tomer Maimon <tmaimon77@gmail.com> To: <ulf.hansson@linaro.org>, <avifishman70@gmail.com>, <tali.perry1@gmail.com>, <joel@jms.id.au>, <venture@google.com>, <yuenn@google.com>, <benjaminfair@google.com>, <adrian.hunter@intel.com>, <skhan@linuxfoundation.org>, <davidgow@google.com>, <pbrobinson@gmail.com>, <gsomlo@gmail.com>, <briannorris@chromium.org>, <arnd@arndb.de>, <krakoczy@antmicro.com>, <andy.shevchenko@gmail.com> CC: <openbmc@lists.ozlabs.org>, <linux-mmc@vger.kernel.org>, <devicetree@vger.kernel.org>, <linux-kernel@vger.kernel.org>, Tomer Maimon <tmaimon77@gmail.com> Subject: [PATCH v2 2/2] mmc: sdhci-npcm: Add NPCM SDHCI driver Date: Mon, 5 Dec 2022 10:53:51 +0200 Message-ID: <20221205085351.27566-3-tmaimon77@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20221205085351.27566-1-tmaimon77@gmail.com> References: <20221205085351.27566-1-tmaimon77@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=0.4 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,NML_ADSP_CUSTOM_MED,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751364213288095968?= X-GMAIL-MSGID: =?utf-8?q?1751364213288095968?= |
Series |
MMC: add NPCM SDHCI driver support
|
|
Commit Message
Tomer Maimon
Dec. 5, 2022, 8:53 a.m. UTC
Add Nuvoton NPCM BMC sdhci-pltfm controller driver.
Signed-off-by: Tomer Maimon <tmaimon77@gmail.com>
---
drivers/mmc/host/Kconfig | 8 ++++
drivers/mmc/host/Makefile | 1 +
drivers/mmc/host/sdhci-npcm.c | 84 +++++++++++++++++++++++++++++++++++
3 files changed, 93 insertions(+)
create mode 100644 drivers/mmc/host/sdhci-npcm.c
Comments
On Mon, Dec 5, 2022 at 10:54 AM Tomer Maimon <tmaimon77@gmail.com> wrote: > > Add Nuvoton NPCM BMC sdhci-pltfm controller driver. Thank you for an update, my comments below. ... > +config MMC_SDHCI_NPCM > config MMC_SDHCI_IPROC Perhaps after IPROC? ... > @@ -97,6 +97,7 @@ obj-$(CONFIG_MMC_SDHCI_MICROCHIP_PIC32) += sdhci-pic32.o > obj-$(CONFIG_MMC_SDHCI_BRCMSTB) += sdhci-brcmstb.o > obj-$(CONFIG_MMC_SDHCI_OMAP) += sdhci-omap.o > obj-$(CONFIG_MMC_SDHCI_SPRD) += sdhci-sprd.o > +obj-$(CONFIG_MMC_SDHCI_NPCM) += sdhci-npcm.o Perhaps after IPROC? (There is a group of platform drivers slightly below than here) > obj-$(CONFIG_MMC_CQHCI) += cqhci.o ... > +#include <linux/clk.h> > +#include <linux/err.h> > +#include <linux/io.h> > +#include <linux/mmc/host.h> > +#include <linux/mmc/mmc.h> > +#include <linux/module.h> I guess platform_device.h is missing here. ... > +static int npcm_sdhci_probe(struct platform_device *pdev) > +{ > + struct sdhci_pltfm_host *pltfm_host; > + struct sdhci_host *host; > + u32 caps; > + int ret; > + > + host = sdhci_pltfm_init(pdev, &npcm_sdhci_pdata, 0); > + if (IS_ERR(host)) > + return PTR_ERR(host); > + > + pltfm_host = sdhci_priv(host); > + pltfm_host->clk = devm_clk_get_optional(&pdev->dev, NULL); You can't mix devm with non-devm in this way. > + if (IS_ERR(pltfm_host->clk)) > + return PTR_ERR(pltfm_host->clk); > + > + ret = clk_prepare_enable(pltfm_host->clk); > + if (ret) > + return ret; > + > + caps = sdhci_readl(host, SDHCI_CAPABILITIES); > + if (caps & SDHCI_CAN_DO_8BIT) > + host->mmc->caps |= MMC_CAP_8_BIT_DATA; > + > + ret = mmc_of_parse(host->mmc); > + if (ret) > + goto err_sdhci_add; > + > + ret = sdhci_add_host(host); > + if (ret) > + goto err_sdhci_add; Why can't you use sdhci_pltfm_register()? > + return 0; > + > +err_sdhci_add: > + clk_disable_unprepare(pltfm_host->clk); > + sdhci_pltfm_free(pdev); > + return ret; > +} Missing ->remove() due to above. Have you tried to compile as a module and then remove and insert it several times?
Hi Andy, Thanks for your comments. On Mon, 5 Dec 2022 at 12:54, Andy Shevchenko <andy.shevchenko@gmail.com> wrote: > > On Mon, Dec 5, 2022 at 10:54 AM Tomer Maimon <tmaimon77@gmail.com> wrote: > > > > Add Nuvoton NPCM BMC sdhci-pltfm controller driver. > > Thank you for an update, my comments below. > > ... > > > +config MMC_SDHCI_NPCM > > > config MMC_SDHCI_IPROC > > Perhaps after IPROC? Will be done in the next version. > > ... > > > @@ -97,6 +97,7 @@ obj-$(CONFIG_MMC_SDHCI_MICROCHIP_PIC32) += sdhci-pic32.o > > obj-$(CONFIG_MMC_SDHCI_BRCMSTB) += sdhci-brcmstb.o > > obj-$(CONFIG_MMC_SDHCI_OMAP) += sdhci-omap.o > > obj-$(CONFIG_MMC_SDHCI_SPRD) += sdhci-sprd.o > > +obj-$(CONFIG_MMC_SDHCI_NPCM) += sdhci-npcm.o > > Perhaps after IPROC? (There is a group of platform drivers slightly > below than here) Will be done in the next version. > > > obj-$(CONFIG_MMC_CQHCI) += cqhci.o > > ... > > > +#include <linux/clk.h> > > +#include <linux/err.h> > > +#include <linux/io.h> > > +#include <linux/mmc/host.h> > > +#include <linux/mmc/mmc.h> > > +#include <linux/module.h> > > I guess platform_device.h is missing here. Build and work without platform_device.h, do I need it for module use? > > ... > > > +static int npcm_sdhci_probe(struct platform_device *pdev) > > +{ > > + struct sdhci_pltfm_host *pltfm_host; > > + struct sdhci_host *host; > > + u32 caps; > > + int ret; > > + > > + host = sdhci_pltfm_init(pdev, &npcm_sdhci_pdata, 0); > > + if (IS_ERR(host)) > > + return PTR_ERR(host); > > + > > + pltfm_host = sdhci_priv(host); > > > + pltfm_host->clk = devm_clk_get_optional(&pdev->dev, NULL); > > You can't mix devm with non-devm in this way. Can you explain what you mean You can't mix devm with non-devm in this way, where is the mix? In version 1 used devm_clk_get, is it problematic? > > > + if (IS_ERR(pltfm_host->clk)) > > + return PTR_ERR(pltfm_host->clk); > > + > > + ret = clk_prepare_enable(pltfm_host->clk); > > + if (ret) > > + return ret; > > + > > + caps = sdhci_readl(host, SDHCI_CAPABILITIES); > > + if (caps & SDHCI_CAN_DO_8BIT) > > + host->mmc->caps |= MMC_CAP_8_BIT_DATA; > > + > > + ret = mmc_of_parse(host->mmc); > > + if (ret) > > + goto err_sdhci_add; > > + > > + ret = sdhci_add_host(host); > > + if (ret) > > + goto err_sdhci_add; > > Why can't you use sdhci_pltfm_register()? two things are missing in sdhci_pltfm_register 1. clock. 2. Adding SDHCI_CAN_DO_8BIT capability according the eMMC capabilities. > > > + return 0; > > + > > +err_sdhci_add: > > + clk_disable_unprepare(pltfm_host->clk); > > + sdhci_pltfm_free(pdev); > > + return ret; > > +} > > Missing ->remove() due to above. Will check > > Have you tried to compile as a module and then remove and insert it > several times? will try > > -- > With Best Regards, > Andy Shevchenko Best regards, Tomer
On Mon, Dec 5, 2022 at 1:20 PM Tomer Maimon <tmaimon77@gmail.com> wrote: > On Mon, 5 Dec 2022 at 12:54, Andy Shevchenko <andy.shevchenko@gmail.com> wrote: > > On Mon, Dec 5, 2022 at 10:54 AM Tomer Maimon <tmaimon77@gmail.com> wrote: ... > > > +#include <linux/clk.h> > > > +#include <linux/err.h> > > > +#include <linux/io.h> > > > +#include <linux/mmc/host.h> > > > +#include <linux/mmc/mmc.h> > > > +#include <linux/module.h> > > > > I guess platform_device.h is missing here. > Build and work without platform_device.h, do I need it for module use? The rule of thumb is to include headers we are the direct user of. I believe you have a data type and API that are defined in that header. ... > > > +static int npcm_sdhci_probe(struct platform_device *pdev) > > > +{ > > > + struct sdhci_pltfm_host *pltfm_host; > > > + struct sdhci_host *host; > > > + u32 caps; > > > + int ret; > > > + > > > + host = sdhci_pltfm_init(pdev, &npcm_sdhci_pdata, 0); > > > + if (IS_ERR(host)) > > > + return PTR_ERR(host); > > > + > > > + pltfm_host = sdhci_priv(host); > > > > > + pltfm_host->clk = devm_clk_get_optional(&pdev->dev, NULL); > > > > You can't mix devm with non-devm in this way. > Can you explain what you mean You can't mix devm with non-devm in this > way, where is the mix? > In version 1 used devm_clk_get, is it problematic? devm_ is problematic in your case. TL;DR: you need to use clk_get_optional() and clk_put(). Your ->remove() callback doesn't free resources in the reversed order which may or, by luck, may not be the case of all possible crashes, UAFs, races, etc during removal stage. All the same for error path in ->probe(). > > > + if (IS_ERR(pltfm_host->clk)) > > > + return PTR_ERR(pltfm_host->clk); > > > + > > > + ret = clk_prepare_enable(pltfm_host->clk); > > > + if (ret) > > > + return ret; > > > + > > > + caps = sdhci_readl(host, SDHCI_CAPABILITIES); > > > + if (caps & SDHCI_CAN_DO_8BIT) > > > + host->mmc->caps |= MMC_CAP_8_BIT_DATA; > > > + > > > + ret = mmc_of_parse(host->mmc); > > > + if (ret) > > > + goto err_sdhci_add; > > > + > > > + ret = sdhci_add_host(host); > > > + if (ret) > > > + goto err_sdhci_add; > > > > Why can't you use sdhci_pltfm_register()? > two things are missing in sdhci_pltfm_register > 1. clock. Taking into account the implementation of the corresponding _unregister() I would add the clock handling to the _register() one. Perhaps via a new member of the platform data that supplies the name and index of the clock and hence all clk_get_optional() / clk_put will be moved there. > 2. Adding SDHCI_CAN_DO_8BIT capability according the eMMC capabilities. All the same, why can't platform data be utilised for this? > > > + return 0; > > > + > > > +err_sdhci_add: > > > + clk_disable_unprepare(pltfm_host->clk); > > > + sdhci_pltfm_free(pdev); > > > + return ret; > > > +}
On 5/12/22 15:25, Andy Shevchenko wrote: > On Mon, Dec 5, 2022 at 1:20 PM Tomer Maimon <tmaimon77@gmail.com> wrote: >> On Mon, 5 Dec 2022 at 12:54, Andy Shevchenko <andy.shevchenko@gmail.com> wrote: >>> On Mon, Dec 5, 2022 at 10:54 AM Tomer Maimon <tmaimon77@gmail.com> wrote: > > ... > >>>> +#include <linux/clk.h> >>>> +#include <linux/err.h> >>>> +#include <linux/io.h> >>>> +#include <linux/mmc/host.h> >>>> +#include <linux/mmc/mmc.h> >>>> +#include <linux/module.h> >>> >>> I guess platform_device.h is missing here. >> Build and work without platform_device.h, do I need it for module use? > > The rule of thumb is to include headers we are the direct user of. I > believe you have a data type and API that are defined in that header. > > ... > >>>> +static int npcm_sdhci_probe(struct platform_device *pdev) >>>> +{ >>>> + struct sdhci_pltfm_host *pltfm_host; >>>> + struct sdhci_host *host; >>>> + u32 caps; >>>> + int ret; >>>> + >>>> + host = sdhci_pltfm_init(pdev, &npcm_sdhci_pdata, 0); >>>> + if (IS_ERR(host)) >>>> + return PTR_ERR(host); >>>> + >>>> + pltfm_host = sdhci_priv(host); >>> >>>> + pltfm_host->clk = devm_clk_get_optional(&pdev->dev, NULL); >>> >>> You can't mix devm with non-devm in this way. >> Can you explain what you mean You can't mix devm with non-devm in this >> way, where is the mix? >> In version 1 used devm_clk_get, is it problematic? > > devm_ is problematic in your case. > TL;DR: you need to use clk_get_optional() and clk_put(). devm_ calls exactly those, so what is the issue? > > Your ->remove() callback doesn't free resources in the reversed order > which may or, by luck, may not be the case of all possible crashes, > UAFs, races, etc during removal stage. All the same for error path in > ->probe(). > >>>> + if (IS_ERR(pltfm_host->clk)) >>>> + return PTR_ERR(pltfm_host->clk); >>>> + >>>> + ret = clk_prepare_enable(pltfm_host->clk); >>>> + if (ret) >>>> + return ret; >>>> + >>>> + caps = sdhci_readl(host, SDHCI_CAPABILITIES); >>>> + if (caps & SDHCI_CAN_DO_8BIT) >>>> + host->mmc->caps |= MMC_CAP_8_BIT_DATA; >>>> + >>>> + ret = mmc_of_parse(host->mmc); >>>> + if (ret) >>>> + goto err_sdhci_add; >>>> + >>>> + ret = sdhci_add_host(host); >>>> + if (ret) >>>> + goto err_sdhci_add; >>> >>> Why can't you use sdhci_pltfm_register()? >> two things are missing in sdhci_pltfm_register >> 1. clock. > > Taking into account the implementation of the corresponding > _unregister() I would add the clock handling to the _register() one. > Perhaps via a new member of the platform data that supplies the name > and index of the clock and hence all clk_get_optional() / clk_put will > be moved there. > >> 2. Adding SDHCI_CAN_DO_8BIT capability according the eMMC capabilities. > > All the same, why can't platform data be utilised for this? > >>>> + return 0; >>>> + >>>> +err_sdhci_add: >>>> + clk_disable_unprepare(pltfm_host->clk); >>>> + sdhci_pltfm_free(pdev); >>>> + return ret; >>>> +} >
On Mon, Dec 5, 2022 at 3:41 PM Adrian Hunter <adrian.hunter@intel.com> wrote: > On 5/12/22 15:25, Andy Shevchenko wrote: > > On Mon, Dec 5, 2022 at 1:20 PM Tomer Maimon <tmaimon77@gmail.com> wrote: > >> On Mon, 5 Dec 2022 at 12:54, Andy Shevchenko <andy.shevchenko@gmail.com> wrote: > >>> On Mon, Dec 5, 2022 at 10:54 AM Tomer Maimon <tmaimon77@gmail.com> wrote: ... > >>>> + pltfm_host->clk = devm_clk_get_optional(&pdev->dev, NULL); > >>> > >>> You can't mix devm with non-devm in this way. > >> Can you explain what you mean You can't mix devm with non-devm in this > >> way, where is the mix? > >> In version 1 used devm_clk_get, is it problematic? > > > > devm_ is problematic in your case. > > TL;DR: you need to use clk_get_optional() and clk_put(). > > devm_ calls exactly those, so what is the issue? The issue is the error path or removal stage where it may or may be not problematic. To be on the safe side, the best approach is to make sure that allocated resources are being deallocated in the reversed order. That said, the 1. call non-devm_func() 2. call devm_func() is wrong strictly speaking. > > Your ->remove() callback doesn't free resources in the reversed order > > which may or, by luck, may not be the case of all possible crashes, > > UAFs, races, etc during removal stage. All the same for error path in > > ->probe(). I also pointed out above what would be the outcome of neglecting this rule. > >>>> + if (IS_ERR(pltfm_host->clk)) > >>>> + return PTR_ERR(pltfm_host->clk); > >>>> + > >>>> + ret = clk_prepare_enable(pltfm_host->clk); > >>>> + if (ret) > >>>> + return ret; > >>>> + > >>>> + caps = sdhci_readl(host, SDHCI_CAPABILITIES); > >>>> + if (caps & SDHCI_CAN_DO_8BIT) > >>>> + host->mmc->caps |= MMC_CAP_8_BIT_DATA; > >>>> + > >>>> + ret = mmc_of_parse(host->mmc); > >>>> + if (ret) > >>>> + goto err_sdhci_add; > >>>> + > >>>> + ret = sdhci_add_host(host); > >>>> + if (ret) > >>>> + goto err_sdhci_add; > >>> > >>> Why can't you use sdhci_pltfm_register()? > >> two things are missing in sdhci_pltfm_register > >> 1. clock. > > > > Taking into account the implementation of the corresponding > > _unregister() I would add the clock handling to the _register() one. > > Perhaps via a new member of the platform data that supplies the name > > and index of the clock and hence all clk_get_optional() / clk_put will > > be moved there. > > > >> 2. Adding SDHCI_CAN_DO_8BIT capability according the eMMC capabilities. > > > > All the same, why can't platform data be utilised for this? > > > >>>> + return 0; > >>>> + > >>>> +err_sdhci_add: > >>>> + clk_disable_unprepare(pltfm_host->clk); > >>>> + sdhci_pltfm_free(pdev); > >>>> + return ret; > >>>> +}
On Mon, Dec 5, 2022 at 4:14 PM Andy Shevchenko <andy.shevchenko@gmail.com> wrote: > > On Mon, Dec 5, 2022 at 3:41 PM Adrian Hunter <adrian.hunter@intel.com> wrote: > > On 5/12/22 15:25, Andy Shevchenko wrote: > > > On Mon, Dec 5, 2022 at 1:20 PM Tomer Maimon <tmaimon77@gmail.com> wrote: > > >> On Mon, 5 Dec 2022 at 12:54, Andy Shevchenko <andy.shevchenko@gmail.com> wrote: > > >>> On Mon, Dec 5, 2022 at 10:54 AM Tomer Maimon <tmaimon77@gmail.com> wrote: > > ... > > > >>>> + pltfm_host->clk = devm_clk_get_optional(&pdev->dev, NULL); > > >>> > > >>> You can't mix devm with non-devm in this way. > > >> Can you explain what you mean You can't mix devm with non-devm in this > > >> way, where is the mix? > > >> In version 1 used devm_clk_get, is it problematic? > > > > > > devm_ is problematic in your case. > > > TL;DR: you need to use clk_get_optional() and clk_put(). > > > > devm_ calls exactly those, so what is the issue? > > The issue is the error path or removal stage where it may or may be > not problematic. To be on the safe side, the best approach is to make > sure that allocated resources are being deallocated in the reversed > order. That said, the > > 1. call non-devm_func() > 2. call devm_func() > > is wrong strictly speaking. To elaborate more, the 1. call all devm_func() 2. call only non-devm_func() is the correct order. Hence in this case the driver can be worked around easily (by shuffling the order in ->probe() to call devm_ first), but as I said looking into implementation of the _unregister() I'm pretty sure that clock management should be in sdhci-pltfm, rather than in all callers who won't need the full customization. Hope this helps to understand my point. > > > Your ->remove() callback doesn't free resources in the reversed order > > > which may or, by luck, may not be the case of all possible crashes, > > > UAFs, races, etc during removal stage. All the same for error path in > > > ->probe(). > > I also pointed out above what would be the outcome of neglecting this rule. > > > >>>> + if (IS_ERR(pltfm_host->clk)) > > >>>> + return PTR_ERR(pltfm_host->clk); > > >>>> + > > >>>> + ret = clk_prepare_enable(pltfm_host->clk); > > >>>> + if (ret) > > >>>> + return ret; > > >>>> + > > >>>> + caps = sdhci_readl(host, SDHCI_CAPABILITIES); > > >>>> + if (caps & SDHCI_CAN_DO_8BIT) > > >>>> + host->mmc->caps |= MMC_CAP_8_BIT_DATA; > > >>>> + > > >>>> + ret = mmc_of_parse(host->mmc); > > >>>> + if (ret) > > >>>> + goto err_sdhci_add; > > >>>> + > > >>>> + ret = sdhci_add_host(host); > > >>>> + if (ret) > > >>>> + goto err_sdhci_add; > > >>> > > >>> Why can't you use sdhci_pltfm_register()? > > >> two things are missing in sdhci_pltfm_register > > >> 1. clock. > > > > > > Taking into account the implementation of the corresponding > > > _unregister() I would add the clock handling to the _register() one. > > > Perhaps via a new member of the platform data that supplies the name > > > and index of the clock and hence all clk_get_optional() / clk_put will > > > be moved there. > > > > > >> 2. Adding SDHCI_CAN_DO_8BIT capability according the eMMC capabilities. > > > > > > All the same, why can't platform data be utilised for this? > > > > > >>>> + return 0; > > >>>> + > > >>>> +err_sdhci_add: > > >>>> + clk_disable_unprepare(pltfm_host->clk); > > >>>> + sdhci_pltfm_free(pdev); > > >>>> + return ret; > > >>>> +} > > > -- > With Best Regards, > Andy Shevchenko
On 5/12/22 16:17, Andy Shevchenko wrote: > On Mon, Dec 5, 2022 at 4:14 PM Andy Shevchenko > <andy.shevchenko@gmail.com> wrote: >> >> On Mon, Dec 5, 2022 at 3:41 PM Adrian Hunter <adrian.hunter@intel.com> wrote: >>> On 5/12/22 15:25, Andy Shevchenko wrote: >>>> On Mon, Dec 5, 2022 at 1:20 PM Tomer Maimon <tmaimon77@gmail.com> wrote: >>>>> On Mon, 5 Dec 2022 at 12:54, Andy Shevchenko <andy.shevchenko@gmail.com> wrote: >>>>>> On Mon, Dec 5, 2022 at 10:54 AM Tomer Maimon <tmaimon77@gmail.com> wrote: >> >> ... >> >>>>>>> + pltfm_host->clk = devm_clk_get_optional(&pdev->dev, NULL); >>>>>> >>>>>> You can't mix devm with non-devm in this way. >>>>> Can you explain what you mean You can't mix devm with non-devm in this >>>>> way, where is the mix? >>>>> In version 1 used devm_clk_get, is it problematic? >>>> >>>> devm_ is problematic in your case. >>>> TL;DR: you need to use clk_get_optional() and clk_put(). >>> >>> devm_ calls exactly those, so what is the issue? >> >> The issue is the error path or removal stage where it may or may be >> not problematic. To be on the safe side, the best approach is to make >> sure that allocated resources are being deallocated in the reversed >> order. That said, the >> >> 1. call non-devm_func() >> 2. call devm_func() >> >> is wrong strictly speaking. > > To elaborate more, the > > 1. call all devm_func() > 2. call only non-devm_func() > > is the correct order. 1. WRT pltfm_host->clk, that is what is happening 2. WRT other resources that is simply not always possible because not every resource is wrapped by devm_ e.g. mmc_alloc_host() / mmc_free_host() > > Hence in this case the driver can be worked around easily (by > shuffling the order in ->probe() to call devm_ first), but as I said > looking into implementation of the _unregister() I'm pretty sure that > clock management should be in sdhci-pltfm, rather than in all callers > who won't need the full customization. > > Hope this helps to understand my point. > >>>> Your ->remove() callback doesn't free resources in the reversed order >>>> which may or, by luck, may not be the case of all possible crashes, >>>> UAFs, races, etc during removal stage. All the same for error path in >>>> ->probe(). >> >> I also pointed out above what would be the outcome of neglecting this rule. >> >>>>>>> + if (IS_ERR(pltfm_host->clk)) >>>>>>> + return PTR_ERR(pltfm_host->clk); >>>>>>> + >>>>>>> + ret = clk_prepare_enable(pltfm_host->clk); >>>>>>> + if (ret) >>>>>>> + return ret; >>>>>>> + >>>>>>> + caps = sdhci_readl(host, SDHCI_CAPABILITIES); >>>>>>> + if (caps & SDHCI_CAN_DO_8BIT) >>>>>>> + host->mmc->caps |= MMC_CAP_8_BIT_DATA; >>>>>>> + >>>>>>> + ret = mmc_of_parse(host->mmc); >>>>>>> + if (ret) >>>>>>> + goto err_sdhci_add; >>>>>>> + >>>>>>> + ret = sdhci_add_host(host); >>>>>>> + if (ret) >>>>>>> + goto err_sdhci_add; >>>>>> >>>>>> Why can't you use sdhci_pltfm_register()? >>>>> two things are missing in sdhci_pltfm_register >>>>> 1. clock. >>>> >>>> Taking into account the implementation of the corresponding >>>> _unregister() I would add the clock handling to the _register() one. >>>> Perhaps via a new member of the platform data that supplies the name >>>> and index of the clock and hence all clk_get_optional() / clk_put will >>>> be moved there. >>>> >>>>> 2. Adding SDHCI_CAN_DO_8BIT capability according the eMMC capabilities. >>>> >>>> All the same, why can't platform data be utilised for this? >>>> >>>>>>> + return 0; >>>>>>> + >>>>>>> +err_sdhci_add: >>>>>>> + clk_disable_unprepare(pltfm_host->clk); >>>>>>> + sdhci_pltfm_free(pdev); >>>>>>> + return ret; >>>>>>> +} >> >> >> -- >> With Best Regards, >> Andy Shevchenko > > >
Hi Andy and Adrian, Thanks for your clarifications On Mon, 5 Dec 2022 at 16:33, Adrian Hunter <adrian.hunter@intel.com> wrote: > > On 5/12/22 16:17, Andy Shevchenko wrote: > > On Mon, Dec 5, 2022 at 4:14 PM Andy Shevchenko > > <andy.shevchenko@gmail.com> wrote: > >> > >> On Mon, Dec 5, 2022 at 3:41 PM Adrian Hunter <adrian.hunter@intel.com> wrote: > >>> On 5/12/22 15:25, Andy Shevchenko wrote: > >>>> On Mon, Dec 5, 2022 at 1:20 PM Tomer Maimon <tmaimon77@gmail.com> wrote: > >>>>> On Mon, 5 Dec 2022 at 12:54, Andy Shevchenko <andy.shevchenko@gmail.com> wrote: > >>>>>> On Mon, Dec 5, 2022 at 10:54 AM Tomer Maimon <tmaimon77@gmail.com> wrote: > >> > >> ... > >> > >>>>>>> + pltfm_host->clk = devm_clk_get_optional(&pdev->dev, NULL); > >>>>>> > >>>>>> You can't mix devm with non-devm in this way. > >>>>> Can you explain what you mean You can't mix devm with non-devm in this > >>>>> way, where is the mix? > >>>>> In version 1 used devm_clk_get, is it problematic? > >>>> > >>>> devm_ is problematic in your case. > >>>> TL;DR: you need to use clk_get_optional() and clk_put(). > >>> > >>> devm_ calls exactly those, so what is the issue? > >> > >> The issue is the error path or removal stage where it may or may be > >> not problematic. To be on the safe side, the best approach is to make > >> sure that allocated resources are being deallocated in the reversed > >> order. That said, the > >> > >> 1. call non-devm_func() > >> 2. call devm_func() > >> > >> is wrong strictly speaking. > > > > To elaborate more, the > > > > 1. call all devm_func() > > 2. call only non-devm_func() > > > > is the correct order. > > 1. WRT pltfm_host->clk, that is what is happening > 2. WRT other resources that is simply not always possible because not every resource is wrapped by devm_ > e.g. mmc_alloc_host() / mmc_free_host() I little confused about what to decide, should I use only non-devm_func because mmc_alloc_host() / mmc_free_host() is not warrped with devm_? > > > > > Hence in this case the driver can be worked around easily (by > > shuffling the order in ->probe() to call devm_ first), but as I said > > looking into implementation of the _unregister() I'm pretty sure that > > clock management should be in sdhci-pltfm, rather than in all callers > > who won't need the full customization. > > > > Hope this helps to understand my point. > > > >>>> Your ->remove() callback doesn't free resources in the reversed order > >>>> which may or, by luck, may not be the case of all possible crashes, > >>>> UAFs, races, etc during removal stage. All the same for error path in > >>>> ->probe(). > >> > >> I also pointed out above what would be the outcome of neglecting this rule. > >> > >>>>>>> + if (IS_ERR(pltfm_host->clk)) > >>>>>>> + return PTR_ERR(pltfm_host->clk); > >>>>>>> + > >>>>>>> + ret = clk_prepare_enable(pltfm_host->clk); > >>>>>>> + if (ret) > >>>>>>> + return ret; > >>>>>>> + > >>>>>>> + caps = sdhci_readl(host, SDHCI_CAPABILITIES); > >>>>>>> + if (caps & SDHCI_CAN_DO_8BIT) > >>>>>>> + host->mmc->caps |= MMC_CAP_8_BIT_DATA; > >>>>>>> + > >>>>>>> + ret = mmc_of_parse(host->mmc); > >>>>>>> + if (ret) > >>>>>>> + goto err_sdhci_add; > >>>>>>> + > >>>>>>> + ret = sdhci_add_host(host); > >>>>>>> + if (ret) > >>>>>>> + goto err_sdhci_add; > >>>>>> > >>>>>> Why can't you use sdhci_pltfm_register()? > >>>>> two things are missing in sdhci_pltfm_register > >>>>> 1. clock. > >>>> > >>>> Taking into account the implementation of the corresponding > >>>> _unregister() I would add the clock handling to the _register() one. > >>>> Perhaps via a new member of the platform data that supplies the name > >>>> and index of the clock and hence all clk_get_optional() / clk_put will > >>>> be moved there. Do you mean to add it to sdhci_pltfm_register function? if yes I believe it will take some time to modify sdhci_pltfm_register I prefer not to use sdhci_pltfm_register. > >>>> > >>>>> 2. Adding SDHCI_CAN_DO_8BIT capability according the eMMC capabilities. > >>>> > >>>> All the same, why can't platform data be utilised for this? > >>>> > >>>>>>> + return 0; > >>>>>>> + > >>>>>>> +err_sdhci_add: > >>>>>>> + clk_disable_unprepare(pltfm_host->clk); > >>>>>>> + sdhci_pltfm_free(pdev); > >>>>>>> + return ret; > >>>>>>> +} > >> > >> > >> -- > >> With Best Regards, > >> Andy Shevchenko > > > > > > > Best regards, Tomer
On Wed, Dec 7, 2022 at 3:01 PM Tomer Maimon <tmaimon77@gmail.com> wrote: > On Mon, 5 Dec 2022 at 16:33, Adrian Hunter <adrian.hunter@intel.com> wrote: > > On 5/12/22 16:17, Andy Shevchenko wrote: > > > On Mon, Dec 5, 2022 at 4:14 PM Andy Shevchenko > > > <andy.shevchenko@gmail.com> wrote: > > >> On Mon, Dec 5, 2022 at 3:41 PM Adrian Hunter <adrian.hunter@intel.com> wrote: > > >>> On 5/12/22 15:25, Andy Shevchenko wrote: > > >>>> On Mon, Dec 5, 2022 at 1:20 PM Tomer Maimon <tmaimon77@gmail.com> wrote: ... > > >>>> devm_ is problematic in your case. > > >>>> TL;DR: you need to use clk_get_optional() and clk_put(). > > >>> > > >>> devm_ calls exactly those, so what is the issue? > > >> > > >> The issue is the error path or removal stage where it may or may be > > >> not problematic. To be on the safe side, the best approach is to make > > >> sure that allocated resources are being deallocated in the reversed > > >> order. That said, the > > >> > > >> 1. call non-devm_func() > > >> 2. call devm_func() > > >> > > >> is wrong strictly speaking. > > > > > > To elaborate more, the > > > > > > 1. call all devm_func() > > > 2. call only non-devm_func() > > > > > > is the correct order. > > > > 1. WRT pltfm_host->clk, that is what is happening > > 2. WRT other resources that is simply not always possible because not every resource is wrapped by devm_ > > e.g. mmc_alloc_host() / mmc_free_host() > I little confused about what to decide, should I use only > non-devm_func because mmc_alloc_host() / mmc_free_host() is not > warrped with devm_? It is up to you how to proceed. I pointed out the problem with your code which may or may not be fatal. If you want to solve it, there are several approaches: 1) get rid of devm_ completely; 2) properly shuffle the ordering in ->probe(), so all devm_ calls are followed by non-devm_; 3) wrap non-devm_ cals to become managed (see devm_add_action_or_reset() approach); 4) fix SDHCI / MMC layer by providing necessary devm_ calls and/or fix sdhci_pltfm_register() to handle the clock. Personally, the list order is from the least, what I prefer, to the most (i.o.w. I would like to see rather 4) than 1) to be implemented). > > > Hence in this case the driver can be worked around easily (by > > > shuffling the order in ->probe() to call devm_ first), but as I said > > > looking into implementation of the _unregister() I'm pretty sure that > > > clock management should be in sdhci-pltfm, rather than in all callers > > > who won't need the full customization. > > > > > > Hope this helps to understand my point. > > > > > >>>> Your ->remove() callback doesn't free resources in the reversed order > > >>>> which may or, by luck, may not be the case of all possible crashes, > > >>>> UAFs, races, etc during removal stage. All the same for error path in > > >>>> ->probe(). > > >> > > >> I also pointed out above what would be the outcome of neglecting this rule. ... > > >>>>>> Why can't you use sdhci_pltfm_register()? > > >>>>> two things are missing in sdhci_pltfm_register > > >>>>> 1. clock. > > >>>> > > >>>> Taking into account the implementation of the corresponding > > >>>> _unregister() I would add the clock handling to the _register() one. > > >>>> Perhaps via a new member of the platform data that supplies the name > > >>>> and index of the clock and hence all clk_get_optional() / clk_put will > > >>>> be moved there. > Do you mean to add it to sdhci_pltfm_register function? if yes I > believe it will take some time to modify sdhci_pltfm_register > I prefer not to use sdhci_pltfm_register. In the Linux kernel we are trying hard to avoid code duplication. Why do you need it to be open coded? (Yes, I heard you, but somebody should fix the issues with that funcion at some point, right?) > > >>>>> 2. Adding SDHCI_CAN_DO_8BIT capability according the eMMC capabilities. > > >>>> > > >>>> All the same, why can't platform data be utilised for this?
On 5/12/22 10:53, Tomer Maimon wrote: > Add Nuvoton NPCM BMC sdhci-pltfm controller driver. > > Signed-off-by: Tomer Maimon <tmaimon77@gmail.com> Acked-by: Adrian Hunter <adrian.hunter@intel.com> > --- > drivers/mmc/host/Kconfig | 8 ++++ > drivers/mmc/host/Makefile | 1 + > drivers/mmc/host/sdhci-npcm.c | 84 +++++++++++++++++++++++++++++++++++ > 3 files changed, 93 insertions(+) > create mode 100644 drivers/mmc/host/sdhci-npcm.c > > diff --git a/drivers/mmc/host/Kconfig b/drivers/mmc/host/Kconfig > index fb1062a6394c..82ab6fc25dca 100644 > --- a/drivers/mmc/host/Kconfig > +++ b/drivers/mmc/host/Kconfig > @@ -415,6 +415,14 @@ config MMC_SDHCI_MILBEAUT > > If unsure, say N. > > +config MMC_SDHCI_NPCM > + tristate "Secure Digital Host Controller Interface support for NPCM" > + depends on ARCH_NPCM || COMPILE_TEST > + depends on MMC_SDHCI_PLTFM > + help > + This provides support for the SD/eMMC controller found in > + NPCM BMC family SoCs. > + > config MMC_SDHCI_IPROC > tristate "SDHCI support for the BCM2835 & iProc SD/MMC Controller" > depends on ARCH_BCM2835 || ARCH_BCM_IPROC || ARCH_BRCMSTB || COMPILE_TEST > diff --git a/drivers/mmc/host/Makefile b/drivers/mmc/host/Makefile > index 4e4ceb32c4b4..a101f87a5f19 100644 > --- a/drivers/mmc/host/Makefile > +++ b/drivers/mmc/host/Makefile > @@ -97,6 +97,7 @@ obj-$(CONFIG_MMC_SDHCI_MICROCHIP_PIC32) += sdhci-pic32.o > obj-$(CONFIG_MMC_SDHCI_BRCMSTB) += sdhci-brcmstb.o > obj-$(CONFIG_MMC_SDHCI_OMAP) += sdhci-omap.o > obj-$(CONFIG_MMC_SDHCI_SPRD) += sdhci-sprd.o > +obj-$(CONFIG_MMC_SDHCI_NPCM) += sdhci-npcm.o > obj-$(CONFIG_MMC_CQHCI) += cqhci.o > cqhci-y += cqhci-core.o > cqhci-$(CONFIG_MMC_CRYPTO) += cqhci-crypto.o > diff --git a/drivers/mmc/host/sdhci-npcm.c b/drivers/mmc/host/sdhci-npcm.c > new file mode 100644 > index 000000000000..beace15b6c00 > --- /dev/null > +++ b/drivers/mmc/host/sdhci-npcm.c > @@ -0,0 +1,84 @@ > +// SPDX-License-Identifier: GPL-2.0+ > +/* > + * NPCM SDHC MMC host controller driver. > + * > + * Copyright (c) 2020 Nuvoton Technology corporation. > + */ > + > +#include <linux/clk.h> > +#include <linux/err.h> > +#include <linux/io.h> > +#include <linux/mmc/host.h> > +#include <linux/mmc/mmc.h> > +#include <linux/module.h> > + > +#include "sdhci-pltfm.h" > + > +static const struct sdhci_pltfm_data npcm_sdhci_pdata = { > + .quirks = SDHCI_QUIRK_DELAY_AFTER_POWER, > + .quirks2 = SDHCI_QUIRK2_STOP_WITH_TC | > + SDHCI_QUIRK2_NO_1_8_V, > +}; > + > +static int npcm_sdhci_probe(struct platform_device *pdev) > +{ > + struct sdhci_pltfm_host *pltfm_host; > + struct sdhci_host *host; > + u32 caps; > + int ret; > + > + host = sdhci_pltfm_init(pdev, &npcm_sdhci_pdata, 0); > + if (IS_ERR(host)) > + return PTR_ERR(host); > + > + pltfm_host = sdhci_priv(host); > + > + pltfm_host->clk = devm_clk_get_optional(&pdev->dev, NULL); > + if (IS_ERR(pltfm_host->clk)) > + return PTR_ERR(pltfm_host->clk); > + > + ret = clk_prepare_enable(pltfm_host->clk); > + if (ret) > + return ret; > + > + caps = sdhci_readl(host, SDHCI_CAPABILITIES); > + if (caps & SDHCI_CAN_DO_8BIT) > + host->mmc->caps |= MMC_CAP_8_BIT_DATA; > + > + ret = mmc_of_parse(host->mmc); > + if (ret) > + goto err_sdhci_add; > + > + ret = sdhci_add_host(host); > + if (ret) > + goto err_sdhci_add; > + > + return 0; > + > +err_sdhci_add: > + clk_disable_unprepare(pltfm_host->clk); > + sdhci_pltfm_free(pdev); > + return ret; > +} > + > +static const struct of_device_id npcm_sdhci_of_match[] = { > + { .compatible = "nuvoton,npcm750-sdhci" }, > + { .compatible = "nuvoton,npcm845-sdhci" }, > + { } > +}; > +MODULE_DEVICE_TABLE(of, npcm_sdhci_of_match); > + > +static struct platform_driver npcm_sdhci_driver = { > + .driver = { > + .name = "npcm-sdhci", > + .of_match_table = npcm_sdhci_of_match, > + .pm = &sdhci_pltfm_pmops, > + }, > + .probe = npcm_sdhci_probe, > + .remove = sdhci_pltfm_unregister, > +}; > +module_platform_driver(npcm_sdhci_driver); > + > +MODULE_DESCRIPTION("NPCM Secure Digital Host Controller Interface driver"); > +MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>"); > +MODULE_LICENSE("GPL");
On 7/12/22 15:25, Andy Shevchenko wrote: > On Wed, Dec 7, 2022 at 3:01 PM Tomer Maimon <tmaimon77@gmail.com> wrote: >> On Mon, 5 Dec 2022 at 16:33, Adrian Hunter <adrian.hunter@intel.com> wrote: >>> On 5/12/22 16:17, Andy Shevchenko wrote: >>>> On Mon, Dec 5, 2022 at 4:14 PM Andy Shevchenko >>>> <andy.shevchenko@gmail.com> wrote: >>>>> On Mon, Dec 5, 2022 at 3:41 PM Adrian Hunter <adrian.hunter@intel.com> wrote: >>>>>> On 5/12/22 15:25, Andy Shevchenko wrote: >>>>>>> On Mon, Dec 5, 2022 at 1:20 PM Tomer Maimon <tmaimon77@gmail.com> wrote: > > ... > >>>>>>> devm_ is problematic in your case. >>>>>>> TL;DR: you need to use clk_get_optional() and clk_put(). >>>>>> >>>>>> devm_ calls exactly those, so what is the issue? >>>>> >>>>> The issue is the error path or removal stage where it may or may be >>>>> not problematic. To be on the safe side, the best approach is to make >>>>> sure that allocated resources are being deallocated in the reversed >>>>> order. That said, the >>>>> >>>>> 1. call non-devm_func() >>>>> 2. call devm_func() >>>>> >>>>> is wrong strictly speaking. >>>> >>>> To elaborate more, the >>>> >>>> 1. call all devm_func() >>>> 2. call only non-devm_func() >>>> >>>> is the correct order. >>> >>> 1. WRT pltfm_host->clk, that is what is happening >>> 2. WRT other resources that is simply not always possible because not every resource is wrapped by devm_ >>> e.g. mmc_alloc_host() / mmc_free_host() >> I little confused about what to decide, should I use only >> non-devm_func because mmc_alloc_host() / mmc_free_host() is not >> warrped with devm_? > > It is up to you how to proceed. I pointed out the problem with your > code which may or may not be fatal. > > If you want to solve it, there are several approaches: > 1) get rid of devm_ completely; > 2) properly shuffle the ordering in ->probe(), so all devm_ calls are > followed by non-devm_; > 3) wrap non-devm_ cals to become managed (see > devm_add_action_or_reset() approach); > 4) fix SDHCI / MMC layer by providing necessary devm_ calls and/or fix > sdhci_pltfm_register() to handle the clock. I can take care of sdhci_pltfm when I next have some time. Otherwise it looks OK to me, so I am acking it. > > Personally, the list order is from the least, what I prefer, to the > most (i.o.w. I would like to see rather 4) than 1) to be implemented). > >>>> Hence in this case the driver can be worked around easily (by >>>> shuffling the order in ->probe() to call devm_ first), but as I said >>>> looking into implementation of the _unregister() I'm pretty sure that >>>> clock management should be in sdhci-pltfm, rather than in all callers >>>> who won't need the full customization. >>>> >>>> Hope this helps to understand my point. >>>> >>>>>>> Your ->remove() callback doesn't free resources in the reversed order >>>>>>> which may or, by luck, may not be the case of all possible crashes, >>>>>>> UAFs, races, etc during removal stage. All the same for error path in >>>>>>> ->probe(). >>>>> >>>>> I also pointed out above what would be the outcome of neglecting this rule. > > ... > >>>>>>>>> Why can't you use sdhci_pltfm_register()? >>>>>>>> two things are missing in sdhci_pltfm_register >>>>>>>> 1. clock. >>>>>>> >>>>>>> Taking into account the implementation of the corresponding >>>>>>> _unregister() I would add the clock handling to the _register() one. >>>>>>> Perhaps via a new member of the platform data that supplies the name >>>>>>> and index of the clock and hence all clk_get_optional() / clk_put will >>>>>>> be moved there. >> Do you mean to add it to sdhci_pltfm_register function? if yes I >> believe it will take some time to modify sdhci_pltfm_register >> I prefer not to use sdhci_pltfm_register. > > In the Linux kernel we are trying hard to avoid code duplication. Why > do you need it to be open coded? (Yes, I heard you, but somebody > should fix the issues with that funcion at some point, right?) > >>>>>>>> 2. Adding SDHCI_CAN_DO_8BIT capability according the eMMC capabilities. >>>>>>> >>>>>>> All the same, why can't platform data be utilised for this? >
On Wed, Dec 7, 2022 at 3:49 PM Adrian Hunter <adrian.hunter@intel.com> wrote: > On 7/12/22 15:25, Andy Shevchenko wrote: > > On Wed, Dec 7, 2022 at 3:01 PM Tomer Maimon <tmaimon77@gmail.com> wrote: > >> On Mon, 5 Dec 2022 at 16:33, Adrian Hunter <adrian.hunter@intel.com> wrote: > >>> On 5/12/22 16:17, Andy Shevchenko wrote: > >>>> On Mon, Dec 5, 2022 at 4:14 PM Andy Shevchenko > >>>> <andy.shevchenko@gmail.com> wrote: > >>>>> On Mon, Dec 5, 2022 at 3:41 PM Adrian Hunter <adrian.hunter@intel.com> wrote: > >>>>>> On 5/12/22 15:25, Andy Shevchenko wrote: > >>>>>>> On Mon, Dec 5, 2022 at 1:20 PM Tomer Maimon <tmaimon77@gmail.com> wrote: ... > >>>>>>> devm_ is problematic in your case. > >>>>>>> TL;DR: you need to use clk_get_optional() and clk_put(). > >>>>>> > >>>>>> devm_ calls exactly those, so what is the issue? > >>>>> > >>>>> The issue is the error path or removal stage where it may or may be > >>>>> not problematic. To be on the safe side, the best approach is to make > >>>>> sure that allocated resources are being deallocated in the reversed > >>>>> order. That said, the > >>>>> > >>>>> 1. call non-devm_func() > >>>>> 2. call devm_func() > >>>>> > >>>>> is wrong strictly speaking. > >>>> > >>>> To elaborate more, the > >>>> > >>>> 1. call all devm_func() > >>>> 2. call only non-devm_func() > >>>> > >>>> is the correct order. > >>> > >>> 1. WRT pltfm_host->clk, that is what is happening > >>> 2. WRT other resources that is simply not always possible because not every resource is wrapped by devm_ > >>> e.g. mmc_alloc_host() / mmc_free_host() > >> I little confused about what to decide, should I use only > >> non-devm_func because mmc_alloc_host() / mmc_free_host() is not > >> warrped with devm_? > > > > It is up to you how to proceed. I pointed out the problem with your > > code which may or may not be fatal. > > > > If you want to solve it, there are several approaches: > > 1) get rid of devm_ completely; > > 2) properly shuffle the ordering in ->probe(), so all devm_ calls are > > followed by non-devm_; > > 3) wrap non-devm_ cals to become managed (see > > devm_add_action_or_reset() approach); > > 4) fix SDHCI / MMC layer by providing necessary devm_ calls and/or fix > > sdhci_pltfm_register() to handle the clock. > > I can take care of sdhci_pltfm when I next have some time. > Otherwise it looks OK to me, so I am acking it. Thank you! > > Personally, the list order is from the least, what I prefer, to the > > most (i.o.w. I would like to see rather 4) than 1) to be implemented). > > > >>>> Hence in this case the driver can be worked around easily (by > >>>> shuffling the order in ->probe() to call devm_ first), but as I said > >>>> looking into implementation of the _unregister() I'm pretty sure that > >>>> clock management should be in sdhci-pltfm, rather than in all callers > >>>> who won't need the full customization. > >>>> > >>>> Hope this helps to understand my point. > >>>> > >>>>>>> Your ->remove() callback doesn't free resources in the reversed order > >>>>>>> which may or, by luck, may not be the case of all possible crashes, > >>>>>>> UAFs, races, etc during removal stage. All the same for error path in > >>>>>>> ->probe(). > >>>>> > >>>>> I also pointed out above what would be the outcome of neglecting this rule. ... > >>>>>>>>> Why can't you use sdhci_pltfm_register()? > >>>>>>>> two things are missing in sdhci_pltfm_register > >>>>>>>> 1. clock. > >>>>>>> > >>>>>>> Taking into account the implementation of the corresponding > >>>>>>> _unregister() I would add the clock handling to the _register() one. > >>>>>>> Perhaps via a new member of the platform data that supplies the name > >>>>>>> and index of the clock and hence all clk_get_optional() / clk_put will > >>>>>>> be moved there. > >> Do you mean to add it to sdhci_pltfm_register function? if yes I > >> believe it will take some time to modify sdhci_pltfm_register > >> I prefer not to use sdhci_pltfm_register. > > > > In the Linux kernel we are trying hard to avoid code duplication. Why > > do you need it to be open coded? (Yes, I heard you, but somebody > > should fix the issues with that funcion at some point, right?) > > > >>>>>>>> 2. Adding SDHCI_CAN_DO_8BIT capability according the eMMC capabilities. > >>>>>>> > >>>>>>> All the same, why can't platform data be utilised for this?
Thanks a lot, Adrian and andy! Appreciate it On Wed, 7 Dec 2022 at 18:49, Andy Shevchenko <andy.shevchenko@gmail.com> wrote: > > On Wed, Dec 7, 2022 at 3:49 PM Adrian Hunter <adrian.hunter@intel.com> wrote: > > On 7/12/22 15:25, Andy Shevchenko wrote: > > > On Wed, Dec 7, 2022 at 3:01 PM Tomer Maimon <tmaimon77@gmail.com> wrote: > > >> On Mon, 5 Dec 2022 at 16:33, Adrian Hunter <adrian.hunter@intel.com> wrote: > > >>> On 5/12/22 16:17, Andy Shevchenko wrote: > > >>>> On Mon, Dec 5, 2022 at 4:14 PM Andy Shevchenko > > >>>> <andy.shevchenko@gmail.com> wrote: > > >>>>> On Mon, Dec 5, 2022 at 3:41 PM Adrian Hunter <adrian.hunter@intel.com> wrote: > > >>>>>> On 5/12/22 15:25, Andy Shevchenko wrote: > > >>>>>>> On Mon, Dec 5, 2022 at 1:20 PM Tomer Maimon <tmaimon77@gmail.com> wrote: > > ... > > > >>>>>>> devm_ is problematic in your case. > > >>>>>>> TL;DR: you need to use clk_get_optional() and clk_put(). > > >>>>>> > > >>>>>> devm_ calls exactly those, so what is the issue? > > >>>>> > > >>>>> The issue is the error path or removal stage where it may or may be > > >>>>> not problematic. To be on the safe side, the best approach is to make > > >>>>> sure that allocated resources are being deallocated in the reversed > > >>>>> order. That said, the > > >>>>> > > >>>>> 1. call non-devm_func() > > >>>>> 2. call devm_func() > > >>>>> > > >>>>> is wrong strictly speaking. > > >>>> > > >>>> To elaborate more, the > > >>>> > > >>>> 1. call all devm_func() > > >>>> 2. call only non-devm_func() > > >>>> > > >>>> is the correct order. > > >>> > > >>> 1. WRT pltfm_host->clk, that is what is happening > > >>> 2. WRT other resources that is simply not always possible because not every resource is wrapped by devm_ > > >>> e.g. mmc_alloc_host() / mmc_free_host() > > >> I little confused about what to decide, should I use only > > >> non-devm_func because mmc_alloc_host() / mmc_free_host() is not > > >> warrped with devm_? > > > > > > It is up to you how to proceed. I pointed out the problem with your > > > code which may or may not be fatal. > > > > > > If you want to solve it, there are several approaches: > > > 1) get rid of devm_ completely; > > > 2) properly shuffle the ordering in ->probe(), so all devm_ calls are > > > followed by non-devm_; > > > 3) wrap non-devm_ cals to become managed (see > > > devm_add_action_or_reset() approach); > > > 4) fix SDHCI / MMC layer by providing necessary devm_ calls and/or fix > > > sdhci_pltfm_register() to handle the clock. > > > > I can take care of sdhci_pltfm when I next have some time. > > Otherwise it looks OK to me, so I am acking it. > > Thank you! > > > > Personally, the list order is from the least, what I prefer, to the > > > most (i.o.w. I would like to see rather 4) than 1) to be implemented). > > > > > >>>> Hence in this case the driver can be worked around easily (by > > >>>> shuffling the order in ->probe() to call devm_ first), but as I said > > >>>> looking into implementation of the _unregister() I'm pretty sure that > > >>>> clock management should be in sdhci-pltfm, rather than in all callers > > >>>> who won't need the full customization. > > >>>> > > >>>> Hope this helps to understand my point. > > >>>> > > >>>>>>> Your ->remove() callback doesn't free resources in the reversed order > > >>>>>>> which may or, by luck, may not be the case of all possible crashes, > > >>>>>>> UAFs, races, etc during removal stage. All the same for error path in > > >>>>>>> ->probe(). > > >>>>> > > >>>>> I also pointed out above what would be the outcome of neglecting this rule. > > ... > > > >>>>>>>>> Why can't you use sdhci_pltfm_register()? > > >>>>>>>> two things are missing in sdhci_pltfm_register > > >>>>>>>> 1. clock. > > >>>>>>> > > >>>>>>> Taking into account the implementation of the corresponding > > >>>>>>> _unregister() I would add the clock handling to the _register() one. > > >>>>>>> Perhaps via a new member of the platform data that supplies the name > > >>>>>>> and index of the clock and hence all clk_get_optional() / clk_put will > > >>>>>>> be moved there. > > >> Do you mean to add it to sdhci_pltfm_register function? if yes I > > >> believe it will take some time to modify sdhci_pltfm_register > > >> I prefer not to use sdhci_pltfm_register. > > > > > > In the Linux kernel we are trying hard to avoid code duplication. Why > > > do you need it to be open coded? (Yes, I heard you, but somebody > > > should fix the issues with that funcion at some point, right?) > > > > > >>>>>>>> 2. Adding SDHCI_CAN_DO_8BIT capability according the eMMC capabilities. > > >>>>>>> > > >>>>>>> All the same, why can't platform data be utilised for this? > > -- > With Best Regards, > Andy Shevchenko
On Mon, Dec 05, 2022 at 10:53:51AM +0200, Tomer Maimon wrote: > Add Nuvoton NPCM BMC sdhci-pltfm controller driver. > > Signed-off-by: Tomer Maimon <tmaimon77@gmail.com> I still don't see this driver in the upstream kernel, or in linux-next. Couple of comments: - devm ordering does not really matter here. The devm resource is the clock, it does not depend on local data, and it will be released last, so that is ok. - sdhci_pltfm_unregister() calls clk_disable_unprepare(), so there is no enabled clock floating around on driver removal. Unfortunately, that also means that the more convenient devm_clk_get_optional_enabled() can not be used. Real problem inline below. Guenter > --- > drivers/mmc/host/Kconfig | 8 ++++ > drivers/mmc/host/Makefile | 1 + > drivers/mmc/host/sdhci-npcm.c | 84 +++++++++++++++++++++++++++++++++++ > 3 files changed, 93 insertions(+) > create mode 100644 drivers/mmc/host/sdhci-npcm.c > > diff --git a/drivers/mmc/host/Kconfig b/drivers/mmc/host/Kconfig > index fb1062a6394c..82ab6fc25dca 100644 > --- a/drivers/mmc/host/Kconfig > +++ b/drivers/mmc/host/Kconfig > @@ -415,6 +415,14 @@ config MMC_SDHCI_MILBEAUT > > If unsure, say N. > > +config MMC_SDHCI_NPCM > + tristate "Secure Digital Host Controller Interface support for NPCM" > + depends on ARCH_NPCM || COMPILE_TEST > + depends on MMC_SDHCI_PLTFM > + help > + This provides support for the SD/eMMC controller found in > + NPCM BMC family SoCs. > + > config MMC_SDHCI_IPROC > tristate "SDHCI support for the BCM2835 & iProc SD/MMC Controller" > depends on ARCH_BCM2835 || ARCH_BCM_IPROC || ARCH_BRCMSTB || COMPILE_TEST > diff --git a/drivers/mmc/host/Makefile b/drivers/mmc/host/Makefile > index 4e4ceb32c4b4..a101f87a5f19 100644 > --- a/drivers/mmc/host/Makefile > +++ b/drivers/mmc/host/Makefile > @@ -97,6 +97,7 @@ obj-$(CONFIG_MMC_SDHCI_MICROCHIP_PIC32) += sdhci-pic32.o > obj-$(CONFIG_MMC_SDHCI_BRCMSTB) += sdhci-brcmstb.o > obj-$(CONFIG_MMC_SDHCI_OMAP) += sdhci-omap.o > obj-$(CONFIG_MMC_SDHCI_SPRD) += sdhci-sprd.o > +obj-$(CONFIG_MMC_SDHCI_NPCM) += sdhci-npcm.o > obj-$(CONFIG_MMC_CQHCI) += cqhci.o > cqhci-y += cqhci-core.o > cqhci-$(CONFIG_MMC_CRYPTO) += cqhci-crypto.o > diff --git a/drivers/mmc/host/sdhci-npcm.c b/drivers/mmc/host/sdhci-npcm.c > new file mode 100644 > index 000000000000..beace15b6c00 > --- /dev/null > +++ b/drivers/mmc/host/sdhci-npcm.c > @@ -0,0 +1,84 @@ > +// SPDX-License-Identifier: GPL-2.0+ > +/* > + * NPCM SDHC MMC host controller driver. > + * > + * Copyright (c) 2020 Nuvoton Technology corporation. > + */ > + > +#include <linux/clk.h> > +#include <linux/err.h> > +#include <linux/io.h> > +#include <linux/mmc/host.h> > +#include <linux/mmc/mmc.h> > +#include <linux/module.h> > + > +#include "sdhci-pltfm.h" > + > +static const struct sdhci_pltfm_data npcm_sdhci_pdata = { > + .quirks = SDHCI_QUIRK_DELAY_AFTER_POWER, > + .quirks2 = SDHCI_QUIRK2_STOP_WITH_TC | > + SDHCI_QUIRK2_NO_1_8_V, > +}; > + > +static int npcm_sdhci_probe(struct platform_device *pdev) > +{ > + struct sdhci_pltfm_host *pltfm_host; > + struct sdhci_host *host; > + u32 caps; > + int ret; > + > + host = sdhci_pltfm_init(pdev, &npcm_sdhci_pdata, 0); > + if (IS_ERR(host)) > + return PTR_ERR(host); > + > + pltfm_host = sdhci_priv(host); > + > + pltfm_host->clk = devm_clk_get_optional(&pdev->dev, NULL); > + if (IS_ERR(pltfm_host->clk)) > + return PTR_ERR(pltfm_host->clk); > + > + ret = clk_prepare_enable(pltfm_host->clk); > + if (ret) > + return ret; > + The two functions above should not return but goto the call to sdhci_pltfm_free() to avoid a memory leak on error. > + caps = sdhci_readl(host, SDHCI_CAPABILITIES); > + if (caps & SDHCI_CAN_DO_8BIT) > + host->mmc->caps |= MMC_CAP_8_BIT_DATA; > + > + ret = mmc_of_parse(host->mmc); > + if (ret) > + goto err_sdhci_add; > + > + ret = sdhci_add_host(host); > + if (ret) > + goto err_sdhci_add; > + > + return 0; > + > +err_sdhci_add: > + clk_disable_unprepare(pltfm_host->clk); > + sdhci_pltfm_free(pdev); > + return ret; > +} > + > +static const struct of_device_id npcm_sdhci_of_match[] = { > + { .compatible = "nuvoton,npcm750-sdhci" }, > + { .compatible = "nuvoton,npcm845-sdhci" }, > + { } > +}; > +MODULE_DEVICE_TABLE(of, npcm_sdhci_of_match); > + > +static struct platform_driver npcm_sdhci_driver = { > + .driver = { > + .name = "npcm-sdhci", > + .of_match_table = npcm_sdhci_of_match, > + .pm = &sdhci_pltfm_pmops, > + }, > + .probe = npcm_sdhci_probe, > + .remove = sdhci_pltfm_unregister, > +}; > +module_platform_driver(npcm_sdhci_driver); > + > +MODULE_DESCRIPTION("NPCM Secure Digital Host Controller Interface driver"); > +MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>"); > +MODULE_LICENSE("GPL"); > -- > 2.33.0 >
On Fri, Mar 17, 2023 at 4:16 PM Guenter Roeck <linux@roeck-us.net> wrote: > > On Mon, Dec 05, 2022 at 10:53:51AM +0200, Tomer Maimon wrote: > > Add Nuvoton NPCM BMC sdhci-pltfm controller driver. > > > > Signed-off-by: Tomer Maimon <tmaimon77@gmail.com> > > I still don't see this driver in the upstream kernel, or in linux-next. > > Couple of comments: > > - devm ordering does not really matter here. The devm resource > is the clock, it does not depend on local data, and it will be > released last, so that is ok. Not sure. Strictly speaking this is the problem. If you leave a clock going on in a wrong period of time it (theoretically) might break your hardware once and forever. Similar discussion about power, clock and reset signals has been held for camera sensors.
On 3/17/23 10:36, Andy Shevchenko wrote: > On Fri, Mar 17, 2023 at 4:16 PM Guenter Roeck <linux@roeck-us.net> wrote: >> >> On Mon, Dec 05, 2022 at 10:53:51AM +0200, Tomer Maimon wrote: >>> Add Nuvoton NPCM BMC sdhci-pltfm controller driver. >>> >>> Signed-off-by: Tomer Maimon <tmaimon77@gmail.com> >> >> I still don't see this driver in the upstream kernel, or in linux-next. >> >> Couple of comments: >> >> - devm ordering does not really matter here. The devm resource >> is the clock, it does not depend on local data, and it will be >> released last, so that is ok. > > Not sure. Strictly speaking this is the problem. If you leave a clock > going on in a wrong period of time it (theoretically) might break your > hardware once and forever. Similar discussion about power, clock and > reset signals has been held for camera sensors. > In general I agree, but not here. The remove function (sdhci_pltfm_unregister) does call clk_disable_unprepare(), so the clock isn't left running. Also, I think it is worthwhile to point out that exactly the same sequence (sdhci_pltfm_init followed by devm_clk_get and cleanup/removal with sdhci_pltfm_unregister) is shared among several sdhci drivers (including the memory leak I pointed out, but only in the aspeed driver). On a higher level I do agree that the sdhci platform code is in need of cleanup, but I don't think it is appropriate to tie such a cleanup to this driver submission. Note that I don't really care much, I just realized that this patch is stuck when I tried to test booting from SD drive with qemu. Guenter
On Fri, 17 Mar 2023 at 15:16, Guenter Roeck <linux@roeck-us.net> wrote: > > On Mon, Dec 05, 2022 at 10:53:51AM +0200, Tomer Maimon wrote: > > Add Nuvoton NPCM BMC sdhci-pltfm controller driver. > > > > Signed-off-by: Tomer Maimon <tmaimon77@gmail.com> > > I still don't see this driver in the upstream kernel, or in linux-next. Tomer, Guenter, Looks like I may have missed picking it up, probably because I thought the review was still ongoing. Please re-submit and include the reviewed-by tags, etc. Kind regards Uffe > > Couple of comments: > > - devm ordering does not really matter here. The devm resource > is the clock, it does not depend on local data, and it will be > released last, so that is ok. > - sdhci_pltfm_unregister() calls clk_disable_unprepare(), > so there is no enabled clock floating around on driver removal. > Unfortunately, that also means that the more convenient > devm_clk_get_optional_enabled() can not be used. > > Real problem inline below. > > Guenter > > > --- > > drivers/mmc/host/Kconfig | 8 ++++ > > drivers/mmc/host/Makefile | 1 + > > drivers/mmc/host/sdhci-npcm.c | 84 +++++++++++++++++++++++++++++++++++ > > 3 files changed, 93 insertions(+) > > create mode 100644 drivers/mmc/host/sdhci-npcm.c > > > > diff --git a/drivers/mmc/host/Kconfig b/drivers/mmc/host/Kconfig > > index fb1062a6394c..82ab6fc25dca 100644 > > --- a/drivers/mmc/host/Kconfig > > +++ b/drivers/mmc/host/Kconfig > > @@ -415,6 +415,14 @@ config MMC_SDHCI_MILBEAUT > > > > If unsure, say N. > > > > +config MMC_SDHCI_NPCM > > + tristate "Secure Digital Host Controller Interface support for NPCM" > > + depends on ARCH_NPCM || COMPILE_TEST > > + depends on MMC_SDHCI_PLTFM > > + help > > + This provides support for the SD/eMMC controller found in > > + NPCM BMC family SoCs. > > + > > config MMC_SDHCI_IPROC > > tristate "SDHCI support for the BCM2835 & iProc SD/MMC Controller" > > depends on ARCH_BCM2835 || ARCH_BCM_IPROC || ARCH_BRCMSTB || COMPILE_TEST > > diff --git a/drivers/mmc/host/Makefile b/drivers/mmc/host/Makefile > > index 4e4ceb32c4b4..a101f87a5f19 100644 > > --- a/drivers/mmc/host/Makefile > > +++ b/drivers/mmc/host/Makefile > > @@ -97,6 +97,7 @@ obj-$(CONFIG_MMC_SDHCI_MICROCHIP_PIC32) += sdhci-pic32.o > > obj-$(CONFIG_MMC_SDHCI_BRCMSTB) += sdhci-brcmstb.o > > obj-$(CONFIG_MMC_SDHCI_OMAP) += sdhci-omap.o > > obj-$(CONFIG_MMC_SDHCI_SPRD) += sdhci-sprd.o > > +obj-$(CONFIG_MMC_SDHCI_NPCM) += sdhci-npcm.o > > obj-$(CONFIG_MMC_CQHCI) += cqhci.o > > cqhci-y += cqhci-core.o > > cqhci-$(CONFIG_MMC_CRYPTO) += cqhci-crypto.o > > diff --git a/drivers/mmc/host/sdhci-npcm.c b/drivers/mmc/host/sdhci-npcm.c > > new file mode 100644 > > index 000000000000..beace15b6c00 > > --- /dev/null > > +++ b/drivers/mmc/host/sdhci-npcm.c > > @@ -0,0 +1,84 @@ > > +// SPDX-License-Identifier: GPL-2.0+ > > +/* > > + * NPCM SDHC MMC host controller driver. > > + * > > + * Copyright (c) 2020 Nuvoton Technology corporation. > > + */ > > + > > +#include <linux/clk.h> > > +#include <linux/err.h> > > +#include <linux/io.h> > > +#include <linux/mmc/host.h> > > +#include <linux/mmc/mmc.h> > > +#include <linux/module.h> > > + > > +#include "sdhci-pltfm.h" > > + > > +static const struct sdhci_pltfm_data npcm_sdhci_pdata = { > > + .quirks = SDHCI_QUIRK_DELAY_AFTER_POWER, > > + .quirks2 = SDHCI_QUIRK2_STOP_WITH_TC | > > + SDHCI_QUIRK2_NO_1_8_V, > > +}; > > + > > +static int npcm_sdhci_probe(struct platform_device *pdev) > > +{ > > + struct sdhci_pltfm_host *pltfm_host; > > + struct sdhci_host *host; > > + u32 caps; > > + int ret; > > + > > + host = sdhci_pltfm_init(pdev, &npcm_sdhci_pdata, 0); > > + if (IS_ERR(host)) > > + return PTR_ERR(host); > > + > > + pltfm_host = sdhci_priv(host); > > + > > + pltfm_host->clk = devm_clk_get_optional(&pdev->dev, NULL); > > + if (IS_ERR(pltfm_host->clk)) > > + return PTR_ERR(pltfm_host->clk); > > + > > + ret = clk_prepare_enable(pltfm_host->clk); > > + if (ret) > > + return ret; > > + > > The two functions above should not return but goto the call > to sdhci_pltfm_free() to avoid a memory leak on error. > > > + caps = sdhci_readl(host, SDHCI_CAPABILITIES); > > + if (caps & SDHCI_CAN_DO_8BIT) > > + host->mmc->caps |= MMC_CAP_8_BIT_DATA; > > + > > + ret = mmc_of_parse(host->mmc); > > + if (ret) > > + goto err_sdhci_add; > > + > > + ret = sdhci_add_host(host); > > + if (ret) > > + goto err_sdhci_add; > > + > > + return 0; > > + > > +err_sdhci_add: > > + clk_disable_unprepare(pltfm_host->clk); > > + sdhci_pltfm_free(pdev); > > + return ret; > > +} > > + > > +static const struct of_device_id npcm_sdhci_of_match[] = { > > + { .compatible = "nuvoton,npcm750-sdhci" }, > > + { .compatible = "nuvoton,npcm845-sdhci" }, > > + { } > > +}; > > +MODULE_DEVICE_TABLE(of, npcm_sdhci_of_match); > > + > > +static struct platform_driver npcm_sdhci_driver = { > > + .driver = { > > + .name = "npcm-sdhci", > > + .of_match_table = npcm_sdhci_of_match, > > + .pm = &sdhci_pltfm_pmops, > > + }, > > + .probe = npcm_sdhci_probe, > > + .remove = sdhci_pltfm_unregister, > > +}; > > +module_platform_driver(npcm_sdhci_driver); > > + > > +MODULE_DESCRIPTION("NPCM Secure Digital Host Controller Interface driver"); > > +MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>"); > > +MODULE_LICENSE("GPL"); > > -- > > 2.33.0 > >
diff --git a/drivers/mmc/host/Kconfig b/drivers/mmc/host/Kconfig index fb1062a6394c..82ab6fc25dca 100644 --- a/drivers/mmc/host/Kconfig +++ b/drivers/mmc/host/Kconfig @@ -415,6 +415,14 @@ config MMC_SDHCI_MILBEAUT If unsure, say N. +config MMC_SDHCI_NPCM + tristate "Secure Digital Host Controller Interface support for NPCM" + depends on ARCH_NPCM || COMPILE_TEST + depends on MMC_SDHCI_PLTFM + help + This provides support for the SD/eMMC controller found in + NPCM BMC family SoCs. + config MMC_SDHCI_IPROC tristate "SDHCI support for the BCM2835 & iProc SD/MMC Controller" depends on ARCH_BCM2835 || ARCH_BCM_IPROC || ARCH_BRCMSTB || COMPILE_TEST diff --git a/drivers/mmc/host/Makefile b/drivers/mmc/host/Makefile index 4e4ceb32c4b4..a101f87a5f19 100644 --- a/drivers/mmc/host/Makefile +++ b/drivers/mmc/host/Makefile @@ -97,6 +97,7 @@ obj-$(CONFIG_MMC_SDHCI_MICROCHIP_PIC32) += sdhci-pic32.o obj-$(CONFIG_MMC_SDHCI_BRCMSTB) += sdhci-brcmstb.o obj-$(CONFIG_MMC_SDHCI_OMAP) += sdhci-omap.o obj-$(CONFIG_MMC_SDHCI_SPRD) += sdhci-sprd.o +obj-$(CONFIG_MMC_SDHCI_NPCM) += sdhci-npcm.o obj-$(CONFIG_MMC_CQHCI) += cqhci.o cqhci-y += cqhci-core.o cqhci-$(CONFIG_MMC_CRYPTO) += cqhci-crypto.o diff --git a/drivers/mmc/host/sdhci-npcm.c b/drivers/mmc/host/sdhci-npcm.c new file mode 100644 index 000000000000..beace15b6c00 --- /dev/null +++ b/drivers/mmc/host/sdhci-npcm.c @@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * NPCM SDHC MMC host controller driver. + * + * Copyright (c) 2020 Nuvoton Technology corporation. + */ + +#include <linux/clk.h> +#include <linux/err.h> +#include <linux/io.h> +#include <linux/mmc/host.h> +#include <linux/mmc/mmc.h> +#include <linux/module.h> + +#include "sdhci-pltfm.h" + +static const struct sdhci_pltfm_data npcm_sdhci_pdata = { + .quirks = SDHCI_QUIRK_DELAY_AFTER_POWER, + .quirks2 = SDHCI_QUIRK2_STOP_WITH_TC | + SDHCI_QUIRK2_NO_1_8_V, +}; + +static int npcm_sdhci_probe(struct platform_device *pdev) +{ + struct sdhci_pltfm_host *pltfm_host; + struct sdhci_host *host; + u32 caps; + int ret; + + host = sdhci_pltfm_init(pdev, &npcm_sdhci_pdata, 0); + if (IS_ERR(host)) + return PTR_ERR(host); + + pltfm_host = sdhci_priv(host); + + pltfm_host->clk = devm_clk_get_optional(&pdev->dev, NULL); + if (IS_ERR(pltfm_host->clk)) + return PTR_ERR(pltfm_host->clk); + + ret = clk_prepare_enable(pltfm_host->clk); + if (ret) + return ret; + + caps = sdhci_readl(host, SDHCI_CAPABILITIES); + if (caps & SDHCI_CAN_DO_8BIT) + host->mmc->caps |= MMC_CAP_8_BIT_DATA; + + ret = mmc_of_parse(host->mmc); + if (ret) + goto err_sdhci_add; + + ret = sdhci_add_host(host); + if (ret) + goto err_sdhci_add; + + return 0; + +err_sdhci_add: + clk_disable_unprepare(pltfm_host->clk); + sdhci_pltfm_free(pdev); + return ret; +} + +static const struct of_device_id npcm_sdhci_of_match[] = { + { .compatible = "nuvoton,npcm750-sdhci" }, + { .compatible = "nuvoton,npcm845-sdhci" }, + { } +}; +MODULE_DEVICE_TABLE(of, npcm_sdhci_of_match); + +static struct platform_driver npcm_sdhci_driver = { + .driver = { + .name = "npcm-sdhci", + .of_match_table = npcm_sdhci_of_match, + .pm = &sdhci_pltfm_pmops, + }, + .probe = npcm_sdhci_probe, + .remove = sdhci_pltfm_unregister, +}; +module_platform_driver(npcm_sdhci_driver); + +MODULE_DESCRIPTION("NPCM Secure Digital Host Controller Interface driver"); +MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>"); +MODULE_LICENSE("GPL");