From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.81]) by mx.groups.io with SMTP id smtpd.web12.22866.1574177447965463343 for ; Tue, 19 Nov 2019 07:30:48 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=efLknFCh; spf=pass (domain: redhat.com, ip: 207.211.31.81, mailfrom: msalter@redhat.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574177447; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=E2hzbtJPLi52fls/8bUGRZWCY+Wtc6HLNV/pRqrYHFI=; b=efLknFChgqk5jvjTEVgyVu1uPI3/y4tWd0kwMea/gUTHVqI3+NXDQtGaGiHHIYgXAit67H gvGlNzVLvLi2rGookyBRvzQBfuP6L13TGvkhwu+oJ1vnTC4pkI56Tx/1bfH9erd3tiuXzr WSpj4DFx5M8iSSzdsZbhcPYRY0fJmTg= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-195-YxhGu2UAMRqyXq6zHIp59w-1; Tue, 19 Nov 2019 10:30:45 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8D0FF8D0CC5; Tue, 19 Nov 2019 15:30:37 +0000 (UTC) Received: from ovpn-124-164.rdu2.redhat.com (ovpn-124-164.rdu2.redhat.com [10.10.124.164]) by smtp.corp.redhat.com (Postfix) with ESMTP id A1F1D5C1B2; Tue, 19 Nov 2019 15:30:36 +0000 (UTC) Message-ID: Subject: Re: [edk2-devel] [platform/devel-riscv-v2 PATCHv5 09/18] U5SeriesPkg/SerialIoLib: Platform Serial Port library From: "Mark Salter" To: devel@edk2.groups.io, abner.chang@hpe.com Cc: Michael D Kinney , Ard Biesheuvel , Leif Lindholm , Gilbert Chen , Palmer Dabbelt Date: Tue, 19 Nov 2019 10:30:36 -0500 In-Reply-To: <1572236433-15404-10-git-send-email-abner.chang@hpe.com> References: <1572236433-15404-1-git-send-email-abner.chang@hpe.com> <1572236433-15404-10-git-send-email-abner.chang@hpe.com> Organization: Red Hat, Inc User-Agent: Evolution 3.34.1 (3.34.1-1.fc31) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-MC-Unique: YxhGu2UAMRqyXq6zHIp59w-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Mon, 2019-10-28 at 12:20 +0800, Abner Chang wrote: > Serial Port library for U5 series platform. >=20 > Signed-off-by: Abner Chang >=20 > Cc: Michael D Kinney > Cc: Ard Biesheuvel > Cc: Leif Lindholm > Cc: Gilbert Chen > Cc: Palmer Dabbelt > Signed-off-by: Abner Chang > --- > .../Library/SerialIoLib/SerialIoLib.inf | 38 ++++ > .../Library/SerialIoLib/SerialPortLib.c | 253 +++++++++++++++= ++++++ > .../Library/SerialIoLib/U5SerialPortLib.uni | 16 ++ > 3 files changed, 307 insertions(+) > create mode 100644 Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/Seria= lIoLib.inf > create mode 100644 Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/Seria= lPortLib.c > create mode 100644 Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/U5Ser= ialPortLib.uni >=20 > diff --git a/Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/SerialIoLib.= inf b/Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/SerialIoLib.inf > new file mode 100644 > index 0000000..0044f84 > --- /dev/null > +++ b/Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/SerialIoLib.inf > @@ -0,0 +1,38 @@ > +## @file > +# Library instance for SerialIo library class > +# > +# Copyright (c) 2019, Hewlett Packard Enterprise Development LP. All ri= ghts reserved.
> +# > +# SPDX-License-Identifier: BSD-2-Clause-Patent > +# > +## > + > +[Defines] > + INF_VERSION =3D 0x0001001b > + BASE_NAME =3D U5SerialPortLib > + MODULE_UNI_FILE =3D U5SerialPortLib.uni > + FILE_GUID =3D FCC4FD2B-2FF6-4FFA-B363-7C1111E5DCE= 9 > + MODULE_TYPE =3D BASE > + VERSION_STRING =3D 1.0 > + LIBRARY_CLASS =3D SerialPortLib > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D RISCV64 > +# > +[Packages] > + MdePkg/MdePkg.dec > + RiscVPlatformPkg/RiscVPlatformPkg.dec > + RiscVPkg/RiscVPkg.dec > + Platform/SiFive/U5SeriesPkg/U5SeriesPkg.dec > + > +[LibraryClasses] > + BaseLib > + IoLib > + RiscVOpensbiLib > + > +[FixedPcd] > + gSiFiveU5SeriesPlatformsPkgTokenSpaceGuid.PcdU5UartBase > + > +[Sources] > + SerialPortLib.c > diff --git a/Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/SerialPortLi= b.c b/Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/SerialPortLib.c > new file mode 100644 > index 0000000..5e06515 > --- /dev/null > +++ b/Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/SerialPortLib.c > @@ -0,0 +1,253 @@ > +/** @file > + UART Serial Port library functions > + > + Copyright (c) 2019, Hewlett Packard Enterprise Development LP. All rig= hts reserved.
> + > + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#include > +#include > +#include > +#include // Reference to header= file in opensbi > + > +//--------------------------------------------- > +// UART Register Offsets > +//--------------------------------------------- > + > +#define UART_REG_IP 5 > +#define UART_IP_RXWM 0x02 > + > +//--------------------------------------------- > +// UART Settings > +//--------------------------------------------- > + > +#define UART_BAUDRATE 115200 > +#define SYS_CLK 100000000 ^^^^^^^^^^ This should be 1000000000 1GHz, not 100MHz > + > +BOOLEAN Initiated =3D FALSE; > + > +/** > + Initialize the serial device hardware. > + > + If no initialization is required, then return RETURN_SUCCESS. > + If the serial device was successfuly initialized, then return RETURN_S= UCCESS. > + If the serial device could not be initialized, then return RETURN_DEVI= CE_ERROR. > + > + @retval RETURN_SUCCESS The serial device was initialized. > + @retval RETURN_DEVICE_ERROR The serail device could not be initializ= ed. > + > +**/ > +RETURN_STATUS > +EFIAPI > +SerialPortInitialize ( > + VOID > + ) > +{ > + if (Initiated) { > + return RETURN_SUCCESS; > + } > + if (sifive_uart_init(FixedPcdGet32(PcdU5UartBase), SYS_CLK / 2, UART_B= AUDRATE) !=3D 0) { > + return EFI_DEVICE_ERROR; > + } > + Initiated =3D TRUE; > + return RETURN_SUCCESS; > +} > + > +/** > + Write data from buffer to serial device. > + > + Writes NumberOfBytes data bytes from Buffer to the serial device. > + The number of bytes actually written to the serial device is returned. > + If the return value is less than NumberOfBytes, then the write operati= on failed. > + > + If Buffer is NULL, then ASSERT(). > + > + If NumberOfBytes is zero, then return 0. > + > + @param Buffer Pointer to the data buffer to be written. > + @param NumberOfBytes Number of bytes to written to the serial devi= ce. > + > + @retval 0 NumberOfBytes is 0. > + @retval >0 The number of bytes written to the serial dev= ice. > + If this value is less than NumberOfBytes, the= n the write operation failed. > + > +**/ > +UINTN > +EFIAPI > +SerialPortWrite ( > + IN UINT8 *Buffer, > + IN UINTN NumberOfBytes > + ) > +{ > + UINTN Index; > + > + if (Buffer =3D=3D NULL || Initiated =3D=3D FALSE) { > + return 0; > + } > + > + for(Index =3D 0; Index < NumberOfBytes; Index ++) { > + sifive_uart_putc (Buffer [Index]); > + } > + > + return Index; > +} > + > +/** > + Reads data from a serial device into a buffer. > + > + @param Buffer Pointer to the data buffer to store the data = read from the serial device. > + @param NumberOfBytes Number of bytes to read from the serial devic= e. > + > + @retval 0 NumberOfBytes is 0. > + @retval >0 The number of bytes read from the serial devi= ce. > + If this value is less than NumberOfBytes, the= n the read operation failed. > + > +**/ > +UINTN > +EFIAPI > +SerialPortRead ( > + OUT UINT8 *Buffer, > + IN UINTN NumberOfBytes > + ) > +{ > + UINTN Index; > + > + if (NULL =3D=3D Buffer || Initiated =3D=3D FALSE) { > + return 0; > + } > + > + for(Index =3D 0; Index < NumberOfBytes; Index ++) { > + Buffer [Index] =3D (UINT8)sifive_uart_getc (); > + } > + > + return Index; > +} > + > +/** > + Polls a serial device to see if there is any data waiting to be read. > + > + Polls aserial device to see if there is any data waiting to be read. > + If there is data waiting to be read from the serial device, then TRUE = is returned. > + If there is no data waiting to be read from the serial device, then FA= LSE is returned. > + > + @retval TRUE Data is waiting to be read from the serial de= vice. > + @retval FALSE There is no data waiting to be read from the = serial device. > + > +**/ > +BOOLEAN > +EFIAPI > +SerialPortPoll ( > + VOID > + ) > +{ > + STATIC volatile UINT32 * const uart =3D (UINT32 *)FixedPcdGet32(PcdU5U= artBase); > + UINT32 IP; > + > + if (Initiated =3D=3D FALSE) { > + return FALSE; > + } > + IP =3D MmioRead32 ((UINTN)(uart + UART_REG_IP)); > + if(IP & UART_IP_RXWM) { > + return TRUE; > + } > + else { > + return FALSE; > + } > +} > + > +/** > + Sets the control bits on a serial device. > + > + @param Control Sets the bits of Control that are settab= le. > + > + @retval RETURN_SUCCESS The new control bits were set on the ser= ial device. > + @retval RETURN_UNSUPPORTED The serial device does not support this = operation. > + @retval RETURN_DEVICE_ERROR The serial device is not functioning cor= rectly. > + > +**/ > +RETURN_STATUS > +EFIAPI > +SerialPortSetControl ( > + IN UINT32 Control > + ) > +{ > + if (Initiated =3D=3D FALSE) { > + return EFI_NOT_READY; > + } > + return RETURN_SUCCESS; > +} > + > +/** > + Retrieve the status of the control bits on a serial device. > + > + @param Control A pointer to return the current control = signals from the serial device. > + > + @retval RETURN_SUCCESS The control bits were read from the seri= al device. > + @retval RETURN_UNSUPPORTED The serial device does not support this = operation. > + @retval RETURN_DEVICE_ERROR The serial device is not functioning cor= rectly. > + > +**/ > +RETURN_STATUS > +EFIAPI > +SerialPortGetControl ( > + OUT UINT32 *Control > + ) > +{ > + if (Initiated =3D=3D FALSE) { > + return EFI_NOT_READY; > + } > + *Control =3D 0; > + return RETURN_SUCCESS; > +} > + > +/** > + Sets the baud rate, receive FIFO depth, transmit/receice time out, par= ity, > + data bits, and stop bits on a serial device. > + > + @param BaudRate The requested baud rate. A BaudRate value of= 0 will use the > + device's default interface speed. > + On output, the value actually set. > + @param ReveiveFifoDepth The requested depth of the FIFO on the recei= ve side of the > + serial interface. A ReceiveFifoDepth value o= f 0 will use > + the device's default FIFO depth. > + On output, the value actually set. > + @param Timeout The requested time out for a single characte= r in microseconds. > + This timeout applies to both the transmit an= d receive side of the > + interface. A Timeout value of 0 will use the= device's default time > + out value. > + On output, the value actually set. > + @param Parity The type of parity to use on this serial dev= ice. A Parity value of > + DefaultParity will use the device's default = parity value. > + On output, the value actually set. > + @param DataBits The number of data bits to use on the serial= device. A DataBits > + vaule of 0 will use the device's default dat= a bit setting. > + On output, the value actually set. > + @param StopBits The number of stop bits to use on this seria= l device. A StopBits > + value of DefaultStopBits will use the device= 's default number of > + stop bits. > + On output, the value actually set. > + > + @retval RETURN_SUCCESS The new attributes were set on the s= erial device. > + @retval RETURN_UNSUPPORTED The serial device does not support t= his operation. > + @retval RETURN_INVALID_PARAMETER One or more of the attributes has an= unsupported value. > + @retval RETURN_DEVICE_ERROR The serial device is not functioning= correctly. > + > +**/ > +RETURN_STATUS > +EFIAPI > +SerialPortSetAttributes ( > + IN OUT UINT64 *BaudRate, > + IN OUT UINT32 *ReceiveFifoDepth, > + IN OUT UINT32 *Timeout, > + IN OUT EFI_PARITY_TYPE *Parity, > + IN OUT UINT8 *DataBits, > + IN OUT EFI_STOP_BITS_TYPE *StopBits > + ) > +{ > + if (Initiated =3D=3D FALSE) { > + return EFI_NOT_READY; > + } > + return RETURN_SUCCESS; > +} > diff --git a/Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/U5SerialPort= Lib.uni b/Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/U5SerialPortLib.u= ni > new file mode 100644 > index 0000000..49163bd > --- /dev/null > +++ b/Platform/SiFive/U5SeriesPkg/Library/SerialIoLib/U5SerialPortLib.uni > @@ -0,0 +1,16 @@ > +// /** @file > +// Library instance for SerialIo library class > +// > +// Library instance for SerialIO library class. > +// > +// Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved. > +// > +// SPDX-License-Identifier: BSD-2-Clause-Patent > +// > +// **/ > + > + > +#string STR_MODULE_ABSTRACT #language en-US "Library instanc= e for SerialIO library class" > + > +#string STR_MODULE_DESCRIPTION #language en-US "Library instanc= e for SerialIO library class." > +