removed unnecessary name space declarations in some top level elements
[mirror_edk2.git] / EdkModulePkg / Bus / Pci / CirrusLogic / Dxe / CirrusLogic5430.c
1 /** @file
2 Cirrus Logic 5430 Controller Driver.
3 This driver is a sample implementation of the UGA Draw Protocol for the
4 Cirrus Logic 5430 family of PCI video controllers. This driver is only
5 usable in the EFI pre-boot environment. This sample is intended to show
6 how the UGA Draw Protocol is able to function. The UGA I/O Protocol is not
7 implemented in this sample. A fully compliant EFI UGA driver requires both
8 the UGA Draw and the UGA I/O Protocol. Please refer to Microsoft's
9 documentation on UGA for details on how to write a UGA driver that is able
10 to function both in the EFI pre-boot environment and from the OS runtime.
11
12 Copyright (c) 2006, Intel Corporation
13 All rights reserved. This program and the accompanying materials
14 are licensed and made available under the terms and conditions of the BSD License
15 which accompanies this distribution. The full text of the license may be found at
16 http://opensource.org/licenses/bsd-license.php
17
18 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
19 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20
21 **/
22
23 //
24 // Cirrus Logic 5430 Controller Driver
25 //
26
27 #include "CirrusLogic5430.h"
28
29 EFI_DRIVER_BINDING_PROTOCOL gCirrusLogic5430DriverBinding = {
30 CirrusLogic5430ControllerDriverSupported,
31 CirrusLogic5430ControllerDriverStart,
32 CirrusLogic5430ControllerDriverStop,
33 0x10,
34 NULL,
35 NULL
36 };
37
38 /**
39 CirrusLogic5430ControllerDriverSupported
40
41 TODO: This - add argument and description to function comment
42 TODO: Controller - add argument and description to function comment
43 TODO: RemainingDevicePath - add argument and description to function comment
44 **/
45 EFI_STATUS
46 EFIAPI
47 CirrusLogic5430ControllerDriverSupported (
48 IN EFI_DRIVER_BINDING_PROTOCOL *This,
49 IN EFI_HANDLE Controller,
50 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
51 )
52 {
53 EFI_STATUS Status;
54 EFI_PCI_IO_PROTOCOL *PciIo;
55 PCI_TYPE00 Pci;
56
57 //
58 // Open the PCI I/O Protocol
59 //
60 Status = gBS->OpenProtocol (
61 Controller,
62 &gEfiPciIoProtocolGuid,
63 (VOID **) &PciIo,
64 This->DriverBindingHandle,
65 Controller,
66 EFI_OPEN_PROTOCOL_BY_DRIVER
67 );
68 if (EFI_ERROR (Status)) {
69 return Status;
70 }
71
72 //
73 // Read the PCI Configuration Header from the PCI Device
74 //
75 Status = PciIo->Pci.Read (
76 PciIo,
77 EfiPciIoWidthUint32,
78 0,
79 sizeof (Pci) / sizeof (UINT32),
80 &Pci
81 );
82 if (EFI_ERROR (Status)) {
83 goto Done;
84 }
85
86 Status = EFI_UNSUPPORTED;
87 //
88 // See if the I/O enable is on. Most systems only allow one VGA device to be turned on
89 // at a time, so see if this is one that is turned on.
90 //
91 // if (((Pci.Hdr.Command & 0x01) == 0x01)) {
92 //
93 // See if this is a Cirrus Logic PCI controller
94 //
95 if (Pci.Hdr.VendorId == CIRRUS_LOGIC_VENDOR_ID) {
96 //
97 // See if this is a 5430 or a 5446 PCI controller
98 //
99 if (Pci.Hdr.DeviceId == CIRRUS_LOGIC_5430_DEVICE_ID) {
100 Status = EFI_SUCCESS;
101 }
102
103 if (Pci.Hdr.DeviceId == CIRRUS_LOGIC_5430_ALTERNATE_DEVICE_ID) {
104 Status = EFI_SUCCESS;
105 }
106
107 if (Pci.Hdr.DeviceId == CIRRUS_LOGIC_5446_DEVICE_ID) {
108 Status = EFI_SUCCESS;
109 }
110 }
111
112 Done:
113 //
114 // Close the PCI I/O Protocol
115 //
116 gBS->CloseProtocol (
117 Controller,
118 &gEfiPciIoProtocolGuid,
119 This->DriverBindingHandle,
120 Controller
121 );
122
123 return Status;
124 }
125
126 /**
127 CirrusLogic5430ControllerDriverStart
128
129 TODO: This - add argument and description to function comment
130 TODO: Controller - add argument and description to function comment
131 TODO: RemainingDevicePath - add argument and description to function comment
132 **/
133 EFI_STATUS
134 EFIAPI
135 CirrusLogic5430ControllerDriverStart (
136 IN EFI_DRIVER_BINDING_PROTOCOL *This,
137 IN EFI_HANDLE Controller,
138 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
139 )
140 {
141 EFI_STATUS Status;
142 CIRRUS_LOGIC_5430_PRIVATE_DATA *Private;
143
144 //
145 // Allocate Private context data for UGA Draw inteface.
146 //
147 Private = NULL;
148 Private = AllocateZeroPool (sizeof (CIRRUS_LOGIC_5430_PRIVATE_DATA));
149 if (Private == NULL) {
150 Status = EFI_OUT_OF_RESOURCES;
151 goto Error;
152 }
153
154 //
155 // Set up context record
156 //
157 Private->Signature = CIRRUS_LOGIC_5430_PRIVATE_DATA_SIGNATURE;
158 Private->Handle = Controller;
159
160 //
161 // Open PCI I/O Protocol
162 //
163 Status = gBS->OpenProtocol (
164 Private->Handle,
165 &gEfiPciIoProtocolGuid,
166 (VOID **) &Private->PciIo,
167 This->DriverBindingHandle,
168 Private->Handle,
169 EFI_OPEN_PROTOCOL_BY_DRIVER
170 );
171 if (EFI_ERROR (Status)) {
172 goto Error;
173 }
174
175 Status = Private->PciIo->Attributes (
176 Private->PciIo,
177 EfiPciIoAttributeOperationEnable,
178 EFI_PCI_DEVICE_ENABLE | EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY | EFI_PCI_IO_ATTRIBUTE_VGA_IO,
179 NULL
180 );
181 if (EFI_ERROR (Status)) {
182 goto Error;
183 }
184
185 //
186 // Start the UGA Draw software stack.
187 //
188 Status = CirrusLogic5430UgaDrawConstructor (Private);
189 if (EFI_ERROR (Status)) {
190 goto Error;
191 }
192
193 //
194 // Publish the UGA Draw interface to the world
195 //
196 Status = gBS->InstallMultipleProtocolInterfaces (
197 &Private->Handle,
198 &gEfiUgaDrawProtocolGuid,
199 &Private->UgaDraw,
200 NULL
201 );
202
203 Error:
204 if (EFI_ERROR (Status)) {
205 if (Private) {
206 if (Private->PciIo) {
207 Private->PciIo->Attributes (
208 Private->PciIo,
209 EfiPciIoAttributeOperationDisable,
210 EFI_PCI_DEVICE_ENABLE | EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY | EFI_PCI_IO_ATTRIBUTE_VGA_IO,
211 NULL
212 );
213 }
214 }
215
216 //
217 // Close the PCI I/O Protocol
218 //
219 gBS->CloseProtocol (
220 Private->Handle,
221 &gEfiPciIoProtocolGuid,
222 This->DriverBindingHandle,
223 Private->Handle
224 );
225 if (Private) {
226 gBS->FreePool (Private);
227 }
228 }
229
230 return Status;
231 }
232
233 /**
234 CirrusLogic5430ControllerDriverStop
235
236 TODO: This - add argument and description to function comment
237 TODO: Controller - add argument and description to function comment
238 TODO: NumberOfChildren - add argument and description to function comment
239 TODO: ChildHandleBuffer - add argument and description to function comment
240 TODO: EFI_SUCCESS - add return value to function comment
241 **/
242 EFI_STATUS
243 EFIAPI
244 CirrusLogic5430ControllerDriverStop (
245 IN EFI_DRIVER_BINDING_PROTOCOL *This,
246 IN EFI_HANDLE Controller,
247 IN UINTN NumberOfChildren,
248 IN EFI_HANDLE *ChildHandleBuffer
249 )
250 {
251 EFI_UGA_DRAW_PROTOCOL *UgaDraw;
252 EFI_STATUS Status;
253 CIRRUS_LOGIC_5430_PRIVATE_DATA *Private;
254
255 Status = gBS->OpenProtocol (
256 Controller,
257 &gEfiUgaDrawProtocolGuid,
258 (VOID **) &UgaDraw,
259 This->DriverBindingHandle,
260 Controller,
261 EFI_OPEN_PROTOCOL_GET_PROTOCOL
262 );
263 if (EFI_ERROR (Status)) {
264 //
265 // If the UGA Draw interface does not exist the driver is not started
266 //
267 return Status;
268 }
269
270 //
271 // Get our private context information
272 //
273 Private = CIRRUS_LOGIC_5430_PRIVATE_DATA_FROM_UGA_DRAW_THIS (UgaDraw);
274
275 //
276 // Remove the UGA Draw interface from the system
277 //
278 Status = gBS->UninstallMultipleProtocolInterfaces (
279 Private->Handle,
280 &gEfiUgaDrawProtocolGuid,
281 &Private->UgaDraw,
282 NULL
283 );
284 if (EFI_ERROR (Status)) {
285 return Status;
286 }
287
288 //
289 // Shutdown the hardware
290 //
291 CirrusLogic5430UgaDrawDestructor (Private);
292
293 Private->PciIo->Attributes (
294 Private->PciIo,
295 EfiPciIoAttributeOperationDisable,
296 EFI_PCI_DEVICE_ENABLE | EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY | EFI_PCI_IO_ATTRIBUTE_VGA_IO,
297 NULL
298 );
299
300 //
301 // Close the PCI I/O Protocol
302 //
303 gBS->CloseProtocol (
304 Controller,
305 &gEfiPciIoProtocolGuid,
306 This->DriverBindingHandle,
307 Controller
308 );
309
310 //
311 // Free our instance data
312 //
313 gBS->FreePool (Private);
314
315 return EFI_SUCCESS;
316 }