From nobody Wed Nov 27 03:50:37 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1702002805; cv=none; d=zohomail.com; s=zohoarc; b=C0qj5jj4nYMlHQMt0USF3xZomqfhNuQQ9pk0IJPn0z7E1C/GXWpfnzPflPNDGqlsZj/l92zPkBS5495G213vrQeOq+1ZBARUb82uDL7KL9X1FyDA3/Lh/PUXmLDG36Dfz1tqnq4O2yobiYiaCz9T30Dg/m+NRZsIpDQjZuB9+QI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1702002805; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ceI73nY+WORMm25Lfb079HY3yTTZKu7+HhKvNA+xgeM=; b=g3IZGrZZkjloE5g+oOn/tf4RDScSHqEE9JT+Mk4rDo2AHtRCYCY9pywMiiEZ9QuAIcmhwPHshcXx+Cb/yY0ulUwnvwJN64Cep6FehXa5F70V6D1pBCnNDGfZBoK75/Mn0VF3Armnk1rxGn3twlOGEnaDAEPLA7Q2ZhTVM7DnnUo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1702002805942497.8288464252936; Thu, 7 Dec 2023 18:33:25 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rBQfN-00044R-6v; Thu, 07 Dec 2023 21:32:37 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rBQfL-00043W-Az; Thu, 07 Dec 2023 21:32:35 -0500 Received: from mail-lj1-x22d.google.com ([2a00:1450:4864:20::22d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rBQf8-0002R4-6v; Thu, 07 Dec 2023 21:32:35 -0500 Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2c9f85eff28so22518551fa.3; Thu, 07 Dec 2023 18:32:20 -0800 (PST) Received: from localhost.localdomain ([185.200.240.39]) by smtp.gmail.com with ESMTPSA id r1-20020a2e94c1000000b002c9e6cbf78esm99062ljh.19.2023.12.07.18.32.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 18:32:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702002738; x=1702607538; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ceI73nY+WORMm25Lfb079HY3yTTZKu7+HhKvNA+xgeM=; b=TLrxwk6UZYjUA2Zevkn7dgjVXb6GW5UIygXXUAa2c5KFYoNobxqwSk98DgRcPThJX+ 0UwY9k/BIn5oMNAI1SeppA3qb+i1FVwjnyDUdT05MO1NkAOx0U8bqzjjTHHYeR2o/wzs VPpjQTleXNQjTzzkTng2p4P7DS2byWTw1+mGjWCCP5qH7VyoIOJDJhdkHhxmEM9I84LI hdkm9YDRp0vbg8o76ifJMAE91sthilSvHw2TCPAUCaH6s9dRAmIZ+uSy/n3//7u6hhOO pC+FOEbkjpgr4NmeFOfzN154O5j5xNVuXDc3vzdLXyTyIQj8IPPtZUViht/DKszdIys9 txWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702002738; x=1702607538; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ceI73nY+WORMm25Lfb079HY3yTTZKu7+HhKvNA+xgeM=; b=b1YZdEWoKesJcUNeysGxkOZ+EURw32mKy1lZKwDywHczGFjKgcmOIz1jwlFNBr+nyw 4/pYwwP7bX+nJCL+EXEhfK/GvZZUETN7IL/HuOLzLKaQzzDxTmhPWHJNTTIrYiBQrz5X M5ch2g7zaW1U+UVqVjaI7NZWRyt02o/PYmyAOYvz3L8C/0UCpkdzaMV8wN64D0RnDv3l CCihaNhbg4A4VzWaKXHd6S1oHmDstBDnHlCiBb1vacHY3PMWv+6je9I+Vl7Km2YkQpfJ L8NJt++BoefhLM9dL9M8ymNogrhD3Tzt0NtrghAjfqo6COnfmqWgabqlwe9jFw4C+oVz fBKg== X-Gm-Message-State: AOJu0YxU6eqqVktJC5Ft02QDK8//IXFuG6X4JDssebTeT8s4oaUDzg5H vA5ni8ohG+daVRk3x6X6GpSGAwg+cfOzRQ== X-Google-Smtp-Source: AGHT+IFwLCZSeIf+t/KFzABzcvpiQaS7EOJtmPOTTyixtydmjA82vuaa+qzXLzv0r0koTCFksrA2rg== X-Received: by 2002:a2e:97d3:0:b0:2ca:388:723a with SMTP id m19-20020a2e97d3000000b002ca0388723amr2021413ljj.49.1702002738614; Thu, 07 Dec 2023 18:32:18 -0800 (PST) From: Sergey Kambalin X-Google-Original-From: Sergey Kambalin To: qemu-arm@nongnu.org Cc: qemu-devel@nongnu.org, Sergey Kambalin Subject: [PATCH v4 07/45] Implement BCM2838 GPIO functionality Date: Thu, 7 Dec 2023 20:31:07 -0600 Message-Id: <20231208023145.1385775-8-sergey.kambalin@auriga.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231208023145.1385775-1-sergey.kambalin@auriga.com> References: <20231208023145.1385775-1-sergey.kambalin@auriga.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=2a00:1450:4864:20::22d; envelope-from=serg.oker@gmail.com; helo=mail-lj1-x22d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 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-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1702002808095100003 Content-Type: text/plain; charset="utf-8" Signed-off-by: Sergey Kambalin Reviewed-by: Peter Maydell --- hw/gpio/bcm2838_gpio.c | 192 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 189 insertions(+), 3 deletions(-) diff --git a/hw/gpio/bcm2838_gpio.c b/hw/gpio/bcm2838_gpio.c index 15b66cb559..51eb55b00a 100644 --- a/hw/gpio/bcm2838_gpio.c +++ b/hw/gpio/bcm2838_gpio.c @@ -19,6 +19,7 @@ #include "hw/sysbus.h" #include "migration/vmstate.h" #include "hw/gpio/bcm2838_gpio.h" +#include "hw/irq.h" =20 #define GPFSEL0 0x00 #define GPFSEL1 0x04 @@ -57,14 +58,139 @@ #define RESET_VAL_CNTRL_REG2 0x50AAA95A; #define RESET_VAL_CNTRL_REG3 0x00055555; =20 +#define NUM_FSELN_IN_GPFSELN 10 +#define NUM_BITS_FSELN 3 +#define MASK_FSELN 0x7 + #define BYTES_IN_WORD 4 =20 +static uint32_t gpfsel_get(BCM2838GpioState *s, uint8_t reg) +{ + int i; + uint32_t value =3D 0; + for (i =3D 0; i < NUM_FSELN_IN_GPFSELN; i++) { + uint32_t index =3D NUM_FSELN_IN_GPFSELN * reg + i; + if (index < sizeof(s->fsel)) { + value |=3D (s->fsel[index] & MASK_FSELN) << (NUM_BITS_FSELN * = i); + } + } + return value; +} + +static void gpfsel_set(BCM2838GpioState *s, uint8_t reg, uint32_t value) +{ + int i; + for (i =3D 0; i < NUM_FSELN_IN_GPFSELN; i++) { + uint32_t index =3D NUM_FSELN_IN_GPFSELN * reg + i; + if (index < sizeof(s->fsel)) { + int fsel =3D (value >> (NUM_BITS_FSELN * i)) & MASK_FSELN; + s->fsel[index] =3D fsel; + } + } +} + +static int gpfsel_is_out(BCM2838GpioState *s, int index) +{ + if (index >=3D 0 && index < BCM2838_GPIO_NUM) { + return s->fsel[index] =3D=3D 1; + } + return 0; +} + +static void gpset(BCM2838GpioState *s, uint32_t val, uint8_t start, + uint8_t count, uint32_t *lev) +{ + uint32_t changes =3D val & ~*lev; + uint32_t cur =3D 1; + + int i; + for (i =3D 0; i < count; i++) { + if ((changes & cur) && (gpfsel_is_out(s, start + i))) { + qemu_set_irq(s->out[start + i], 1); + } + cur <<=3D 1; + } + + *lev |=3D val; +} + +static void gpclr(BCM2838GpioState *s, uint32_t val, uint8_t start, + uint8_t count, uint32_t *lev) +{ + uint32_t changes =3D val & *lev; + uint32_t cur =3D 1; + + int i; + for (i =3D 0; i < count; i++) { + if ((changes & cur) && (gpfsel_is_out(s, start + i))) { + qemu_set_irq(s->out[start + i], 0); + } + cur <<=3D 1; + } + + *lev &=3D ~val; +} + static uint64_t bcm2838_gpio_read(void *opaque, hwaddr offset, unsigned si= ze) { + BCM2838GpioState *s =3D (BCM2838GpioState *)opaque; uint64_t value =3D 0; =20 - qemu_log_mask(LOG_UNIMP, "%s: %s: not implemented for %"HWADDR_PRIx"\n= ", - TYPE_BCM2838_GPIO, __func__, offset); + switch (offset) { + case GPFSEL0: + case GPFSEL1: + case GPFSEL2: + case GPFSEL3: + case GPFSEL4: + case GPFSEL5: + value =3D gpfsel_get(s, offset / BYTES_IN_WORD); + break; + case GPSET0: + case GPSET1: + case GPCLR0: + case GPCLR1: + /* Write Only */ + qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: Attempt reading from write= only" + " register. %lu will be returned. Address 0x%"HWADDR= _PRIx + ", size %u\n", TYPE_BCM2838_GPIO, __func__, value, o= ffset, + size); + break; + case GPLEV0: + value =3D s->lev0; + break; + case GPLEV1: + value =3D s->lev1; + break; + case GPEDS0: + case GPEDS1: + case GPREN0: + case GPREN1: + case GPFEN0: + case GPFEN1: + case GPHEN0: + case GPHEN1: + case GPLEN0: + case GPLEN1: + case GPAREN0: + case GPAREN1: + case GPAFEN0: + case GPAFEN1: + /* Not implemented */ + qemu_log_mask(LOG_UNIMP, "%s: %s: not implemented for %"HWADDR_PRI= x"\n", + TYPE_BCM2838_GPIO, __func__, offset); + break; + case GPIO_PUP_PDN_CNTRL_REG0: + case GPIO_PUP_PDN_CNTRL_REG1: + case GPIO_PUP_PDN_CNTRL_REG2: + case GPIO_PUP_PDN_CNTRL_REG3: + value =3D s->pup_cntrl_reg[(offset - GPIO_PUP_PDN_CNTRL_REG0) + / sizeof(s->pup_cntrl_reg[0])]; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: bad offset %"HWADDR_PRIx"\= n", + TYPE_BCM2838_GPIO, __func__, offset); + break; + } =20 return value; } @@ -72,14 +198,74 @@ static uint64_t bcm2838_gpio_read(void *opaque, hwaddr= offset, unsigned size) static void bcm2838_gpio_write(void *opaque, hwaddr offset, uint64_t value, unsigned size) { - qemu_log_mask(LOG_UNIMP, "%s: %s: not implemented for %"HWADDR_PRIx"\n= ", + BCM2838GpioState *s =3D (BCM2838GpioState *)opaque; + + switch (offset) { + case GPFSEL0: + case GPFSEL1: + case GPFSEL2: + case GPFSEL3: + case GPFSEL4: + case GPFSEL5: + gpfsel_set(s, offset / BYTES_IN_WORD, value); + break; + case GPSET0: + gpset(s, value, 0, 32, &s->lev0); + break; + case GPSET1: + gpset(s, value, 32, 22, &s->lev1); + break; + case GPCLR0: + gpclr(s, value, 0, 32, &s->lev0); + break; + case GPCLR1: + gpclr(s, value, 32, 22, &s->lev1); + break; + case GPLEV0: + case GPLEV1: + /* Read Only */ + qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: Attempt writing %lu to rea= d " + "only register. Ignored. Address 0x%"HWADDR_PRIx", s= ize " + "%u\n", TYPE_BCM2838_GPIO, __func__, value, offset, = size); + break; + case GPEDS0: + case GPEDS1: + case GPREN0: + case GPREN1: + case GPFEN0: + case GPFEN1: + case GPHEN0: + case GPHEN1: + case GPLEN0: + case GPLEN1: + case GPAREN0: + case GPAREN1: + case GPAFEN0: + case GPAFEN1: + /* Not implemented */ + qemu_log_mask(LOG_UNIMP, "%s: %s: not implemented for %"HWADDR_PRI= x"\n", + TYPE_BCM2838_GPIO, __func__, offset); + break; + case GPIO_PUP_PDN_CNTRL_REG0: + case GPIO_PUP_PDN_CNTRL_REG1: + case GPIO_PUP_PDN_CNTRL_REG2: + case GPIO_PUP_PDN_CNTRL_REG3: + s->pup_cntrl_reg[(offset - GPIO_PUP_PDN_CNTRL_REG0) + / sizeof(s->pup_cntrl_reg[0])] =3D value; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: bad offset %"HWADDR_PRIx"\= n", TYPE_BCM2838_GPIO, __func__, offset); + } + return; } =20 static void bcm2838_gpio_reset(DeviceState *dev) { BCM2838GpioState *s =3D BCM2838_GPIO(dev); =20 + memset(s->fsel, 0, sizeof(s->fsel)); + s->lev0 =3D 0; s->lev1 =3D 0; =20 --=20 2.34.1