From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=40.107.68.95; helo=nam04-bn3-obe.outbound.protection.outlook.com; envelope-from=christopher.co@microsoft.com; receiver=edk2-devel@lists.01.org Received: from NAM04-BN3-obe.outbound.protection.outlook.com (mail-eopbgr680095.outbound.protection.outlook.com [40.107.68.95]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 73A372098EABA for ; Thu, 19 Jul 2018 23:33:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=IS0R24Um5Zm5pE/R+WV5H3jzGrIwCtEdcsH7910tcoE=; b=PG9GzKH7S7b/c8lQ89xR+XQPGZxbJXkvl0Vpel8kZ4Qdar+JO4FriLS0R13etURNCotuRC6g4DPj7z59kMfinX0sTqEWDes3RJHOw109JxXqW07gZu73mMJilA5i2fiMMfyS+Q4cwc6U64YG2NkIsRzcoBi4aHBJsv2dXfqB5fg= Received: from SN6PR2101MB1136.namprd21.prod.outlook.com (52.132.114.25) by SN6PR2101MB1117.namprd21.prod.outlook.com (52.132.115.31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.995.1; Fri, 20 Jul 2018 06:33:52 +0000 Received: from SN6PR2101MB1136.namprd21.prod.outlook.com ([fe80::78f8:214:33a:3c4]) by SN6PR2101MB1136.namprd21.prod.outlook.com ([fe80::78f8:214:33a:3c4%5]) with mapi id 15.20.0995.008; Fri, 20 Jul 2018 06:33:52 +0000 From: Chris Co To: "edk2-devel@lists.01.org" CC: Ard Biesheuvel , Leif Lindholm , Michael D Kinney Thread-Topic: [PATCH edk2-platforms 05/13] Silicon/NXP: Add i.MX6 GPT Timer library Thread-Index: AQHUH/Ofq5o8vpyTPkmumFfdfr5mgw== Date: Fri, 20 Jul 2018 06:33:52 +0000 Message-ID: <20180720063328.26856-6-christopher.co@microsoft.com> References: <20180720063328.26856-1-christopher.co@microsoft.com> In-Reply-To: <20180720063328.26856-1-christopher.co@microsoft.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: CY4PR18CA0029.namprd18.prod.outlook.com (2603:10b6:903:9a::15) To SN6PR2101MB1136.namprd21.prod.outlook.com (2603:10b6:805:4::25) x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [2001:4898:80e8:a:75ac:13c7:7dde:5215] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; SN6PR2101MB1117; 6:icylrhg+XRjpFQlR96LEmQ2toTqpP1IH66jV4K+bunzpc0yovUJ1BJLyUf+JUdTcaQ19uaJeHHGLwtRiKm9c7eLGNqhPd4a1sRAwQTiqbjW5peU27EJDm5W4ZvNH9wAorzTd3+E6AfNfau0gFpzxFtSzvKHmq4qLDhAS6rG8Nh8vSjYg0O18F+sdGkzr5QB+rxmg1oZeaLY2WRnt7J7NlPYqx6RsQhXzGCCnUnTuk5z7v6Tx4uwYvSJRO3VE6k1/t5tLxgDDPC1MCDraibxf5mhvbsLMOB5sodL8Qd+FD5apkaRy5qkEWmY60xXi5zIUcC6ClQJE3vqaCl9eH2y9136htIoKEwV/TgOgAtGONKErN70C9rhdIhr+UTHGSZAjb37zSEKrlda2nxy4z6ulQXnWvhlmPtGxTcBBVHS32sEBQVwcNc+ynV4vX7fXUN08N+u4U6fcQFRe9tKb7STr9A==; 5:DTnz5+/XM6Au7a3WAsNuafcIW1ZpT8l3m4jPb2NB684mfdmkQzpk3tVPl44ro3Lrk5F1IRFc+zWRZ5mHf8KT3Yscb/0LPkx/jgdt+Tf4zs/sEuh2KE/VzcnRa16i8R2wwRjf1SEeuXQtsHA5dbz8LE35uPe9r6jNSUYPjw5+a6Q=; 7:K5m+HJ0RUWLACGEA2HZVHU5zhzsxJ25yrwDYPcT6fl5YwqjyMT9dCk0N7XnryOC1S7mkjiXNT0SdvNpyzBU0G9yr/ffSeEwLEUY+14yx6WVKm0APzQZh0QsrMHRDvHe5zu/voq4Cho232/U0PrCPSA4BJsIQ08L9h2X/nmYwOG0fhk9IVBEZnYZP7wouTsG/pYiBQvfhR+6kT+9/Rs4Hhzl1fv1tKXwfBsb+49Ic176TP3xD1yNiXyil54OiyHpS x-ms-office365-filtering-correlation-id: dc97e66a-99df-4530-a1a2-08d5ee0ac07b x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(7020095)(4652040)(8989117)(5600067)(711020)(4618075)(4534165)(4627221)(201703031133081)(201702281549075)(8990107)(2017052603328)(7193020); SRVR:SN6PR2101MB1117; x-ms-traffictypediagnostic: SN6PR2101MB1117: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Christopher.Co@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(228905959029699); x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(8121501046)(5005006)(3002001)(10201501046)(3231311)(944501410)(52105095)(2018427008)(93006095)(93001095)(6055026)(149027)(150027)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(20161123562045)(20161123564045)(20161123560045)(6072148)(201708071742011)(7699016); SRVR:SN6PR2101MB1117; BCL:0; PCL:0; RULEID:; SRVR:SN6PR2101MB1117; x-forefront-prvs: 073966E86B x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(1496009)(396003)(39860400002)(366004)(346002)(376002)(136003)(199004)(189003)(52116002)(8676002)(81156014)(105586002)(2616005)(446003)(53376002)(256004)(99286004)(46003)(11346002)(22452003)(106356001)(97736004)(6306002)(6436002)(561924002)(6506007)(386003)(72206003)(86612001)(6116002)(5660300001)(76176011)(25786009)(966005)(102836004)(1076002)(53936002)(81166006)(2900100001)(6512007)(478600001)(476003)(10290500003)(6916009)(16799955002)(186003)(2351001)(14454004)(5250100002)(36756003)(19627235002)(6486002)(54906003)(2501003)(10090500001)(86362001)(4326008)(8936002)(5640700003)(68736007)(2906002)(305945005)(15188155005)(486006)(7736002)(316002); DIR:OUT; SFP:1102; SCL:1; SRVR:SN6PR2101MB1117; H:SN6PR2101MB1136.namprd21.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: 7ObDNgThSMuZG4NRqASY/stNsMKLyNXx6wRGpjUpfZEes63OGL103fc8kFbf4+KBW+xMYiMUgCGpQGl1IGsYSuGeUzFyZuQJdkciT9LYFDU6owqgdYFfJSkGZxrITiwPO1soHP7/A1vULM1lgkpv7vGJHd4lLf8gINDUHe1zdtCfZLON1eR3nq9m8zV8cHew11KT4MY2u+0RYF7/u7DAnCJmMZgh6xsYvoOYlUByoVVqdrwBWh9yu0AZ7OqoSgrVoLZ+mGI+TmkP3kbDgas8qTw91K2/4tvB8t6A1OL8E65aCSWf2HsCXgiGdbM3+H/fejh07cSXyXfovGO9PUVWHz0oCkSFqRxO4eadYwNscQM= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: dc97e66a-99df-4530-a1a2-08d5ee0ac07b X-MS-Exchange-CrossTenant-originalarrivaltime: 20 Jul 2018 06:33:52.1392 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR2101MB1117 Subject: [PATCH edk2-platforms 05/13] Silicon/NXP: Add i.MX6 GPT Timer library X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 20 Jul 2018 06:33:54 -0000 Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable This adds support for GPT Timer on NXP i.MX6 SoCs. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Christopher Co Cc: Ard Biesheuvel Cc: Leif Lindholm Cc: Michael D Kinney --- Silicon/NXP/iMX6Pkg/Library/TimerLib/TimerLib.c | 257 ++++++++++++++++++= ++ Silicon/NXP/iMX6Pkg/Library/TimerLib/TimerLib.inf | 37 +++ 2 files changed, 294 insertions(+) diff --git a/Silicon/NXP/iMX6Pkg/Library/TimerLib/TimerLib.c b/Silicon/NXP/= iMX6Pkg/Library/TimerLib/TimerLib.c new file mode 100644 index 000000000000..54dd3b5f7004 --- /dev/null +++ b/Silicon/NXP/iMX6Pkg/Library/TimerLib/TimerLib.c @@ -0,0 +1,257 @@ +/** @file +* +* Copyright (c) Microsoft Corporation. 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 +#include + +#include +#include +#include +#include +#include +#include + +// Select appropriate multiply function for platform architecture. +#ifdef MDE_CPU_ARM +#define MultU64xN MultU64x32 +#else +#define MultU64xN MultU64x64 +#endif + +RETURN_STATUS +EFIAPI +TimerConstructor ( + VOID + ) +{ + PCSP_GPT_REGS pGPT; + UINT32 FreqPreScale; + + pGPT =3D (PCSP_GPT_REGS)CSP_BASE_REG_PA_GPT; + + ASSERT(SOC_OSC_FREQUENCY_REF_HZ >=3D PcdGet32 (PcdArmArchTimerFreqInHz))= ; + + // Calculate the scale factor since we are using the 24Mhz oscillator + // as reference. + FreqPreScale =3D SOC_OSC_FREQUENCY_REF_HZ / PcdGet32 (PcdArmArchTimerFre= qInHz); + ASSERT(FreqPreScale <=3D (1<PR,FreqPreScale - 1); + +#if defined(CPU_IMX6DQ) + // Set GPT configuration: + // - GPT Enabled + // - Use the 24Mhz oscillator source + OUTREG32(&pGPT->CR, + CSP_BITFVAL(GPT_CR_EN, GPT_CR_EN_ENABLE) | + CSP_BITFVAL(GPT_CR_CLKSRC, GPT_CR_CLKSRC_CLK24M)); +#elif defined(CPU_IMX6SDL) || defined(CPU_IMX6SX) + // Set GPT configuration: + // - GPT Enabled + // - Enable 24 Mhz Oscillator + // - Use the 24Mhz oscillator source + OUTREG32(&pGPT->CR, + CSP_BITFVAL(GPT_CR_EN, GPT_CR_EN_ENABLE) | + CSP_BITFVAL(GPT_CR_EN_24M, GPT_CR_EN_24M_ENABLE) | + CSP_BITFVAL(GPT_CR_CLKSRC, GPT_CR_CLKSRC_CLK24M)); +#else +#error CPU Preprocessor Flag Not Defined +#endif + + return EFI_SUCCESS; +} + +/** + Stalls the CPU for at least the given number of microseconds. + + Stalls the CPU for the number of microseconds specified by MicroSeconds. + + @param MicroSeconds The minimum number of microseconds to delay. + + @return The value of MicroSeconds inputted. + +**/ +UINTN +EFIAPI +MicroSecondDelay ( + IN UINTN MicroSeconds + ) +{ + UINT64 TimerTicks64; + UINT32 CurCounterRead; + UINT32 PrevCounterRead; + UINT64 CountOffset; + + // Convert uSec delay to counter ticks: + TimerTicks64 =3D ((UINT64)MicroSeconds * PcdGet32 (PcdArmArchTimerF= reqInHz)) / 1000000U; + CurCounterRead =3D (UINT32)GetPerformanceCounter(); + PrevCounterRead =3D CurCounterRead; + TimerTicks64 +=3D (UINT64)CurCounterRead; + CountOffset =3D 0; + + // GPT is a 32bit counter, thus we need to handle rollover cases. + while(((UINT64)CurCounterRead + CountOffset) < TimerTicks64) { + + CurCounterRead =3D (UINT32)GetPerformanceCounter(); + if (CurCounterRead < PrevCounterRead) { + CountOffset +=3D 0x100000000; + } + + PrevCounterRead =3D CurCounterRead; + } + + return MicroSeconds; +} + +/** + Stalls the CPU for at least the given number of nanoseconds. + + Stalls the CPU for the number of nanoseconds specified by NanoSeconds. + + @param NanoSeconds The minimum number of nanoseconds to delay. + + @return The value of NanoSeconds inputted. + +**/ +UINTN +EFIAPI +NanoSecondDelay ( + IN UINTN NanoSeconds + ) +{ + if (NanoSeconds < (0xffffffff - 999)) { + NanoSeconds +=3D 999; + } + MicroSecondDelay(NanoSeconds / 1000); + + return 0; +} + +/** + Retrieves the current value of a 64-bit free running performance counter= . + + The counter can either count up by 1 or count down by 1. If the physical + performance counter counts by a larger increment, then the counter value= s + must be translated. The properties of the counter can be retrieved from + GetPerformanceCounterProperties(). + + @return The current value of the free running performance counter. + +**/ +UINT64 +EFIAPI +GetPerformanceCounter ( + VOID + ) +{ + PCSP_GPT_REGS pGPT =3D (PCSP_GPT_REGS)CSP_BASE_REG_PA_GPT; + + return INREG32 (&pGPT->CNT); +} + +/** + Retrieves the 64-bit frequency in Hz and the range of performance counte= r + values. + + If StartValue is not NULL, then the value that the performance counter s= tarts + with immediately after is it rolls over is returned in StartValue. If + EndValue is not NULL, then the value that the performance counter end wi= th + immediately before it rolls over is returned in EndValue. The 64-bit + frequency of the performance counter in Hz is always returned. If StartV= alue + is less than EndValue, then the performance counter counts up. If StartV= alue + is greater than EndValue, then the performance counter counts down. For + example, a 64-bit free running counter that counts up would have a Start= Value + of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counte= r + that counts down would have a StartValue of 0xFFFFFF and an EndValue of = 0. + + @param StartValue The value the performance counter starts with when i= t + rolls over. + @param EndValue The value that the performance counter ends with bef= ore + it rolls over. + + @return The frequency in Hz. + +**/ +UINT64 +EFIAPI +GetPerformanceCounterProperties ( + OUT UINT64 *StartValue, OPTIONAL + OUT UINT64 *EndValue OPTIONAL + ) +{ + if (StartValue !=3D NULL) { + *StartValue =3D 0x0; + } + + if (EndValue !=3D NULL) { + *EndValue =3D 0xFFFFFFFF; + } + + return PcdGet32 (PcdArmArchTimerFreqInHz); +} + +/** + Converts elapsed ticks of performance counter to time in nanoseconds. + + This function converts the elapsed ticks of running performance counter = to + time value in unit of nanoseconds. + + @param Ticks The number of elapsed ticks of running performance cou= nter. + + @return The elapsed time in nanoseconds. + +**/ +UINT64 +EFIAPI +GetTimeInNanoSecond ( + IN UINT64 Ticks + ) +{ + UINT64 NanoSeconds; + UINT32 Remainder; + UINT32 TimerFreq; + + TimerFreq =3D PcdGet32 (PcdArmArchTimerFreqInHz); + + // + // Ticks + // Time =3D --------- x 1,000,000,000 + // Frequency + // + NanoSeconds =3D MultU64xN ( + DivU64x32Remainder ( + Ticks, + TimerFreq, + &Remainder), + 1000000000U + ); + + // + // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder = * 1,000,000,000) + // will not overflow 64-bit. + // + NanoSeconds +=3D DivU64x32 ( + MultU64xN ( + (UINT64) Remainder, + 1000000000U), + TimerFreq + ); + + return NanoSeconds; +} diff --git a/Silicon/NXP/iMX6Pkg/Library/TimerLib/TimerLib.inf b/Silicon/NX= P/iMX6Pkg/Library/TimerLib/TimerLib.inf new file mode 100644 index 000000000000..4715b90db153 --- /dev/null +++ b/Silicon/NXP/iMX6Pkg/Library/TimerLib/TimerLib.inf @@ -0,0 +1,37 @@ +## @file +# +# Copyright (c) Microsoft Corporation. All rights reserved. +# Copyright (c) 2007 - 2008, Intel Corporation. +# +# 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 0x00010005 + BASE_NAME =3D iMX6TimerLib + FILE_GUID =3D 2956C1A6-6FF8-4763-9FD8-D45892E025E3 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D TimerLib + +[Sources.common] + TimerLib.c + +[Packages] + MdePkg/MdePkg.dec + ArmPkg/ArmPkg.dec + Silicon/NXP/iMX6Pkg/iMX6Pkg.dec + +[LibraryClasses] + DebugLib + IoLib + +[Pcd] + gArmTokenSpaceGuid.PcdArmArchTimerFreqInHz --=20 2.16.2.gvfs.1.33.gf5370f1