]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Drivers/PL061GpioDxe/PL061Gpio.c
ArmPlatformPkg: PL061 - drop pointless initialize function
[mirror_edk2.git] / ArmPlatformPkg / Drivers / PL061GpioDxe / PL061Gpio.c
1 /** @file
2 *
3 * Copyright (c) 2011, ARM Limited. All rights reserved.
4 * Copyright (c) 2016, Linaro Limited. All rights reserved.
5 *
6 * This program and the accompanying materials
7 * are licensed and made available under the terms and conditions of the BSD
8 * License which accompanies this distribution. The full text of the license
9 * may be found at http://opensource.org/licenses/bsd-license.php
10 *
11 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 *
14 **/
15
16
17 #include <PiDxe.h>
18
19 #include <Library/BaseLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/IoLib.h>
23 #include <Library/PcdLib.h>
24 #include <Library/UefiBootServicesTableLib.h>
25 #include <Library/UefiLib.h>
26 #include <Library/UefiRuntimeServicesTableLib.h>
27
28 #include <Protocol/EmbeddedGpio.h>
29 #include <Drivers/PL061Gpio.h>
30
31
32 /**
33 Function implementations
34 **/
35
36 EFI_STATUS
37 PL061Identify (
38 VOID
39 )
40 {
41 // Check if this is a PrimeCell Peripheral
42 if ( (MmioRead8 (PL061_GPIO_PCELL_ID0) != 0x0D)
43 || (MmioRead8 (PL061_GPIO_PCELL_ID1) != 0xF0)
44 || (MmioRead8 (PL061_GPIO_PCELL_ID2) != 0x05)
45 || (MmioRead8 (PL061_GPIO_PCELL_ID3) != 0xB1)) {
46 return EFI_NOT_FOUND;
47 }
48
49 // Check if this PrimeCell Peripheral is the PL061 GPIO
50 if ( (MmioRead8 (PL061_GPIO_PERIPH_ID0) != 0x61)
51 || (MmioRead8 (PL061_GPIO_PERIPH_ID1) != 0x10)
52 || ((MmioRead8 (PL061_GPIO_PERIPH_ID2) & 0xF) != 0x04)
53 || (MmioRead8 (PL061_GPIO_PERIPH_ID3) != 0x00)) {
54 return EFI_NOT_FOUND;
55 }
56
57 return EFI_SUCCESS;
58 }
59
60 /**
61
62 Routine Description:
63
64 Gets the state of a GPIO pin
65
66 Arguments:
67
68 This - pointer to protocol
69 Gpio - which pin to read
70 Value - state of the pin
71
72 Returns:
73
74 EFI_SUCCESS - GPIO state returned in Value
75 EFI_INVALID_PARAMETER - Value is NULL pointer or Gpio pin is out of range
76 **/
77 EFI_STATUS
78 EFIAPI
79 Get (
80 IN EMBEDDED_GPIO *This,
81 IN EMBEDDED_GPIO_PIN Gpio,
82 OUT UINTN *Value
83 )
84 {
85 if ( (Value == NULL)
86 || (Gpio > LAST_GPIO_PIN))
87 {
88 return EFI_INVALID_PARAMETER;
89 }
90
91 if (MmioRead8 (PL061_GPIO_DATA_REG) & GPIO_PIN_MASK_HIGH_8BIT(Gpio)) {
92 *Value = 1;
93 } else {
94 *Value = 0;
95 }
96
97 return EFI_SUCCESS;
98 }
99
100 /**
101
102 Routine Description:
103
104 Sets the state of a GPIO pin
105
106 Arguments:
107
108 This - pointer to protocol
109 Gpio - which pin to modify
110 Mode - mode to set
111
112 Returns:
113
114 EFI_SUCCESS - GPIO set as requested
115 EFI_UNSUPPORTED - Mode is not supported
116 EFI_INVALID_PARAMETER - Gpio pin is out of range
117 **/
118 EFI_STATUS
119 EFIAPI
120 Set (
121 IN EMBEDDED_GPIO *This,
122 IN EMBEDDED_GPIO_PIN Gpio,
123 IN EMBEDDED_GPIO_MODE Mode
124 )
125 {
126 EFI_STATUS Status = EFI_SUCCESS;
127
128 // Check for errors
129 if (Gpio > LAST_GPIO_PIN) {
130 Status = EFI_INVALID_PARAMETER;
131 goto EXIT;
132 }
133
134 switch (Mode)
135 {
136 case GPIO_MODE_INPUT:
137 // Set the corresponding direction bit to LOW for input
138 MmioAnd8 (PL061_GPIO_DIR_REG, GPIO_PIN_MASK_LOW_8BIT(Gpio));
139 break;
140
141 case GPIO_MODE_OUTPUT_0:
142 // Set the corresponding data bit to LOW for 0
143 MmioAnd8 (PL061_GPIO_DATA_REG, GPIO_PIN_MASK_LOW_8BIT(Gpio));
144 // Set the corresponding direction bit to HIGH for output
145 MmioOr8 (PL061_GPIO_DIR_REG, GPIO_PIN_MASK_HIGH_8BIT(Gpio));
146 break;
147
148 case GPIO_MODE_OUTPUT_1:
149 // Set the corresponding data bit to HIGH for 1
150 MmioOr8 (PL061_GPIO_DATA_REG, GPIO_PIN_MASK_HIGH_8BIT(Gpio));
151 // Set the corresponding direction bit to HIGH for output
152 MmioOr8 (PL061_GPIO_DIR_REG, GPIO_PIN_MASK_HIGH_8BIT(Gpio));
153 break;
154
155 default:
156 // Other modes are not supported
157 return EFI_UNSUPPORTED;
158 }
159
160 EXIT:
161 return Status;
162 }
163
164 /**
165
166 Routine Description:
167
168 Gets the mode (function) of a GPIO pin
169
170 Arguments:
171
172 This - pointer to protocol
173 Gpio - which pin
174 Mode - pointer to output mode value
175
176 Returns:
177
178 EFI_SUCCESS - mode value retrieved
179 EFI_INVALID_PARAMETER - Mode is a null pointer or Gpio pin is out of range
180
181 **/
182 EFI_STATUS
183 EFIAPI
184 GetMode (
185 IN EMBEDDED_GPIO *This,
186 IN EMBEDDED_GPIO_PIN Gpio,
187 OUT EMBEDDED_GPIO_MODE *Mode
188 )
189 {
190 // Check for errors
191 if ( (Mode == NULL)
192 || (Gpio > LAST_GPIO_PIN)) {
193 return EFI_INVALID_PARAMETER;
194 }
195
196 // Check if it is input or output
197 if (MmioRead8 (PL061_GPIO_DIR_REG) & GPIO_PIN_MASK_HIGH_8BIT(Gpio)) {
198 // Pin set to output
199 if (MmioRead8 (PL061_GPIO_DATA_REG) & GPIO_PIN_MASK_HIGH_8BIT(Gpio)) {
200 *Mode = GPIO_MODE_OUTPUT_1;
201 } else {
202 *Mode = GPIO_MODE_OUTPUT_0;
203 }
204 } else {
205 // Pin set to input
206 *Mode = GPIO_MODE_INPUT;
207 }
208
209 return EFI_SUCCESS;
210 }
211
212 /**
213
214 Routine Description:
215
216 Sets the pull-up / pull-down resistor of a GPIO pin
217
218 Arguments:
219
220 This - pointer to protocol
221 Gpio - which pin
222 Direction - pull-up, pull-down, or none
223
224 Returns:
225
226 EFI_UNSUPPORTED - Can not perform the requested operation
227
228 **/
229 EFI_STATUS
230 EFIAPI
231 SetPull (
232 IN EMBEDDED_GPIO *This,
233 IN EMBEDDED_GPIO_PIN Gpio,
234 IN EMBEDDED_GPIO_PULL Direction
235 )
236 {
237 return EFI_UNSUPPORTED;
238 }
239
240 /**
241 Protocol variable definition
242 **/
243 EMBEDDED_GPIO gGpio = {
244 Get,
245 Set,
246 GetMode,
247 SetPull
248 };
249
250 /**
251 Initialize the state information for the Embedded Gpio protocol.
252
253 @param ImageHandle of the loaded driver
254 @param SystemTable Pointer to the System Table
255
256 @retval EFI_SUCCESS Protocol registered
257 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
258 @retval EFI_DEVICE_ERROR Hardware problems
259
260 **/
261 EFI_STATUS
262 EFIAPI
263 PL061InstallProtocol (
264 IN EFI_HANDLE ImageHandle,
265 IN EFI_SYSTEM_TABLE *SystemTable
266 )
267 {
268 EFI_STATUS Status;
269 EFI_HANDLE Handle;
270
271 //
272 // Make sure the Gpio protocol has not been installed in the system yet.
273 //
274 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEmbeddedGpioProtocolGuid);
275
276 Status = PL061Identify();
277 if (EFI_ERROR(Status)) {
278 return EFI_DEVICE_ERROR;
279 }
280
281 // Install the Embedded GPIO Protocol onto a new handle
282 Handle = NULL;
283 Status = gBS->InstallMultipleProtocolInterfaces(
284 &Handle,
285 &gEmbeddedGpioProtocolGuid, &gGpio,
286 NULL
287 );
288 if (EFI_ERROR(Status)) {
289 Status = EFI_OUT_OF_RESOURCES;
290 }
291
292 return Status;
293 }