summaryrefslogtreecommitdiff
path: root/Board/EM/IdeAcoustic/AcousticSetup.c
blob: 605d33b1596ae531008cc093d9fe8d868da9b729 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
//**********************************************************************
// $Header: /Alaska/SOURCE/Modules/HddAcoustic/AcousticSetup.c 7     3/05/12 3:14a Rameshr $
//
// $Revision: 7 $
//
// $Date: 3/05/12 3:14a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/HddAcoustic/AcousticSetup.c $
// 
// 7     3/05/12 3:14a Rameshr
// [TAG]  		EIP82971
// [Category]  	Improvement
// [Description]  	Added dynamich PCIe base Support in IdeAcoustic
// [Files]  		AcousticBoard.c, AcousticSetup.c
// 
// 6     1/24/12 1:42a Rameshr
// [TAG]  		EIP67652
// [Category]  	Bug Fix
// [Severity]  	Minor
// [Symptom]  	Build error when token SB_SETUP_SUPPORT disabled
// [RootCause]  	Unresolved external symbol
// gEfiDiskInfoProtocolGuid,SwapEntries referenced in function
// InitAcousticStrings
// [Solution]  	Added gEfiDiskInfoProtocolGuid and SwapEntries function
// locally
// [Files]  		AcousticSetup.c
// 
// 5     2/10/11 11:28a Pats
// [TAG] - EIP 52920
// [Category]- Function Request
// [Severity]- Minor
// [Symptom] - The Acoustic mode (Bypass/Quiet/Max Performance) of port 5
// is invalid on AMD SB700.
// [RootCause] - Special case where 2nd controller counts by incrementing
// device number only not handled properly
// [Solution] - Modified functions that handle this case.
// [Files] - Acoustic.c, AcousticBoard.c, AcousticSetup.c
// 
// 4     1/07/11 5:52p Pats
// Added processing for SECOND_CONTROLLER_COUNTS_BY_DEVICE token.
// 
// 3     1/07/11 12:15p Pats
// Changed how devices on second controller are handled.
// 
// 2     1/05/11 6:33p Pats
// Made more generic. Removed all south bridge-specific references.
// 
// 1     1/05/11 12:22a Rameshr
// Initial check-in for Hdd Acoustic Management Support.
// 
//**********************************************************************

//**********************************************************************
//<AMI_FHDR_START>
//
// Name: AcousticSetup.c
//
// Description: This file contains functions used by the Acoustic module
//
//<AMI_FHDR_END>
//**********************************************************************

#include <SetupStrTokens.h>
#if (EFI_SPECIFICATION_VERSION >= 0x0002000A)
#include <UefiHii.h>
#else
#include <Protocol\HII.h>
#endif
#include <Protocol\DevicePath.h>
#include "Acoustic.h"

#if DYNAMIC_PCIEXBASE_SUPPORT
UINT32 GetPciBaseAddr(VOID);
#define PCIEX_BASE GetPciBaseAddr()
#else
#define PCIEX_BASE PCIEX_BASE_ADDRESS
#endif
#if LOCAL_ACOUSTIC_SETUP
#include <Protocol\PDiskInfo.h>

static EFI_GUID gEfiDiskInfoProtocolGuid = EFI_DISK_INFO_PROTOCOL_GUID;

UINT8 gIdeControllerLocation[] = IDE_CONTROLLER_LOCATION;
UINT8 gIdeControllerLocation2[] = SECOND_CONTROLLER_LOCATION;

//
// Add more elements if there are more than 6 SATA ports
//
STRING_REF DevStrings[] = {
STRING_TOKEN( STR_ACHDD_0 ),
STRING_TOKEN( STR_ACHDD_1 ),
STRING_TOKEN( STR_ACHDD_2 ),
STRING_TOKEN( STR_ACHDD_3 ),
STRING_TOKEN( STR_ACHDD_4 ),
STRING_TOKEN( STR_ACHDD_5 ),
};


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   Swap_Entries
//
// Description: 
//
// Inpuut:      IN CHAR8    *Data,
//              IN UINT16   Size
//
// Output:      None
//
// Notes:       
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID Swap_Entries (
  IN CHAR8    *Data,
  IN UINT16   Size
)
{
  UINT16  Index;
  CHAR8   Temp8;

  for (Index = 0; (Index+1) < Size; Index+=2) {
    Temp8           = Data[Index];
    Data[Index]     = Data[Index + 1];
    Data[Index + 1] = Temp8;
  }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   InitAcousticStrings
//
// Description: Initialize Acoustic misc configuration strings.
//
// Input:
//              IN EFI_HII_HANDLE   HiiHandle
//              IN UINT16           Class
//
// Output:
//              VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID 
InitAcousticStrings(
    EFI_HII_HANDLE      HiiHandle, 
    UINT16              Class
)
{
    EFI_STATUS                      Status;
    PCI_DEVICE_PATH                 *PciDevicePath;
    CHAR8                           *NewString;
    UINT8                           Index;
    UINT8                           SataMode;
    UINTN                           HandleCount;
    EFI_HANDLE                      *HandleBuffer;
    EFI_DEVICE_PATH_PROTOCOL        *DevicePath;
    EFI_DEVICE_PATH_PROTOCOL        *DevicePathNode;
    EFI_DISK_INFO_PROTOCOL          *DiskInfo;
    UINT32                          IdeDevice, IdeChannel;
    IDENTIFY_DATA                   *IdentifyDriveInfo = NULL;
    UINT32                          BufferSize = 0;
    STRING_REF                      Token;
    CHAR8                           ModelNumber[43];
    UINTN                           PciAddress;
    UINT8                           IdeControllerLocation[] = IDE_CONTROLLER_LOCATION;

    if (Class == ADVANCED_FORM_SET_CLASS) {
        Status = pBS->AllocatePool(EfiBootServicesData, 43, &NewString);
        ASSERT_EFI_ERROR(Status);

        PciDevicePath = NULL;

        PciAddress = MmPciAddress(PCIEX_BASE, 
                                  IdeControllerLocation[0], 
                                  IdeControllerLocation[1], 
                                  IdeControllerLocation[2], 
                                  PCI_SCC);

        SataMode = *((volatile UINT8 *)(UINTN)(PciAddress));

        Status = pBS->LocateHandleBuffer (
                                          ByProtocol,
                                          &gEfiDiskInfoProtocolGuid,
                                          NULL,
                                          &HandleCount,
                                          &HandleBuffer
                                          );
        if (EFI_ERROR(Status)) {
            HandleCount = 0;
        }

        for (Index = 0; Index < HandleCount; Index++) {
            Status = pBS->HandleProtocol (
                                          HandleBuffer[Index],
                                          &gEfiDevicePathProtocolGuid,
                                          (VOID *) &DevicePath
                                          );
            ASSERT_EFI_ERROR(Status);

            DevicePathNode = DevicePath;
            while (!isEndNode (DevicePathNode)) {
                if ((DevicePathNode->Type == HARDWARE_DEVICE_PATH) &&
                    (DevicePathNode->SubType == HW_PCI_DP)) {
                    PciDevicePath = (PCI_DEVICE_PATH *) DevicePathNode;
                    break;
                }

                DevicePathNode = NEXT_NODE (DevicePathNode);
            }

            if (PciDevicePath == NULL) {
                continue;
            }

            if ( ((PciDevicePath->Device == gIdeControllerLocation[1]) && \
                  (PciDevicePath->Function == gIdeControllerLocation[2])) || \
                 ((PciDevicePath->Device == gIdeControllerLocation2[1]) && \
                  (PciDevicePath->Function == gIdeControllerLocation2[2])) ) {
                Status = pBS->HandleProtocol (
                                              HandleBuffer[Index],
                                              &gEfiDiskInfoProtocolGuid,
                                              &DiskInfo
                                              );
                ASSERT_EFI_ERROR (Status);

                Status = DiskInfo->WhichIde (
                                            DiskInfo,
                                            &IdeChannel,
                                            &IdeDevice
                                            );
                if (SataMode == PCI_CL_MASS_STOR_SCL_IDE) {   // IDE MODE

                    IdeDevice = (IdeChannel * 2) + IdeDevice;

#if SECOND_CONTROLLER_COUNTS_BY_DEVICE
                    if ( (PciDevicePath->Device == gIdeControllerLocation2[1]) && \
                         (PciDevicePath->Function == gIdeControllerLocation2[2]) ) { //Port 4, 5 in SATA2
                        IdeDevice += 4;
                    }
#endif
                    if (IdeDevice == 1 || IdeDevice == 2) {   // SINDX ¡V Serial ATA Index (D31:F2)
                        IdeDevice ^= 3;                       // Port 0 = Primary Master
                    }                                         // Port 2 = Primary Slave
#if !SECOND_CONTROLLER_COUNTS_BY_DEVICE
                    if ( (PciDevicePath->Device == gIdeControllerLocation2[1]) && \
                         (PciDevicePath->Function == gIdeControllerLocation2[2]) ) { //Port 4, 5 in SATA2
                        IdeDevice += 4;
                    }
#endif
                    IdeChannel = IdeDevice;
                }                                             // SATA MODE
                Token = DevStrings[IdeChannel];

                Status = pBS->AllocatePool(EfiBootServicesData, sizeof (IDENTIFY_DATA), &IdentifyDriveInfo);
                ASSERT_EFI_ERROR (Status);

                pBS->SetMem(IdentifyDriveInfo, sizeof (IDENTIFY_DATA), 0);

                BufferSize = sizeof (IDENTIFY_DATA);
                Status = DiskInfo->Identify (
                                            DiskInfo,
                                            IdentifyDriveInfo,
                                            &BufferSize
                                            );
                ASSERT_EFI_ERROR (Status);

            } else {
                //
                // Off board
                //
                continue;
            }

            pBS->SetMem(ModelNumber, 42, 0x20);
            pBS->CopyMem (ModelNumber+2, IdentifyDriveInfo->Model_Number_27, 40);
            Swap_Entries (ModelNumber+2, 40);
            ModelNumber[42] = '\0';

            Sprintf(NewString, "%a", ModelNumber);

            InitString(
                       HiiHandle,
                       Token,
                       L"%a",
                       NewString
                       );

            if (IdentifyDriveInfo) {
                pBS->FreePool (IdentifyDriveInfo);
                IdentifyDriveInfo = NULL;
            }
        }

        if (HandleBuffer) {
            pBS->FreePool (HandleBuffer);
        }

        pBS->FreePool (NewString);
    }
}
#endif

//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************