From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by mx.groups.io with SMTP id smtpd.web11.5340.1593692401034821654 for ; Thu, 02 Jul 2020 05:20:01 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@akeo-ie.20150623.gappssmtp.com header.s=20150623 header.b=ui7YRK3Q; spf=none, err=permanent DNS error (domain: akeo.ie, ip: 209.85.128.68, mailfrom: pete@akeo.ie) Received: by mail-wm1-f68.google.com with SMTP id w3so15237252wmi.4 for ; Thu, 02 Jul 2020 05:20:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=akeo-ie.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=tceOB6cNfHqsqyfuASJOXtM54cSeUj0oQWbnfxCWaos=; b=ui7YRK3QR0XBe6Clqcsgf8ZzR0tgsUai74mD1RKrU2aP/lcr1NaigqvE2r9yLCyhbO V8msQoW6ZKKpcs9AM43E21OiZhyCXZJnaXGHRf8SzxpSQnHbru48rPVt6I/fOPnjF7uD hEzrPGWe+l1kyFDEtGPjLGvIfv4U/r4IbVopQNiUr3VreC3D/K9miKjHdRH85lhCRDeb xgne2+MHCM+gUXb+JgL8Fni7lvVBTuL+WyiClHvLsWlGvKXcOYWsa4HDCnztsGLYg9XB mmKupBmz5o9ugS8LT22H4JIai5GjHxpeCzbMRZ8bgLYMwzudqvYcqp1uxQTU1hgCT86F 4Whw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=tceOB6cNfHqsqyfuASJOXtM54cSeUj0oQWbnfxCWaos=; b=UKwcJzKtzIHKzGSVjDArAnU4i9WElFjqZR7KUgWisURfsiQ/dWxVFl5AF3QZiLw0yJ UYycFas/v/YdbrGggyHaSDF7GeTG6Dwx1uey/EHWFNU5wO8dSYItjCHFsfF4pAhC2o4k gQX2NsEncJGBrKQpHkbeCn/HqOIRN3DWCA0Z3eCiJJRSPBo3GIFSW4pVM4SwUFl6ozCB gyH0eE65XvfrVgJO8DynEoRQBH0hllL2iWWdqrWGCHh2pgJXq+2J8x93jUtBYv4mqLUk 1r08sWewc01dJdAgc+MprKgW27S2ywndk8hzp536qfaJVnj91v/cYDwLA6EkF8qyEsKN DJgw== X-Gm-Message-State: AOAM530JOMUkr2KliMDGu6uN3YBXmQO/BtPeF7aIoURug8sBA4/Mm0Kv 5JRDgfQzXLN6tdiWZKofbyiuVA== X-Google-Smtp-Source: ABdhPJx0CM1TEGu51p1C5msi9hHtN1hUnNV3eudR0wG5wfspsTrj+ThH6YZZjwgkmwHfAr4hJQ12UQ== X-Received: by 2002:a05:600c:2202:: with SMTP id z2mr33274424wml.13.1593692399459; Thu, 02 Jul 2020 05:19:59 -0700 (PDT) Return-Path: Received: from [10.0.0.122] ([84.203.82.173]) by smtp.googlemail.com with ESMTPSA id w13sm10480268wrr.67.2020.07.02.05.19.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Jul 2020 05:19:58 -0700 (PDT) Subject: Re: [edk2-platforms][PATCH 1/1] Silicon/Broadcom/Bcm283x: GpioLib enhancements To: Ard Biesheuvel , devel@edk2.groups.io Cc: leif@nuviainc.com, awarkentin@vmware.com References: <20200629183926.12332-1-pete@akeo.ie> From: "Pete Batard" Message-ID: Date: Thu, 2 Jul 2020 13:19:57 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 8bit On 2020.07.02 13:03, Ard Biesheuvel wrote: > On 6/29/20 8:39 PM, Pete Batard wrote: >> * Add GpioPinSet () function to set a GPIO pin value >> * Add GpioPinGet () function to read a GPIO pin value >> * Add GpioSetPull () function to set pullup/down state of a GPIO pin >> >> GpioSetPull () supports both the legacy method used on Bcm283[5-7] >> as well as the new method used on Bcm2711. >> >> Each of these calls was tested on Raspberry Pi 3 B+ as well as on a >> Raspberry Pi 4 B. >> >> Signed-off-by: Pete Batard > > Remind me again what the purpose of this patch is? Feature completion, potential future improvements and providing additional options for testing (since one will now be able to use this driver to control LEDs or read switches). It doesn't serve any *immediate* purpose. However, I've been playing with it to simulate an SD card detect, which came handy for developing the other patch I sent, and I would assert having an established means to control GPIO signals is likely to help others as well. Regards, /Pete > >> --- >>   Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Gpio.h | 44 >> ++++++++- >>   Silicon/Broadcom/Bcm283x/Include/Library/GpioLib.h              | 17 >> ++++ >>   Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c              | 93 >> ++++++++++++++++++++ >>   Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.inf            |  1 + >>   4 files changed, 152 insertions(+), 3 deletions(-) >> >> diff --git >> a/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Gpio.h >> b/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Gpio.h >> index e65cc5c3bbb4..8ad81776b43a 100644 >> --- a/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Gpio.h >> +++ b/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836Gpio.h >> @@ -1,5 +1,6 @@ >>   /** @file >>    * >> + *  Copyright (c) 2020, Pete Batard >>    *  Copyright (c) 2018, Andrei Warkentin >>    *  Copyright (c) Microsoft Corporation. All rights reserved. >>    * >> @@ -23,12 +24,45 @@ >>   #define GPIO_GPFSEL4       (GPIO_BASE_ADDRESS + 0x10) >>   #define GPIO_GPFSEL5       (GPIO_BASE_ADDRESS + 0x14) >> -#define GPIO_GPCLR0        (GPIO_BASE_ADDRESS + 0x28) >> -#define GPIO_GPCLR1        (GPIO_BASE_ADDRESS + 0x2C) >> - >>   #define GPIO_GPSET0        (GPIO_BASE_ADDRESS + 0x1C) >>   #define GPIO_GPSET1        (GPIO_BASE_ADDRESS + 0x20) >> +#define GPIO_GPCLR0        (GPIO_BASE_ADDRESS + 0x28) >> +#define GPIO_GPCLR1        (GPIO_BASE_ADDRESS + 0x2C) >> + >> +#define GPIO_GPLEV0        (GPIO_BASE_ADDRESS + 0x34) >> +#define GPIO_GPLEV1        (GPIO_BASE_ADDRESS + 0x38) >> + >> +#define GPIO_GPEDS0        (GPIO_BASE_ADDRESS + 0x40) >> +#define GPIO_GPEDS1        (GPIO_BASE_ADDRESS + 0x44) >> + >> +#define GPIO_GPREN0        (GPIO_BASE_ADDRESS + 0x4C) >> +#define GPIO_GPREN1        (GPIO_BASE_ADDRESS + 0x50) >> + >> +#define GPIO_GPFEN0        (GPIO_BASE_ADDRESS + 0x58) >> +#define GPIO_GPFEN1        (GPIO_BASE_ADDRESS + 0x5C) >> + >> +#define GPIO_GPHEN0        (GPIO_BASE_ADDRESS + 0x64) >> +#define GPIO_GPHEN1        (GPIO_BASE_ADDRESS + 0x68) >> + >> +#define GPIO_GPLEN0        (GPIO_BASE_ADDRESS + 0x70) >> +#define GPIO_GPLEN1        (GPIO_BASE_ADDRESS + 0x74) >> + >> +#define GPIO_GPAREN0       (GPIO_BASE_ADDRESS + 0x7C) >> +#define GPIO_GPAREN1       (GPIO_BASE_ADDRESS + 0x80) >> + >> +#define GPIO_GPAFEN0       (GPIO_BASE_ADDRESS + 0x88) >> +#define GPIO_GPAFEN1       (GPIO_BASE_ADDRESS + 0x8C) >> + >> +#define GPIO_GPPUD         (GPIO_BASE_ADDRESS + 0x94) >> +#define GPIO_GPPUDCLK0     (GPIO_BASE_ADDRESS + 0x98) >> +#define GPIO_GPPUDCLK1     (GPIO_BASE_ADDRESS + 0x9C) >> + >> +#define GPIO_GPPUPPDN0     (GPIO_BASE_ADDRESS + 0xE4) >> +#define GPIO_GPPUPPDN1     (GPIO_BASE_ADDRESS + 0xE8) >> +#define GPIO_GPPUPPDN2     (GPIO_BASE_ADDRESS + 0xEC) >> +#define GPIO_GPPUPPDN3     (GPIO_BASE_ADDRESS + 0xF0) >> + >>   #define GPIO_FSEL_INPUT    0x0 >>   #define GPIO_FSEL_OUTPUT   0x1 >>   #define GPIO_FSEL_ALT0     0x4 >> @@ -44,4 +78,8 @@ >>   #define GPIO_PINS          54 >> +#define GPIO_PULL_NONE     0x00 >> +#define GPIO_PULL_DOWN     0x01 >> +#define GPIO_PULL_UP       0x02 >> + >>   #endif /* __BCM2836_GPIO_H__ */ >> diff --git a/Silicon/Broadcom/Bcm283x/Include/Library/GpioLib.h >> b/Silicon/Broadcom/Bcm283x/Include/Library/GpioLib.h >> index 014c6b07a29f..75c2c8be51aa 100644 >> --- a/Silicon/Broadcom/Bcm283x/Include/Library/GpioLib.h >> +++ b/Silicon/Broadcom/Bcm283x/Include/Library/GpioLib.h >> @@ -24,4 +24,21 @@ GpioPinFuncGet ( >>     IN  UINTN Pin >>     ); >> +VOID >> +GpioPinSet ( >> +  IN  UINTN Pin, >> +  IN  UINTN Val >> +  ); >> + >> +UINTN >> +GpioPinGet ( >> +  IN  UINTN Pin >> +  ); >> + >> +VOID >> +GpioSetPull ( >> +  IN  UINTN   Pin, >> +  IN  UINTN   Pud >> +); >> + >>   #endif /* __GPIO_LIB__ */ >> diff --git a/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c >> b/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c >> index 542b6e8f6b2f..a4b4af59ebb1 100644 >> --- a/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c >> +++ b/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.c >> @@ -2,6 +2,7 @@ >>    * >>    *  GPIO manipulation. >>    * >> + *  Copyright (c) 2020, Pete Batard >>    *  Copyright (c) 2018, Andrei Warkentin >>    * >>    *  SPDX-License-Identifier: BSD-2-Clause-Patent >> @@ -13,6 +14,7 @@ >>   #include >>   #include >>   #include >> +#include >>   #include >>   #include >> @@ -81,3 +83,94 @@ GpioPinFuncGet ( >>     Val &= GPIO_FSEL_MASK; >>     return Val; >>   } >> + >> +VOID >> +GpioPinSet ( >> +  IN  UINTN Pin, >> +  IN  UINTN Val >> +  ) >> +{ >> +  EFI_PHYSICAL_ADDRESS Reg; >> +  UINTN RegIndex; >> +  UINTN SelIndex; >> + >> +  ASSERT (Pin < GPIO_PINS); >> + >> +  RegIndex = Pin / 32; >> +  SelIndex = Pin % 32; >> + >> +  // >> +  // Different base addresses are used for clear and set >> +  // >> +  Reg = (Val == 0) ? GPIO_GPCLR0 : GPIO_GPSET0; >> +  Reg += RegIndex * sizeof (UINT32); >> +  MmioWrite32 (Reg, 1 << SelIndex); >> +} >> + >> +UINTN >> +GpioPinGet ( >> +  IN  UINTN Pin >> +  ) >> +{ >> +  EFI_PHYSICAL_ADDRESS Reg; >> +  UINTN RegIndex; >> +  UINTN SelIndex; >> +  UINT32 Val; >> + >> +  ASSERT (Pin < GPIO_PINS); >> + >> +  RegIndex = Pin / 32; >> +  SelIndex = Pin % 32; >> + >> +  Reg = RegIndex * sizeof (UINT32) + GPIO_GPLEV0; >> +  Val = MmioRead32 (Reg) >> SelIndex; >> +  return Val & 1; >> +} >> + >> +VOID >> +GpioSetPull ( >> +  IN  UINTN Pin, >> +  IN  UINTN Pud >> +) >> +{ >> +  EFI_PHYSICAL_ADDRESS Reg; >> +  UINTN RegIndex; >> +  UINTN SelIndex; >> +  UINT32 Val; >> + >> +  ASSERT (Pin < GPIO_PINS); >> +  ASSERT (Pud <= GPIO_PULL_UP); >> + >> +  // >> +  // Check if we should use the legacy or newer method of >> +  // enabling pullup/pulldown by querying GPPUPPDN3, which >> +  // is unused on Bcm2835/2836/2837 and returns 'gpio'. >> +  // >> +  if (MmioRead32 (GPIO_GPPUPPDN3) == 0x6770696f) { >> +    RegIndex = Pin / 32; >> +    SelIndex = Pin % 32; >> + >> +    MmioWrite32 (GPIO_GPPUD, Pud); >> +    MicroSecondDelay (5); >> + >> +    Reg = RegIndex * sizeof (UINT32) + GPIO_GPPUDCLK0; >> +    MmioWrite32 (Reg, 1 << SelIndex); >> +    MicroSecondDelay (5); >> + >> +    MmioWrite32 (GPIO_GPPUD, 0); >> +    MmioWrite32 (Reg, 0); >> +  } else { >> +    // >> +    // The newer method uses inverted values for up/down. >> +    // >> +    if (Pud != 0) >> +      Pud ^= 0x3; >> +    RegIndex = Pin / 16; >> +    SelIndex = (Pin % 16) * 2; >> +    Reg = RegIndex * sizeof (UINT32) + GPIO_GPPUPPDN0; >> +    Val = MmioRead32 (Reg); >> +    Val &= ~(0x3 << SelIndex); >> +    Val |= Pud << SelIndex; >> +    MmioWrite32 (Reg, Val); >> +  } >> +} >> diff --git a/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.inf >> b/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.inf >> index ff1b5af6db6e..a0356306d83c 100644 >> --- a/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.inf >> +++ b/Silicon/Broadcom/Bcm283x/Library/GpioLib/GpioLib.inf >> @@ -30,6 +30,7 @@ [LibraryClasses] >>     BaseLib >>     DebugLib >>     IoLib >> +  TimerLib >>   [FixedPcd] >>     gBcm283xTokenSpaceGuid.PcdBcm283xRegistersAddress >> >