[edk2] NOR Flash Driver

Subject: [edk2] NOR Flash Driver

From: vijaikumar k <vijaikumar.k@mistralsolutions.com>

To: edk2-devel@lists.sourceforge.net

Date: 2014-06-13 00:12:45

Hi All,
I am a newbie to EFI. I have the following problem
Problem: We have Atom Processor connected to a PCIe to PCI bridge. On the other side of the PCI bridge we have a PCI device which gives Memory Mapped access to NOR Flash via BAR0. I have written a driver to boot from the NOR flash. The driver is registered successfully but I am not able to see my device when I do "Map -r".
By the way the NOR has no File System associated with it. It is still RAW. Following are the line of code written by me for start routine.

Code:
EFI_STATUS
EFIAPI
NorFlashDriverBindingStart (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE ControllerHandle,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
)
{
EFI_STATUS Status;
EFI_PCI_IO_PROTOCOL *PciIo;
EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
NOR_FLASH_INSTANCE *Instance;
UINT8 CmdSts;
Instance=NULL;
Print(L"\nInside Start of NorFlash");
Status = gBS->OpenProtocol(
ControllerHandle,
&gEfiPciIoProtocolGuid,
(VOID **)&PciIo,
This->DriverBindingHandle,
ControllerHandle,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
goto Error;
}
Print(L"\nPCI IO Protocol opened successfully");

Status = gBS->OpenProtocol (
ControllerHandle,
&gEfiDevicePathProtocolGuid,
(VOID **) &ParentDevicePath,
This->DriverBindingHandle,
ControllerHandle,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
Print(L"\nDevice Path Protocol Failed to register");
goto Error;
}
Print(L"\nDevice Path Protocol Registered Successfully");
Instance = AllocateCopyPool (sizeof(NOR_FLASH_INSTANCE),&mNorFlashInstanceTemplate);
if (Instance == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Print(L"\nResource Pool has been allocated");

Instance->Signature=NOR_FLASH_SIGNATURE;
Instance->Size = NOR_SIZE;
Instance->PciIo=PciIo;
Instance->BlockIoProtocol.Media = &Instance->Media;
Instance->Media.MediaId = NOR_MEDIA_ID;
Instance->Media.BlockSize = NOR_BLOCK_SIZE;
Instance->Media.LastBlock = (NOR_SIZE / NOR_BLOCK_SIZE)-1;

Instance->Initialized = TRUE;
Print(L"\nBefore Installing multiple protocols");
//Set Command register to enable Bus Master and MemorySpace
PciIo->Pci.Read (
PciIo,
EfiPciIoWidthUint8,
PCI_COMMAND_OFFSET,
sizeof (UINT8),
&CmdSts
);
CmdSts |= EFI_PCI_COMMAND_MEMORY_SPACE |
EFI_PCI_COMMAND_BUS_MASTER;
PciIo->Pci.Write(
PciIo,
EfiPciIoWidthUint8,
PCI_COMMAND_OFFSET,
sizeof (UINT8),
&CmdSts
);
//Removed Device path as already there is a device path associated with PCIIO: by Andrew Fish
Status = gBS->InstallMultipleProtocolInterfaces (
&Instance->Handle,
&gEfiBlockIoProtocolGuid, &Instance->BlockIoProtocol,
&gEfiDevicePathProtocolGuid, Instance->DevPath,
NULL
);

if (EFI_ERROR(Status)) {
Print(L"\nFailedInstalling multiple protocols");
FreePool(Instance);
return Status;
}
Print(L"\nAfter Installing multiple protocols");
return Status;

Error:
gBS->CloseProtocol (
ControllerHandle,
&gEfiDevicePathProtocolGuid,
This->DriverBindingHandle,
ControllerHandle
);
gBS->CloseProtocol (
ControllerHandle,
&gEfiPciIoProtocolGuid,
This->DriverBindingHandle,
ControllerHandle
);
if (Instance->DevPath != NULL) {
FreePool (Instance->DevPath);
}
FreePool (Instance);
return Status;
}

Regards
Vijai Kumar K
+918095863361
Ext:699