From nobody Sun May 5 22:21:26 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; dkim=fail spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1493847501934357.3319896128871; Wed, 3 May 2017 14:38:21 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 7319121A134BB; Wed, 3 May 2017 14:38:18 -0700 (PDT) Received: from mail-wr0-x22d.google.com (mail-wr0-x22d.google.com [IPv6:2a00:1450:400c:c0c::22d]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 63C0921A13493 for ; Wed, 3 May 2017 14:38:16 -0700 (PDT) Received: by mail-wr0-x22d.google.com with SMTP id z52so1129274wrc.2 for ; Wed, 03 May 2017 14:38:16 -0700 (PDT) Received: from mohikan.hemma.eciton.net ([2a01:348:6:8735:e23f:49ff:fe6d:9d26]) by smtp.gmail.com with ESMTPSA id z14sm41380wmz.2.2017.05.03.14.38.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 May 2017 14:38:14 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=S6Eq/bwLbe0WhGOaz/wwdFeI2zS4e0y/85MM4sA72Ik=; b=ePjWPvqDHLKIv6Lld4ypqojK0iPqWvyozZlj/YeR/idDFeD8yigEY3KraMsMdQgE72 OzynAGzH7hW7wgdIFoWXthqH3L34Uq3llxnxpRnsD/dHKl+ldAG7pPOABxJNt3AWvsDv xby5g09kfkJCGpMUwH7mfEZLNGUL62i5o/PtE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=S6Eq/bwLbe0WhGOaz/wwdFeI2zS4e0y/85MM4sA72Ik=; b=FZSeK+CuYYRlsmLY6d4kDnK89lMLOvww3f+wGAfTmTJMX02uDGtRz/8s5xdfVvs0wN i56ls8xMGDi3ODSm5xJhxP0NyKbFPvMzgyLVGJsxtDR9FD9pfP/oVQDAlCFzSVIQZ3sL TdfhlGuDI4xCcZpCAV63Ns0zUfS98UnkxtsfsOTxh4sLnx3Oq6zTA9Yvv2ofhWNOaUTs BJhb5wghI7dAO1e8vnELeDlVk5WgsPsaWYYiogygPvqi0SgAJ+h6uJYyOqN1f2rQoAH7 o0+B1T8INrUqBLR61T6HoKNsSYLuKCaHkGvjvRQYo9XfJ+BLwmuFhUIf3DWQlqR4r7Ji vjWg== X-Gm-Message-State: AN3rC/6aOu5vVvNttjEN+Z5j504iGYeh384QgOFH8i7uDgpofHzs/i7Q 1QugHlDteKmXnmZX X-Received: by 10.223.130.104 with SMTP id 95mr25671738wrb.150.1493847494622; Wed, 03 May 2017 14:38:14 -0700 (PDT) From: Leif Lindholm To: edk2-devel@lists.01.org Date: Wed, 3 May 2017 22:38:10 +0100 Message-Id: <20170503213812.5971-2-leif.lindholm@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170503213812.5971-1-leif.lindholm@linaro.org> References: <20170503213812.5971-1-leif.lindholm@linaro.org> Subject: [edk2] [PATCH 1/3] EmbeddedPkg: import DesignWare EMMC driver X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ard Biesheuvel MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Imported from OpenPlatformPkg 0434ff62e3eb896e6c561dea84cfb8b80391603e. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Leif Lindholm --- EmbeddedPkg/Drivers/DwEmmcDxe/DwEmmc.h | 127 ++++++ EmbeddedPkg/Drivers/DwEmmcDxe/DwEmmcDxe.c | 648 ++++++++++++++++++++++++= ++++ EmbeddedPkg/Drivers/DwEmmcDxe/DwEmmcDxe.inf | 53 +++ EmbeddedPkg/EmbeddedPkg.dec | 4 + 4 files changed, 832 insertions(+) create mode 100644 EmbeddedPkg/Drivers/DwEmmcDxe/DwEmmc.h create mode 100644 EmbeddedPkg/Drivers/DwEmmcDxe/DwEmmcDxe.c create mode 100644 EmbeddedPkg/Drivers/DwEmmcDxe/DwEmmcDxe.inf diff --git a/EmbeddedPkg/Drivers/DwEmmcDxe/DwEmmc.h b/EmbeddedPkg/Drivers/D= wEmmcDxe/DwEmmc.h new file mode 100644 index 0000000000..055f1e05d1 --- /dev/null +++ b/EmbeddedPkg/Drivers/DwEmmcDxe/DwEmmc.h @@ -0,0 +1,127 @@ +/** @file +* +* Copyright (c) 2014-2017, Linaro Limited. All rights reserved. +* +* This program and the accompanying materials +* are licensed and made available under the terms and conditions of the B= SD License +* which accompanies this distribution. The full text of the license may = be found at +* http://opensource.org/licenses/bsd-license.php +* +* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IM= PLIED. +* +**/ + + +#ifndef __DWEMMC_H__ +#define __DWEMMC_H__ + +#include + +// DW MMC Registers +#define DWEMMC_CTRL ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x000) +#define DWEMMC_PWREN ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x004) +#define DWEMMC_CLKDIV ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x008) +#define DWEMMC_CLKSRC ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x00c) +#define DWEMMC_CLKENA ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x010) +#define DWEMMC_TMOUT ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x014) +#define DWEMMC_CTYPE ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x018) +#define DWEMMC_BLKSIZ ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x01c) +#define DWEMMC_BYTCNT ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x020) +#define DWEMMC_INTMASK ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x024) +#define DWEMMC_CMDARG ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x028) +#define DWEMMC_CMD ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x02c) +#define DWEMMC_RESP0 ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x030) +#define DWEMMC_RESP1 ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x034) +#define DWEMMC_RESP2 ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x038) +#define DWEMMC_RESP3 ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x03c) +#define DWEMMC_RINTSTS ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x044) +#define DWEMMC_STATUS ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x048) +#define DWEMMC_FIFOTH ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x04c) +#define DWEMMC_DEBNCE ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x064) +#define DWEMMC_UHSREG ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x074) +#define DWEMMC_BMOD ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x080) +#define DWEMMC_DBADDR ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x088) +#define DWEMMC_IDSTS ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x08c) +#define DWEMMC_IDINTEN ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x090) +#define DWEMMC_DSCADDR ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x094) +#define DWEMMC_BUFADDR ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0x098) +#define DWEMMC_CARDTHRCTL ((UINT32)PcdGet32 (PcdDwEmmcDxeBaseAddress= ) + 0X100) + +#define CMD_UPDATE_CLK 0x80202000 +#define CMD_START_BIT (1 << 31) + +#define MMC_8BIT_MODE (1 << 16) + +#define BIT_CMD_RESPONSE_EXPECT (1 << 6) +#define BIT_CMD_LONG_RESPONSE (1 << 7) +#define BIT_CMD_CHECK_RESPONSE_CRC (1 << 8) +#define BIT_CMD_DATA_EXPECTED (1 << 9) +#define BIT_CMD_READ (0 << 10) +#define BIT_CMD_WRITE (1 << 10) +#define BIT_CMD_BLOCK_TRANSFER (0 << 11) +#define BIT_CMD_STREAM_TRANSFER (1 << 11) +#define BIT_CMD_SEND_AUTO_STOP (1 << 12) +#define BIT_CMD_WAIT_PRVDATA_COMPLETE (1 << 13) +#define BIT_CMD_STOP_ABORT_CMD (1 << 14) +#define BIT_CMD_SEND_INIT (1 << 15) +#define BIT_CMD_UPDATE_CLOCK_ONLY (1 << 21) +#define BIT_CMD_READ_CEATA_DEVICE (1 << 22) +#define BIT_CMD_CCS_EXPECTED (1 << 23) +#define BIT_CMD_ENABLE_BOOT (1 << 24) +#define BIT_CMD_EXPECT_BOOT_ACK (1 << 25) +#define BIT_CMD_DISABLE_BOOT (1 << 26) +#define BIT_CMD_MANDATORY_BOOT (0 << 27) +#define BIT_CMD_ALTERNATE_BOOT (1 << 27) +#define BIT_CMD_VOLT_SWITCH (1 << 28) +#define BIT_CMD_USE_HOLD_REG (1 << 29) +#define BIT_CMD_START (1 << 31) + +#define DWEMMC_INT_EBE (1 << 15) /* End-bit= Err */ +#define DWEMMC_INT_SBE (1 << 13) /* Start-b= it Err */ +#define DWEMMC_INT_HLE (1 << 12) /* Hardwar= e-lock Err */ +#define DWEMMC_INT_FRUN (1 << 11) /* FIFO UN= /OV RUN */ +#define DWEMMC_INT_DRT (1 << 9) /* Data ti= meout */ +#define DWEMMC_INT_RTO (1 << 8) /* Respons= e timeout */ +#define DWEMMC_INT_DCRC (1 << 7) /* Data CR= C err */ +#define DWEMMC_INT_RCRC (1 << 6) /* Respons= e CRC err */ +#define DWEMMC_INT_RXDR (1 << 5) +#define DWEMMC_INT_TXDR (1 << 4) +#define DWEMMC_INT_DTO (1 << 3) /* Data tr= ans over */ +#define DWEMMC_INT_CMD_DONE (1 << 2) +#define DWEMMC_INT_RE (1 << 1) + +#define DWEMMC_IDMAC_DES0_DIC (1 << 1) +#define DWEMMC_IDMAC_DES0_LD (1 << 2) +#define DWEMMC_IDMAC_DES0_FS (1 << 3) +#define DWEMMC_IDMAC_DES0_CH (1 << 4) +#define DWEMMC_IDMAC_DES0_ER (1 << 5) +#define DWEMMC_IDMAC_DES0_CES (1 << 30) +#define DWEMMC_IDMAC_DES0_OWN (1 << 31) +#define DWEMMC_IDMAC_DES1_BS1(x) ((x) & 0x1fff) +#define DWEMMC_IDMAC_DES2_BS2(x) (((x) & 0x1fff) << 13) +#define DWEMMC_IDMAC_SWRESET (1 << 0) +#define DWEMMC_IDMAC_FB (1 << 1) +#define DWEMMC_IDMAC_ENABLE (1 << 7) + +#define EMMC_FIX_RCA 6 + +/* bits in MMC0_CTRL */ +#define DWEMMC_CTRL_RESET (1 << 0) +#define DWEMMC_CTRL_FIFO_RESET (1 << 1) +#define DWEMMC_CTRL_DMA_RESET (1 << 2) +#define DWEMMC_CTRL_INT_EN (1 << 4) +#define DWEMMC_CTRL_DMA_EN (1 << 5) +#define DWEMMC_CTRL_IDMAC_EN (1 << 25) +#define DWEMMC_CTRL_RESET_ALL (DWEMMC_CTRL_RESET | DWEMM= C_CTRL_FIFO_RESET | DWEMMC_CTRL_DMA_RESET) + +#define DWEMMC_STS_DATA_BUSY (1 << 9) + +#define DWEMMC_FIFO_TWMARK(x) (x & 0xfff) +#define DWEMMC_FIFO_RWMARK(x) ((x & 0x1ff) << 16) +#define DWEMMC_DMA_BURST_SIZE(x) ((x & 0x7) << 28) + +#define DWEMMC_CARD_RD_THR(x) ((x & 0xfff) << 16) +#define DWEMMC_CARD_RD_THR_EN (1 << 0) + +#endif // __DWEMMC_H__ diff --git a/EmbeddedPkg/Drivers/DwEmmcDxe/DwEmmcDxe.c b/EmbeddedPkg/Driver= s/DwEmmcDxe/DwEmmcDxe.c new file mode 100644 index 0000000000..fe23d113ee --- /dev/null +++ b/EmbeddedPkg/Drivers/DwEmmcDxe/DwEmmcDxe.c @@ -0,0 +1,648 @@ +/** @file + This file implement the MMC Host Protocol for the DesignWare eMMC. + + Copyright (c) 2014-2017, Linaro Limited. All rights reserved. + + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP= LIED. + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "DwEmmc.h" + +#define DWEMMC_DESC_PAGE 1 +#define DWEMMC_BLOCK_SIZE 512 +#define DWEMMC_DMA_BUF_SIZE (512 * 8) +#define DWEMMC_MAX_DESC_PAGES 512 + +typedef struct { + UINT32 Des0; + UINT32 Des1; + UINT32 Des2; + UINT32 Des3; +} DWEMMC_IDMAC_DESCRIPTOR; + +EFI_MMC_HOST_PROTOCOL *gpMmcHost; +DWEMMC_IDMAC_DESCRIPTOR *gpIdmacDesc; +EFI_GUID mDwEmmcDevicePathGuid =3D EFI_CALLER_ID_GUID; +STATIC UINT32 mDwEmmcCommand; +STATIC UINT32 mDwEmmcArgument; + +EFI_STATUS +DwEmmcReadBlockData ( + IN EFI_MMC_HOST_PROTOCOL *This, + IN EFI_LBA Lba, + IN UINTN Length, + IN UINT32* Buffer + ); + +BOOLEAN +DwEmmcIsPowerOn ( + VOID + ) +{ + return TRUE; +} + +EFI_STATUS +DwEmmcInitialize ( + VOID + ) +{ + DEBUG ((DEBUG_BLKIO, "DwEmmcInitialize()")); + return EFI_SUCCESS; +} + +BOOLEAN +DwEmmcIsCardPresent ( + IN EFI_MMC_HOST_PROTOCOL *This + ) +{ + return TRUE; +} + +BOOLEAN +DwEmmcIsReadOnly ( + IN EFI_MMC_HOST_PROTOCOL *This + ) +{ + return FALSE; +} + +BOOLEAN +DwEmmcIsDmaSupported ( + IN EFI_MMC_HOST_PROTOCOL *This + ) +{ + return TRUE; +} + +EFI_STATUS +DwEmmcBuildDevicePath ( + IN EFI_MMC_HOST_PROTOCOL *This, + IN EFI_DEVICE_PATH_PROTOCOL **DevicePath + ) +{ + EFI_DEVICE_PATH_PROTOCOL *NewDevicePathNode; + + NewDevicePathNode =3D CreateDeviceNode (HARDWARE_DEVICE_PATH, HW_VENDOR_= DP, sizeof (VENDOR_DEVICE_PATH)); + CopyGuid (& ((VENDOR_DEVICE_PATH*)NewDevicePathNode)->Guid, &mDwEmmcDevi= cePathGuid); + + *DevicePath =3D NewDevicePathNode; + return EFI_SUCCESS; +} + +EFI_STATUS +DwEmmcUpdateClock ( + VOID + ) +{ + UINT32 Data; + + /* CMD_UPDATE_CLK */ + Data =3D BIT_CMD_WAIT_PRVDATA_COMPLETE | BIT_CMD_UPDATE_CLOCK_ONLY | + BIT_CMD_START; + MmioWrite32 (DWEMMC_CMD, Data); + while (1) { + Data =3D MmioRead32 (DWEMMC_CMD); + if (!(Data & CMD_START_BIT)) { + break; + } + Data =3D MmioRead32 (DWEMMC_RINTSTS); + if (Data & DWEMMC_INT_HLE) { + Print (L"failed to update mmc clock frequency\n"); + return EFI_DEVICE_ERROR; + } + } + return EFI_SUCCESS; +} + +EFI_STATUS +DwEmmcSetClock ( + IN UINTN ClockFreq + ) +{ + UINT32 Divider, Rate, Data; + EFI_STATUS Status; + BOOLEAN Found =3D FALSE; + + for (Divider =3D 1; Divider < 256; Divider++) { + Rate =3D PcdGet32 (PcdDwEmmcDxeClockFrequencyInHz); + if ((Rate / (2 * Divider)) <=3D ClockFreq) { + Found =3D TRUE; + break; + } + } + if (Found =3D=3D FALSE) { + return EFI_NOT_FOUND; + } + + // Wait until MMC is idle + do { + Data =3D MmioRead32 (DWEMMC_STATUS); + } while (Data & DWEMMC_STS_DATA_BUSY); + + // Disable MMC clock first + MmioWrite32 (DWEMMC_CLKENA, 0); + Status =3D DwEmmcUpdateClock (); + ASSERT (!EFI_ERROR (Status)); + + MmioWrite32 (DWEMMC_CLKDIV, Divider); + Status =3D DwEmmcUpdateClock (); + ASSERT (!EFI_ERROR (Status)); + + // Enable MMC clock + MmioWrite32 (DWEMMC_CLKENA, 1); + MmioWrite32 (DWEMMC_CLKSRC, 0); + Status =3D DwEmmcUpdateClock (); + ASSERT (!EFI_ERROR (Status)); + return EFI_SUCCESS; +} + +EFI_STATUS +DwEmmcNotifyState ( + IN EFI_MMC_HOST_PROTOCOL *This, + IN MMC_STATE State + ) +{ + UINT32 Data; + EFI_STATUS Status; + + switch (State) { + case MmcInvalidState: + return EFI_INVALID_PARAMETER; + case MmcHwInitializationState: + MmioWrite32 (DWEMMC_PWREN, 1); + + // If device already turn on then restart it + Data =3D DWEMMC_CTRL_RESET_ALL; + MmioWrite32 (DWEMMC_CTRL, Data); + do { + // Wait until reset operation finished + Data =3D MmioRead32 (DWEMMC_CTRL); + } while (Data & DWEMMC_CTRL_RESET_ALL); + + // Setup clock that could not be higher than 400KHz. + Status =3D DwEmmcSetClock (400000); + ASSERT (!EFI_ERROR (Status)); + // Wait clock stable + MicroSecondDelay (100); + + MmioWrite32 (DWEMMC_RINTSTS, ~0); + MmioWrite32 (DWEMMC_INTMASK, 0); + MmioWrite32 (DWEMMC_TMOUT, ~0); + MmioWrite32 (DWEMMC_IDINTEN, 0); + MmioWrite32 (DWEMMC_BMOD, DWEMMC_IDMAC_SWRESET); + + MmioWrite32 (DWEMMC_BLKSIZ, DWEMMC_BLOCK_SIZE); + do { + Data =3D MmioRead32 (DWEMMC_BMOD); + } while (Data & DWEMMC_IDMAC_SWRESET); + break; + case MmcIdleState: + break; + case MmcReadyState: + break; + case MmcIdentificationState: + break; + case MmcStandByState: + break; + case MmcTransferState: + break; + case MmcSendingDataState: + break; + case MmcReceiveDataState: + break; + case MmcProgrammingState: + break; + case MmcDisconnectState: + break; + default: + return EFI_INVALID_PARAMETER; + } + return EFI_SUCCESS; +} + +// Need to prepare DMA buffer first before sending commands to MMC card +BOOLEAN +IsPendingReadCommand ( + IN MMC_CMD MmcCmd + ) +{ + UINTN Mask; + + Mask =3D BIT_CMD_DATA_EXPECTED | BIT_CMD_READ; + if ((MmcCmd & Mask) =3D=3D Mask) { + return TRUE; + } + return FALSE; +} + +BOOLEAN +IsPendingWriteCommand ( + IN MMC_CMD MmcCmd + ) +{ + UINTN Mask; + + Mask =3D BIT_CMD_DATA_EXPECTED | BIT_CMD_WRITE; + if ((MmcCmd & Mask) =3D=3D Mask) { + return TRUE; + } + return FALSE; +} + +EFI_STATUS +SendCommand ( + IN MMC_CMD MmcCmd, + IN UINT32 Argument + ) +{ + UINT32 Data, ErrMask; + + // Wait until MMC is idle + do { + Data =3D MmioRead32 (DWEMMC_STATUS); + } while (Data & DWEMMC_STS_DATA_BUSY); + + MmioWrite32 (DWEMMC_RINTSTS, ~0); + MmioWrite32 (DWEMMC_CMDARG, Argument); + MmioWrite32 (DWEMMC_CMD, MmcCmd); + + ErrMask =3D DWEMMC_INT_EBE | DWEMMC_INT_HLE | DWEMMC_INT_RTO | + DWEMMC_INT_RCRC | DWEMMC_INT_RE; + ErrMask |=3D DWEMMC_INT_DCRC | DWEMMC_INT_DRT | DWEMMC_INT_SBE; + do { + MicroSecondDelay(500); + Data =3D MmioRead32 (DWEMMC_RINTSTS); + + if (Data & ErrMask) { + return EFI_DEVICE_ERROR; + } + if (Data & DWEMMC_INT_DTO) { // Transfer Done + break; + } + } while (!(Data & DWEMMC_INT_CMD_DONE)); + return EFI_SUCCESS; +} + +EFI_STATUS +DwEmmcSendCommand ( + IN EFI_MMC_HOST_PROTOCOL *This, + IN MMC_CMD MmcCmd, + IN UINT32 Argument + ) +{ + UINT32 Cmd =3D 0; + EFI_STATUS Status =3D EFI_SUCCESS; + + switch (MMC_GET_INDX(MmcCmd)) { + case MMC_INDX(0): + Cmd =3D BIT_CMD_SEND_INIT; + break; + case MMC_INDX(1): + Cmd =3D BIT_CMD_RESPONSE_EXPECT; + break; + case MMC_INDX(2): + Cmd =3D BIT_CMD_RESPONSE_EXPECT | BIT_CMD_LONG_RESPONSE | + BIT_CMD_CHECK_RESPONSE_CRC | BIT_CMD_SEND_INIT; + break; + case MMC_INDX(3): + Cmd =3D BIT_CMD_RESPONSE_EXPECT | BIT_CMD_CHECK_RESPONSE_CRC | + BIT_CMD_SEND_INIT; + break; + case MMC_INDX(7): + if (Argument) + Cmd =3D BIT_CMD_RESPONSE_EXPECT | BIT_CMD_CHECK_RESPONSE_CRC; + else + Cmd =3D 0; + break; + case MMC_INDX(8): + Cmd =3D BIT_CMD_RESPONSE_EXPECT | BIT_CMD_CHECK_RESPONSE_CRC | + BIT_CMD_DATA_EXPECTED | BIT_CMD_READ | + BIT_CMD_WAIT_PRVDATA_COMPLETE; + break; + case MMC_INDX(9): + Cmd =3D BIT_CMD_RESPONSE_EXPECT | BIT_CMD_CHECK_RESPONSE_CRC | + BIT_CMD_LONG_RESPONSE; + break; + case MMC_INDX(12): + Cmd =3D BIT_CMD_RESPONSE_EXPECT | BIT_CMD_CHECK_RESPONSE_CRC | + BIT_CMD_STOP_ABORT_CMD; + break; + case MMC_INDX(13): + Cmd =3D BIT_CMD_RESPONSE_EXPECT | BIT_CMD_CHECK_RESPONSE_CRC | + BIT_CMD_WAIT_PRVDATA_COMPLETE; + break; + case MMC_INDX(16): + Cmd =3D BIT_CMD_RESPONSE_EXPECT | BIT_CMD_CHECK_RESPONSE_CRC | + BIT_CMD_DATA_EXPECTED | BIT_CMD_READ | + BIT_CMD_WAIT_PRVDATA_COMPLETE; + break; + case MMC_INDX(17): + case MMC_INDX(18): + Cmd =3D BIT_CMD_RESPONSE_EXPECT | BIT_CMD_CHECK_RESPONSE_CRC | + BIT_CMD_DATA_EXPECTED | BIT_CMD_READ | + BIT_CMD_WAIT_PRVDATA_COMPLETE; + break; + case MMC_INDX(24): + case MMC_INDX(25): + Cmd =3D BIT_CMD_RESPONSE_EXPECT | BIT_CMD_CHECK_RESPONSE_CRC | + BIT_CMD_DATA_EXPECTED | BIT_CMD_WRITE | + BIT_CMD_WAIT_PRVDATA_COMPLETE; + break; + case MMC_INDX(30): + Cmd =3D BIT_CMD_RESPONSE_EXPECT | BIT_CMD_CHECK_RESPONSE_CRC | + BIT_CMD_DATA_EXPECTED; + break; + default: + Cmd =3D BIT_CMD_RESPONSE_EXPECT | BIT_CMD_CHECK_RESPONSE_CRC; + break; + } + + Cmd |=3D MMC_GET_INDX(MmcCmd) | BIT_CMD_USE_HOLD_REG | BIT_CMD_START; + if (IsPendingReadCommand (Cmd) || IsPendingWriteCommand (Cmd)) { + mDwEmmcCommand =3D Cmd; + mDwEmmcArgument =3D Argument; + } else { + Status =3D SendCommand (Cmd, Argument); + } + return Status; +} + +EFI_STATUS +DwEmmcReceiveResponse ( + IN EFI_MMC_HOST_PROTOCOL *This, + IN MMC_RESPONSE_TYPE Type, + IN UINT32* Buffer + ) +{ + if (Buffer =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + if ( (Type =3D=3D MMC_RESPONSE_TYPE_R1) + || (Type =3D=3D MMC_RESPONSE_TYPE_R1b) + || (Type =3D=3D MMC_RESPONSE_TYPE_R3) + || (Type =3D=3D MMC_RESPONSE_TYPE_R6) + || (Type =3D=3D MMC_RESPONSE_TYPE_R7)) + { + Buffer[0] =3D MmioRead32 (DWEMMC_RESP0); + } else if (Type =3D=3D MMC_RESPONSE_TYPE_R2) { + Buffer[0] =3D MmioRead32 (DWEMMC_RESP0); + Buffer[1] =3D MmioRead32 (DWEMMC_RESP1); + Buffer[2] =3D MmioRead32 (DWEMMC_RESP2); + Buffer[3] =3D MmioRead32 (DWEMMC_RESP3); + } + return EFI_SUCCESS; +} + +EFI_STATUS +PrepareDmaData ( + IN DWEMMC_IDMAC_DESCRIPTOR* IdmacDesc, + IN UINTN Length, + IN UINT32* Buffer + ) +{ + UINTN Cnt, Blks, Idx, LastIdx; + + Cnt =3D (Length + DWEMMC_DMA_BUF_SIZE - 1) / DWEMMC_DMA_BUF_SIZE; + Blks =3D (Length + DWEMMC_BLOCK_SIZE - 1) / DWEMMC_BLOCK_SIZE; + Length =3D DWEMMC_BLOCK_SIZE * Blks; + + for (Idx =3D 0; Idx < Cnt; Idx++) { + (IdmacDesc + Idx)->Des0 =3D DWEMMC_IDMAC_DES0_OWN | DWEMMC_IDMAC_DES0_= CH | + DWEMMC_IDMAC_DES0_DIC; + (IdmacDesc + Idx)->Des1 =3D DWEMMC_IDMAC_DES1_BS1(DWEMMC_DMA_BUF_SIZE); + /* Buffer Address */ + (IdmacDesc + Idx)->Des2 =3D (UINT32)((UINTN)Buffer + DWEMMC_DMA_BUF_SI= ZE * Idx); + /* Next Descriptor Address */ + (IdmacDesc + Idx)->Des3 =3D (UINT32)((UINTN)IdmacDesc + + (sizeof(DWEMMC_IDMAC_DESCRIPTOR) * = (Idx + 1))); + } + /* First Descriptor */ + IdmacDesc->Des0 |=3D DWEMMC_IDMAC_DES0_FS; + /* Last Descriptor */ + LastIdx =3D Cnt - 1; + (IdmacDesc + LastIdx)->Des0 |=3D DWEMMC_IDMAC_DES0_LD; + (IdmacDesc + LastIdx)->Des0 &=3D ~(DWEMMC_IDMAC_DES0_DIC | DWEMMC_IDMAC_= DES0_CH); + (IdmacDesc + LastIdx)->Des1 =3D DWEMMC_IDMAC_DES1_BS1(Length - + (LastIdx * DWEMMC_DM= A_BUF_SIZE)); + /* Set the Next field of Last Descriptor */ + (IdmacDesc + LastIdx)->Des3 =3D 0; + MmioWrite32 (DWEMMC_DBADDR, (UINT32)((UINTN)IdmacDesc)); + + return EFI_SUCCESS; +} + +VOID +StartDma ( + UINTN Length + ) +{ + UINT32 Data; + + Data =3D MmioRead32 (DWEMMC_CTRL); + Data |=3D DWEMMC_CTRL_INT_EN | DWEMMC_CTRL_DMA_EN | DWEMMC_CTRL_IDMAC_EN; + MmioWrite32 (DWEMMC_CTRL, Data); + Data =3D MmioRead32 (DWEMMC_BMOD); + Data |=3D DWEMMC_IDMAC_ENABLE | DWEMMC_IDMAC_FB; + MmioWrite32 (DWEMMC_BMOD, Data); + + MmioWrite32 (DWEMMC_BLKSIZ, DWEMMC_BLOCK_SIZE); + MmioWrite32 (DWEMMC_BYTCNT, Length); +} + +EFI_STATUS +DwEmmcReadBlockData ( + IN EFI_MMC_HOST_PROTOCOL *This, + IN EFI_LBA Lba, + IN UINTN Length, + IN UINT32* Buffer + ) +{ + EFI_STATUS Status; + UINT32 DescPages, CountPerPage, Count; + EFI_TPL Tpl; + + Tpl =3D gBS->RaiseTPL (TPL_NOTIFY); + + CountPerPage =3D EFI_PAGE_SIZE / 16; + Count =3D (Length + DWEMMC_DMA_BUF_SIZE - 1) / DWEMMC_DMA_BUF_SIZE; + DescPages =3D (Count + CountPerPage - 1) / CountPerPage; + + InvalidateDataCacheRange (Buffer, Length); + + Status =3D PrepareDmaData (gpIdmacDesc, Length, Buffer); + if (EFI_ERROR (Status)) { + goto out; + } + + WriteBackDataCacheRange (gpIdmacDesc, DescPages * EFI_PAGE_SIZE); + StartDma (Length); + + Status =3D SendCommand (mDwEmmcCommand, mDwEmmcArgument); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed to read data, mDwEmmcCommand:%x, mDwEmmcA= rgument:%x, Status:%r\n", mDwEmmcCommand, mDwEmmcArgument, Status)); + goto out; + } +out: + // Restore Tpl + gBS->RestoreTPL (Tpl); + return Status; +} + +EFI_STATUS +DwEmmcWriteBlockData ( + IN EFI_MMC_HOST_PROTOCOL *This, + IN EFI_LBA Lba, + IN UINTN Length, + IN UINT32* Buffer + ) +{ + EFI_STATUS Status; + UINT32 DescPages, CountPerPage, Count; + EFI_TPL Tpl; + + Tpl =3D gBS->RaiseTPL (TPL_NOTIFY); + + CountPerPage =3D EFI_PAGE_SIZE / 16; + Count =3D (Length + DWEMMC_DMA_BUF_SIZE - 1) / DWEMMC_DMA_BUF_SIZE; + DescPages =3D (Count + CountPerPage - 1) / CountPerPage; + + WriteBackDataCacheRange (Buffer, Length); + + Status =3D PrepareDmaData (gpIdmacDesc, Length, Buffer); + if (EFI_ERROR (Status)) { + goto out; + } + + WriteBackDataCacheRange (gpIdmacDesc, DescPages * EFI_PAGE_SIZE); + StartDma (Length); + + Status =3D SendCommand (mDwEmmcCommand, mDwEmmcArgument); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed to write data, mDwEmmcCommand:%x, mDwEmmc= Argument:%x, Status:%r\n", mDwEmmcCommand, mDwEmmcArgument, Status)); + goto out; + } +out: + // Restore Tpl + gBS->RestoreTPL (Tpl); + return Status; +} + +EFI_STATUS +DwEmmcSetIos ( + IN EFI_MMC_HOST_PROTOCOL *This, + IN UINT32 BusClockFreq, + IN UINT32 BusWidth, + IN UINT32 TimingMode + ) +{ + EFI_STATUS Status =3D EFI_SUCCESS; + UINT32 Data; + + if (TimingMode !=3D EMMCBACKWARD) { + Data =3D MmioRead32 (DWEMMC_UHSREG); + switch (TimingMode) { + case EMMCHS52DDR1V2: + case EMMCHS52DDR1V8: + Data |=3D 1 << 16; + break; + case EMMCHS52: + case EMMCHS26: + Data &=3D ~(1 << 16); + break; + default: + return EFI_UNSUPPORTED; + } + MmioWrite32 (DWEMMC_UHSREG, Data); + } + + switch (BusWidth) { + case 1: + MmioWrite32 (DWEMMC_CTYPE, 0); + break; + case 4: + MmioWrite32 (DWEMMC_CTYPE, 1); + break; + case 8: + MmioWrite32 (DWEMMC_CTYPE, 1 << 16); + break; + default: + return EFI_UNSUPPORTED; + } + if (BusClockFreq) { + Status =3D DwEmmcSetClock (BusClockFreq); + } + return Status; +} + +BOOLEAN +DwEmmcIsMultiBlock ( + IN EFI_MMC_HOST_PROTOCOL *This + ) +{ + return TRUE; +} + +EFI_MMC_HOST_PROTOCOL gMciHost =3D { + MMC_HOST_PROTOCOL_REVISION, + DwEmmcIsCardPresent, + DwEmmcIsReadOnly, + DwEmmcBuildDevicePath, + DwEmmcNotifyState, + DwEmmcSendCommand, + DwEmmcReceiveResponse, + DwEmmcReadBlockData, + DwEmmcWriteBlockData, + DwEmmcSetIos, + DwEmmcIsMultiBlock +}; + +EFI_STATUS +DwEmmcDxeInitialize ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + EFI_HANDLE Handle; + + Handle =3D NULL; + + gpIdmacDesc =3D (DWEMMC_IDMAC_DESCRIPTOR *)AllocatePages (DWEMMC_MAX_DES= C_PAGES); + if (gpIdmacDesc =3D=3D NULL) { + return EFI_BUFFER_TOO_SMALL; + } + + DEBUG ((DEBUG_BLKIO, "DwEmmcDxeInitialize()\n")); + + //Publish Component Name, BlockIO protocol interfaces + Status =3D gBS->InstallMultipleProtocolInterfaces ( + &Handle, + &gEfiMmcHostProtocolGuid, &gMciHost, + NULL + ); + ASSERT_EFI_ERROR (Status); + + return EFI_SUCCESS; +} diff --git a/EmbeddedPkg/Drivers/DwEmmcDxe/DwEmmcDxe.inf b/EmbeddedPkg/Driv= ers/DwEmmcDxe/DwEmmcDxe.inf new file mode 100644 index 0000000000..e3c8313bc5 --- /dev/null +++ b/EmbeddedPkg/Drivers/DwEmmcDxe/DwEmmcDxe.inf @@ -0,0 +1,53 @@ +#/** @file +# INF file for the eMMC Host Protocol implementation for the DesignWare M= MC. +# +# Copyright (c) 2014-2017, Linaro Limited. All rights reserved. +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the B= SD License +# which accompanies this distribution. The full text of the license may = be found at +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IM= PLIED. +# +#**/ + +[Defines] + INF_VERSION =3D 0x00010019 + BASE_NAME =3D DwEmmcDxe + FILE_GUID =3D b549f005-4bd4-4020-a0cb-06f42bda68c3 + MODULE_TYPE =3D DXE_DRIVER + VERSION_STRING =3D 1.0 + + ENTRY_POINT =3D DwEmmcDxeInitialize + +[Sources.common] + DwEmmcDxe.c + +[Packages] + EmbeddedPkg/EmbeddedPkg.dec + MdePkg/MdePkg.dec + +[LibraryClasses] + ArmLib + BaseLib + BaseMemoryLib + CacheMaintenanceLib + IoLib + MemoryAllocationLib + TimerLib + UefiDriverEntryPoint + UefiLib + +[Protocols] + gEfiCpuArchProtocolGuid + gEfiDevicePathProtocolGuid + gEfiMmcHostProtocolGuid + +[Pcd] + gEmbeddedTokenSpaceGuid.PcdDwEmmcDxeBaseAddress + gEmbeddedTokenSpaceGuid.PcdDwEmmcDxeClockFrequencyInHz + +[Depex] + TRUE diff --git a/EmbeddedPkg/EmbeddedPkg.dec b/EmbeddedPkg/EmbeddedPkg.dec index e90f039c28..0d4a0625a3 100644 --- a/EmbeddedPkg/EmbeddedPkg.dec +++ b/EmbeddedPkg/EmbeddedPkg.dec @@ -164,6 +164,10 @@ # LAN91x Ethernet Driver PCDs gEmbeddedTokenSpaceGuid.PcdLan91xDxeBaseAddress|0x0|UINT32|0x00000029 =20 + # DwEmmc Driver PCDs + gEmbeddedTokenSpaceGuid.PcdDwEmmcDxeBaseAddress|0x0|UINT32|0x00000035 + gEmbeddedTokenSpaceGuid.PcdDwEmmcDxeClockFrequencyInHz|0x0|UINT32|0x0000= 0036 + # # Android FastBoot # --=20 2.11.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Sun May 5 22:21:26 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; dkim=fail spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1493847505025447.7788243584945; Wed, 3 May 2017 14:38:25 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id A1AF421955D8D; Wed, 3 May 2017 14:38:18 -0700 (PDT) Received: from mail-wr0-x22b.google.com (mail-wr0-x22b.google.com [IPv6:2a00:1450:400c:c0c::22b]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id EAD8521A13482 for ; Wed, 3 May 2017 14:38:16 -0700 (PDT) Received: by mail-wr0-x22b.google.com with SMTP id w50so1153851wrc.0 for ; Wed, 03 May 2017 14:38:16 -0700 (PDT) Received: from mohikan.hemma.eciton.net ([2a01:348:6:8735:e23f:49ff:fe6d:9d26]) by smtp.gmail.com with ESMTPSA id z14sm41380wmz.2.2017.05.03.14.38.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 May 2017 14:38:14 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=PhnczJxqONsNCfE7D4U/JJ8JkA6VXqK++U0Uc86qAN4=; b=XERZaJMgyD/7Ii9XwO3o8hCBX7Hk+wzWugu5P3veetMf+1UWJvIs9hGu0Ti99YIihf Q8gWpm6hPa/05/3FPDAHUQA9/+HQC8LNh3A+zJ7h8JQ5GDyl7PN85EPUe0XqTLZsGptu RuWCAlNUFOIJUZNp3dIpXiXN12nnExgAeu74Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=PhnczJxqONsNCfE7D4U/JJ8JkA6VXqK++U0Uc86qAN4=; b=gBywLx+9mT/yllV4zOR+WgcazSrdloMaIKjjG15e+2ynys2uAYvKq46qmMkyIkXYdX mokZzhLB469M4N4qwCpUlyW+ltLo06eHkDTO2aXqat6bcqYqvCQoBCi8yHb/xZicLjn8 el9fYUvAyMciIu5FsqTniIk+yA3cr1w3ncwMolCelhyZMKzexFtdlFr23O9HDpc2IrMc CWW3mzY26c7zQlXPKVywbseppEIrZSUcQ4YajvL6NIrI/EuUGdDxIssM7h9NX+7lf0Ti oEAscttOoxGX1NyNklXz7UyKL6eom407xLxLCt4NLP6Gq0G3cr5clfmIQeaRXX3dQihT gvPw== X-Gm-Message-State: AN3rC/5dY1znhhyCkhRyDqWzY+/lL1qEb8XQE8xeqiC+Zkhh45xsW3WY rgG9Ieo72VAlErKa X-Received: by 10.223.151.6 with SMTP id r6mr25502589wrb.189.1493847495442; Wed, 03 May 2017 14:38:15 -0700 (PDT) From: Leif Lindholm To: edk2-devel@lists.01.org Date: Wed, 3 May 2017 22:38:11 +0100 Message-Id: <20170503213812.5971-3-leif.lindholm@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170503213812.5971-1-leif.lindholm@linaro.org> References: <20170503213812.5971-1-leif.lindholm@linaro.org> Subject: [edk2] [PATCH 2/3] EmbeddedPkg: import EfiTimeBaseLib X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ard Biesheuvel MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Some generic RTC helper functions were broken out from the PL031 library for use with other RTCs in OpenPlatformPkg. Import the code back here, realigning it with the current state of PL031RealTimeClockLib to simplify comparisons. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Leif Lindholm --- EmbeddedPkg/Include/Library/EfiTimeBaseLib.h | 68 ++++++++ .../Library/EfiTimeBaseLib/EfiTimeBaseLib.c | 171 +++++++++++++++++= ++++ .../Library/EfiTimeBaseLib/EfiTimeBaseLib.inf | 34 ++++ 3 files changed, 273 insertions(+) create mode 100644 EmbeddedPkg/Include/Library/EfiTimeBaseLib.h create mode 100644 EmbeddedPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.c create mode 100644 EmbeddedPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.inf diff --git a/EmbeddedPkg/Include/Library/EfiTimeBaseLib.h b/EmbeddedPkg/Inc= lude/Library/EfiTimeBaseLib.h new file mode 100644 index 0000000000..63db6c6eb8 --- /dev/null +++ b/EmbeddedPkg/Include/Library/EfiTimeBaseLib.h @@ -0,0 +1,68 @@ +/** @file +* +* Copyright (c) 2016, Hisilicon Limited. All rights reserved. +* Copyright (c) 2016, Linaro Limited. All rights reserved. +* +* This program and the accompanying materials +* are licensed and made available under the terms and conditions of the B= SD License +* which accompanies this distribution. The full text of the license may = be found at +* http://opensource.org/licenses/bsd-license.php +* +* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IM= PLIED. +* +**/ + +#ifndef _EFI_TIME_BASE_LIB_H_ +#define _EFI_TIME_BASE_LIB_H_ + +#include + +// Define EPOCH (1970-JANUARY-01) in the Julian Date representation +#define EPOCH_JULIAN_DATE 2440588 + +// Seconds per unit +#define SEC_PER_MIN ((UINTN) 60) +#define SEC_PER_HOUR ((UINTN) 3600) +#define SEC_PER_DAY ((UINTN) 86400) +#define SEC_PER_MONTH ((UINTN) 2,592,00= 0) +#define SEC_PER_YEAR ((UINTN) 31,536,00= 0) + +BOOLEAN +EFIAPI +IsLeapYear ( + IN EFI_TIME *Time + ); + +BOOLEAN +EFIAPI +IsDayValid ( + IN EFI_TIME *Time + ); + +BOOLEAN +EFIAPI +IsTimeValid ( + IN EFI_TIME *Time + ); + +/** + Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to = EFI_TIME + **/ +VOID +EFIAPI +EpochToEfiTime ( + IN UINTN EpochSeconds, + OUT EFI_TIME *Time + ); + +/** + Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00= :00 UTC) + **/ +UINTN +EFIAPI +EfiTimeToEpoch ( + IN EFI_TIME *Time + ); + +#endif diff --git a/EmbeddedPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.c b/Embedded= Pkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.c new file mode 100644 index 0000000000..5a6dee6cdd --- /dev/null +++ b/EmbeddedPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.c @@ -0,0 +1,171 @@ +/** @file +* +* Copyright (c) 2016, Hisilicon Limited. All rights reserved. +* Copyright (c) 2016, Linaro Limited. All rights reserved. +* +* This program and the accompanying materials +* are licensed and made available under the terms and conditions of the B= SD License +* which accompanies this distribution. The full text of the license may = be found at +* http://opensource.org/licenses/bsd-license.php +* +* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IM= PLIED. +* +**/ + +#include +#include +#include +#include + +/** + Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to = EFI_TIME + **/ +VOID +EFIAPI +EpochToEfiTime ( + IN UINTN EpochSeconds, + OUT EFI_TIME *Time + ) +{ + UINTN a; + UINTN b; + UINTN c; + UINTN d; + UINTN g; + UINTN j; + UINTN m; + UINTN y; + UINTN da; + UINTN db; + UINTN dc; + UINTN dg; + UINTN hh; + UINTN mm; + UINTN ss; + UINTN J; + + J =3D (EpochSeconds / 86400) + 2440588; + j =3D J + 32044; + g =3D j / 146097; + dg =3D j % 146097; + c =3D (((dg / 36524) + 1) * 3) / 4; + dc =3D dg - (c * 36524); + b =3D dc / 1461; + db =3D dc % 1461; + a =3D (((db / 365) + 1) * 3) / 4; + da =3D db - (a * 365); + y =3D (g * 400) + (c * 100) + (b * 4) + a; + m =3D (((da * 5) + 308) / 153) - 2; + d =3D da - (((m + 4) * 153) / 5) + 122; + + Time->Year =3D y - 4800 + ((m + 2) / 12); + Time->Month =3D ((m + 2) % 12) + 1; + Time->Day =3D d + 1; + + ss =3D EpochSeconds % 60; + a =3D (EpochSeconds - ss) / 60; + mm =3D a % 60; + b =3D (a - mm) / 60; + hh =3D b % 24; + + Time->Hour =3D hh; + Time->Minute =3D mm; + Time->Second =3D ss; + Time->Nanosecond =3D 0; + +} + +/** + Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00= :00 UTC) + **/ +UINTN +EFIAPI +EfiTimeToEpoch ( + IN EFI_TIME *Time + ) +{ + UINTN a; + UINTN y; + UINTN m; + UINTN JulianDate; // Absolute Julian Date representation of the supplie= d Time + UINTN EpochDays; // Number of days elapsed since EPOCH_JULIAN_DAY + UINTN EpochSeconds; + + a =3D (14 - Time->Month) / 12 ; + y =3D Time->Year + 4800 - a; + m =3D Time->Month + (12*a) - 3; + + JulianDate =3D Time->Day + ((153*m + 2)/5) + (365*y) + (y/4) - (y/100) += (y/400) - 32045; + + ASSERT (JulianDate >=3D EPOCH_JULIAN_DATE); + EpochDays =3D JulianDate - EPOCH_JULIAN_DATE; + + EpochSeconds =3D (EpochDays * SEC_PER_DAY) + ((UINTN)Time->Hour * SEC_PE= R_HOUR) + (Time->Minute * SEC_PER_MIN) + Time->Second; + + return EpochSeconds; +} + +BOOLEAN +EFIAPI +IsLeapYear ( + IN EFI_TIME *Time + ) +{ + if (Time->Year % 4 =3D=3D 0) { + if (Time->Year % 100 =3D=3D 0) { + if (Time->Year % 400 =3D=3D 0) { + return TRUE; + } else { + return FALSE; + } + } else { + return TRUE; + } + } else { + return FALSE; + } +} + +BOOLEAN +EFIAPI +IsDayValid ( + IN EFI_TIME *Time + ) +{ + STATIC CONST INTN DayOfMonth[12] =3D { 31, 29, 31, 30, 31, 30, 31, 31, 3= 0, 31, 30, 31 }; + + if (Time->Day < 1 || + Time->Day > DayOfMonth[Time->Month - 1] || + (Time->Month =3D=3D 2 && (!IsLeapYear (Time) && Time->Day > 28)) + ) { + return FALSE; + } + + return TRUE; +} + +BOOLEAN +EFIAPI +IsTimeValid( + IN EFI_TIME *Time + ) +{ + // Check the input parameters are within the range specified by UEFI + if ((Time->Year < 2000) || + (Time->Year > 2099) || + (Time->Month < 1 ) || + (Time->Month > 12 ) || + (!IsDayValid (Time) ) || + (Time->Hour > 23 ) || + (Time->Minute > 59 ) || + (Time->Second > 59 ) || + (Time->Nanosecond > 999999999) || + (!((Time->TimeZone =3D=3D EFI_UNSPECIFIED_TIMEZONE) || ((Time->TimeZo= ne >=3D -1440) && (Time->TimeZone <=3D 1440)))) || + (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)= )) + ) { + return FALSE; + } + + return TRUE; +} diff --git a/EmbeddedPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.inf b/Embedd= edPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.inf new file mode 100644 index 0000000000..bd962ae7d2 --- /dev/null +++ b/EmbeddedPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.inf @@ -0,0 +1,34 @@ +#/** @file +# +# Copyright (c) 2016, Hisilicon Limited. All rights reserved. +# Copyright (c) 2016, Linaro Limited. All rights reserved. +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the= BSD License +# which accompanies this distribution. The full text of the license may= be found at +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR = IMPLIED. +# +#**/ + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D EfiTimeBaseLib + FILE_GUID =3D B1B07E01-6896-448C-8E75-F0E119ABDF49 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D EfiTimeBaseLib + +[Sources.common] + EfiTimeBaseLib.c + +[Packages] + EmbeddedPkg/EmbeddedPkg.dec + MdePkg/MdePkg.dec + +[LibraryClasses] + DebugLib + +[Pcd] --=20 2.11.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Sun May 5 22:21:26 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; dkim=fail spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1493847506455100.80337188105341; Wed, 3 May 2017 14:38:26 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id D0A3121955D91; Wed, 3 May 2017 14:38:19 -0700 (PDT) Received: from mail-wr0-x22b.google.com (mail-wr0-x22b.google.com [IPv6:2a00:1450:400c:c0c::22b]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id B737A21A13482 for ; Wed, 3 May 2017 14:38:17 -0700 (PDT) Received: by mail-wr0-x22b.google.com with SMTP id z52so1129472wrc.2 for ; Wed, 03 May 2017 14:38:17 -0700 (PDT) Received: from mohikan.hemma.eciton.net ([2a01:348:6:8735:e23f:49ff:fe6d:9d26]) by smtp.gmail.com with ESMTPSA id z14sm41380wmz.2.2017.05.03.14.38.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 May 2017 14:38:15 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=B9yc8JJNoLV6e+aUcSUgjXYwwOtaFrM9pl9nFhFG0IE=; b=HUdryZcVPoV/4x9mBl7+Fj8n/3dNgHFcf9An5wn+j4kR6DLe5jJWowh8BpP1CALEGG UhesCbNDiCMvq/kL5Go0FwEbp1I1KaTeFq0YoKJC56tKW4N9JBU3tvjo/QU1y/rKucI5 77YjbRwEkfcrgmAPnTAt1A1CNPQ4f5b0XL4YQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=B9yc8JJNoLV6e+aUcSUgjXYwwOtaFrM9pl9nFhFG0IE=; b=UFB1vdSSJvJ1mOi/OJJ4bpyo711GLIVgQXTenFxyH2DZLfnB4VVV41Pdn7tGDb/giR FfmfnPn7o5atGLqw8XYO21AOu3OHsqNGXzcTPir8YzWJR1EX1tBCsPmCCrJp+QaZVUHK 5QtrsDo2eayY9Mf/MXMh0nQpyME/kp4sw6o5xtXtTLfzRmQF46IfnoArQSFEWc/TyKqV YbGTMuiIzypoEDM0nFDgXR33s+5D4bQ3nPQbBG28kWL3L6NVo3MY4qbwa4VHD94lbu4y 01yLpD14Cp88/MiRCilc3ULtxu8CGJdnhCY4NIQMC2JigxqxvJn1Djtha9r/XZACNzZa qAnQ== X-Gm-Message-State: AN3rC/7ZovVifVRWXWbyu1JK6JQKP6fyE7iMEbnJdDgLQ6Nqn3whirvJ DfVeJ/4GpswFP5rH X-Received: by 10.223.139.20 with SMTP id n20mr20912757wra.169.1493847496336; Wed, 03 May 2017 14:38:16 -0700 (PDT) From: Leif Lindholm To: edk2-devel@lists.01.org Date: Wed, 3 May 2017 22:38:12 +0100 Message-Id: <20170503213812.5971-4-leif.lindholm@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170503213812.5971-1-leif.lindholm@linaro.org> References: <20170503213812.5971-1-leif.lindholm@linaro.org> Subject: [edk2] [PATCH 3/3] ArmPlatformPkg, ArmVirtPkg: delete redundant PL031 functions X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laszlo Ersek , Ard Biesheuvel MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Remove the functions now provided by EfiTimeBaseLib from PL031RealTimeClockLib. Add EfiTimeBaseLib resolution to ArmVirtPkg in same commit to prevent breakage. Cc: Laszlo Ersek Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Leif Lindholm Reviewed-by: Ard Biesheuvel --- .../Include/Drivers/PL031RealTimeClock.h | 11 -- .../PL031RealTimeClockLib/PL031RealTimeClockLib.c | 127 +----------------= ---- .../PL031RealTimeClockLib.inf | 1 + ArmVirtPkg/ArmVirt.dsc.inc | 1 + 4 files changed, 5 insertions(+), 135 deletions(-) diff --git a/ArmPlatformPkg/Include/Drivers/PL031RealTimeClock.h b/ArmPlatf= ormPkg/Include/Drivers/PL031RealTimeClock.h index 76fbd0eb82..812cd9b397 100644 --- a/ArmPlatformPkg/Include/Drivers/PL031RealTimeClock.h +++ b/ArmPlatformPkg/Include/Drivers/PL031RealTimeClock.h @@ -42,15 +42,4 @@ =20 #define PL031_COUNTS_PER_SECOND 1 =20 -// Define EPOCH (1970-JANUARY-01) in the Julian Date representation -#define EPOCH_JULIAN_DATE 2440588 - -// Seconds per unit -#define SEC_PER_MIN ((UINTN) 60) -#define SEC_PER_HOUR ((UINTN) 3600) -#define SEC_PER_DAY ((UINTN) 86400) - -#define SEC_PER_MONTH ((UINTN) 2,592,00= 0) -#define SEC_PER_YEAR ((UINTN) 31,536,00= 0) - #endif diff --git a/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeCloc= kLib.c b/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib= .c index 4aa448b528..9f115d383c 100644 --- a/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c +++ b/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c @@ -36,6 +36,8 @@ =20 #include =20 +#include + #include =20 STATIC CONST CHAR16 mTimeZoneVariableName[] =3D L"PL031RtcTimeZo= ne"; @@ -110,129 +112,6 @@ InitializePL031 ( } =20 /** - Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to = EFI_TIME - **/ -VOID -EpochToEfiTime ( - IN UINTN EpochSeconds, - OUT EFI_TIME *Time - ) -{ - UINTN a; - UINTN b; - UINTN c; - UINTN d; - UINTN g; - UINTN j; - UINTN m; - UINTN y; - UINTN da; - UINTN db; - UINTN dc; - UINTN dg; - UINTN hh; - UINTN mm; - UINTN ss; - UINTN J; - - J =3D (EpochSeconds / 86400) + 2440588; - j =3D J + 32044; - g =3D j / 146097; - dg =3D j % 146097; - c =3D (((dg / 36524) + 1) * 3) / 4; - dc =3D dg - (c * 36524); - b =3D dc / 1461; - db =3D dc % 1461; - a =3D (((db / 365) + 1) * 3) / 4; - da =3D db - (a * 365); - y =3D (g * 400) + (c * 100) + (b * 4) + a; - m =3D (((da * 5) + 308) / 153) - 2; - d =3D da - (((m + 4) * 153) / 5) + 122; - - Time->Year =3D y - 4800 + ((m + 2) / 12); - Time->Month =3D ((m + 2) % 12) + 1; - Time->Day =3D d + 1; - - ss =3D EpochSeconds % 60; - a =3D (EpochSeconds - ss) / 60; - mm =3D a % 60; - b =3D (a - mm) / 60; - hh =3D b % 24; - - Time->Hour =3D hh; - Time->Minute =3D mm; - Time->Second =3D ss; - Time->Nanosecond =3D 0; - -} - -/** - Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00= :00 UTC) - **/ -UINTN -EfiTimeToEpoch ( - IN EFI_TIME *Time - ) -{ - UINTN a; - UINTN y; - UINTN m; - UINTN JulianDate; // Absolute Julian Date representation of the supplie= d Time - UINTN EpochDays; // Number of days elapsed since EPOCH_JULIAN_DAY - UINTN EpochSeconds; - - a =3D (14 - Time->Month) / 12 ; - y =3D Time->Year + 4800 - a; - m =3D Time->Month + (12*a) - 3; - - JulianDate =3D Time->Day + ((153*m + 2)/5) + (365*y) + (y/4) - (y/100) += (y/400) - 32045; - - ASSERT (JulianDate >=3D EPOCH_JULIAN_DATE); - EpochDays =3D JulianDate - EPOCH_JULIAN_DATE; - - EpochSeconds =3D (EpochDays * SEC_PER_DAY) + ((UINTN)Time->Hour * SEC_PE= R_HOUR) + (Time->Minute * SEC_PER_MIN) + Time->Second; - - return EpochSeconds; -} - -BOOLEAN -IsLeapYear ( - IN EFI_TIME *Time - ) -{ - if (Time->Year % 4 =3D=3D 0) { - if (Time->Year % 100 =3D=3D 0) { - if (Time->Year % 400 =3D=3D 0) { - return TRUE; - } else { - return FALSE; - } - } else { - return TRUE; - } - } else { - return FALSE; - } -} - -BOOLEAN -DayValid ( - IN EFI_TIME *Time - ) -{ - STATIC CONST INTN DayOfMonth[12] =3D { 31, 29, 31, 30, 31, 30, 31, 31, 3= 0, 31, 30, 31 }; - - if (Time->Day < 1 || - Time->Day > DayOfMonth[Time->Month - 1] || - (Time->Month =3D=3D 2 && (!IsLeapYear (Time) && Time->Day > 28)) - ) { - return FALSE; - } - - return TRUE; -} - -/** Returns the current time and date information, and the time-keeping capa= bilities of the hardware platform. =20 @@ -431,7 +310,7 @@ LibSetTime ( (Time->Year > 9999) || (Time->Month < 1 ) || (Time->Month > 12 ) || - (!DayValid (Time) ) || + (!IsDayValid (Time) ) || (Time->Hour > 23 ) || (Time->Minute > 59 ) || (Time->Second > 59 ) || diff --git a/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeCloc= kLib.inf b/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockL= ib.inf index 3a98fc757e..41a4e6d6bd 100644 --- a/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf +++ b/ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf @@ -36,6 +36,7 @@ PcdLib ArmPlatformSysConfigLib DxeServicesTableLib + EfiTimeBaseLib UefiRuntimeLib =20 [Guids] diff --git a/ArmVirtPkg/ArmVirt.dsc.inc b/ArmVirtPkg/ArmVirt.dsc.inc index e74eaf16e7..bc96709d8a 100644 --- a/ArmVirtPkg/ArmVirt.dsc.inc +++ b/ArmVirtPkg/ArmVirt.dsc.inc @@ -105,6 +105,7 @@ =20 # ARM PL031 RTC Driver RealTimeClockLib|ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealT= imeClockLib.inf + EfiTimeBaseLib|EmbeddedPkg/Library/EfiTimeBaseLib/EfiTimeBaseLib.inf # ARM PL011 UART Driver PL011UartLib|ArmPlatformPkg/Drivers/PL011Uart/PL011Uart.inf SerialPortLib|ArmVirtPkg/Library/FdtPL011SerialPortLib/FdtPL011SerialPor= tLib.inf --=20 2.11.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel