summaryrefslogtreecommitdiff
path: root/Board/EM/IdeAcoustic/Acoustic.c
blob: f52d7c15acd7b23158249215b5b5cba2301b0dd9 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (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/Acoustic.c 5     12/19/11 1:09a Rameshr $
//
// $Revision: 5 $
//
// $Date: 12/19/11 1:09a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/HddAcoustic/Acoustic.c $
// 
// 5     12/19/11 1:09a Rameshr
// [TAG]  		EIP77527
// [Category]  	Improvement
// [Description]  	IdeSecurity, IdeAcoustic and IdeSmart changes should be
// done based on the Core Version checking.
// [Files]  		IdeSecurity.c, IdeSmart.c , Acoustic.c 
// 
// 4     12/05/11 6:22p Rajkumarkc
// [TAG]  		EIP77142 
// [Category]  	Improvement
// [Description]	Added the function 'IdeNonDataCommandExp' in the
// 'IDE_BUS_PROTOCOL' and removed the existing function
// 'IdeNonDataCommand' for supporting the upper 24bits of LBA.
// [Files]
//     Ata.c, IdeBus.c, Idebus.h, PIDEBus.h, Acoustic.c, IdeSecurity.c,
// IdeSMART.c
// 
// 3     2/10/11 11:25a 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
// 
// 2     1/07/11 12:07p Pats
// Changed the name of a variable from SataPortIndex to IdeDevice, for
// consistancy. No coding change.
// 
// 1     1/05/11 12:22a Rameshr
// Initial check-in for Hdd Acoustic Management Support.
// 
//**********************************************************************


//<AMI_FHDR_START>
//----------------------------------------------------------------------------
//
// Name: Acoustic.c
//
// Description: Initialize and provide a protocol for the Acoustic support.
//
//----------------------------------------------------------------------------
//<AMI_FHDR_END>

#include <Efi.h>
#include <Dxe.h>

#include "Acoustic.h"   

extern EFI_GUID gAcousticSetupProtocolguid;

EFI_GUID gEfiDiskInfoProtocolGuid           = EFI_DISK_INFO_PROTOCOL_GUID;
EFI_GUID gEfiBlockIoProtocolGuid            = EFI_BLOCK_IO_PROTOCOL_GUID;
EFI_GUID gEfiAhciBusProtocolGuid            = AHCI_BUS_INIT_PROTOCOL_GUID;
EFI_GUID gEfiIdeBusInitProtocolGuid         = IDE_BUS_INIT_PROTOCOL_GUID;

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   AcousticEntryPoint
//
// Description: This routine is the entry point for the Acoustic driver. It
//              Initializes various controllers and installs Driver Binding
//              protocol 
//
// Input:       ImageHandle     Handle to this driver image
//              SystemTable     Pointer to the system table
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS 
AcousticEntryPoint (
    IN EFI_HANDLE        ImageHandle,
    IN EFI_SYSTEM_TABLE  *SystemTable
)
{
    InitAmiLib(ImageHandle, SystemTable);

#ifdef Debug_Level_1
    TRACE((-1,"AcousticEntryPoint Status = %x\n", Status));
#endif

    Acoustic_BoardInit (ImageHandle, SystemTable);

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   AcousticProgramming
//
// Description: This function is called after all the controllers have been 
//              dispatched and at the end of BDS phase.
//
//
// Input:       IN EFI_EVENT    Event
//              IN VOID         *Context
//
// Output:      VOID
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
AcousticProgramming (
    IN EFI_EVENT        Event,
    IN VOID             *Context 
)
{
    EFI_STATUS                      Status;
    EFI_DISK_INFO_PROTOCOL          *DiskInfo;
    EFI_PCI_IO_PROTOCOL             *PciIo;
    EFI_DEVICE_PATH_PROTOCOL        *Dp;
    ACOUSTIC_SETUP_PROTOCOL         *AcousticSetupProtocol;
    IDENTIFY_DATA                   *IdentifyDriveInfo = NULL;
    UINT32                          IdeDevice, IdeChannel, PortIndex;
    UINTN                           i, j, Number, Number1;
    EFI_HANDLE                      *Controller_Handle = NULL, *Device_Handle = NULL, Handle = NULL;
    SETUP_DATA                      *SetupData = NULL;
    UINT32                          BufferSize;
    UINT16                          CurrentLevel;
    UINT8                           SubClassCode;

    Status = pBS->CloseEvent (Event);

    Status = pBS->LocateHandleBuffer(ByProtocol,&gAcousticSetupProtocolguid, NULL, &Number, &Controller_Handle);
    if (EFI_ERROR(Status)) {
        return;
    }
    //
    // Get the Identify Data
    //
    Status = pBS->AllocatePool(EfiBootServicesData, sizeof (IDENTIFY_DATA), &IdentifyDriveInfo);
    ASSERT_EFI_ERROR (Status);

    for(i=0; i<Number; i++) {
        Status = pBS->HandleProtocol(Controller_Handle[i],&gAcousticSetupProtocolguid,&AcousticSetupProtocol);
        if (EFI_ERROR(Status)) {
            continue;
        }

        Status = pBS->LocateHandleBuffer(ByProtocol,&gEfiDiskInfoProtocolGuid, NULL, &Number1, &Device_Handle);
        if (EFI_ERROR(Status)) {
            continue;
        }

        for(j = 0; j < Number1; j++) {
            // 
            // Now try to locate the Controller on which the device is connected
            //
            Status = pBS->HandleProtocol(Device_Handle[j],&gEfiDevicePathProtocolGuid,&Dp); 
            if (EFI_ERROR(Status)) {
                continue;
            }
                
            Status = pBS->LocateDevicePath(&gEfiPciIoProtocolGuid, &Dp, &Handle);
            //
            // If the controller handle doesn't match then it is a different IDE controller
            //
            if (Handle != Controller_Handle[i]) {
                continue;
            }

            Status = pBS->HandleProtocol(Device_Handle[j], &gEfiDiskInfoProtocolGuid, &DiskInfo);
            if (EFI_ERROR(Status)) {
                continue;
            }

            Status = pBS->HandleProtocol(Controller_Handle[i], &gEfiPciIoProtocolGuid, &PciIo);
            if (EFI_ERROR(Status)) {
                continue;
            }

            Status = PciIo->Pci.Read(PciIo, EfiPciIoWidthUint8, 0xA, 1, &SubClassCode);
            //
            // Get the location of the drive
            //
            Status = DiskInfo->WhichIde (DiskInfo, &IdeChannel, &IdeDevice);

            if (SubClassCode == 1) {           // Ide mode
                PortIndex = (IdeChannel * 2) + IdeDevice;
                if (PortIndex == 1 || PortIndex == 2) {
                    PortIndex ^= 3;
                }
            } else {
                PortIndex = IdeChannel;
            }
#if SECOND_CONTROLLER_COUNTS_BY_DEVICE
            if ( (SubClassCode == 1) && \
                 (AcousticSetupProtocol->ControllerNumber == 1) ) {
                PortIndex = IdeDevice;
            }
#endif
            AcousticSetupProtocol->DeviceDetected[PortIndex] = 1;

            BufferSize = sizeof (IDENTIFY_DATA);

            Status = DiskInfo->Identify (DiskInfo, IdentifyDriveInfo, &BufferSize);
            ASSERT_EFI_ERROR (Status);
            if (EFI_ERROR(Status)) {
                continue;
            }
            //
            // Does it support Acoustics?
            //
            if (!(IdentifyDriveInfo->Command_Set_Supported_83 & AUTOMATIC_ACOUSTIC_FEATURE_SET_SUPPORTED)) { 
                AcousticSetupProtocol->AcousticSupportIndv[PortIndex] = 0;
                continue;
            }
            //
            // Get the drive's current acoustic value
            //
            CurrentLevel = (IdentifyDriveInfo->Acoustic_Level_94);
            //
            // Acoustic is supported by the drive
            //
            AcousticSetupProtocol->AcousticSupportIndv[PortIndex] = 1;

            if (AcousticSetupProtocol->AcousticLevelIndv[PortIndex] != ACOUSTIC_SUPPORT_DISABLE) {
                if (AcousticSetupProtocol->AcousticLevelIndv[PortIndex] == ACOUSTIC_LEVEL_BYPASS) {
                    if ( (CurrentLevel & 0xFF) != (CurrentLevel >> 8) ) { // If not already set
                        Status = IssueSetFeatureCmd(Controller_Handle[i], 
                                                    Device_Handle[j],
                                                    ACOUSTIC_MANAGEMENT_ENABLE,
                                                    (UINT8)(IdentifyDriveInfo->Acoustic_Level_94 >> 8));
                    }
                } else {
                    if ( AcousticSetupProtocol->AcousticLevelIndv[PortIndex] != (UINT8)(CurrentLevel & 0xFF) ) {
                        Status = IssueSetFeatureCmd(Controller_Handle[i], 
                                                    Device_Handle[j],
                                                    ACOUSTIC_MANAGEMENT_ENABLE,
                                                    AcousticSetupProtocol->AcousticLevelIndv[PortIndex]);
                    }
                }
            } else {
                //
                // Acoustic disabled in Setup but not in drive then issue cmd
                //
                if (IdentifyDriveInfo->Command_Set_Enabled_86 & AUTOMATIC_ACOUSTIC_FEATURE_SET_SUPPORTED) {
                    Status = IssueSetFeatureCmd(Controller_Handle[i], 
                                                Device_Handle[j], 
                                                ACOUSTIC_MANAGEMENT_DISABLE, 
                                                0);
                }
            }
        }
    }

    pBS->FreePool (IdentifyDriveInfo);

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

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

    return;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure: IssueSetFeatureCmd
//
// Description: This function issues the SetFeature cmd
//
// Input:       EFI_HANDLE  ControllerHandle,
//              EFI_HANDLE  DeviceHandle,
//              UINT8       SubCommand,
//              UINT8       Mode
//
// Output:      Status
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS 
IssueSetFeatureCmd (
    EFI_HANDLE  ControllerHandle,
    EFI_HANDLE  DeviceHandle,
    UINT8       SubCommand,
    UINT8       Mode
)
{
    EFI_STATUS              Status;
    EFI_BLOCK_IO_PROTOCOL   *BlockIO;
    AHCI_BUS_PROTOCOL       *AhciBusInterface;
    SATA_DEVICE_INTERFACE   *SataDevInterface;
    IDE_BUS_PROTOCOL        *IdeBusInterface;
    COMMAND_STRUCTURE       CommandStructure = {0};

    Status = pBS->HandleProtocol(DeviceHandle, &gEfiBlockIoProtocolGuid, &BlockIO);
    ASSERT_EFI_ERROR(Status);

    if (EFI_ERROR(Status)) {
        return  Status;
    }
    //
    // Check if IDE/AHCI by check if IDEBus/AHCUBus Protocol is installed
    //
    Status = pBS->OpenProtocol( ControllerHandle,
                                &gEfiIdeBusInitProtocolGuid,
                                &IdeBusInterface,
                                TheImageHandle,
                                ControllerHandle,
                                EFI_OPEN_PROTOCOL_GET_PROTOCOL );
    if(!EFI_ERROR(Status)) {
        //
        // We are in IDE mode
        //
        IdeBusInterface = ((IDE_BLOCK_IO *)BlockIO)->IdeBusInterface;

        //
        // Idebus API changed from Core 4.6.5.2. Added Core Version check for the OLD 
        // Core support.
        //
#if defined CORE_COMBINED_VERSION && (CORE_COMBINED_VERSION > 0x4028b) 

        Status = IdeBusInterface->IdeNonDataCommand(IdeBusInterface,
                                                       SubCommand, Mode, 0, 0, 0, 0, 0,
                                                       0, 0, IdeBusInterface->IdeDevice.Device << 4,
                                                       SET_FEATURE_COMMAND);
#else
        Status = IdeBusInterface->IdeNonDataCommand(IdeBusInterface,
                                                       SubCommand, Mode, 0,
                                                       0, 0, IdeBusInterface->IdeDevice.Device << 4,
                                                       SET_FEATURE_COMMAND);
#endif
        return  Status;
    }

    Status = pBS->OpenProtocol( ControllerHandle,
                                &gEfiAhciBusProtocolGuid,
                                &AhciBusInterface,
                                TheImageHandle,
                                ControllerHandle,
                                EFI_OPEN_PROTOCOL_GET_PROTOCOL );

    if(!EFI_ERROR(Status)) {
        SataDevInterface = ((SATA_BLOCK_IO *)BlockIO)->SataDevInterface;
        //
        // It is in AHCI mode
        //
        CommandStructure.Command = SET_FEATURE_COMMAND;
        CommandStructure.Features = SubCommand;
        CommandStructure.SectorCount = Mode;
        Status = AhciBusInterface->ExecuteNonDataCommand (SataDevInterface, CommandStructure);
    }

    return Status;
}

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