From patchwork Tue Feb 20 17:49:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 128941 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp4898064ljc; Tue, 20 Feb 2018 09:50:08 -0800 (PST) X-Google-Smtp-Source: AH8x227XO1zphfDi33ySrgkI/OcX8/OlwOPOet4IbL3mT7+eifXj9dFsQIxPCwIEuK0hyDcQ3ADk X-Received: by 2002:a17:902:9a04:: with SMTP id v4-v6mr391781plp.252.1519149008030; Tue, 20 Feb 2018 09:50:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519149008; cv=none; d=google.com; s=arc-20160816; b=FD7XC6awIz7Ic634//8GpsjeDV4qjVngx8M0sGJo2Io+xRjOj6n1lZawKYYAuJ+1ZD cn1GX88VgNkmGahH/O54+AYckZGc4qni9GrA08xWcx/s8H6/p5gl2i/DVwD4wg4Nrayw AdNk7kue/YrDsqlhHAjFaBo9SqEMts/ufMS8hG6eAiTjy1uu4DFCX3ivcbvvqG4gUbEq z6LkdXNvXIFNgiq8BCXf2gkUbYCt+35j1QLECYigM7wJgzCs2F1jhNjZB63QtYmyvUus Qgh6F5M4s0QPYlCDN9EzuYqO1RFEP3l4PJSkHrUjsOrszEae+fvb7JBtDz+vpm2KwY6d UNcA== 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=J0AFvEw+77f5KO5gUNpnDEX/7UKNPMqESchbW1u0BHc=; b=CJa1KF3RzbzxXXyCGH7LkmIz+lUUnVL5YNHMe2/Fc32W1JQiu6OYMvbUEKIhSvfCxD wC6FJeezJHAZnWE7t9b7nP6WBupcXyp7QhTCDJ8TKbxqp91snUTiONnSQlSrW3YiBb3N z/7v7JnQHhMIx18DNGAeKPQBz78ZhxWdB3quQSqSBc09v+O9mDgzkiP80ucWnMN+/uM6 bhPDpQi7AoVHQnFv97M0Cjr1QKZepf4nv0kLLBRPHhuXK216DeaeuecB60M+eerqKU+N 9C+PVM5/ytP9jVDNwXH4q4DhJEa9/D3q3Q4Q/9YXyHkp0GgkdyE4ID4wu8sM6IctW8xx 9COg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=KOrkG1AT; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 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. [2001:19d0:306:5::1]) by mx.google.com with ESMTPS id u8-v6si164851plk.130.2018.02.20.09.50.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Feb 2018 09:50:08 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) client-ip=2001:19d0:306:5::1; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=KOrkG1AT; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 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 7982F21FD73CF; Tue, 20 Feb 2018 09:44:06 -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:c0c::244; helo=mail-wr0-x244.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-wr0-x244.google.com (mail-wr0-x244.google.com [IPv6:2a00:1450:400c:c0c::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 0687F21FD73C8 for ; Tue, 20 Feb 2018 09:44:03 -0800 (PST) Received: by mail-wr0-x244.google.com with SMTP id v65so16793784wrc.11 for ; Tue, 20 Feb 2018 09:50:02 -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=dzRKFSaO1bmI29LeUUjeoeBYAGu84mYY5BMsMXXPRFg=; b=KOrkG1ATxcF1tRbj02IGuzSpHVGTYM4U7fUHp3NGlBJNxOka272HtTIsYSKKAyUJS3 HmyfxRzRCQSg5SEf0A2xlWbiEkTqX8c1r7UySPaAKXMaPc7vpuBUTDWqvFjL9mM0MJVa V+wNZxE8bjNUEWOj+KNgrYO5GMBWJNbk2f6qw= 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=dzRKFSaO1bmI29LeUUjeoeBYAGu84mYY5BMsMXXPRFg=; b=IiNucugRQlIxpiFpaBz5NAmgc7l+DKMMh/uGVgVLad4nQFdhH2RkIkuX1Vq2jIUHCe AQ3+lBgUONvey8j/lBOy9A990+VC3d0zLby9c0mw5b4jdfSTgRMac9q+4fsSaBGeSO6y GSWK5FRg026ewhFlecHqZfgwZUEdXs5WGT8fhm9BlH+RlTBHxLHv6H+M6MmfZoBz/feX 5ej88XttCGUuJP7++/1/fcGsy4K/VR6gh6fyob/WyvxxhOBq7CUqp25ywAp+wxymSX1q SLWI+Gjby1cGGBQqFCYbKwUhxDWSrmKDxo00/+W3E2F+WZmbXCG+OJLEQpU2s3UZtggz cWmg== X-Gm-Message-State: APf1xPBjHWsi1gmDqcjbvDEKx4Eo+yNcjC41Wk9x7YRZRLK10rNOtfPm KL9AkTYpF4/+0vy0AJVIvNs2G3ij3nE= X-Received: by 10.28.22.145 with SMTP id 139mr914339wmw.56.1519149000725; Tue, 20 Feb 2018 09:50:00 -0800 (PST) Received: from localhost.localdomain ([105.149.187.179]) by smtp.gmail.com with ESMTPSA id u63sm18282208wrc.26.2018.02.20.09.49.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Feb 2018 09:49:59 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org Date: Tue, 20 Feb 2018 17:49:40 +0000 Message-Id: <20180220174944.525-4-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180220174944.525-1-ard.biesheuvel@linaro.org> References: <20180220174944.525-1-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH edk2-platforms v2 3/7] Platform/NinetySixBoards: 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/NinetySixBoards/NinetySixBoardsI2cDxe/NinetySixBoardsI2cDxe.c | 202 ++++++++++++++++++++ Platform/NinetySixBoards/NinetySixBoardsI2cDxe/NinetySixBoardsI2cDxe.inf | 51 +++++ 2 files changed, 253 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/NinetySixBoards/NinetySixBoardsI2cDxe/NinetySixBoardsI2cDxe.c b/Platform/NinetySixBoards/NinetySixBoardsI2cDxe/NinetySixBoardsI2cDxe.c new file mode 100644 index 000000000000..9a779626b19f --- /dev/null +++ b/Platform/NinetySixBoards/NinetySixBoardsI2cDxe/NinetySixBoardsI2cDxe.c @@ -0,0 +1,202 @@ +/** @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 (*Device == NULL) { + *Device = &Bus->Devices[0]; + } else if (*Device >= &Bus->Devices[0] && + *Device < &Bus->Devices[Bus->NumDevices - 2]) { + ++*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 (&gNinetySixBoardsMezzanineProtocolGuid, NULL, + (VOID **)&mMezzanine); + ASSERT_EFI_ERROR (Status); + + RegisterI2cBus (&gNinetySixBoardsI2c0MasterGuid, &mI2cBus0, + mMezzanine->I2c0NumDevices, mMezzanine->I2c0DeviceArray); + RegisterI2cBus (&gNinetySixBoardsI2c1MasterGuid, &mI2cBus1, + mMezzanine->I2c1NumDevices, mMezzanine->I2c1DeviceArray); + + return EFI_SUCCESS; +} diff --git a/Platform/NinetySixBoards/NinetySixBoardsI2cDxe/NinetySixBoardsI2cDxe.inf b/Platform/NinetySixBoards/NinetySixBoardsI2cDxe/NinetySixBoardsI2cDxe.inf new file mode 100644 index 000000000000..9875566b6425 --- /dev/null +++ b/Platform/NinetySixBoards/NinetySixBoardsI2cDxe/NinetySixBoardsI2cDxe.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 = NinetySixBoardsI2cDxe + FILE_GUID = a59176bc-a151-49c8-b54a-b4ac96f436c3 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 0.1 + ENTRY_POINT = EntryPoint + +[Sources] + NinetySixBoardsI2cDxe.c + +[Packages] + MdePkg/MdePkg.dec + Platform/NinetySixBoards/NinetySixBoards.dec + +[LibraryClasses] + DebugLib + UefiBootServicesTableLib + UefiDriverEntryPoint + UefiLib + +[Protocols] + gNinetySixBoardsMezzanineProtocolGuid ## CONSUMES + gEfiI2cBusConfigurationManagementProtocolGuid ## PRODUCES + gEfiI2cEnumerateProtocolGuid ## PRODUCES + gEfiI2cMasterProtocolGuid ## CONSUMES + +[Guids] + gNinetySixBoardsI2c0MasterGuid + gNinetySixBoardsI2c1MasterGuid + +[FixedPcd] + gNinetySixBoardsTokenSpaceGuid.PcdI2c0BusFrequencyHz + gNinetySixBoardsTokenSpaceGuid.PcdI2c1BusFrequencyHz + +[Depex] + gNinetySixBoardsMezzanineProtocolGuid AND gNinetySixBoardsI2c0MasterGuid OR gNinetySixBoardsI2c1MasterGuid