From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=104.47.38.90; helo=nam02-bl2-obe.outbound.protection.outlook.com; envelope-from=christopher.co@microsoft.com; receiver=edk2-devel@lists.01.org Received: from NAM02-BL2-obe.outbound.protection.outlook.com (mail-bl2nam02on0090.outbound.protection.outlook.com [104.47.38.90]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 4EA6021154111 for ; Fri, 21 Sep 2018 01:26:09 -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=VkWuvmnCj/a5cUqgL6oS8Mg7STVfpvINiV8v/ytPrVQ=; b=DHc3XCSWfNb8bcJeWiP0Hh27OTobhz4qOJSxuIXJXFrvrW4na7xWDnVqKtMWUQlUSbVwjlpir/RCvkvgUVsXjfsn3ZbEJhE6lKr2gipje1WSRsmmF0q+LXEf3bhen54lLhTDuV8spenXTNlw3BQ6GvDDHDBDaQ/67yndLmb4d0Y= Received: from DM5PR2101MB1128.namprd21.prod.outlook.com (52.132.133.20) by DM5PR2101MB0727.namprd21.prod.outlook.com (10.167.110.39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1185.6; Fri, 21 Sep 2018 08:26:07 +0000 Received: from DM5PR2101MB1128.namprd21.prod.outlook.com ([fe80::81f8:300e:d90:d49]) by DM5PR2101MB1128.namprd21.prod.outlook.com ([fe80::81f8:300e:d90:d49%3]) with mapi id 15.20.1164.008; Fri, 21 Sep 2018 08:26:07 +0000 From: Chris Co To: "edk2-devel@lists.01.org" CC: Ard Biesheuvel , Leif Lindholm , Michael D Kinney Thread-Topic: [PATCH edk2-platforms 15/27] Silicon/NXP: Add i.MX6 GPT Timer library Thread-Index: AQHUUYS+/VC4H46K7kmkCeYmq/92nQ== Date: Fri, 21 Sep 2018 08:26:07 +0000 Message-ID: <20180921082542.35768-16-christopher.co@microsoft.com> References: <20180921082542.35768-1-christopher.co@microsoft.com> In-Reply-To: <20180921082542.35768-1-christopher.co@microsoft.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MWHPR17CA0054.namprd17.prod.outlook.com (2603:10b6:300:93::16) To DM5PR2101MB1128.namprd21.prod.outlook.com (2603:10b6:4:a8::20) x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [2001:4898:80e8:8:388a:edc9:7085:c18] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; DM5PR2101MB0727; 6:JktaFxHTz7zTkRav/W7WHlFmf6mZw9uMDdz1BROCnG0rSnyyeUsACQKy4vVX0Sj2FfJbsGtCQLnwPSqEMzwHVeX0HmNZWHv04HUQa2Yv7Xa4adSIcYyiUv9H4PnK8uMBkoPWPAsBSS+OAgycG/T6Ho+Zz/MT3scCLvUHrDBPl/noc2ipIICNR2ypQ+yt0atZPfD9Zx2UJwJyzbGtYcwukiVElbRv1WjmxKTlUNwaLoPHgfPBKT+u/hNYvZSdihJ8j6ENDm0bnXTKZv6J81PMcrAX+CUb8WOZMkljPrgWLf8V3YrDvG+f0ON4j7YiXwpC777yVxQO8xqZLf+6GlFPtJmy6hIokOPfelejGkwsfYL8MSvghaI98MdIWXpfswWlRVArfGd52RCVMTolN1F2UAREcdM8kZgUY9oNIIspwXp6423kOCiKKXrqethpBgbnP0jZudkZak84yIx7pQk4UQ==; 5:H3o50wjxnFIj+MRxmjpqsdhipShNm8PT1xQkTftmcQBCFRW7iQUluW2+77yBsvJf54dUmK8KWXjta4OsxMpxbQL765UpIErv1Uc8ypiix+jhpNKtmbVC6DPNcECvFhFj5wZTnZlSWh0xmZkC8W9m4G/RwUkBcGl80YhqbXMH5yw=; 7:wPGtPxC6DQQpZScqKZwVfe3DhraIuiJC1upbtQwy4JO5avBxea/abOfEgDZTcKwbEZx70s1u9UjZ8lFmqkxIXx+3wb2EBTDHIxoNN0Q5vROvwU7u3fnap+VsARWNiimtDjfi07q+jX/1/XJxHcmWRkTX+n1gFDfsALXKhBku1tYuVlJaDgswcepmYtJHi0RhP6qEflZ+l36KeHbtPx/VlxO2njndTOGMHTSgtCyULE7aVuvaA8BK0AqrN4H/C6N6 x-ms-office365-filtering-correlation-id: 1523a655-c17f-4385-faf2-08d61f9be003 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(7020095)(4652040)(8989299)(4534165)(4627221)(201703031133081)(201702281549075)(8990200)(5600074)(711020)(4618075)(2017052603328)(7193020); SRVR:DM5PR2101MB0727; x-ms-traffictypediagnostic: DM5PR2101MB0727: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(12401385986421)(228905959029699); x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(8121501046)(5005006)(3231355)(2280164)(944501410)(52105095)(2018427008)(93006095)(93001095)(3002001)(10201501046)(6055026)(149027)(150027)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(20161123560045)(20161123564045)(20161123558120)(201708071742011)(7699051)(76991041); SRVR:DM5PR2101MB0727; BCL:0; PCL:0; RULEID:; SRVR:DM5PR2101MB0727; x-forefront-prvs: 0802ADD973 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(1496009)(39860400002)(396003)(136003)(376002)(366004)(346002)(199004)(189003)(8676002)(81156014)(25786009)(446003)(2900100001)(102836004)(46003)(52116002)(6346003)(81166006)(11346002)(105586002)(2351001)(8936002)(386003)(4326008)(76176011)(6506007)(561924002)(10090500001)(5250100002)(7736002)(2616005)(486006)(305945005)(53376002)(6116002)(186003)(16799955002)(1076002)(2501003)(476003)(6916009)(99286004)(22452003)(97736004)(45954006)(71200400001)(71190400001)(72206003)(478600001)(966005)(6306002)(10290500003)(5640700003)(14454004)(36756003)(86362001)(19627235002)(6436002)(15188155005)(2906002)(86612001)(106356001)(54906003)(316002)(6486002)(256004)(6512007)(68736007)(5660300001)(53936002)(60540400001); DIR:OUT; SFP:1102; SCL:1; SRVR:DM5PR2101MB0727; H:DM5PR2101MB1128.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) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Christopher.Co@microsoft.com; x-microsoft-antispam-message-info: N7afn2UF6K4M75M2V7tnO0RfLGtWfbyElUFM8Eo59ERkKKoeuQqsfx7CxHy2TS//xist1PF++Un/3fmgOcz2s1/7/q8sBNOUKLiqg2VE3esNPLiOL9GGcvSaKDf97w3B5YpBKuoBLHUB5Be2P4H/ROSr63RB/aYfi44YApN/Uau7vonc3v+Wyy6UwIA+981C0WlpC8/mgfo4CUHRjZFxiYCgpXW9e2r4jecYZkCv1+amXl//zN8M7RiVm+92IQDbIU4aB4bLEawkA3yb8V5kbFDd8Rx/5IcUB1QUGcmrvCtqVgggTrezVCGPm2/Ds0aHYxf+JGl4oFFsWIpJWhQeZnhpF2l4ay9xUTpG6jjp2wVcvGGGKi+LGBsdKTPnm2JBzjxlOBQkrjmMTg5AQhFHoQLy5wwP29s/hgwjo0ChSY65Rc+J9OmRF5LoqEJj13JxOUvdZ/duo5nhBjKXRKCtg9/1HSr0Hje5TvrXbppRTCZEu8/WfSQBZKcDzjrrCosFQGs/so0cOQdEOGPRLnjxNUBBAUinSmamkbZBa/9g9fcJWTDvmS2dz9BcPOloQPp4 spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1523a655-c17f-4385-faf2-08d61f9be003 X-MS-Exchange-CrossTenant-originalarrivaltime: 21 Sep 2018 08:26:07.5909 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB0727 Subject: [PATCH edk2-platforms 15/27] Silicon/NXP: Add i.MX6 GPT Timer library X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Sep 2018 08:26:09 -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/Include/iMX6Timer.h | 24 ++ Silicon/NXP/iMX6Pkg/Library/TimerLib/TimerLib.c | 246 ++++++++++++++++++= ++ Silicon/NXP/iMX6Pkg/Library/TimerLib/TimerLib.inf | 45 ++++ 3 files changed, 315 insertions(+) diff --git a/Silicon/NXP/iMX6Pkg/Include/iMX6Timer.h b/Silicon/NXP/iMX6Pkg/= Include/iMX6Timer.h new file mode 100644 index 000000000000..fbac9d2a61c0 --- /dev/null +++ b/Silicon/NXP/iMX6Pkg/Include/iMX6Timer.h @@ -0,0 +1,24 @@ +/** @file +* +* Copyright (c) 2018 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. +* +**/ + +#ifndef _IMX6_TIMER_H_ +#define _IMX6_TIMER_H_ + +RETURN_STATUS +EFIAPI +TimerConstructor ( + VOID + ); + +#endif /* _IMX6_TIMER_H_ */ diff --git a/Silicon/NXP/iMX6Pkg/Library/TimerLib/TimerLib.c b/Silicon/NXP/= iMX6Pkg/Library/TimerLib/TimerLib.c new file mode 100644 index 000000000000..fa55cee242ef --- /dev/null +++ b/Silicon/NXP/iMX6Pkg/Library/TimerLib/TimerLib.c @@ -0,0 +1,246 @@ +/** @file +* +* Copyright (c) 2018 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 + +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 << GPT_PR_PRESCALER_WID)); + + // Set the frequency scale + MmioWrite32 ((UINTN)&pGpt->PR, FreqPreScale - 1); + +#if defined(CPU_IMX6DQ) || defined (CPU_IMX6DQP) + // Set GPT configuration: + // - GPT Enabled + // - Use the 24Mhz oscillator source + MmioWrite32 ((UINTN)&pGpt->CR, + (GPT_CR_EN_ENABLE << GPT_CR_EN_LSH) | + (GPT_CR_CLKSRC_CLK24M << GPT_CR_CLKSRC_LSH)); +#elif defined(CPU_IMX6SDL) || defined(CPU_IMX6SX) + // Set GPT configuration: + // - GPT Enabled + // - Enable 24 Mhz Oscillator + // - Use the 24Mhz oscillator source + MmioWrite32 ((UINTN)&pGpt->CR, + (GPT_CR_EN_ENABLE << GPT_CR_EN_LSH) | + (GPT_CR_EN_24M_ENABLE << GPT_CR_EN_24M_LSH) | + (GPT_CR_CLKSRC_CLK24M << GPT_CR_CLKSRC_LSH)); +#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 ( + PcdArmArchTimerFreqInHz)) / 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; + + pGpt =3D (PCSP_GPT_REGS)CSP_BASE_REG_PA_GPT; + return MmioRead32 ((UINTN)(&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 MAX_UINT64; + } + + 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 MultU64x32 ( + DivU64x32Remainder ( + Ticks, + TimerFreq, + &Remainder), + 1000000000U + ); + + // Frequency < 0x100000000, so Remainder < 0x100000000, + // then (Remainder * 1,000,000,000) will not overflow 64-bit. + NanoSeconds +=3D DivU64x32 ( + MultU64x32 ( + (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..4716dee65a5d --- /dev/null +++ b/Silicon/NXP/iMX6Pkg/Library/TimerLib/TimerLib.inf @@ -0,0 +1,45 @@ +## @file +# +# NULL instance of Timer Library as a template. +# +# A non-functional instance of the Timer Library that can be used as a te= mplate +# for the implementation of a functional timer library instance. This lib= rary +# instance can also be used to test build DXE, Runtime, DXE SAL, and DXE = SMM +# modules that require timer services as well as EBC modules that require +# timer services. +# +# Copyright (c) 2018 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 0x0001001A + 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] + ArmPkg/ArmPkg.dec + MdePkg/MdePkg.dec + Silicon/NXP/iMX6Pkg/iMX6Pkg.dec + +[LibraryClasses] + DebugLib + IoLib + +[Pcd] + gArmTokenSpaceGuid.PcdArmArchTimerFreqInHz --=20 2.16.2.gvfs.1.33.gf5370f1