summaryrefslogtreecommitdiff
path: root/BraswellPlatformPkg/Flash/SpiDeviceDxe/SpiDevice.h
blob: cb8229629d18e07f4269e89cb13838df7208cfc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/** @file
  SPI Device driver for Braswell Platform.

  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>

  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.

**/

#ifndef _SPI_DEVICE_H_
#define _SPI_DEVICE_H_

#include <Protocol/Spi.h>
#include <Protocol/SpiFlashPart.h>
#include <Library/PcdLib.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiRuntimeLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>

//
// Defines the offset in the SPI device where the BIOS region starts.
//
#define BIOS_REGION_FLASH_OFFSET        (PcdGet32 (PcdBiosImageBase) - PcdGet32 (PcdFlashAreaBaseAddress))
#define VN_STORAGE_REGION_FLASH_OFFSET  (PcdGet32 (PcdFlashNvStorageVariableBase) - PcdGet32 (PcdFlashAreaBaseAddress))

extern EFI_SPI_PROTOCOL  *mSpiProtocol;
extern UINTN mNvStorageBase;

//
// Prefix Opcode Index on the host SPI controller
//
typedef enum {
  SPI_WREN,             // Prefix Opcode 0: Write Enable
  SPI_EWSR,             // Prefix Opcode 1: Enable Write Status Register
} PREFIX_OPCODE_INDEX;

//
// Opcode Menu Index on the host SPI controller
//
typedef enum {
  SPI_READ_ID,        // Opcode 0: READ ID, Read cycle with address
  SPI_READ,           // Opcode 1: READ, Read cycle with address
  SPI_RDSR,           // Opcode 2: Read Status Register, No address
  SPI_WRDI_SFDP,      // Opcode 3: Write Disable or Discovery Parameters, No address
  SPI_SERASE,         // Opcode 4: Sector Erase (4KB), Write cycle with address
  SPI_BERASE,         // Opcode 5: Block Erase (32KB), Write cycle with address
  SPI_PROG,           // Opcode 6: Byte Program, Write cycle with address
  SPI_WRSR,           // Opcode 7: Write Status Register, No address
} SPI_OPCODE_INDEX;

EFI_STATUS
EFIAPI
InitSpiDevice (
  IN EFI_HANDLE           ImageHandle,
  IN EFI_SYSTEM_TABLE     *SystemTable
  );

EFI_STATUS
EFIAPI
SpiRead (
  IN      UINTN       SpiOffset,
  IN OUT  UINTN       *Size,
  OUT     UINT8       *Buffer
  );

EFI_STATUS
EFIAPI
SpiWrite (
  IN      UINTN       SpiOffset,
  IN OUT  UINTN       *Size,
  IN      UINT8       *Buffer
  );

EFI_STATUS
EFIAPI
SpiErase (
  IN      UINTN       SpiOffset,
  IN OUT  UINTN       Size
  );

EFI_STATUS
EFIAPI
SpiLock (
  IN      UINTN       SpiOffset,
  IN OUT  UINTN       Size,
  IN      BOOLEAN     Lock
  );

EFI_STATUS
EFIAPI
SpiSetRange (
  IN      UINTN       SpiOffset,
  IN      UINTN       Size,
  IN      BOOLEAN     ReadLock,
  IN      BOOLEAN     WriteLock
  );

EFI_STATUS
EFIAPI
SpiLockRanges (
  );

BOOLEAN
ReadUsingMmio (
  IN UINTN  SpiOffset
  );

/**
  Get the JEDED ID from the SPI flash part.

  @param[in]  Context             Pointer to a context data structure
                                  needed by the SPI controller driver
  @param[in]  Description         Description of the flash device
  @param[in]  BufferLength        Length of the JedecId buffer
  @param[out] JedecId             Pointer to a buffer to fill with
                                  the JEDEC ID value

  @retval  EFI_SUCCESS            The JEDEC ID value is in the buffer
  @retval  EFI_INVALID_PARAMETER  JedecId is NULL
  @retval  EFI_INVALID_PARAMETER  Description is NULL
  @retval  EFI_INVALID_PARAMETER  Too few opcode entries
  @retval  EFI_INVALID_PARAMETER  JEDEC ID response buffer too small
  @retval  EFI_UNSUPPORTED        JEDEC ID opcode not found

**/
EFI_STATUS
EFIAPI
JedecIdRead (
  IN       VOID                    *Context,
  IN CONST FLASH_PART_DESCRIPTION  *Description,
  IN       UINTN                   BufferLength,
  OUT      UINT8                   *JedecId
  );

/**
  Get the JEDED ID from the SPI flash part.

  @param[in]  Context             Pointer to a context data structure
                                  needed by the SPI controller driver
  @param[in]  Description         Description of the flash device
  @param[in]  BufferLength        Length of the JedecId buffer
  @param[out] JedecId             Pointer to a buffer to fill with
                                  the JEDEC ID value

  @retval  EFI_SUCCESS            The JEDEC ID value is in the buffer
  @retval  EFI_INVALID_PARAMETER  JedecId is NULL
  @retval  EFI_INVALID_PARAMETER  Description is NULL
  @retval  EFI_INVALID_PARAMETER  Too few opcode entries
  @retval  EFI_INVALID_PARAMETER  JEDEC ID response buffer too small

**/
typedef
EFI_STATUS
(EFIAPI  *PERFORM_JEDEC_ID_OPERATION) (
  IN       VOID                    *Context,
  IN CONST FLASH_PART_DESCRIPTION  *Description,
  IN       UINTN                   BufferLength,
  OUT      UINT8                   *JedecId
  );

/**
  Determine the flash size and description

  @param[in]  PerformJedecIdOperation  Callback routine to initiate
                                       the JEDEC ID operation using
                                       the SPI controller to identify
                                       the flash part.
  @param[in]  Context                  Pointer to a context structure to pass
                                       to PerformJedecIdOperation
  @param[out] FlashDescription         Pointer to a buffer to receive a
                                       pointer to a FLASH_PART_DESCRIPTION
                                       data structure containing the flash
                                       part information.

  @return                              This routine returns the size of the flash part if it is
                                       supported.  Zero is returned if the flash part is not
                                       supported.

**/
UINT64
EFIAPI
FindFlashSupport (
  IN        PERFORM_JEDEC_ID_OPERATION  PerformJedecIdOperation,
  IN        VOID                        *Context,
  OUT CONST FLASH_PART_DESCRIPTION      **FlashDescription
  );


/**
  Load opcode into the SPI controller for specific flash device

  @param[in]  FlashDescription       Description of the flash device

  @retval  EFI_SUCCESS               The opcode was successfully loaded
  @retval  EFI_UNSUPPORTED           The opcode was not found

**/
EFI_STATUS
SpiFlashInit (
  IN CONST FLASH_PART_DESCRIPTION  *FlashDescription
  );

#endif