Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Spi support #6

Open
wants to merge 298 commits into
base: apu2_new_uefi
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 250 commits
Commits
Show all changes
298 commits
Select commit Hold shift + click to select a range
514fadd
edd extern guid
Sep 21, 2020
17a2730
Add libraries
Sep 21, 2020
523c1c2
fix include
Sep 21, 2020
6a20222
replace guid with NULL
Sep 21, 2020
3a683ef
fix guid
Sep 21, 2020
3dd0737
fix not initialized var
Sep 21, 2020
cb953a0
change log
Sep 21, 2020
9f8b4a4
protocol is null
Sep 21, 2020
2a82460
Add Fvb
Sep 21, 2020
27c3dec
add pkg
Sep 21, 2020
c1b8b4c
move to to guids
Sep 21, 2020
49fc4e7
add libs
Sep 21, 2020
7ac00cd
add deps
Sep 21, 2020
f65497d
remove guid
Sep 21, 2020
e488cc4
remove SMMStoreLib
Sep 21, 2020
46db81b
comment out asserts
Sep 21, 2020
3c9d032
copy some definitions
Sep 21, 2020
179cfe1
fix
Sep 21, 2020
6abaff5
fix typo
Sep 21, 2020
b0816d3
remove CB
Sep 21, 2020
1ac013e
empty problematic function
Sep 21, 2020
4384528
debug
Sep 21, 2020
d12ceff
debug 2
Sep 21, 2020
e3ea223
add debug signature to CR()
Sep 21, 2020
8dcde45
add debug sigs
Sep 21, 2020
fff59d6
remove assert
Sep 21, 2020
0c82b54
remove assert 2
Sep 21, 2020
87efeeb
restore asserts
Sep 22, 2020
a26fd1a
restore asserts
Sep 22, 2020
2df578e
copy inf from another driver
Sep 22, 2020
3c71360
debug fucntions entry
Sep 22, 2020
fa677ae
add more debug
Sep 22, 2020
2dacb59
disable SPI
Sep 22, 2020
91a463b
initialize status
Sep 22, 2020
13c3312
move some debug
Sep 22, 2020
c224848
empty protocol functions
Sep 22, 2020
7937a27
fix void fucntion
Sep 22, 2020
052fdb6
comment out unused code
Sep 22, 2020
4411d86
check guid hob
Sep 22, 2020
30221eb
change driver type to DXE
Sep 22, 2020
94ceed4
add uefi pkg
Sep 22, 2020
6b7acd6
remove redefinition
Sep 22, 2020
09807a6
add hob lib
Sep 22, 2020
3e085a6
add hob lib 2
Sep 22, 2020
ca3a271
remove unused definitions
Sep 22, 2020
87cdfba
install protocol
Sep 22, 2020
ce95917
add fvb initialization
Sep 22, 2020
c43f25e
add memoryallocation include
Sep 22, 2020
f4ed5a2
fix
Sep 22, 2020
b5bad3b
add guid
Sep 22, 2020
78d8020
cast
Sep 22, 2020
a9b3c75
empty protocol fuctions
Sep 22, 2020
b91d4d7
repair reference
Sep 22, 2020
ed69ad8
repair reference 2
Sep 22, 2020
db1c33d
remove unused code
Sep 22, 2020
cdb0b57
check unsigned int size
Sep 22, 2020
dac9430
remove SMM
Sep 23, 2020
16fbc09
add some spi
Sep 23, 2020
e0a6343
add implementation of memset
Sep 23, 2020
2542967
remove string.h
Sep 23, 2020
6dee5ae
fix types
Sep 23, 2020
e09c644
fix types 2
Sep 23, 2020
8a77a97
fix memset
Sep 23, 2020
a6df17f
define region struct
Sep 23, 2020
7319901
fixes
Sep 23, 2020
a83161d
fix types
Sep 23, 2020
997df4f
remove assert.h
Sep 23, 2020
c6e0d0e
fix includes again
Sep 23, 2020
380fd40
some fixes
Sep 23, 2020
6d67717
fix memset
Sep 23, 2020
246871b
fix missing semicolon
Sep 23, 2020
e6dcefa
fix double definition
Sep 23, 2020
76a1241
call spi_setup_slave()
Sep 23, 2020
80f7c87
print result ofcall spi_setup_slave()
Sep 23, 2020
0dbef6e
fix semicolon
Sep 23, 2020
7babc4d
add spi controller
Sep 23, 2020
e2d7994
add files to inf
Sep 23, 2020
bf3ad5f
try fix
Sep 23, 2020
61aeabd
change print format
Sep 23, 2020
6211dfe
add some functions to ctrl
Sep 23, 2020
3da0e4a
implement spi controler
Sep 24, 2020
fc8933e
fix some errors
Sep 24, 2020
e8a2ec3
fix some errors 2
Sep 24, 2020
5dcecd3
fix some errors 3
Sep 24, 2020
6c981e5
fix some errors 4
Sep 24, 2020
1e95508
fix some errors 5
Sep 24, 2020
2db3334
fix some errors 6
Sep 24, 2020
acf2df2
fix some errors 7
Sep 24, 2020
241b457
fix some errors 8
Sep 24, 2020
3ae2006
fix some errors 9
Sep 24, 2020
fa01f26
fix some errors 10
Sep 24, 2020
b2dead4
fix some errors 11
Sep 24, 2020
83ba6c0
fix some errors 12
Sep 24, 2020
9c5dc40
fix some errors 13
Sep 24, 2020
a94154a
fix some errors 14
Sep 25, 2020
8e6ee3d
fix some errors 15
Sep 25, 2020
3f5ad57
fix some errors 16
Sep 25, 2020
86112ad
fix some errors 17
Sep 25, 2020
bb8ff71
fix some errors 18
Sep 25, 2020
aaa5059
fix some errors 19
Sep 25, 2020
535c35d
fix some errors 20
Sep 25, 2020
a5cde05
fix some errors 21
Sep 25, 2020
53cebdc
fix some errors 22
Sep 25, 2020
4a6350a
fix some errors 23
Sep 25, 2020
245c86d
fix some errors 24
Sep 25, 2020
445e21c
fix some errors 25
Sep 25, 2020
eeef282
fix some errors 26
Sep 25, 2020
ed00462
fix some errors 27
Sep 25, 2020
66305cf
fix some errors 28
Sep 25, 2020
54a89f4
fix some errors 29
Sep 25, 2020
b9db4aa
fix some errors 30
Sep 25, 2020
e93baa1
fix some errors 31
Sep 25, 2020
5909a0d
fix some errors 32
Sep 25, 2020
9e6ed2e
fix some errors 33
Sep 25, 2020
05acef0
fix some errors 34
Sep 25, 2020
226a96f
fix some errors 35
Sep 25, 2020
546c762
fix some errors 36
Sep 25, 2020
f1ebf93
fix some errors 37
Sep 25, 2020
368374c
spi_setup_slave
Sep 25, 2020
e0e21bb
spi_xfer
Sep 25, 2020
f160413
change max xfer size
Sep 25, 2020
01af3d5
change max xfer size 2
Sep 25, 2020
e10f307
change max xfer size 3
Sep 25, 2020
f6ef8c2
add debug to spi_init
Sep 28, 2020
536c68a
add some debug
Sep 28, 2020
4219956
add some debug 2
Sep 28, 2020
7959037
add some debug 3
Sep 28, 2020
a95c049
call spi_init
Sep 28, 2020
3219400
typo
Sep 28, 2020
1900a02
add more debug
Sep 28, 2020
943443c
debug
Sep 28, 2020
0db62d1
fix spi_init()
Sep 28, 2020
7408837
fix redefinitions
Sep 28, 2020
b1e8e40
some debug is not working
Sep 28, 2020
85f2bca
port amd, not intel
Sep 28, 2020
87dbb90
add files to .inf
Sep 28, 2020
f40cf58
fix type
Sep 28, 2020
f950a13
missing references
Sep 28, 2020
3cd1b32
add include
Sep 28, 2020
ea423e4
add include 2
Sep 28, 2020
012ed35
add some amdSPI implementation
Sep 29, 2020
d4877a7
fixes
Sep 29, 2020
a43fa74
fixes 2
Sep 29, 2020
519d426
fixes 3
Sep 29, 2020
00add80
fixes 4
Sep 29, 2020
a69468e
fixes 5
Sep 29, 2020
1cffff2
fixes 6
Sep 29, 2020
eade647
fixes 7
Sep 29, 2020
055bd68
fixes 8
Sep 29, 2020
4ba3542
comment out Fvb
Sep 29, 2020
effefc6
implement more
Sep 29, 2020
2846511
add includes, fix types
Sep 29, 2020
75ab148
more implementation
Sep 29, 2020
a6eafb7
remove reference to nonexisiting file
Sep 29, 2020
408a5ab
fixes
Sep 29, 2020
5036966
try to add memset
Sep 29, 2020
2206129
add own memset
Sep 29, 2020
09ec8b9
fix include
Sep 29, 2020
834bd57
fixes 0
Sep 29, 2020
ecf80b2
fixes 1
Sep 29, 2020
5a9a78c
fixes 2
Sep 29, 2020
16ac87c
fixes 3
Sep 29, 2020
e611461
fixes 4
Sep 29, 2020
71528a4
fixes 5
Sep 29, 2020
f420cde
fixes 6
Sep 29, 2020
bba1649
fixes 7
Sep 29, 2020
dc737d1
fixes 8
Sep 29, 2020
f58ddc2
fixes 9
Sep 29, 2020
1e4016e
fixes 10
Sep 29, 2020
8528a9c
fixes 11
Sep 29, 2020
662a043
fixes 12
Sep 29, 2020
fdf4c5c
fixes 13
Sep 29, 2020
496fe80
fix errors
Sep 30, 2020
4270780
fix pointer type
Sep 30, 2020
6b8d057
fix include
Sep 30, 2020
8a54944
fix include
Sep 30, 2020
14d7859
fix types
Sep 30, 2020
0fee411
fix types 2
Sep 30, 2020
9cfce29
fix types 3
Sep 30, 2020
bc79abe
fix
Sep 30, 2020
0fb3629
fix type
Sep 30, 2020
f542216
fix inf
Sep 30, 2020
5670731
fixes
Sep 30, 2020
399e0ac
dummy data to pci_mmconf
Sep 30, 2020
734e5cf
fix static
Sep 30, 2020
e2a0d95
fix types
Sep 30, 2020
c477d7b
fix definitions
Sep 30, 2020
bb64d3f
fix
Sep 30, 2020
b8e1438
fix 1
Sep 30, 2020
932dc18
fix 2
Sep 30, 2020
4f70b54
fix 3
Sep 30, 2020
8a79933
fix 4
Sep 30, 2020
61f3fc8
fix compilation errors
Sep 30, 2020
44841ae
fix compilation errors 2
Sep 30, 2020
70483fa
dummy change
Sep 30, 2020
fe065b5
debug
Sep 30, 2020
90a26d7
REMOVE PROTOCOL INSTALL
Sep 30, 2020
40b62f4
UefiPayloadPkg/SPI/*: Port SPI controller, Initialize PcdFlashNvStora…
Sep 30, 2020
f41dfe1
efiPayloadPkg/SPI/fch_spi_ctrl.c: implement SPI xfer and xfer_vector
Oct 5, 2020
cd6c2b5
UefiPayloadPkg/SPI*: Fix missing SPI BAR address and start porting wi…
Oct 6, 2020
4d3d0bb
Port Winbond and Adesto specific code, fix issues with acquisition of…
Oct 8, 2020
af672bd
try to fix invalid SPI response
Oct 9, 2020
60371cc
UefiPayloadPkg/SPI/Fvb.c: Implement reading from SPI flash memory
Oct 12, 2020
9719bd7
UefiPayloadPkg/SPI/Fvb.h: fix FvbRead, first implement FvbWrite and F…
Oct 13, 2020
cfe64ee
fixes after code review
Oct 14, 2020
a227e15
UefiPayloadPkg/SPI/Fvb.c: Implement FlashErase. FlashWrite is not wor…
Oct 14, 2020
7ce6cf0
UefiPayloadPkg/SPI/Fvb.c: fix FvbWrite
Oct 15, 2020
61e718e
UefiPayloadPkg/SPI/Fvb.c: Fix FvbRead() to accept buffers up to BLOCK…
Oct 15, 2020
4308372
UefiPayloadPkg/SPI/*: Applied review suggestions
Oct 15, 2020
bfe0be8
UefiPayloadPkg/SPI/*: clean the unnecessery code
Oct 15, 2020
ac17469
Revert "UefiPayloadPkg/SPI/*: clean the unnecessery code"
Oct 16, 2020
d2715b7
UefiPayloadPkg/SPI*: cleanup the code
Oct 16, 2020
6ecb3ec
UefiPayloadPkg/SPI/pci_ops.c dekete the file and start using PciLib.h
Oct 16, 2020
1f56fc5
UefiPayloadPkg/SPI/*: delete stopwatch.h and pci_type.h
Oct 16, 2020
a5930e7
UefiPayloadPkg/SPI/* delete device.h, path.h
Oct 16, 2020
5b802f0
UefiPayloadPkg/Library/CbSMMStoreLib/CorebootSMMStore.c: fix compilat…
miczyg1 Oct 16, 2020
e97752e
UefiPayloadPkg/UefiPayloadPkgIa32.dsc: remove SMMStore from build
Oct 19, 2020
dd124d6
Merge branch 'spi' of github.com:3mdeb/edk2 into spi
Oct 19, 2020
23452e0
UefiPayloadPkg/SPI/Fvb.c: Start implementing VariableStoreHeaders
Oct 20, 2020
aa1da81
UefiPayloadPkg/SPI/Fvb.c: style changes
Oct 21, 2020
e40a530
UefiPayloadPkg/SPI/Fvb.c: fix FvbWrite writing only 0x43 bytes
Oct 21, 2020
55b4b94
UefiPayloadPkg/SPI/Fvb.c: Initialize FirmwareVolumeHeader
Oct 22, 2020
4910143
UefiPayloadPkg/SPI/FvbSPI.c Install gEdkiiNvVarStoreFormattedGuid
Oct 22, 2020
c0744ea
UefiPayloadPkg/SPI/Fvb.c: Add gEfiVariableWriteArchProtocolGuid proto…
Oct 23, 2020
0319bae
UefiPayloadPkg: fix build under coreboot-sdk
miczyg1 Oct 26, 2020
8a7bb9a
UefiPayloadPkg: strip unused packages
miczyg1 Oct 26, 2020
9ebbc35
UefiPayloadPkg/UefiPayloadPkg.fdf: fix iPXE include
miczyg1 Oct 26, 2020
1d082d6
UefiPayloadPkg: use COM2 as output port
miczyg1 Oct 27, 2020
38d69b9
UefiPayloadPkg/Library/PlatformBootManagerLib/PlatformBootManager.c: …
miczyg1 Oct 27, 2020
e55e77b
UefiPayloadPkg/Library/AmdSpiLib: refactor code from SPI module to Am…
miczyg1 Oct 29, 2020
337fad7
UefiPayloadPkg/AmdSpiDxe: add AmdSpiDxe module to provide FVB protocol
miczyg1 Oct 29, 2020
13d8ae7
UefiPayloadPkg/Library/PlatformBootManagerLib/PlatformBootManager.c: …
miczyg1 Oct 29, 2020
b57cc1d
UefiPayloadPkg/Library/AmdSpiLib: clean up the library from debug
miczyg1 Nov 2, 2020
783a152
UefiPayloadPkg/AmdSpiDxe/AmdSpiFvbDxe.c: validate header after intial…
miczyg1 Nov 2, 2020
64ef4ef
UefiPayloadPkg/Library/AmdSpiLib/SPIFlashInternal.c: fix typo
miczyg1 Nov 2, 2020
7d3ae3d
UefiPayloadPkg/AmdSpiDxe/AmdSpiFvbDxe.c: fix build issue
miczyg1 Nov 2, 2020
b948002
UefiPayloadPkg: remove obsolete modules
miczyg1 Nov 2, 2020
86c3aac
UefiPayloadPkg: bring back FaultTolerantWrite
miczyg1 Nov 2, 2020
21f61e3
UefiPayloadPkg: bring back common modules
miczyg1 Nov 2, 2020
e2e3d0d
UefiPayloadPkg: bring back UEFI capsule for DxeCore
miczyg1 Nov 2, 2020
87d9748
UefiPayloadPkg/Library/AmdSpiLib: make AMD SPI more robust
miczyg1 Nov 4, 2020
a43c3da
UefiPayloadPkg/UefiPayloadPkgIa32.dsc: use COM2
miczyg1 Nov 4, 2020
6d83bf4
UefiPayloadPkg/Library/AmdSpiLib/FchSPICtrl.c: remove obsolete function
miczyg1 Nov 4, 2020
0863dd9
UefiPayloadPkg/Library/AmdSpiLib/FchSPICtrl.c: replace spi_read32 wit…
miczyg1 Nov 4, 2020
fddb698
UefiPayloadPkg/Library/AmdSpiLib: fix bugs in the implementation
miczyg1 Nov 5, 2020
13e240a
UefiPayloadPkg/SecureBootEnrollDefaultKeys/SecureBootSetup.c: add debug
miczyg1 Nov 5, 2020
d6c0b60
PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe: add debug
miczyg1 Nov 5, 2020
74aa375
UefiPayloadPkg/SecureBootEnrollDefaultKeys/SecureBootSetup: let AuthS…
miczyg1 Nov 5, 2020
a019ca1
UefiPayloadPkg/UefiPayloadPkg.fdf: remove obsolete space
miczyg1 Nov 5, 2020
95c1d9f
UefiPayloadPkg/AmdSpiDxe/AmdSpiFvbDxe.c: change to AuthFormat
miczyg1 Nov 5, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreFvbDxe.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ InitializeFvAndVariableStoreHeaders (
IN SMMSTORE_INSTANCE *Instance
)
{
DEBUG((EFI_D_INFO, "%a\n", __FUNCTION__));
IgorBagnucki marked this conversation as resolved.
Show resolved Hide resolved
EFI_STATUS Status;
VOID* Headers;
UINTN HeadersLength;
Expand Down Expand Up @@ -129,6 +130,7 @@ ValidateFvHeader (
IN SMMSTORE_INSTANCE *Instance
)
{
DEBUG((EFI_D_INFO, "%a\n", __FUNCTION__));
UINT16 Checksum;
EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
VARIABLE_STORE_HEADER *VariableStoreHeader;
Expand Down Expand Up @@ -257,6 +259,7 @@ FvbGetAttributes(
OUT EFI_FVB_ATTRIBUTES_2 *Attributes
)
{
DEBUG((EFI_D_INFO, "%a\n", __FUNCTION__));
EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes;
SMMSTORE_INSTANCE *Instance;

Expand Down Expand Up @@ -313,6 +316,7 @@ FvbSetAttributes(
IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
)
{
DEBUG((EFI_D_INFO, "%a\n", __FUNCTION__));
DEBUG ((DEBUG_BLKIO, "FvbSetAttributes(0x%X) is not supported\n",*Attributes));
return EFI_UNSUPPORTED;
}
Expand Down Expand Up @@ -341,6 +345,7 @@ FvbGetPhysicalAddress (
OUT EFI_PHYSICAL_ADDRESS *Address
)
{
DEBUG((EFI_D_INFO, "%a\n", __FUNCTION__));
ASSERT(Address != NULL);

*Address = mFlashNvStorageVariableBase;
Expand Down Expand Up @@ -382,6 +387,7 @@ FvbGetBlockSize (
OUT UINTN *NumberOfBlocks
)
{
DEBUG((EFI_D_INFO, "%a\n", __FUNCTION__));
EFI_STATUS Status;
SMMSTORE_INSTANCE *Instance;

Expand Down Expand Up @@ -455,6 +461,7 @@ FvbRead (
IN OUT UINT8 *Buffer
)
{
DEBUG((EFI_D_INFO, "%a\n", __FUNCTION__));
UINTN BlockSize;
SMMSTORE_INSTANCE *Instance;

Expand Down Expand Up @@ -548,6 +555,7 @@ FvbWrite (
IN UINT8 *Buffer
)
{
DEBUG((EFI_D_INFO, "%a\n", __FUNCTION__));
UINTN BlockSize;
SMMSTORE_INSTANCE *Instance;

Expand Down Expand Up @@ -625,6 +633,7 @@ FvbEraseBlocks (
...
)
{
DEBUG((EFI_D_INFO, "%a\n", __FUNCTION__));
EFI_STATUS Status;
VA_LIST Args;
EFI_LBA StartingLba; // Lba from which we start erasing
Expand Down Expand Up @@ -731,6 +740,7 @@ FvbVirtualNotifyEvent (
IN VOID *Context
)
{
DEBUG((EFI_D_INFO, "%a\n", __FUNCTION__));
EfiConvertPointer (0x0, (VOID**)&mFlashNvStorageVariableBase);
return;
}
Expand All @@ -741,6 +751,7 @@ SMMStoreFvbInitialize (
IN SMMSTORE_INSTANCE* Instance
)
{
DEBUG((EFI_D_INFO, "%a\n", __FUNCTION__));
EFI_STATUS Status;
UINT32 FvbNumLba;
EFI_BOOT_MODE BootMode;
Expand Down
157 changes: 157 additions & 0 deletions UefiPayloadPkg/SPI/FchSPICtrl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#include <Include/Library/DebugLib.h>
#include <Include/Library/PciLib.h>
#include <Include/Library/TimerLib.h>
#include <Include/PiDxe.h>
#include "FchSPIUtil.h"
#include "GenericSPI.h"
#include "SPI.h"
#include "spi_flash_internal.h"

/* SPDX-License-Identifier: GPL-2.0-only */

#define GRANULARITY_TEST_4k 0x0000f000 /* bits 15-12 */
#define WORD_TO_DWORD_UPPER(x) ((x << 16) & 0xffff0000)

/* SPI MMIO registers */
#define SPI_RESTRICTED_CMD1 0x04
#define SPI_RESTRICTED_CMD2 0x08
#define SPI_CNTRL1 0x0c
#define SPI_CMD_CODE 0x45
#define SPI_CMD_TRIGGER 0x47
#define SPI_CMD_TRIGGER_EXECUTE 0x80
#define SPI_TX_BYTE_COUNT 0x48
#define SPI_RX_BYTE_COUNT 0x4b
#define SPI_STATUS 0x4c
#define SPI_DONE_BYTE_COUNT_SHIFT 0
#define SPI_DONE_BYTE_COUNT_MASK 0xff
#define SPI_FIFO_WR_PTR_SHIFT 8
#define SPI_FIFO_WR_PTR_MASK 0x7f
#define SPI_FIFO_RD_PTR_SHIFT 16
#define SPI_FIFO_RD_PTR_MASK 0x7f

#define MAX_ROM_PROTECT_RANGES 4
#define ROM_PROTECT_RANGE0 0x50
#define ROM_PROTECT_RANGE_REG(n) (ROM_PROTECT_RANGE0 + (4 * n))

static int wait_for_ready(VOID)
{
CONST UINT64 timeoutMilisecond = 500;
CONST UINT64 nanoToMiliDivider = 1000000;
CONST UINT64 startTimeMilisec =
GetTimeInNanoSecond(GetPerformanceCounter()) / nanoToMiliDivider;
CONST UINT64 endTimeMilisec = startTimeMilisec + timeoutMilisecond;
UINT64 timeNow = startTimeMilisec;

do {
if (!(spi_read32(SPI_STATUS) & SPI_BUSY))
return 0;
timeNow = GetTimeInNanoSecond(GetPerformanceCounter()) / nanoToMiliDivider;
} while (
timeNow<startTimeMilisec
|| (timeNow>startTimeMilisec && timeNow>endTimeMilisec));

return -1;
}

int execute_command(VOID)
{
spi_write8(SPI_CMD_TRIGGER, SPI_CMD_TRIGGER_EXECUTE);
if(wait_for_ready())
DEBUG((EFI_D_INFO,
"%a: FCH_SC Error: Timeout executing command\n", __FUNCTION__));

return 0;
}

VOID spi_init(VOID)
{
spi_get_bar();
}

int spi_ctrlr_xfer(CONST struct spi_slave *slave, CONST VOID *dout,
__SIZE_TYPE__ bytesout, VOID *din, __SIZE_TYPE__ bytesin)
{
__SIZE_TYPE__ count;
UINT8 cmd;
UINT8 *bufin = din;
CONST UINT8 *bufout = dout;

/* First byte is cmd which cannot be sent through FIFO */
cmd = bufout[0];
bufout++;
bytesout--;

/*
* Check if this is a write command attempting to transfer more bytes
* than the controller can handle. Iterations for writes are not
* supported here because each SPI write command needs to be preceded
* and followed by other SPI commands.
*/
if (bytesout + bytesin > SPI_FIFO_DEPTH) {
DEBUG((EFI_D_INFO,
"%a: FCH_SC: Too much to transfer, code error!\n", __FUNCTION__));
return -1;
}

if (wait_for_ready())
return -1;

spi_write8(SPI_CMD_CODE, cmd);
spi_write8(SPI_TX_BYTE_COUNT, bytesout);
spi_write8(SPI_RX_BYTE_COUNT, bytesin);

for (count = 0; count < bytesout; count++)
spi_write8(SPI_FIFO + count, bufout[count]);

if (execute_command())
return -1;

for (count = 0; count < bytesin; count++)
bufin[count] = spi_read8(SPI_FIFO + count + bytesout);

return 0;
}

int xfer_vectors(CONST struct spi_slave *slave,
struct spi_op vectors[], __SIZE_TYPE__ count)
{
return spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer);
}

int protect_a_range(UINT32 value)
{
UINT32 reg32;
UINT8 n;

/* find a free protection register */
for (n = 0; n < MAX_ROM_PROTECT_RANGES; n++) {
reg32 = PciRead32(
PCI_LIB_ADDRESS(PCU_BUS, PCU_DEV, LPC_FUNC, ROM_PROTECT_RANGE_REG(n)));
if (!reg32)
break;
}
if (n == MAX_ROM_PROTECT_RANGES)
return -1; /* no free range */

PciWrite32(
PCI_LIB_ADDRESS(PCU_BUS, PCU_DEV, LPC_FUNC, ROM_PROTECT_RANGE_REG(n)),
value);
return 0;
}

CONST struct spi_ctrlr fch_spi_flash_ctrlr = {
.xfer = spi_ctrlr_xfer,
.xfer_vector = xfer_vectors,
.max_xfer_size = SPI_FIFO_DEPTH,
.flags = SPI_CNTRLR_DEDUCT_CMD_LEN | SPI_CNTRLR_DEDUCT_OPCODE_LEN,
};

CONST struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
{
.ctrlr = &fch_spi_flash_ctrlr,
.bus_start = 0,
.bus_end = 0,
},
};

CONST __SIZE_TYPE__ spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);
37 changes: 37 additions & 0 deletions UefiPayloadPkg/SPI/FchSPICtrl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef FCH_SPI_CTRL_H
#define FCH_SPI_CTRL_H

#include <Include/PiDxe.h>
#include "GenericSPI.h"

union pci_bank {
UINT8 reg8[4096];
UINT16 reg16[4096 / sizeof(UINT16)];
UINT32 reg32[4096 / sizeof(UINT32)];
};

int execute_command(VOID);
VOID spi_init(VOID);
int spi_ctrlr_xfer(CONST struct spi_slave *slave, CONST VOID *dout,
__SIZE_TYPE__ bytesout, VOID *din, __SIZE_TYPE__ bytesin);
int xfer_vectors(CONST struct spi_slave *slave,
struct spi_op vectors[], __SIZE_TYPE__ count);
int protect_a_range(UINT32 value);

/*
* Protect range of SPI flash defined by region using the SPI flash controller.
*
* Note: Up to 4 ranges can be protected, though if a particular region requires more than one
* range, total number of regions decreases accordingly. Each range can be programmed to 4KiB or
* 64KiB granularity.
*
* Warning: If more than 1 region needs protection, and they need mixed protections (read/write)
* than start with the region that requires the most protection. After the restricted commands
* have been written, they can't be changed (write once). So if first region is write protection
* and second region is read protection, it's best to define first region as read and write
* protection.
*/
int fch_spi_flash_protect(CONST struct spi_flash *flash, CONST struct region *region,
CONST enum ctrlr_prot_type type);

#endif /* FCH_SPI_CTRL_H */
67 changes: 67 additions & 0 deletions UefiPayloadPkg/SPI/FchSPIUtil.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <Include/Library/DebugLib.h>
#include <Include/Library/PciLib.h>
#include <Include/PiDxe.h>
#include <Library/IoLib.h>
#include "FchSPICtrl.h"
#include "FchSPIUtil.h"
#include "SPI.h"

#define _LPCB_DEV PCI_DEV(0, 0x14, 0x3)
#define SPIROM_BASE_ADDRESS_REGISTER 0xa0
#define SPI_BASE_ALIGNMENT 0x00000040
#define ALIGN_DOWN(x,a) ((x) & ~((typeof(x))(a)-1UL))

static UINTN spi_base = 0;

UINTN lpc_get_spibase(VOID)
{
UINT32 base;
base = PciRead32(
PCI_LIB_ADDRESS(PCU_BUS, PCU_DEV, LPC_FUNC, SPIROM_BASE_ADDRESS_REGISTER));
base = ALIGN_DOWN(base, SPI_BASE_ALIGNMENT);
return (unsigned long int)base;
}

VOID spi_set_base(UINTN base)
{
spi_base = base;
}

UINTN spi_get_bar(VOID)
{
if (spi_base == 0) {
spi_set_base(lpc_get_spibase());
}
return spi_base;
}

UINT8 spi_read8(UINT8 reg)
{
return MmioRead8((spi_get_bar() + reg));
}

UINT16 spi_read16(UINT8 reg)
{
return MmioRead16((spi_get_bar() + reg));
}

UINT32 spi_read32(UINT8 reg)
{
return MmioRead32((spi_get_bar() + reg));
}

VOID spi_write8(UINT8 reg, UINT8 val)
{
MmioWrite8((spi_get_bar() + reg), val);
}

VOID spi_write16(UINT8 reg, UINT16 val)
{
MmioWrite16((spi_get_bar() + reg), val);
}

VOID spi_write32(UINT8 reg, UINT32 val)
{
MmioWrite32((spi_get_bar() + reg), val);
}
16 changes: 16 additions & 0 deletions UefiPayloadPkg/SPI/FchSPIUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef FCH_SPI_UTIL_H
#define FCH_SPI_UTIL_H

#include <Include/PiDxe.h>

UINTN lpc_get_spibase(VOID);
VOID spi_set_base(UINTN base);
UINTN spi_get_bar(VOID);
UINT8 spi_read8(UINT8 reg);
UINT16 spi_read16(UINT8 reg);
UINT32 spi_read32(UINT8 reg);
VOID spi_write8(UINT8 reg, UINT8 val);
VOID spi_write16(UINT8 reg, UINT16 val);
VOID spi_write32(UINT8 reg, UINT32 val);

#endif /* FCH_SPI_UTIL_H */
Loading