From patchwork Fri Feb 23 15:40:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 129392 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp744751lja; Fri, 23 Feb 2018 07:41:13 -0800 (PST) X-Google-Smtp-Source: AH8x227vTulcNaEfzJP4O65mA1HTESBxKs/l38ved1w5gVI/uCAYKcMXxKCvS7r/ZsiyA1YeMAMl X-Received: by 2002:a17:902:8d81:: with SMTP id v1-v6mr2091720plo.430.1519400473560; Fri, 23 Feb 2018 07:41:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519400473; cv=none; d=google.com; s=arc-20160816; b=nDxyX6nveG6MjMSmB02+dThl34zBsrN+N9mpoK95REV8/92EfToYZCMLTh4eDIp2P4 7uwkEp38JmCp1TzpGlLaFE9wjKN9R4td8+3eULsQ0jPZY23mjFNLXhGnNlGZBGt46oOg 8kYa6TKXFDQWFGJicHUNhi5qIH978fDh7pCqDL6L98dFmKOP76I54B01h/YhSJZqzHtd xkoCFLlxec3fNlcIJ7L8qz7pwPFWjC2l5EhDXE4DP0u3pHbOZ8xklwP3tJMs5lNeUcL+ Z0GcbHdmXBmQ89MHivHpnmBbiQJMB4dX7wHF4f2ami5c7Vxc/mlh7rJUHVax9HLNy1iu HGPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=4p01IR51UChvJizG9jWJLCUJMPD9GXyTHQL0BxY7WM8=; b=KLk4CNQvL2lJ2n89rvx5JCZhOrQ1a7x9dXj82P0S/zNf704J+Is3cgsFZ/OmZ1hTbB iiWjjrgiBmxCPySavw/QDrIUlEmotHfK1glFCHeS/mCWYSlArA6uHViULqiW685DzEF6 uhnJS5AhduzCdCjf8UEmO2LxR/pKpiPBfZJ0w844h+M77Amttwoi/Pdx7DfWtFckpx+3 +QmcOOov1DtfFRKlfDp/PIe9T10Zi3zCqvYu23Ag+4Nha6134tJGKom5qh6DBdilJ5xp tCg+dS6vJxBQPTa/sKn+Fe4FsYKov2rnbLDsNioPDCbutl2hIgUizBRb8IYGf1ItXByh ZLUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=Rr2x1aN6; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org. [198.145.21.10]) by mx.google.com with ESMTPS id k186si1659380pgc.15.2018.02.23.07.41.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Feb 2018 07:41:13 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) client-ip=198.145.21.10; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=Rr2x1aN6; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 0811721CB2E20; Fri, 23 Feb 2018 07:35:11 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2a00:1450:400c:c09::244; helo=mail-wm0-x244.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-wm0-x244.google.com (mail-wm0-x244.google.com [IPv6:2a00:1450:400c:c09::244]) (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 E1EEA21FD73D5 for ; Fri, 23 Feb 2018 07:35:08 -0800 (PST) Received: by mail-wm0-x244.google.com with SMTP id t3so5430089wmc.2 for ; Fri, 23 Feb 2018 07:41:10 -0800 (PST) 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=ezIbCjg3eKOeXdPeiOG39Q3U8uY4SEqaumdSAPCAp5Y=; b=Rr2x1aN6DagSrIcJSeWj/LT2PJ6OwuM2eksB4ACwDdAT3aH4OKGBgFwC182VHlgA94 Xb/qXObOG4zEIToR6z+hl5HugS2SltnBU+CMQ5MhK9YwhmVUdR3Ki12g2y/Pvhe3X5mr vxRdaCRkI6b11iGlKQynxkgIVzPc0IeEimnHU= 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=ezIbCjg3eKOeXdPeiOG39Q3U8uY4SEqaumdSAPCAp5Y=; b=LMUIq/p+d4I1psn9nxZyams+uzlbYsAUWSNpsZPArOfaigbLfm9FE6ADLI9mNatICu 3Mqo0hx2iFkFVSUh1GWhxlugncYVmBhITLE6DE0iBG+fpVod8QtEYq91Ll4hHxcCpsXo 1Fk19JvEaKZWI2AvCg33exaJkwCEK+EJrjHUxyhvkwCaEO9PANrZ4oJaEgckplBgggof e3wOYZX+w24iyii6m7HnkLSexi0EyIghveWccWPMng4IFm12R1hkTjmHXSn479SMKq0m 7xRsZl5PxJ7XPf1HGx1I8lDz0b188gDgM3YUJkWFMsYyBOQMvG2V/YaQV8ujMwrJrBAH Xl0A== X-Gm-Message-State: APf1xPBw9qSExXMjICnHeHCcHn72MkLUASPvbdQRY1tDYiAdKKJoDQgu YpjDmXE2SPk/LXVlXCS7QBpdEvGeoII= X-Received: by 10.28.30.130 with SMTP id e124mr2204734wme.153.1519400468616; Fri, 23 Feb 2018 07:41:08 -0800 (PST) Received: from localhost.localdomain ([196.90.4.100]) by smtp.gmail.com with ESMTPSA id 188sm2273215wmg.29.2018.02.23.07.41.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Feb 2018 07:41:07 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org Date: Fri, 23 Feb 2018 15:40:48 +0000 Message-Id: <20180223154052.9828-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180223154052.9828-1-ard.biesheuvel@linaro.org> References: <20180223154052.9828-1-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH edk2-platforms v3 2/6] Platform/96Boards: introduce I2C driver X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: leif.lindholm@linaro.org, Ard Biesheuvel MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" Implement a I2C DXE driver that wires up the I2C devices exposed by a 96boards mezzanine into the EDK2 I2C stack. Note that this requires the platform to identify its I2C master implementations using special GUIDs-as-protocols. It also assumes [for now] that I2C buses are not shared between the 96boards connector and other platform peripherals. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ard Biesheuvel --- Platform/96Boards/96BoardsI2cDxe/96BoardsI2cDxe.c | 206 ++++++++++++++++++++ Platform/96Boards/96BoardsI2cDxe/96BoardsI2cDxe.inf | 51 +++++ 2 files changed, 257 insertions(+) -- 2.11.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel diff --git a/Platform/96Boards/96BoardsI2cDxe/96BoardsI2cDxe.c b/Platform/96Boards/96BoardsI2cDxe/96BoardsI2cDxe.c new file mode 100644 index 000000000000..79bb9ed5ffee --- /dev/null +++ b/Platform/96Boards/96BoardsI2cDxe/96BoardsI2cDxe.c @@ -0,0 +1,206 @@ +/** @file + + Copyright (c) 2018, Linaro, Ltd. 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. + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +STATIC MEZZANINE_PROTOCOL *mMezzanine; + +typedef struct { + EFI_I2C_ENUMERATE_PROTOCOL I2cEnumerate; + EFI_I2C_BUS_CONFIGURATION_MANAGEMENT_PROTOCOL I2cConfigManagement; + EFI_HANDLE I2cMasterHandle; + UINT32 BusFrequency; + UINTN NumDevices; + CONST EFI_I2C_DEVICE *Devices; +} I2C_BUS; + +STATIC +EFI_STATUS +EFIAPI +I2cEnumerate ( + IN CONST EFI_I2C_ENUMERATE_PROTOCOL *This, + IN OUT CONST EFI_I2C_DEVICE **Device + ) +{ + I2C_BUS *Bus; + + if (Device == NULL) { + return EFI_INVALID_PARAMETER; + } + + Bus = BASE_CR (This, I2C_BUS, I2cEnumerate); + + if (Bus->NumDevices == 0 || + (Bus->NumDevices > 0 && *Device == &Bus->Devices[Bus->NumDevices - 1])) { + *Device = NULL; + } else if (*Device == NULL) { + *Device = &Bus->Devices[0]; + } else if (Bus->NumDevices > 1 && + *Device >= &Bus->Devices[0] && + *Device < &Bus->Devices[Bus->NumDevices - 1]) { + ++*Device; + } else { + return EFI_NO_MAPPING; + } + return EFI_SUCCESS; +} + +STATIC +EFI_STATUS +EFIAPI +I2cGetBusFrequency ( + IN CONST EFI_I2C_ENUMERATE_PROTOCOL *This, + IN UINTN I2cBusConfiguration, + OUT UINTN *BusClockHertz + ) +{ + I2C_BUS *Bus; + + if (BusClockHertz == NULL) { + return EFI_INVALID_PARAMETER; + } + + if (I2cBusConfiguration > 0) { + return EFI_NO_MAPPING; + } + + Bus = BASE_CR (This, I2C_BUS, I2cEnumerate); + + *BusClockHertz = Bus->BusFrequency; + + return EFI_SUCCESS; +} + +STATIC +EFI_STATUS +EFIAPI +EnableI2cBusConfiguration ( + IN CONST EFI_I2C_BUS_CONFIGURATION_MANAGEMENT_PROTOCOL *This, + IN UINTN I2cBusConfiguration, + IN EFI_EVENT Event OPTIONAL, + IN EFI_STATUS *I2cStatus OPTIONAL + ) +{ + EFI_I2C_MASTER_PROTOCOL *I2cMaster; + EFI_STATUS Status; + UINTN BusClockHertz; + I2C_BUS *Bus; + + if (I2cBusConfiguration > 0) { + return EFI_NO_MAPPING; + } + + Bus = BASE_CR (This, I2C_BUS, I2cConfigManagement); + + Status = gBS->HandleProtocol (Bus->I2cMasterHandle, + &gEfiI2cMasterProtocolGuid, (VOID **)&I2cMaster); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: gBS->HandleProtocol() failed - %r\n", + __FUNCTION__, Status)); + return Status; + } + + BusClockHertz = Bus->BusFrequency; + Status = I2cMaster->SetBusFrequency (I2cMaster, &BusClockHertz); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: I2cMaster->SetBusFrequency() failed - %r\n", + __FUNCTION__, Status)); + return Status; + } + + if (Event != NULL) { + *I2cStatus = EFI_SUCCESS; + gBS->SignalEvent (Event); + } + return EFI_SUCCESS; +} + +STATIC I2C_BUS mI2cBus0 = { + { I2cEnumerate, I2cGetBusFrequency }, + { EnableI2cBusConfiguration }, + NULL, + FixedPcdGet32 (PcdI2c0BusFrequencyHz), + 0, + NULL, +}; + +STATIC I2C_BUS mI2cBus1 = { + { I2cEnumerate, I2cGetBusFrequency }, + { EnableI2cBusConfiguration }, + NULL, + FixedPcdGet32 (PcdI2c1BusFrequencyHz), + 0, + NULL, +}; + +STATIC +VOID +RegisterI2cBus ( + IN EFI_GUID *Guid, + IN I2C_BUS *I2cBus, + IN UINTN NumDevices, + IN CONST EFI_I2C_DEVICE *Devices + ) +{ + EFI_STATUS Status; + UINTN BufferSize; + + BufferSize = sizeof (EFI_HANDLE); + Status = gBS->LocateHandle (ByProtocol, Guid, NULL, &BufferSize, + &I2cBus->I2cMasterHandle); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "%a: gBS->LocateHandle() failed - %r\n", __FUNCTION__, + Status)); + return; + } + + I2cBus->NumDevices = NumDevices; + I2cBus->Devices = Devices; + + Status = gBS->InstallMultipleProtocolInterfaces (&I2cBus->I2cMasterHandle, + &gEfiI2cEnumerateProtocolGuid, + &I2cBus->I2cEnumerate, + &gEfiI2cBusConfigurationManagementProtocolGuid, + &I2cBus->I2cConfigManagement, + NULL); + ASSERT_EFI_ERROR (Status); +} + +EFI_STATUS +EFIAPI +EntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + Status = gBS->LocateProtocol (&g96BoardsMezzanineProtocolGuid, NULL, + (VOID **)&mMezzanine); + ASSERT_EFI_ERROR (Status); + + RegisterI2cBus (&g96BoardsI2c0MasterGuid, &mI2cBus0, + mMezzanine->I2c0NumDevices, mMezzanine->I2c0DeviceArray); + RegisterI2cBus (&g96BoardsI2c1MasterGuid, &mI2cBus1, + mMezzanine->I2c1NumDevices, mMezzanine->I2c1DeviceArray); + + return EFI_SUCCESS; +} diff --git a/Platform/96Boards/96BoardsI2cDxe/96BoardsI2cDxe.inf b/Platform/96Boards/96BoardsI2cDxe/96BoardsI2cDxe.inf new file mode 100644 index 000000000000..7eb1fc503062 --- /dev/null +++ b/Platform/96Boards/96BoardsI2cDxe/96BoardsI2cDxe.inf @@ -0,0 +1,51 @@ +## @file +# +# Copyright (c) 2018, Linaro Ltd. 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 = 0x0001001A + BASE_NAME = 96BoardsI2cDxe + FILE_GUID = a59176bc-a151-49c8-b54a-b4ac96f436c3 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 0.1 + ENTRY_POINT = EntryPoint + +[Sources] + 96BoardsI2cDxe.c + +[Packages] + MdePkg/MdePkg.dec + Platform/96Boards/96Boards.dec + +[LibraryClasses] + DebugLib + UefiBootServicesTableLib + UefiDriverEntryPoint + UefiLib + +[Protocols] + g96BoardsMezzanineProtocolGuid ## CONSUMES + gEfiI2cBusConfigurationManagementProtocolGuid ## PRODUCES + gEfiI2cEnumerateProtocolGuid ## PRODUCES + gEfiI2cMasterProtocolGuid ## CONSUMES + +[Guids] + g96BoardsI2c0MasterGuid + g96BoardsI2c1MasterGuid + +[FixedPcd] + g96BoardsTokenSpaceGuid.PcdI2c0BusFrequencyHz + g96BoardsTokenSpaceGuid.PcdI2c1BusFrequencyHz + +[Depex] + g96BoardsMezzanineProtocolGuid AND g96BoardsI2c0MasterGuid OR g96BoardsI2c1MasterGuid