From patchwork Tue Aug 21 11:35:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haojian Zhuang X-Patchwork-Id: 144711 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp5155598ljj; Tue, 21 Aug 2018 04:35:51 -0700 (PDT) X-Google-Smtp-Source: AA+uWPy/6f+owVTaTj+0GJ3bEpK4ngVvypEyYWd2gH/KUtD1Jg0x1heLTr5vpiu9rbK85WCFMr+J X-Received: by 2002:a17:902:9893:: with SMTP id s19-v6mr49334462plp.130.1534851351147; Tue, 21 Aug 2018 04:35:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534851351; cv=none; d=google.com; s=arc-20160816; b=I7kAV5EtJAXm3ePxZWj+QL+AZXcSZmcE7GlhIf+2EjN5kvaBjAsnYZLU6N2caLM7V+ 4ktPx6jNQ/cO9R+iKLhvnSKc1xljRdWSyaYbhyD9uTtTW7h8b6+M4r2PJwntfQMNmetO Y6UX4obyhaRbvOzNvaPVcuxxGQLAEwYGYlxieW2bIFFqbWbSCutrjOyWkx8+XCdKafun IC10BuFJWjw1jRn868hf1hw7/4H0zwZu6MW2oPpk7DUHIEbMdyyRsgE48VIWB/1cCYdN CRYy4jzW1AOZfrW0q5GVTpT/ckVmPgcNdWY48+DV9EcYTg5Xl4oL/dFwJetTlFtq9fu9 aAOw== 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 :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=RZwEQFexb4AWO/uDt5dUb5gyYVDatsFUI3Ao01dtWvI=; b=OQrmohYh+AM8wzj+s3bL9R4YDO+Q64IWIsaqPaU2rncPZpQEacvuOmFXF4y7BBY7Uq X7fvHImm2F1tEaU/xe2VQoseBFVZJsimH+xqcW3U7lTfOHt5BmBoWWIJM4ayN56br8sV o6JSVE6nUCQuhfjyeGJogH2gHVrbT1x6a9Dgp4wP51UefHf/fJN9FXw+/3O7afdF8Mn2 KvdZKoMdxCUHyijGR0uPeI6JdlhuUgMysx4e5/IPjsUrsj0SXa3taZi0qV7llcmrFqFf VpFMa2R5/2BzKNpv8U2VuA5n1JHwvlAL5LbhsmtGDzO3Xq7O/FapG4b/W6pVGWxyT6tC 364Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=YffhfAlr; 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 l63-v6si12823271pfg.326.2018.08.21.04.35.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Aug 2018 04:35:51 -0700 (PDT) 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=YffhfAlr; 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 B0A2F210F93CE; Tue, 21 Aug 2018 04:35:50 -0700 (PDT) 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=2607:f8b0:400e:c01::241; helo=mail-pl0-x241.google.com; envelope-from=haojian.zhuang@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-pl0-x241.google.com (mail-pl0-x241.google.com [IPv6:2607:f8b0:400e:c01::241]) (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 4997F210F92B3 for ; Tue, 21 Aug 2018 04:35:49 -0700 (PDT) Received: by mail-pl0-x241.google.com with SMTP id w14-v6so8681182plp.6 for ; Tue, 21 Aug 2018 04:35:49 -0700 (PDT) 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=pzHhZbnR9UFQiY+GzflYsDx2MK9fn72+/hT/g6ANg+g=; b=YffhfAlrQ8cnUFJC/pqlm1agLFIH26MsRJZ9QQak5r4BpO5GleSkjwZxd6R2JPFAuY AdnmTuCUCbWqr+7VxgASPbju8eXJT3g3WSpW8eaNnWIEoMfNLUKkjgICNXg5FVjuzr9N oIUIM5UFxyQJH/77M4A2INUBveyZZhPLbYMDA= 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=pzHhZbnR9UFQiY+GzflYsDx2MK9fn72+/hT/g6ANg+g=; b=SOn8J+30VbnIPHf1mqGeb479pFkSeCxULJF16UtDSDBEm5T9DWiPTivvgZ/KQHyUHr izwdA+eAHsNzomaU7eI1laOvnTMNqHIyz/+Ll8a0mAP+q+tki9kQb0BhPEiJfkNqdlaW 7uco0JrPMawy7Aa3yU/1oCqsheCSQKPfKNCLK20bNn9DmIuEU9pricY7+SV4/zf8YWgJ CSaZ1jurP730tPUmyz5r/OOuwsVZc2G+VyyX0dI6IaaEOLnVFWQ3MZlGqX6zmA3YvM3V u5ves0jlWzFxhmDNDeIn48V4HghGqN0CQaamsk6NrTo/hWTHHlxKZrELKuJWptJih7N5 jhjQ== X-Gm-Message-State: AOUpUlF6Cc2pYNjkLiskxsGJaOuSmC1013Rjt4QPQmTh6HqhKsatFUEi KNjnlfJhur4mgat3M0TYL777iLZHd1L/lA== X-Received: by 2002:a17:902:6946:: with SMTP id k6-v6mr49917361plt.268.1534851348487; Tue, 21 Aug 2018 04:35:48 -0700 (PDT) Received: from localhost.localdomain ([64.64.108.219]) by smtp.gmail.com with ESMTPSA id s14-v6sm21678388pfj.105.2018.08.21.04.35.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 21 Aug 2018 04:35:47 -0700 (PDT) From: Haojian Zhuang To: edk2-devel@lists.01.org Date: Tue, 21 Aug 2018 19:35:36 +0800 Message-Id: <1534851338-21419-3-git-send-email-haojian.zhuang@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1534851338-21419-1-git-send-email-haojian.zhuang@linaro.org> References: <1534851338-21419-1-git-send-email-haojian.zhuang@linaro.org> Subject: [edk2] [PATCH edk-platforms v1 2/4] Platform/HiKey: add usb platform driver 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: , MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" Implement DwUsb protocol in the usb platform driver on HiKey platform. Cc: Leif Lindholm Cc: Ard Biesheuvel Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Haojian Zhuang --- Platform/Hisilicon/HiKey/HiKeyUsbDxe/HiKeyUsbDxe.inf | 46 ++++ Platform/Hisilicon/HiKey/HiKeyUsbDxe/HiKeyUsbDxe.c | 280 ++++++++++++++++++++ 2 files changed, 326 insertions(+) -- 2.7.4 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel diff --git a/Platform/Hisilicon/HiKey/HiKeyUsbDxe/HiKeyUsbDxe.inf b/Platform/Hisilicon/HiKey/HiKeyUsbDxe/HiKeyUsbDxe.inf new file mode 100644 index 000000000000..bcba3f758c1a --- /dev/null +++ b/Platform/Hisilicon/HiKey/HiKeyUsbDxe/HiKeyUsbDxe.inf @@ -0,0 +1,46 @@ +#/** @file +# +# Copyright (c) 2018, Linaro. 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 = 0x00010019 + BASE_NAME = HiKeyUsbDxe + FILE_GUID = c5c7089e-9b00-448c-8b23-a552688e2833 + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = HiKeyUsbEntryPoint + +[Sources.common] + HiKeyUsbDxe.c + +[LibraryClasses] + DebugLib + IoLib + TimerLib + UefiBootServicesTableLib + UefiDriverEntryPoint + +[Protocols] + gDwUsbProtocolGuid + gEfiDriverBindingProtocolGuid + gEmbeddedGpioProtocolGuid + +[Packages] + EmbeddedPkg/Drivers/DwUsbDxe/DwUsbDxe.dec + EmbeddedPkg/EmbeddedPkg.dec + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + Platform/Hisilicon/HiKey/HiKey.dec + +[Depex] + TRUE diff --git a/Platform/Hisilicon/HiKey/HiKeyUsbDxe/HiKeyUsbDxe.c b/Platform/Hisilicon/HiKey/HiKeyUsbDxe/HiKeyUsbDxe.c new file mode 100644 index 000000000000..4d651746d590 --- /dev/null +++ b/Platform/Hisilicon/HiKey/HiKeyUsbDxe/HiKeyUsbDxe.c @@ -0,0 +1,280 @@ +/** @file +* +* Copyright (c) 2018, Linaro. 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 + +#include + + +#define USB_SEL_GPIO0_3 3 // GPIO 0_3 +#define USB_5V_HUB_EN 7 // GPIO 0_7 +#define USB_ID_DET_GPIO2_5 21 // GPIO 2_5 +#define USB_VBUS_DET_GPIO2_6 22 // GPIO 2_6 + +// +// Jumper on pin5-6 of J15 determines whether boot to fastboot +// +#define DETECT_J15_FASTBOOT 24 // GPIO 3_0 + +#define IOCG_GPIO0_BASE 0xF8001800 +#define IOCG_GPIO0_3_OFFSET 0x1C +#define IOCG_GPIO0_7_OFFSET 0x2C +#define IOCG_GPIO2_5_OFFSET 0x64 +#define IOCG_GPIO2_6_OFFSET 0x68 + +#define IOCG_PULLUP 1 +#define IOCG_PULLDOWN 2 + +#define USB_EYE_PATTERN 0x70533483 + +#define LANG_EN 0x409 + +STATIC EMBEDDED_GPIO *mGpio; + +STATIC +EFI_STATUS +HiKeyDetectUsbModeInit ( + IN VOID + ) +{ + EFI_STATUS Status; + + /* set pullup on both GPIO2_5 & GPIO2_6. It's required for inupt. */ + MmioWrite32 (IOCG_GPIO0_BASE + IOCG_GPIO2_5_OFFSET, IOCG_PULLUP); + MmioWrite32 (IOCG_GPIO0_BASE + IOCG_GPIO2_6_OFFSET, IOCG_PULLUP); + + Status = gBS->LocateProtocol ( + &gEmbeddedGpioProtocolGuid, + NULL, + (VOID **)&mGpio + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Can't locate gEmbeddedGpioProtocolGuid\n")); + return Status; + } + /* power on USB HUB */ + Status = mGpio->Set (mGpio, USB_5V_HUB_EN, GPIO_MODE_OUTPUT_0); + ASSERT_EFI_ERROR (Status); + /* start to detect USB device or host */ + Status = mGpio->Set (mGpio, USB_SEL_GPIO0_3, GPIO_MODE_OUTPUT_0); + ASSERT_EFI_ERROR (Status); + + Status = mGpio->Set (mGpio, USB_ID_DET_GPIO2_5, GPIO_MODE_INPUT); + ASSERT_EFI_ERROR (Status); + Status = mGpio->Set (mGpio, USB_VBUS_DET_GPIO2_6, GPIO_MODE_INPUT); + ASSERT_EFI_ERROR (Status); + return EFI_SUCCESS; +} + +UINTN +HiKeyGetUsbMode ( + IN VOID + ) +{ + EFI_STATUS Status; + UINTN GpioId, GpioVbus; + + Status = mGpio->Get (mGpio, USB_ID_DET_GPIO2_5, &GpioId); + ASSERT_EFI_ERROR (Status); + Status = mGpio->Get (mGpio, USB_VBUS_DET_GPIO2_6, &GpioVbus); + ASSERT_EFI_ERROR (Status); + + if ((GpioId == 1) && (GpioVbus == 0)) { + return USB_DEVICE_MODE; + } else if ((GpioId == 0) && (GpioVbus == 1)) { + return USB_CABLE_NOT_ATTACHED; + } + return USB_HOST_MODE; +} + +EFI_STATUS +HiKeyUsbPhyInit ( + IN UINT8 Mode + ) +{ + UINTN Value; + UINT32 Data; + EFI_STATUS Status; + + Status = HiKeyDetectUsbModeInit (); + if (EFI_ERROR (Status)) { + return Status; + } + + // + //setup clock + // + MmioWrite32 (PERI_CTRL_BASE + SC_PERIPH_CLKEN0, PERIPH_CLKEN0_USBOTG); + do { + Value = MmioRead32 (PERI_CTRL_BASE + SC_PERIPH_CLKSTAT0); + } while ((Value & PERIPH_CLKEN0_USBOTG) == 0); + + // + //setup phy + // + Data = RST0_USBOTG_BUS | RST0_POR_PICOPHY | + RST0_USBOTG | RST0_USBOTG_32K; + MmioWrite32 (PERI_CTRL_BASE + SC_PERIPH_RSTDIS0, Data); + do { + Value = MmioRead32 (PERI_CTRL_BASE + SC_PERIPH_RSTSTAT0); + Value &= Data; + } while (Value); + + Value = MmioRead32 (PERI_CTRL_BASE + SC_PERIPH_CTRL4); + Value &= ~(CTRL4_PICO_SIDDQ | CTRL4_FPGA_EXT_PHY_SEL | + CTRL4_OTG_PHY_SEL); + Value |= CTRL4_PICO_VBUSVLDEXT | CTRL4_PICO_VBUSVLDEXTSEL; + MmioWrite32 (PERI_CTRL_BASE + SC_PERIPH_CTRL4, Value); + + if (HiKeyGetUsbMode () != Mode) { + return EFI_INVALID_PARAMETER; + } + // + // If Mode = 1, USB in Device Mode + // If Mode = 0, USB in Host Mode + // + if (Mode == USB_DEVICE_MODE) { + DEBUG ((DEBUG_INFO, "usb work as device mode.\n")); + + Value = MmioRead32 (PERI_CTRL_BASE + SC_PERIPH_CTRL5); + Value &= ~CTRL5_PICOPHY_BC_MODE; + MmioWrite32 (PERI_CTRL_BASE + SC_PERIPH_CTRL5, Value); + /* wait for stable */ + MicroSecondDelay (20000); + } else { + DEBUG ((DEBUG_INFO, "usb work as host mode.\n")); + + /* CTRL5 */ + Data = MmioRead32 (PERI_CTRL_BASE + SC_PERIPH_CTRL5); + Data &= ~CTRL5_PICOPHY_BC_MODE; + Data |= CTRL5_USBOTG_RES_SEL | CTRL5_PICOPHY_ACAENB | + CTRL5_PICOPHY_VDATDETENB | CTRL5_PICOPHY_DCDENB; + MmioWrite32 (PERI_CTRL_BASE + SC_PERIPH_CTRL5, Data); + /* wait for stable */ + MicroSecondDelay (20000); + /* + * Set the USB phy timing with tuned value that shows an eye + * pattern on oscillator. + */ + MmioWrite32 (PERI_CTRL_BASE + SC_PERIPH_CTRL8, USB_EYE_PATTERN); + /* wait for eye pattern effective */ + MicroSecondDelay (5000); + } + + return EFI_SUCCESS; +} + +EFI_STATUS +EFIAPI +HiKeyUsbGetLang ( + OUT CHAR16 *Lang, + OUT UINT8 *Length + ) +{ + if ((Lang == NULL) || (Length == NULL)) { + return EFI_INVALID_PARAMETER; + } + Lang[0] = LANG_EN; + *Length = sizeof (CHAR16); + return EFI_SUCCESS; +} + +EFI_STATUS +EFIAPI +HiKeyUsbGetManufacturer ( + OUT CHAR16 *Manufacturer, + OUT UINT8 *Length + ) +{ + CHAR16 DataUnicode[] = L"96Boards"; + + if ((Manufacturer == NULL) || (Length == NULL)) { + return EFI_INVALID_PARAMETER; + } + StrCpy (Manufacturer, DataUnicode); + /* include '\0' for string */ + *Length = (StrLen (DataUnicode) + 1) * sizeof (CHAR16); + return EFI_SUCCESS; +} + +EFI_STATUS +EFIAPI +HiKeyUsbGetProduct ( + OUT CHAR16 *Product, + OUT UINT8 *Length + ) +{ + CHAR16 DataUnicode[] = L"HiKey"; + + if ((Product == NULL) || (Length == NULL)) { + return EFI_INVALID_PARAMETER; + } + StrCpy (Product, DataUnicode); + /* include '\0' for string */ + *Length = (StrLen (DataUnicode) + 1) * sizeof (CHAR16); + return EFI_SUCCESS; +} + +EFI_STATUS +EFIAPI +HiKeyUsbGetSerialNo ( + OUT CHAR16 *SerialNo, + OUT UINT8 *Length + ) +{ + CHAR16 DataUnicode[] = L"0123456789abcdef"; + + if ((SerialNo == NULL) || (Length == NULL)) { + return EFI_INVALID_PARAMETER; + } + StrCpy (SerialNo, DataUnicode); + /* include '\0' for string */ + *Length = (StrLen (DataUnicode) + 1) * sizeof (CHAR16); + return EFI_SUCCESS; +} + +DW_USB_PROTOCOL mDwUsbDevice = { + HiKeyUsbGetLang, + HiKeyUsbGetManufacturer, + HiKeyUsbGetProduct, + HiKeyUsbGetSerialNo, + HiKeyUsbPhyInit +}; + +EFI_STATUS +EFIAPI +HiKeyUsbEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + Status = gBS->InstallProtocolInterface ( + &ImageHandle, + &gDwUsbProtocolGuid, + EFI_NATIVE_INTERFACE, + &mDwUsbDevice + ); + return Status; +}