summaryrefslogtreecommitdiffstats
path: root/EmbeddedPkg/Drivers/SataSiI3132Dxe/SataSiI3132.c
blob: df0f16d19a8f0b8e8a874e1930fdc2e75c770e0d (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/** @file
*  PCIe Sata support for the Silicon Image I3132
*
*  Copyright (c) 2011-2015, ARM Limited. All rights reserved.
*
*  SPDX-License-Identifier: BSD-2-Clause-Patent
*
**/

#include "SataSiI3132.h"

#include <IndustryStandard/Acpi10.h>

#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/DxeServicesTableLib.h>
#include <Library/BaseLib.h>

#define ACPI_SPECFLAG_PREFETCHABLE      0x06

EFI_DRIVER_BINDING_PROTOCOL
gSataSiI3132DriverBinding = {
  SataSiI3132DriverBindingSupported,
  SataSiI3132DriverBindingStart,
  SataSiI3132DriverBindingStop,
  0x30,
  NULL,
  NULL
};

EFI_STATUS
SataSiI3132PortConstructor (
  IN  SATA_SI3132_INSTANCE *SataSiI3132Instance,
  IN  UINTN                Index
  )
{
  EFI_STATUS            Status;
  SATA_SI3132_PORT      *Port;
  VOID                  *HostPRB;
  EFI_PHYSICAL_ADDRESS  PhysAddrHostPRB;
  VOID                  *PciAllocMappingPRB;
  UINTN                 NumberOfBytes;

  Port = &(SataSiI3132Instance->Ports[Index]);

  Port->Index    = Index;
  Port->RegBase  = Index * 0x2000;
  Port->Instance = SataSiI3132Instance;
  InitializeListHead (&(Port->Devices));

  NumberOfBytes = sizeof (SATA_SI3132_PRB);
  Status = SataSiI3132Instance->PciIo->AllocateBuffer (
             SataSiI3132Instance->PciIo, AllocateAnyPages, EfiBootServicesData,
             EFI_SIZE_TO_PAGES (NumberOfBytes), &HostPRB, 0
             );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  // Check the alignment of the PCI Buffer
  ASSERT (((UINTN)HostPRB & (0x1000 - 1)) == 0);
  Status = SataSiI3132Instance->PciIo->Map (
             SataSiI3132Instance->PciIo, EfiPciIoOperationBusMasterCommonBuffer, HostPRB,
             &NumberOfBytes, &PhysAddrHostPRB, &PciAllocMappingPRB
             );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Port->HostPRB            = HostPRB;
  Port->PhysAddrHostPRB    = PhysAddrHostPRB;
  Port->PciAllocMappingPRB = PciAllocMappingPRB;

  return Status;
}

STATIC
EFI_STATUS
SataSiI3132Constructor (
  IN  EFI_PCI_IO_PROTOCOL     *PciIo,
  OUT SATA_SI3132_INSTANCE**  SataSiI3132Instance
  )
{
  SATA_SI3132_INSTANCE    *Instance;
  EFI_ATA_PASS_THRU_MODE  *AtaPassThruMode;

  if (!SataSiI3132Instance) {
    return EFI_INVALID_PARAMETER;
  }

  Instance = (SATA_SI3132_INSTANCE*)AllocateZeroPool (sizeof (SATA_SI3132_INSTANCE));
  if (Instance == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  Instance->Signature           = SATA_SII3132_SIGNATURE;
  Instance->PciIo               = PciIo;

  AtaPassThruMode = (EFI_ATA_PASS_THRU_MODE*)AllocatePool (sizeof (EFI_ATA_PASS_THRU_MODE));
  AtaPassThruMode->Attributes = EFI_ATA_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_ATA_PASS_THRU_ATTRIBUTES_LOGICAL;
  AtaPassThruMode->IoAlign = 0x1000;

  // Initialize SiI3132 ports
  SataSiI3132PortConstructor (Instance, 0);
  SataSiI3132PortConstructor (Instance, 1);

  // Set ATA Pass Thru Protocol
  Instance->AtaPassThruProtocol.Mode            = AtaPassThruMode;
  Instance->AtaPassThruProtocol.PassThru        = SiI3132AtaPassThru;
  Instance->AtaPassThruProtocol.GetNextPort     = SiI3132GetNextPort;
  Instance->AtaPassThruProtocol.GetNextDevice   = SiI3132GetNextDevice;
  Instance->AtaPassThruProtocol.BuildDevicePath = SiI3132BuildDevicePath;
  Instance->AtaPassThruProtocol.GetDevice       = SiI3132GetDevice;
  Instance->AtaPassThruProtocol.ResetPort       = SiI3132ResetPort;
  Instance->AtaPassThruProtocol.ResetDevice     = SiI3132ResetDevice;

  *SataSiI3132Instance = Instance;

  return EFI_SUCCESS;
}

EFI_STATUS
SiI3132SoftResetCommand (
  IN   SATA_SI3132_PORT *Port,
  OUT  UINT32* Signature
  )
{
  EFI_STATUS                        Status;
  EFI_ATA_PASS_THRU_COMMAND_PACKET  Packet;
  EFI_ATA_STATUS_BLOCK              Asb;
  EFI_ATA_COMMAND_BLOCK             Acb;
  CONST UINT16                      PortMultiplierPort = 0;

  ZeroMem (&Acb, sizeof (EFI_ATA_COMMAND_BLOCK));

  Acb.Reserved1[1] = 0;

  Packet.Asb      = &Asb;
  Packet.Acb      = &Acb;
  Packet.Timeout  = 100000;
  Packet.Protocol = EFI_ATA_PASS_THRU_PROTOCOL_ATA_SOFTWARE_RESET;

  Status = SiI3132AtaPassThruCommand (Port->Instance, Port, PortMultiplierPort, &Packet, 0);

  if (Status == EFI_SUCCESS) {
    *Signature = (Asb.AtaCylinderHigh << 24) | (Asb.AtaCylinderLow << 16) |
                 (Asb.AtaSectorNumber << 8 ) | (Asb.AtaSectorCount);
  }
  return Status;
}

EFI_STATUS
SataSiI3132PortInitialization (
  IN SATA_SI3132_PORT *Port
  )
{
  UINT32                  Value32;
  SATA_SI3132_DEVICE*     Device;
  UINT32                  Signature;
  EFI_STATUS              Status;
  EFI_PCI_IO_PROTOCOL*    PciIo;

  Status = SiI3132HwResetPort (Port);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  PciIo = Port->Instance->PciIo;

  // Is a device is present ?
  Status = SATA_PORT_READ32 (Port->RegBase + SII3132_PORT_SSTATUS_REG, &Value32);
  if (!EFI_ERROR (Status) && (Value32 & 0x3)) {
    // Do a soft reset to see if it is a port multiplier
    SATA_TRACE ("SataSiI3132PortInitialization: soft reset - it is a port multiplier\n");
    Status = SiI3132SoftResetCommand (Port, &Signature);
    if (!EFI_ERROR (Status)) {
      if (Signature == SII3132_PORT_SIGNATURE_PMP) {
        SATA_TRACE ("SataSiI3132PortInitialization(): a Port Multiplier is present");
        if (FeaturePcdGet (PcdSataSiI3132FeaturePMPSupport)) {
          ASSERT (0); // Not supported yet
        } else {
          return EFI_UNSUPPORTED;
        }
      } else if (Signature == SII3132_PORT_SIGNATURE_ATAPI) {
        ASSERT (0); // Not supported yet
        SATA_TRACE ("SataSiI3132PortInitialization(): an ATAPI device is present");
        return EFI_UNSUPPORTED;
      } else if (Signature == SII3132_PORT_SIGNATURE_ATA) {
        SATA_TRACE ("SataSiI3132PortInitialization(): an ATA device is present");
      } else {
        SATA_TRACE ("SataSiI3132PortInitialization(): Present device unknown!");
        ASSERT (0); // Not supported
        return EFI_UNSUPPORTED;
      }

      // Create Device
      Device            = (SATA_SI3132_DEVICE*)AllocatePool (sizeof (SATA_SI3132_DEVICE));
      Device->Index     = Port->Index; //TODO: Could need to be fixed when SATA Port Multiplier support
      Device->Port      = Port;
      Device->BlockSize = 0;

      // Attached the device to the Sata Port
      InsertTailList (&Port->Devices, &Device->Link);

      SATA_TRACE ("SataSiI3132PortInitialization(): Port Ready");
    }
  }
  return Status;
}

EFI_STATUS
SataSiI3132Initialization (
  IN SATA_SI3132_INSTANCE* SataSiI3132Instance
  )
{
  UINTN                 Index;
  EFI_PCI_IO_PROTOCOL*  PciIo;

  if (!SataSiI3132Instance) {
    return EFI_INVALID_PARAMETER;
  }

  PciIo = SataSiI3132Instance->PciIo;

  // Turn Off GPIO
  SATA_GLOBAL_WRITE32 (SII3132_GLOBAL_FLASHADDR_REG, 0x0);

  // Clear Global Control Register
  SATA_GLOBAL_WRITE32 (SII3132_GLOBAL_CONTROL_REG, 0x0);

  for (Index = 0; Index < SATA_SII3132_MAXPORT; Index++) {
    SataSiI3132PortInitialization (&(SataSiI3132Instance->Ports[Index]));
  }

  return EFI_SUCCESS;
}

/**
  Test to see if this driver supports ControllerHandle.

  @param  This                 Protocol instance pointer.
  @param  Controller           Handle of device to test.
  @param  RemainingDevicePath  Not used.

  @return EFI_SUCCESS          This driver supports this device.
  @return EFI_UNSUPPORTED      This driver does not support this device.

**/
EFI_STATUS
EFIAPI
SataSiI3132DriverBindingSupported (
  IN EFI_DRIVER_BINDING_PROTOCOL *This,
  IN EFI_HANDLE                  Controller,
  IN EFI_DEVICE_PATH_PROTOCOL    *RemainingDevicePath
  )
{
  EFI_STATUS              Status;
  EFI_PCI_IO_PROTOCOL     *PciIo;
  UINT32                   PciID;

  //
  // Test whether there is PCI IO Protocol attached on the controller handle.
  //
  Status = gBS->OpenProtocol (
                Controller,
                &gEfiPciIoProtocolGuid,
                (VOID **) &PciIo,
                This->DriverBindingHandle,
                Controller,
                EFI_OPEN_PROTOCOL_BY_DRIVER
                );
  if (EFI_ERROR (Status)) {
      return Status;
  }

  Status = PciIo->Pci.Read (
                      PciIo,
                      EfiPciIoWidthUint32,
                      PCI_VENDOR_ID_OFFSET,
                      1,
                      &PciID
                      );
  if (EFI_ERROR (Status)) {
      Status = EFI_UNSUPPORTED;
      goto ON_EXIT;
  }

  //
  // Test whether the controller belongs to SATA Mass Storage type
  //
  if (PciID != ((SATA_SII3132_DEVICE_ID << 16) | SATA_SII3132_VENDOR_ID)) {
      Status = EFI_UNSUPPORTED;
  }

ON_EXIT:
  gBS->CloseProtocol (
       Controller,
       &gEfiPciIoProtocolGuid,
       This->DriverBindingHandle,
       Controller
       );

  return Status;
}

BOOLEAN mbStarted = FALSE;

/**
  Starting the Pci SATA Driver.

  @param  This                 Protocol instance pointer.
  @param  Controller           Handle of device to test.
  @param  RemainingDevicePath  Not used.

  @return EFI_SUCCESS          supports this device.
  @return EFI_UNSUPPORTED      do not support this device.
  @return EFI_DEVICE_ERROR     cannot be started due to device Error.
  @return EFI_OUT_OF_RESOURCES cannot allocate resources.

**/
EFI_STATUS
EFIAPI
SataSiI3132DriverBindingStart (
  IN EFI_DRIVER_BINDING_PROTOCOL *This,
  IN EFI_HANDLE                  Controller,
  IN EFI_DEVICE_PATH_PROTOCOL    *RemainingDevicePath
  )
{
  EFI_STATUS              Status;
  EFI_PCI_IO_PROTOCOL     *PciIo;
  UINT64                  Supports;
  UINT64                  OriginalPciAttributes;
  BOOLEAN                 PciAttributesSaved;
  UINT32                  PciID;
  SATA_SI3132_INSTANCE    *SataSiI3132Instance = NULL;

  SATA_TRACE ("SataSiI3132DriverBindingStart()");

  //TODO: Find a nicer way to do it !
  if (mbStarted) {
    return EFI_SUCCESS; // Don't restart me !
  }

  //
  // Open the PciIo Protocol
  //
  Status = gBS->OpenProtocol (
                Controller,
                &gEfiPciIoProtocolGuid,
                (VOID **) &PciIo,
                This->DriverBindingHandle,
                Controller,
                EFI_OPEN_PROTOCOL_BY_DRIVER
                );
  if (EFI_ERROR (Status)) {
      return Status;
  }

  PciAttributesSaved = FALSE;
  //
  // Save original PCI attributes
  //
  Status = PciIo->Attributes (
                  PciIo,
                  EfiPciIoAttributeOperationGet,
                  0,
                  &OriginalPciAttributes
                  );
  if (EFI_ERROR (Status)) {
      goto CLOSE_PCIIO;
  }
  PciAttributesSaved = TRUE;

  Status = PciIo->Attributes (
                  PciIo,
                  EfiPciIoAttributeOperationSupported,
                  0,
                  &Supports
                  );
  if (!EFI_ERROR (Status)) {
      Supports &= EFI_PCI_DEVICE_ENABLE | EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE;
      Status = PciIo->Attributes (
                        PciIo,
                        EfiPciIoAttributeOperationEnable,
                        Supports,
                        NULL
                        );
  }
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "SataSiI3132DriverBindingStart: failed to enable controller\n"));
    goto CLOSE_PCIIO;
  }

  //
  // Get the Pci device class code.
  //
  Status = PciIo->Pci.Read (
                      PciIo,
                      EfiPciIoWidthUint32,
                      PCI_VENDOR_ID_OFFSET,
                      1,
                      &PciID
                      );
  if (EFI_ERROR (Status)) {
    Status = EFI_UNSUPPORTED;
    goto CLOSE_PCIIO;
  }

  //
  // Test whether the controller belongs to SATA Mass Storage type
  //
  if (PciID != ((SATA_SII3132_DEVICE_ID << 16) | SATA_SII3132_VENDOR_ID)) {
    Status = EFI_UNSUPPORTED;
    goto CLOSE_PCIIO;
  }

  // Create SiI3132 Sata Instance
  Status = SataSiI3132Constructor (PciIo, &SataSiI3132Instance);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  // Initialize SiI3132 Sata Controller
  Status = SataSiI3132Initialization (SataSiI3132Instance);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  // Install Ata Pass Thru Protocol
  Status = gBS->InstallProtocolInterface (
              &Controller,
              &gEfiAtaPassThruProtocolGuid,
              EFI_NATIVE_INTERFACE,
              &(SataSiI3132Instance->AtaPassThruProtocol)
              );
  if (EFI_ERROR (Status)) {
    goto FREE_POOL;
  }

/*  //
  // Create event to stop the HC when exit boot service.
  //
  Status = gBS->CreateEventEx (
                EVT_NOTIFY_SIGNAL,
                TPL_NOTIFY,
                EhcExitBootService,
                Ehc,
                &gEfiEventExitBootServicesGuid,
                &Ehc->ExitBootServiceEvent
                );
  if (EFI_ERROR (Status)) {
      goto UNINSTALL_USBHC;
  }*/

  mbStarted = TRUE;

  SATA_TRACE ("SataSiI3132DriverBindingStart() Success!");
  return EFI_SUCCESS;

FREE_POOL:
  //TODO: Free SATA Instance

CLOSE_PCIIO:
  if (PciAttributesSaved) {
      //
      // Restore original PCI attributes
      //
      PciIo->Attributes (
                      PciIo,
                      EfiPciIoAttributeOperationSet,
                      OriginalPciAttributes,
                      NULL
                      );
  }

  gBS->CloseProtocol (
       Controller,
       &gEfiPciIoProtocolGuid,
       This->DriverBindingHandle,
       Controller
       );

  return Status;
}

/**
  Stop this driver on ControllerHandle. Support stopping any child handles
  created by this driver.

  @param  This                 Protocol instance pointer.
  @param  Controller           Handle of device to stop driver on.
  @param  NumberOfChildren     Number of Children in the ChildHandleBuffer.
  @param  ChildHandleBuffer    List of handles for the children we need to stop.

  @return EFI_SUCCESS          Success.
  @return EFI_DEVICE_ERROR     Fail.

**/
EFI_STATUS
EFIAPI
SataSiI3132DriverBindingStop (
  IN EFI_DRIVER_BINDING_PROTOCOL *This,
  IN EFI_HANDLE                  Controller,
  IN UINTN                       NumberOfChildren,
  IN EFI_HANDLE                  *ChildHandleBuffer
  )
{
  SATA_TRACE ("SataSiI3132DriverBindingStop()");
  return EFI_UNSUPPORTED;
}

/**
  Entry point of this driver

  @param ImageHandle     Handle of driver image
  @param SystemTable     Point to EFI_SYSTEM_TABLE

  @retval EFI_OUT_OF_RESOURCES  Can not allocate memory resource
  @retval EFI_DEVICE_ERROR      Can not install the protocol instance
  @retval EFI_SUCCESS           Success to initialize the Pci host bridge.
**/
EFI_STATUS
EFIAPI
InitializeSataSiI3132 (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  SATA_TRACE ("InitializeSataSiI3132 ()");

  return EfiLibInstallDriverBindingComponentName2 (
         ImageHandle,
         SystemTable,
         &gSataSiI3132DriverBinding,
         ImageHandle,
         &gSataSiI3132ComponentName,
         &gSataSiI3132ComponentName2
         );
}