From patchwork Thu Jan 26 15:03:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leif Middelschulte X-Patchwork-Id: 48741 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp321138wrn; Thu, 26 Jan 2023 07:12:04 -0800 (PST) X-Google-Smtp-Source: AMrXdXvffZaEmhT+9sg6+k91tz1c53N2xbEiaMJC+S0T8TQWDAQlkq81KY9oVvw1Bk30OFuPY5qX X-Received: by 2002:a17:907:1110:b0:872:bb39:a6e1 with SMTP id qu16-20020a170907111000b00872bb39a6e1mr36801193ejb.52.1674745924499; Thu, 26 Jan 2023 07:12:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674745924; cv=none; d=google.com; s=arc-20160816; b=MnIxzM2W5sCiVQTG81lSDDO5C8c7s+mI2J+Lj/B5TeUTN+BMv8/owbt9fr2DkglTjY cRJNv/ZdfoFiqcwhbngQM0PHvhoDdX3U5DzM0qWPDTIbVQPAmpoZSxjE386Icm933GOX NDTAXeLCOsxveXRTIndrlnJhDozwtAIo5Gq0eTTUxWXSFAoBwZNMzeHw3in+kHEg4mYR epc2GfihtEZU61McuzxxntaR0C3dEXekqf9cRrFsZGLgoxS/cMBP5FcySYhw+wZ6K6xg +Bi6w4TXDgoXFMAe/LOYckVcFKWEOVViY7uj5g4z3hcgrBLKHbZ+MRnzE4XuCAazM6Kf 4p4g== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=6hGazIFpNgMsQBh0HkRBOxGzy6+XWZOvugjcdhOak+s=; b=DUdcbZtiHFXRxXWpdR/B5yDnZPfrdtvJQhYiO+VWwVXf0PJq9b2XEXBygj6G5T5y/Q oI9eORYGlh5kNsD2pRXhlP7RchNXAZNhQKGKoL+7dHdS9J5Gd7VWktmYllI6ZN6DSFUO m96KCP9oRR9P8OV4yEOMCG7T1WtY7uwCMemwFkbtj3EcNSxiiiMrlvQHVumDMFDScdIJ tL8jAZX/hJUuKF8Km4i0DgMIznrEfF6lTVk7hd1t+8K7EJQKahVFDwzFieu6SwLUrFQz 0kysPrB/tI4SCncM5P+hNdNbQnSgqWLIBUBjz+nz/YyQjFu2VHRoqKjNTNW7LTuwHwCm D7ZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=mTjsrxvy; 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=pass (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 y9-20020a170906518900b008787e4ac0bfsi598193ejk.719.2023.01.26.07.11.40; Thu, 26 Jan 2023 07:12:04 -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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=mTjsrxvy; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231274AbjAZPDv (ORCPT + 99 others); Thu, 26 Jan 2023 10:03:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231590AbjAZPDs (ORCPT ); Thu, 26 Jan 2023 10:03:48 -0500 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C01C6AF6B; Thu, 26 Jan 2023 07:03:47 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id m15so1315340wms.4; Thu, 26 Jan 2023 07:03:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=6hGazIFpNgMsQBh0HkRBOxGzy6+XWZOvugjcdhOak+s=; b=mTjsrxvyQ0i5vQW9fiCojh0KHZUVIbFGqjsUGUPayCpoxwkL352L2/Y1YDVlDtbeNp 4EG+jagxpkpCGphip07OugfBVauvutCvSS7T+VfT9NP8Gw+iM65hPQOqOdiB/ymCoKbX EYKRHb9XwiIdI7oSGQU3m7BRlU64QwfSn7Qz6QjZUYmbOydFdK5Nm6OVNtgZPnq0xqyE +CmytVW5eFISBkBdraEYNjw0z66ViBU1jvf+C7w2u5h3WZuouzqv+teYwHwpnNRTRLXd E5BC2nfHxSf2afdhVJ9Iw1du3EwUJ5o+oXuNJhx/++1vfMcXuYSYXZeLmyr0sQ1TJvf7 vsJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6hGazIFpNgMsQBh0HkRBOxGzy6+XWZOvugjcdhOak+s=; b=SU0wQpmYxnSOLD9nWXpF9LcbPU0RhzATXZJE5lTr2qy1O1GPUtOXpjzzmkjK3DBLS4 6oOsBZmvC6mL8aUi2hOwsHbzQX2shG/ZzxnX2zVd89ydhkCGWWAQtFmrlZJbcyKoVxAW i+7LlWk8qeOQoZg5PP5I+InsfHDpT4WFPtInFEKRbbAZVGtS12O3Q157i8UQhI4lcdJ0 qvrxBszr/UNkuR86vtq5PGwiHIm1DfqiKmURd1AwH/5AyeZi8Zp+xP11tDQQR/OWaB50 /gwL8AU9BKlUufgQZIIwBWmy3XyV4nFNbiWic0cQioEUQV9/R7w2T6lxUY/eZxlYqwPj aTfw== X-Gm-Message-State: AFqh2krbkOJwbdWpBNJlPpGU8bDpxIpdjQn2TBA7Z4aPfcjAdKV59LZB L8+kzAhvVp5oaY2ybHyUQck= X-Received: by 2002:a05:600c:19d0:b0:3dc:c05:9db6 with SMTP id u16-20020a05600c19d000b003dc0c059db6mr11626561wmq.33.1674745425630; Thu, 26 Jan 2023 07:03:45 -0800 (PST) Received: from DE008-GL-00034.UMK.kls.zentral (p5dda2d96.dip0.t-ipconnect.de. [93.218.45.150]) by smtp.gmail.com with ESMTPSA id n13-20020a05600c500d00b003db2b81660esm5254565wmr.21.2023.01.26.07.03.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 07:03:44 -0800 (PST) From: Leif Middelschulte X-Google-Original-From: Leif Middelschulte To: Thierry Reding , =?utf-8?q?Uwe_Kleine-K=C3=B6n?= =?utf-8?q?ig?= , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team Cc: Leif Middelschulte , linux-pwm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] pwm: imx27: fix race condition .apply,.get_state Date: Thu, 26 Jan 2023 16:03:13 +0100 Message-Id: <20230126150313.764284-1-Leif.Middelschulte@gmail.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756098382301687616?= X-GMAIL-MSGID: =?utf-8?q?1756098382301687616?= From: Leif Middelschulte With CONFIG_PWM_DEBUG=y after writing a value to the PWMSAR register in .apply(), the register is read in .get_state(). Unless a period completed in the meantime, this read yields the previously used duty cycle configuration. As the PWM_DEBUG code applies the read out configuration for testing purposes this effectively undoes the intended effect by rewriting the previous hardware state. Note that this change merely implements a sensible heuristic. The i.MX has a 4 slot FIFO to configure the duty cycle. This FIFO cannot be read back in its entirety. The "write x then read back x from hw" semantics are therefore not easily applicable. With this change, the .get_state() function tries to wait for some stabilization in the FIFO (empty state). In this state it keeps applying the last value written to the sample register. Signed-off-by: Leif Middelschulte --- drivers/pwm/pwm-imx27.c | 50 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/drivers/pwm/pwm-imx27.c b/drivers/pwm/pwm-imx27.c index 29a3089c534c..32389ca2da3e 100644 --- a/drivers/pwm/pwm-imx27.c +++ b/drivers/pwm/pwm-imx27.c @@ -75,6 +75,7 @@ (x)) + 1) #define MX3_PWM_SWR_LOOP 5 +#define MX3_PWM_FIFOAV_EMPTY_LOOP 4 /* PWMPR register value of 0xffff has the same effect as 0xfffe */ #define MX3_PWMPR_MAX 0xfffe @@ -118,8 +119,28 @@ static void pwm_imx27_clk_disable_unprepare(struct pwm_imx27_chip *imx) clk_disable_unprepare(imx->clk_ipg); } +static int pwm_imx27_wait_fifo_empty(struct pwm_chip *chip, + struct pwm_device *pwm) +{ + struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip); + struct device *dev = chip->dev; + unsigned int period_ms = DIV_ROUND_UP_ULL(pwm_get_period(pwm), NSEC_PER_MSEC); + int tries = MX3_PWM_FIFOAV_EMPTY_LOOP; + int fifoav; + u32 sr; + + while (tries--) { + sr = readl(imx->mmio_base + MX3_PWMSR); + fifoav = FIELD_GET(MX3_PWMSR_FIFOAV, sr); + if (fifoav == MX3_PWMSR_FIFOAV_EMPTY) + return; + msleep(period_ms); + } + dev_warn(dev, "FIFO has been refilled concurrently\n"); +} + static int pwm_imx27_get_state(struct pwm_chip *chip, - struct pwm_device *pwm, struct pwm_state *state) + struct pwm_device *pwm, struct pwm_state *state) { struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip); u32 period, prescaler, pwm_clk, val; @@ -161,10 +182,33 @@ static int pwm_imx27_get_state(struct pwm_chip *chip, * PWMSAR can be read only if PWM is enabled. If the PWM is disabled, * use the cached value. */ - if (state->enabled) + if (state->enabled) { + /* + * From the i.MX PWM reference manual: + * "A read on the sample register yields the current FIFO value that + * is being used, or will be used, by the PWM for generation on the + * output signal. Therefore, a write and a subsequent read on the + * sample register may result in different values being obtained." + * Furthermore: + * "When a new value is written, the duty cycle changes after the + * current period is over." + * Note "changes" vs. "changes to the given value"! + * Finally: + * "The PWM will run at the last set duty-cycle setting if all the + * values of the FIFO has been utilized, until the FIFO is reloaded + * or the PWM is disabled." + * Try to be at least a bit more deterministic about which value is + * read by waiting until the FIFO is empty. In this state the last/most + * recently pushed sample (duty cycle) value is continuously applied. + * Beware that this approach is still racy, as a new value could have + * been supplied and a period expired between the call of the wait + * function and the subsequent readl. + */ + pwm_imx27_wait_fifo_empty(chip, pwm); val = readl(imx->mmio_base + MX3_PWMSAR); - else + } else { val = imx->duty_cycle; + } tmp = NSEC_PER_SEC * (u64)(val) * prescaler; state->duty_cycle = DIV_ROUND_UP_ULL(tmp, pwm_clk);