Imported Upstream version 3.0
[platform/upstream/gnu-efi.git] / lib / hw.c
1 /*++
2
3 Copyright (c) 1998  Intel Corporation
4
5 Module Name:
6
7     hw.c
8
9 Abstract:
10
11     Debug library functions for Hardware IO access
12
13
14
15 Revision History
16
17 --*/
18
19 #include "lib.h"
20
21
22 EFI_STATUS
23 InitializeGlobalIoDevice (
24         IN  EFI_DEVICE_PATH             *DevicePath,
25         IN  EFI_GUID                    *Protocol,
26         IN  CHAR8                       *ErrorStr,
27         OUT EFI_DEVICE_IO_INTERFACE     **GlobalIoFncs 
28         )
29 /*++
30
31 Routine Description:
32
33     Check to see if DevicePath exists for a given Protocol. Return Error if it 
34     exists. Return GlobalIoFuncs set match the DevicePath
35
36   Arguments:
37
38     DevicePath      - to operate on
39     Protocol        - to check the DevicePath against
40     ErrorStr        - ASCII string to display on error
41     GlobalIoFncs    - Returned with DeviceIoProtocol for the DevicePath
42
43 Returns:
44
45     Pass or Fail based on  wether GlobalIoFncs where found
46
47 --*/
48 {
49     EFI_STATUS      Status;
50     EFI_HANDLE      Handle;
51
52     //
53     // Check to see if this device path already has Protocol on it.
54     //  if so we are loading recursivly and should exit with an error
55     //
56     Status = uefi_call_wrapper(BS->LocateDevicePath, 3, Protocol, &DevicePath, &Handle);
57     if (!EFI_ERROR(Status)) {
58         DEBUG ((D_INIT, "Device Already Loaded for %a device\n", ErrorStr));
59         return EFI_LOAD_ERROR;
60     }
61
62     Status = uefi_call_wrapper(BS->LocateDevicePath, 3, &DeviceIoProtocol, &DevicePath, &Handle);
63     if (!EFI_ERROR(Status)) {
64         Status = uefi_call_wrapper(BS->HandleProtocol, 3, Handle, &DeviceIoProtocol, (VOID*)GlobalIoFncs);
65     }
66
67     ASSERT (!EFI_ERROR(Status));
68     return Status;
69 }
70
71 UINT32 
72 ReadPort (
73         IN  EFI_DEVICE_IO_INTERFACE     *GlobalIoFncs, 
74         IN  EFI_IO_WIDTH                Width,
75         IN  UINTN                       Port
76         )
77 {
78     UINT32       Data;
79     EFI_STATUS  Status;
80
81     Status = uefi_call_wrapper(GlobalIoFncs->Io.Read, 5, GlobalIoFncs, Width, (UINT64)Port, 1, &Data);
82     ASSERT(!EFI_ERROR(Status));
83     return Data;
84 }
85
86 UINT32 
87 WritePort (
88         IN  EFI_DEVICE_IO_INTERFACE     *GlobalIoFncs, 
89         IN  EFI_IO_WIDTH                Width,
90         IN  UINTN                       Port,
91         IN  UINTN                       Data
92         )
93 {
94     EFI_STATUS  Status;
95
96     Status = uefi_call_wrapper(GlobalIoFncs->Io.Write, 5, GlobalIoFncs, Width, (UINT64)Port, 1, &Data);
97     ASSERT(!EFI_ERROR(Status));
98     return (UINT32)Data;
99 }
100
101 UINT32 
102 ReadPciConfig (
103         IN  EFI_DEVICE_IO_INTERFACE     *GlobalIoFncs, 
104         IN  EFI_IO_WIDTH                Width,
105         IN  UINTN                       Address
106         )
107 {
108     UINT32       Data;
109     EFI_STATUS  Status;
110
111     Status = uefi_call_wrapper(GlobalIoFncs->Pci.Read, 5, GlobalIoFncs, Width, (UINT64)Address, 1, &Data);
112     ASSERT(!EFI_ERROR(Status));
113     return Data;
114 }
115
116 UINT32 
117 WritePciConfig (
118         IN  EFI_DEVICE_IO_INTERFACE     *GlobalIoFncs, 
119         IN  EFI_IO_WIDTH                Width,
120         IN  UINTN                       Address,
121         IN  UINTN                       Data
122         )
123 {
124     EFI_STATUS  Status;
125
126     Status = uefi_call_wrapper(GlobalIoFncs->Pci.Write, 5, GlobalIoFncs, Width, (UINT64)Address, 1, &Data);
127     ASSERT(!EFI_ERROR(Status));
128     return (UINT32)Data;
129 }
130
131
132