From nobody Wed Apr 16 13:10:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1553524478457545.5179602274778; Mon, 25 Mar 2019 07:34:38 -0700 (PDT) Received: from localhost ([127.0.0.1]:43445 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8QgN-0001Z5-9G for importer@patchew.org; Mon, 25 Mar 2019 10:34:35 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43545) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8Qds-0000Mu-KL for qemu-devel@nongnu.org; Mon, 25 Mar 2019 10:32:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h8Qdr-00021U-He for qemu-devel@nongnu.org; Mon, 25 Mar 2019 10:32:00 -0400 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]:34405) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h8Qdr-00020U-9A for qemu-devel@nongnu.org; Mon, 25 Mar 2019 10:31:59 -0400 Received: by mail-wr1-x42b.google.com with SMTP id p10so10452240wrq.1 for ; Mon, 25 Mar 2019 07:31:59 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id j1sm18858048wme.4.2019.03.25.07.31.56 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Mar 2019 07:31:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=G+9b/2FCZg8Q2GVJp+K7c4fI7/z1KC/e6TkX+Id3Cr8=; b=GGzFmjrRMdcUDQEEu3kIR3wtKXhz7xhySVU6V51PrSuw3kHw/rWP8qXFSnmZjL7TFq 4BwEj8xfK0MaIumvnpTrQs+na2dvF7ofc3zG7T2RjOeBH8JeJOFfFY3DlJU2ySPZ8i/r WTE1JR96j415Cq2qqmXxNJO9ULWpXsxmRgOhIS3IKzSnt3WnMXJG+XaaIcRpXw9MElMj cpO4wPgfRUUUvZwz1Nvz/KGHkgV2FXP1ixSvG7yVo8ak3KIdVYbvlCTlKI5mryGUrRbi ofM4CC2FEcxtma86RkQKP91hlnHNJs7kTUUQYR6Pwob0ju5N8E6cKCCs+9uqbLGZAsFN 24zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=G+9b/2FCZg8Q2GVJp+K7c4fI7/z1KC/e6TkX+Id3Cr8=; b=npMPXufSianKJHrjom2Jdykriv1v/dzGwy2y1P8QO4wNb/VJ9FFyRjeOqBmP5GzXLu B+ssOS7cSZ7XG4ZHjAD7+kOnPmnJN7FAe+Y1yFkE/S7P7mptieZVIfrAS2NMIV5oNtF5 UEOXGvF9trzYFIwNtXXUMe/1GABvkCRv/wCYfz9wKG7Tf+AOVahXp5izaiZ1cjnf8zNF wiObdejqCYzW99ckYgN1Gms8QwuxEDH6RpzP656w31+zdAtxDAbRiFMLmuGjJtGAD2z4 0IKrKFmNOvbDo9qP4blqOfMZesAamFd+psdkK0G63+eiDBG2sY+V1YcvteXdOcjvik99 jcQQ== X-Gm-Message-State: APjAAAUuMubDz3Tu5ZXAWIf4Wb5A85DVjuibXW6oyOZwkAdqFpUSvrxK sbNAEwNfHKPxzyuFgY4B5JY7sDqONjg= X-Google-Smtp-Source: APXvYqwK/B9wSO0SpjpvFmE8YuVwmqpz9lVEazDOHDRR4XCGlkt9k4jJ/sZFDL2Y2h2fS6PLp82kDw== X-Received: by 2002:adf:e610:: with SMTP id p16mr15800699wrm.64.1553524317973; Mon, 25 Mar 2019 07:31:57 -0700 (PDT) From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 25 Mar 2019 14:31:48 +0000 Message-Id: <20190325143152.9981-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190325143152.9981-1-peter.maydell@linaro.org> References: <20190325143152.9981-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::42b Subject: [Qemu-devel] [PULL 2/6] nrf51_gpio: reflect pull-up/pull-down to IRQs X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Paolo Bonzini Some drivers do I2C bitbanging by keeping the output to 0 and flipping the GPIO direction between input and output (see for example in Linux gpio_set_open_drain_value_commit, in drivers/gpio/gpiolib.c). When the GPIO is set to input, the pull-up resistor brings the output to 1, while when the GPIO is set to output, the output driver brings the output to 0. Implement this for the nRF51 GPIO device model. First, if both input and output are floating, and there is a pull-up or pull-down resistor configured, do not just set s->in, but also make any devices listening on the output qemu_irq receive that value. Second, if the pin is driven both internally (output pin) and externally you don't get a short circuit if both sides drive the pin to the same value. Signed-off-by: Paolo Bonzini Acked-by: Stefan Hajnoczi Message-id: 20190317141001.3346-1-pbonzini@redhat.com [PMM: wrapped long line] Reviewed-by: Peter Maydell Signed-off-by: Peter Maydell --- hw/gpio/nrf51_gpio.c | 65 +++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/hw/gpio/nrf51_gpio.c b/hw/gpio/nrf51_gpio.c index 86e047d649f..87a2f2a0dc3 100644 --- a/hw/gpio/nrf51_gpio.c +++ b/hw/gpio/nrf51_gpio.c @@ -43,6 +43,17 @@ static bool is_connected(uint32_t config, uint32_t level) return state; } =20 +static int pull_value(uint32_t config) +{ + int pull =3D extract32(config, 2, 2); + if (pull =3D=3D NRF51_GPIO_PULLDOWN) { + return 0; + } else if (pull =3D=3D NRF51_GPIO_PULLUP) { + return 1; + } + return -1; +} + static void update_output_irq(NRF51GPIOState *s, size_t i, bool connected, bool level) { @@ -61,43 +72,47 @@ static void update_output_irq(NRF51GPIOState *s, size_t= i, =20 static void update_state(NRF51GPIOState *s) { - uint32_t pull; + int pull; size_t i; - bool connected_out, dir, connected_in, out, input; + bool connected_out, dir, connected_in, out, in, input; =20 for (i =3D 0; i < NRF51_GPIO_PINS; i++) { - pull =3D extract32(s->cnf[i], 2, 2); + pull =3D pull_value(s->cnf[i]); dir =3D extract32(s->cnf[i], 0, 1); connected_in =3D extract32(s->in_mask, i, 1); out =3D extract32(s->out, i, 1); + in =3D extract32(s->in, i, 1); input =3D !extract32(s->cnf[i], 1, 1); connected_out =3D is_connected(s->cnf[i], out) && dir; =20 - update_output_irq(s, i, connected_out, out); - - /* Pin both driven externally and internally */ - if (connected_out && connected_in) { - qemu_log_mask(LOG_GUEST_ERROR, "GPIO pin %zu short circuited\n= ", i); - } - - /* - * Input buffer disconnected from internal/external drives, so - * pull-up/pull-down becomes relevant - */ - if (!input || (input && !connected_in && !connected_out)) { - if (pull =3D=3D NRF51_GPIO_PULLDOWN) { - s->in =3D deposit32(s->in, i, 1, 0); - } else if (pull =3D=3D NRF51_GPIO_PULLUP) { - s->in =3D deposit32(s->in, i, 1, 1); + if (!input) { + if (pull >=3D 0) { + /* Input buffer disconnected from external drives */ + s->in =3D deposit32(s->in, i, 1, pull); + } + } else { + if (connected_out && connected_in && out !=3D in) { + /* Pin both driven externally and internally */ + qemu_log_mask(LOG_GUEST_ERROR, + "GPIO pin %zu short circuited\n", i); + } + if (!connected_in) { + /* + * Floating input: the output stimulates IN if connected, + * otherwise pull-up/pull-down resistors put a value on bo= th + * IN and OUT. + */ + if (pull >=3D 0 && !connected_out) { + connected_out =3D true; + out =3D pull; + } + if (connected_out) { + s->in =3D deposit32(s->in, i, 1, out); + } } } - - /* Self stimulation through internal output driver */ - if (connected_out && !connected_in && input) { - s->in =3D deposit32(s->in, i, 1, out); - } + update_output_irq(s, i, connected_out, out); } - } =20 /* --=20 2.20.1