Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp2020232pxv; Sat, 24 Jul 2021 02:50:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1+qNzqbQ4ymDmlSVMUTjRvtJfhoFV1pTwZq7aoNW2PJg8TRT7lWfdPOnaa+PRSV6gtMpk X-Received: by 2002:a05:6e02:1905:: with SMTP id w5mr6413101ilu.270.1627120225216; Sat, 24 Jul 2021 02:50:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627120225; cv=none; d=google.com; s=arc-20160816; b=wWmrgzR338xVhkDrSH88tFX5Q8jdKOA5DbKebeY5lOoi+MKGeEE71/7bhM3deLm/0L nDvAjkhTRKQThhPswqYmtO1fxbT+wkY54JmwxYi1GjCAQ898f6CRLBZ9AGduAIMJla75 3lzTS8sCbNcT6KihIhvhNxj6s57Kkt5xxf91BHBHv1tS8issQVin2qlaA+i5WliXCsDV AJ35jf0ymuezTHWsIAsVvFiOgvP27H7edTGB84mOCKQdFGJvJnGuBhUPVbv6qAMPh0TO 365/EmRrYxeB0yibHAKa+0SS9gVl7Gc/egOaDF1kFLFBqw9gc35dKzmzHmF4OjY9TMwF eb8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=XZ1NPxV3+lfsZ26AiXJzMZmbFWQSPGfsdZBPXTXQmnc=; b=n8Ym5atc5SMIDifvoU0lINbx80O/uIJ2FMHi3GSOMGKLoTgdZpSvgrQbDT2Z4Sdesb wfwMCSHwZ/htRXXm3FSo0gAloJAxRNaDQaZoIrZIwFc/oBzmaorKMVPavN1bBSmHuCiu VYdVPmw4/vRwVskSuG3BDoSy0vS03v/OokCVxdY9AxjVWV9zeI5RxWJ+LlEcQJX23Qbs qk5uvdJ+pazfgJdj2X/bdaImUVVS9mNQmIaic3MA+VvvRlIBfq0Z77gJL5MtUM5t51V/ fGau9VcyuhXeFQLKOR4pfOOaTQqNYf6JBvmwacptZRO63dUuO3i0mNqzq1vayn0JzW1b 5cGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DJIia4Ao; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d20si26446967ioo.61.2021.07.24.02.50.13; Sat, 24 Jul 2021 02:50:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=DJIia4Ao; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235065AbhGXJHr (ORCPT + 99 others); Sat, 24 Jul 2021 05:07:47 -0400 Received: from mail.kernel.org ([198.145.29.99]:58364 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234867AbhGXJHr (ORCPT ); Sat, 24 Jul 2021 05:07:47 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id EBD7C60E90; Sat, 24 Jul 2021 09:48:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1627120099; bh=02mOMO2izxa1ZFn2U56YOuNLSWyPlG5ExV3Ei5AtcAk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=DJIia4AoVWzaW0oOK9aPfEt+s27DI+7fhN1yqbfozNICvd+gP+gAH6K+WMWSjqf5R M2udrmo+yl3nD7FYaBDMdy5A8dcSHx2TWD9cXP5LbGIbcmr20KRAJNWoZNxfbVZxxW PVWkAC/Q4BMVDvzUrNKPEYZR/aqz4BG7E7y284ViY0qa4b6SiosOhGmV0FYl79hVAJ 83A6HJTzu4R9HoUGxCX1Ehe8ZggeO0h4DNLF65RuXlb8bBP9Mavcl5lRFS6/qVDbnQ s4eh73c7vqCSSg47DLK5m7Re8jw8G4EzdrBV3EjUOjLok1kNkY5idtyYLA7abaJhpw 43hdCPopMheBg== Received: by pali.im (Postfix) id 65867EDF; Sat, 24 Jul 2021 11:48:16 +0200 (CEST) Date: Sat, 24 Jul 2021 11:48:16 +0200 From: Pali =?utf-8?B?Um9ow6Fy?= To: Andrew Lunn Cc: Michael Turquette , Stephen Boyd , Rob Herring , Greg Kroah-Hartman , Gregory Clement , Sebastian Hesselbarth , Vladimir Vid , Marek =?utf-8?B?QmVow7pu?= , Geert Uytterhoeven , linux-clk@vger.kernel.org, linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: Re: [PATCH v3 2/5] serial: mvebu-uart: implement UART clock driver for configuring UART base clock Message-ID: <20210724094816.2y3peclaftx26kwj@pali> References: <20210624224909.6350-1-pali@kernel.org> <20210717123829.5201-1-pali@kernel.org> <20210717123829.5201-3-pali@kernel.org> <20210717180540.ersg5bslik6ivjie@pali> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20210717180540.ersg5bslik6ivjie@pali> User-Agent: NeoMutt/20180716 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Saturday 17 July 2021 20:05:40 Pali Rohár wrote: > On Saturday 17 July 2021 19:26:51 Andrew Lunn wrote: > > On Sat, Jul 17, 2021 at 02:38:26PM +0200, Pali Rohár wrote: > > > @@ -445,6 +472,7 @@ static void mvebu_uart_shutdown(struct uart_port *port) > > > static int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud) > > > { > > > unsigned int d_divisor, m_divisor; > > > + unsigned long flags; > > > u32 brdv, osamp; > > > > > > if (!port->uartclk) > > > @@ -463,10 +491,12 @@ static int mvebu_uart_baud_rate_set(struct uart_port *port, unsigned int baud) > > > m_divisor = OSAMP_DEFAULT_DIVISOR; > > > d_divisor = DIV_ROUND_CLOSEST(port->uartclk, baud * m_divisor); > > > > > > + spin_lock_irqsave(&mvebu_uart_lock, flags); > > > > Hi Pali > > > > You only need spin_lock_irqsave() if you plan on taking the spinlock > > in an interrupt handler. It seems unlikely the baud rate will be > > changed in interrupt context? Please check, and then swap to plain > > spin_lock(). > > Hello! Ok, I will check it. Well, driver is already using spin_lock_irqsave() in all other functions. And in linux/clk-provider.h is documented that drivers can call clk_enable() from an interrupt, so it means that spin_lock_irqsave() is really needed for mvebu_uart_lock. > > > brdv = readl(port->membase + UART_BRDV); > > > brdv &= ~BRDV_BAUD_MASK; > > > brdv |= d_divisor; > > > writel(brdv, port->membase + UART_BRDV); > > > + spin_unlock_irqrestore(&mvebu_uart_lock, flags); > > > > > > osamp = readl(port->membase + UART_OSAMP); > > > osamp &= ~OSAMP_DIVISORS_MASK; > > > > > + /* Recalculate UART1 divisor so UART1 baudrate does not change */ > > > + if (prev_clock_rate) { > > > + divisor = DIV_U64_ROUND_CLOSEST((u64)(val & BRDV_BAUD_MASK) * > > > + parent_clock_rate * prev_d1d2, > > > + prev_clock_rate * d1 * d2); > > > + if (divisor < 1) > > > + divisor = 1; > > > + else if (divisor > BRDV_BAUD_MAX) > > > + divisor = BRDV_BAUD_MAX; > > > + val = (val & ~BRDV_BAUD_MASK) | divisor; > > > + } > > > > I don't see any range checks in the patch which verifies the requested > > baud rate is actually possible. With code like this, it seems like the > > baud rate change will be successful, but the actual baud rate will not > > be what is requested. > > This code is in function which changes parent UART clock from one used > by bootloader to clock which will be used by kernel UART driver. > > Yes, it is possible if you configure something unusual in bootloader > that that this code breaks it. But I think there is not so much what we > can done here. > > In other patches is updated function mvebu_uart_set_termios() which > verifies that you can set particular baudrate. > > > > + /* Recalculate UART2 divisor so UART2 baudrate does not change */ > > > + if (prev_clock_rate) { > > > + val = readl(uart_clock_base->reg2); > > > + divisor = DIV_U64_ROUND_CLOSEST((u64)(val & BRDV_BAUD_MASK) * > > > + parent_clock_rate * prev_d1d2, > > > + prev_clock_rate * d1 * d2); > > > + if (divisor < 1) > > > + divisor = 1; > > > + else if (divisor > BRDV_BAUD_MAX) > > > + divisor = BRDV_BAUD_MAX; > > > + val = (val & ~BRDV_BAUD_MASK) | divisor; > > > + writel(val, uart_clock_base->reg2); > > > > Here it looks like UART1 could request a baud rate change, which ends > > up setting the clocks so that UART2 is out of range? Could the change > > for UART1 be successful, but you end up breaking UART2? I'm thinking > > when you are at opposite ends of the scale. UART2 is running at > > 110baud and UART1 at 230400baud. > > This code is also in function which just do one time change of UART > parent clock. Once clk driver is probed this parent clock (and its d1 > and d2 divisors) are not changed anymore. Parent clock and divisors are > chosen in way that kernel can always configure minimal baudrate 9600 on > both UARTs. > > You are right that some combinations are not possible. But with these > patches it is fixed what is supported at clk driver probe time. > > In v3 patch 5/5 is described how to calculate final baudrate from parent > clock and divisors d1, d2, d, m1, m2, m3, m4. Note that parent clock and > divisors d1 and d2 are shared for both UARTs. Other parameters (d, m1, > m2, m3, m4) can be set differently both UART1 and UART2. Changing shared > values is not possible during usage of UART. > > If you have any idea how to improve current implementation, please let > me know. > > Also note that all A3720 boards have disabled UART2 in DTS. And I'm not > sure if there is somebody who uses UART2 or who uses both UARTs.