]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c
ArmPkg/ArmScmiDxe: Add clock enable function
[mirror_edk2.git] / ArmPkg / Drivers / ArmScmiDxe / ScmiClockProtocol.c
CommitLineData
4f2494cf
GP
1/** @file\r
2\r
3 Copyright (c) 2017-2018, Arm Limited. All rights reserved.\r
4\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13 System Control and Management Interface V1.0\r
14 http://infocenter.arm.com/help/topic/com.arm.doc.den0056a/\r
15 DEN0056A_System_Control_and_Management_Interface.pdf\r
16**/\r
17\r
18#include <Library/BaseLib.h>\r
19#include <Library/DebugLib.h>\r
20#include <Library/UefiBootServicesTableLib.h>\r
21#include <Protocol/ArmScmiClockProtocol.h>\r
559a07d8 22#include <Protocol/ArmScmiClock2Protocol.h>\r
4f2494cf
GP
23\r
24#include "ArmScmiClockProtocolPrivate.h"\r
25#include "ScmiPrivate.h"\r
26\r
27/** Convert to 64 bit value from two 32 bit words.\r
28\r
29 @param[in] Low Lower 32 bits.\r
30 @param[in] High Higher 32 bits.\r
31\r
32 @retval UINT64 64 bit value.\r
33**/\r
34STATIC\r
35UINT64\r
36ConvertTo64Bit (\r
37 IN UINT32 Low,\r
38 IN UINT32 High\r
39 )\r
40{\r
41 return (Low | ((UINT64)High << 32));\r
42}\r
43\r
44/** Return version of the clock management protocol supported by SCP firmware.\r
45\r
46 @param[in] This A Pointer to SCMI_CLOCK_PROTOCOL Instance.\r
47\r
48 @param[out] Version Version of the supported SCMI Clock management protocol.\r
49\r
50 @retval EFI_SUCCESS The version is returned.\r
51 @retval EFI_DEVICE_ERROR SCP returns an SCMI error.\r
52 @retval !(EFI_SUCCESS) Other errors.\r
53**/\r
54STATIC\r
55EFI_STATUS\r
56ClockGetVersion (\r
57 IN SCMI_CLOCK_PROTOCOL *This,\r
58 OUT UINT32 *Version\r
59 )\r
60{\r
61 return ScmiGetProtocolVersion (SCMI_PROTOCOL_ID_CLOCK, Version);\r
62}\r
63\r
64/** Return total number of clock devices supported by the clock management\r
65 protocol.\r
66\r
67 @param[in] This A Pointer to SCMI_CLOCK_PROTOCOL Instance.\r
68\r
69 @param[out] TotalClocks Total number of clocks supported.\r
70\r
71 @retval EFI_SUCCESS Total number of clocks supported is returned.\r
72 @retval EFI_DEVICE_ERROR SCP returns an SCMI error.\r
73 @retval !(EFI_SUCCESS) Other errors.\r
74**/\r
75STATIC\r
76EFI_STATUS\r
77ClockGetTotalClocks (\r
78 IN SCMI_CLOCK_PROTOCOL *This,\r
79 OUT UINT32 *TotalClocks\r
80 )\r
81{\r
82 EFI_STATUS Status;\r
83 UINT32 *ReturnValues;\r
84\r
85 Status = ScmiGetProtocolAttributes (SCMI_PROTOCOL_ID_CLOCK, &ReturnValues);\r
86 if (EFI_ERROR (Status)) {\r
87 return Status;\r
88 }\r
89\r
90 *TotalClocks = SCMI_CLOCK_PROTOCOL_TOTAL_CLKS (ReturnValues[0]);\r
91\r
92 return EFI_SUCCESS;\r
93}\r
94\r
95/** Return attributes of a clock device.\r
96\r
97 @param[in] This A Pointer to SCMI_CLOCK_PROTOCOL Instance.\r
98 @param[in] ClockId Identifier for the clock device.\r
99\r
100 @param[out] Enabled If TRUE, the clock device is enabled.\r
101 @param[out] ClockAsciiName A NULL terminated ASCII string with the clock\r
102 name, of up to 16 bytes.\r
103\r
104 @retval EFI_SUCCESS Clock device attributes are returned.\r
105 @retval EFI_DEVICE_ERROR SCP returns an SCMI error.\r
106 @retval !(EFI_SUCCESS) Other errors.\r
107**/\r
108STATIC\r
109EFI_STATUS\r
110ClockGetClockAttributes (\r
111 IN SCMI_CLOCK_PROTOCOL *This,\r
112 IN UINT32 ClockId,\r
113 OUT BOOLEAN *Enabled,\r
114 OUT CHAR8 *ClockAsciiName\r
115 )\r
116{\r
117 EFI_STATUS Status;\r
118\r
119 UINT32 *MessageParams;\r
120 CLOCK_ATTRIBUTES *ClockAttributes;\r
121 SCMI_COMMAND Cmd;\r
122 UINT32 PayloadLength;\r
123\r
124 Status = ScmiCommandGetPayload (&MessageParams);\r
125 if (EFI_ERROR (Status)) {\r
126 return Status;\r
127 }\r
128\r
129 *MessageParams = ClockId;\r
130\r
131 Cmd.ProtocolId = SCMI_PROTOCOL_ID_CLOCK;\r
132 Cmd.MessageId = SCMI_MESSAGE_ID_CLOCK_ATTRIBUTES;\r
133\r
134 PayloadLength = sizeof (ClockId);\r
135\r
136 Status = ScmiCommandExecute (\r
137 &Cmd,\r
138 &PayloadLength,\r
139 (UINT32**)&ClockAttributes\r
140 );\r
141 if (EFI_ERROR (Status)) {\r
142 return Status;\r
143 }\r
144 // TRUE if bit 0 of ClockAttributes->Attributes is set.\r
145 *Enabled = CLOCK_ENABLED (ClockAttributes->Attributes);\r
146\r
147 AsciiStrCpyS (\r
148 ClockAsciiName,\r
149 SCMI_MAX_STR_LEN,\r
150 (CONST CHAR8*)ClockAttributes->ClockName\r
151 );\r
152\r
153 return EFI_SUCCESS;\r
154}\r
155\r
156/** Return list of rates supported by a given clock device.\r
157\r
158 @param[in] This A pointer to SCMI_CLOCK_PROTOCOL Instance.\r
159 @param[in] ClockId Identifier for the clock device.\r
160\r
161 @param[out] Format SCMI_CLOCK_RATE_FORMAT_DISCRETE: Clock device\r
162 supports range of clock rates which are non-linear.\r
163\r
164 SCMI_CLOCK_RATE_FORMAT_LINEAR: Clock device supports\r
165 range of linear clock rates from Min to Max in steps.\r
166\r
167 @param[out] TotalRates Total number of rates.\r
168\r
169 @param[in,out] RateArraySize Size of the RateArray.\r
170\r
171 @param[out] RateArray List of clock rates.\r
172\r
173 @retval EFI_SUCCESS List of clock rates is returned.\r
174 @retval EFI_DEVICE_ERROR SCP returns an SCMI error.\r
175 @retval EFI_BUFFER_TOO_SMALL RateArraySize is too small for the result.\r
176 It has been updated to the size needed.\r
177 @retval !(EFI_SUCCESS) Other errors.\r
178**/\r
179STATIC\r
180EFI_STATUS\r
181ClockDescribeRates (\r
182 IN SCMI_CLOCK_PROTOCOL *This,\r
183 IN UINT32 ClockId,\r
184 OUT SCMI_CLOCK_RATE_FORMAT *Format,\r
185 OUT UINT32 *TotalRates,\r
186 IN OUT UINT32 *RateArraySize,\r
187 OUT SCMI_CLOCK_RATE *RateArray\r
188 )\r
189{\r
190 EFI_STATUS Status;\r
191\r
192 UINT32 PayloadLength;\r
193 SCMI_COMMAND Cmd;\r
194 UINT32 *MessageParams;\r
195 CLOCK_DESCRIBE_RATES *DescribeRates;\r
196 CLOCK_RATE_DWORD *Rate;\r
197\r
198 UINT32 RequiredArraySize = 0;\r
199 UINT32 RateIndex = 0;\r
200 UINT32 RateNo;\r
201 UINT32 RateOffset;\r
202\r
203 *TotalRates = 0;\r
204\r
205 Status = ScmiCommandGetPayload (&MessageParams);\r
206 if (EFI_ERROR (Status)) {\r
207 return Status;\r
208 }\r
209\r
210 Cmd.ProtocolId = SCMI_PROTOCOL_ID_CLOCK;\r
211 Cmd.MessageId = SCMI_MESSAGE_ID_CLOCK_DESCRIBE_RATES;\r
212\r
213 *MessageParams++ = ClockId;\r
214\r
215 do {\r
216\r
217 *MessageParams = RateIndex;\r
218\r
219 // Set Payload length, note PayloadLength is a IN/OUT parameter.\r
220 PayloadLength = sizeof (ClockId) + sizeof (RateIndex);\r
221\r
222 // Execute and wait for response on a SCMI channel.\r
223 Status = ScmiCommandExecute (\r
224 &Cmd,\r
225 &PayloadLength,\r
226 (UINT32**)&DescribeRates\r
227 );\r
228 if (EFI_ERROR (Status)) {\r
229 return Status;\r
230 }\r
231\r
232 if (*TotalRates == 0) {\r
233 // In the first iteration we will get number of returned rates and number\r
234 // of remaining rates. With this information calculate required size\r
235 // for rate array. If provided RateArraySize is less, return an\r
236 // error.\r
237\r
238 *Format = RATE_FORMAT (DescribeRates->NumRatesFlags);\r
239\r
240 *TotalRates = NUM_RATES (DescribeRates->NumRatesFlags)\r
241 + NUM_REMAIN_RATES (DescribeRates->NumRatesFlags);\r
242\r
243 if (*Format == SCMI_CLOCK_RATE_FORMAT_DISCRETE) {\r
244 RequiredArraySize = (*TotalRates) * sizeof (UINT64);\r
245 } else {\r
246 // We need to return triplet of 64 bit value for each rate\r
247 RequiredArraySize = (*TotalRates) * 3 * sizeof (UINT64);\r
248 }\r
249\r
250 if (RequiredArraySize > (*RateArraySize)) {\r
251 *RateArraySize = RequiredArraySize;\r
252 return EFI_BUFFER_TOO_SMALL;\r
253 }\r
254 }\r
255\r
256 RateOffset = 0;\r
257\r
258 if (*Format == SCMI_CLOCK_RATE_FORMAT_DISCRETE) {\r
259 for (RateNo = 0; RateNo < NUM_RATES (DescribeRates->NumRatesFlags); RateNo++) {\r
260 Rate = &DescribeRates->Rates[RateOffset++];\r
261 // Non-linear discrete rates.\r
262 RateArray[RateIndex++].Rate = ConvertTo64Bit (Rate->Low, Rate->High);\r
263 }\r
264 } else {\r
265 for (RateNo = 0; RateNo < NUM_RATES (DescribeRates->NumRatesFlags); RateNo++) {\r
266 // Linear clock rates from minimum to maximum in steps\r
267 // Minimum clock rate.\r
268 Rate = &DescribeRates->Rates[RateOffset++];\r
269 RateArray[RateIndex].Min = ConvertTo64Bit (Rate->Low, Rate->High);\r
270\r
271 Rate = &DescribeRates->Rates[RateOffset++];\r
272 // Maximum clock rate.\r
273 RateArray[RateIndex].Max = ConvertTo64Bit (Rate->Low, Rate->High);\r
274\r
275 Rate = &DescribeRates->Rates[RateOffset++];\r
276 // Step.\r
277 RateArray[RateIndex++].Step = ConvertTo64Bit (Rate->Low, Rate->High);\r
278 }\r
279 }\r
280 } while (NUM_REMAIN_RATES (DescribeRates->NumRatesFlags) != 0);\r
281\r
282 // Update RateArraySize with RequiredArraySize.\r
283 *RateArraySize = RequiredArraySize;\r
284\r
285 return EFI_SUCCESS;\r
286}\r
287\r
288/** Get clock rate.\r
289\r
290 @param[in] This A Pointer to SCMI_CLOCK_PROTOCOL Instance.\r
291 @param[in] ClockId Identifier for the clock device.\r
292\r
293 @param[out] Rate Clock rate.\r
294\r
295 @retval EFI_SUCCESS Clock rate is returned.\r
296 @retval EFI_DEVICE_ERROR SCP returns an SCMI error.\r
297 @retval !(EFI_SUCCESS) Other errors.\r
298**/\r
299STATIC\r
300EFI_STATUS\r
301ClockRateGet (\r
302 IN SCMI_CLOCK_PROTOCOL *This,\r
303 IN UINT32 ClockId,\r
304 OUT UINT64 *Rate\r
305 )\r
306{\r
307 EFI_STATUS Status;\r
308\r
309 UINT32 *MessageParams;\r
310 CLOCK_RATE_DWORD *ClockRate;\r
311 SCMI_COMMAND Cmd;\r
312\r
313 UINT32 PayloadLength;\r
314\r
315 Status = ScmiCommandGetPayload (&MessageParams);\r
316 if (EFI_ERROR (Status)) {\r
317 return Status;\r
318 }\r
319\r
320 // Fill arguments for clock protocol command.\r
321 *MessageParams = ClockId;\r
322\r
323 Cmd.ProtocolId = SCMI_PROTOCOL_ID_CLOCK;\r
324 Cmd.MessageId = SCMI_MESSAGE_ID_CLOCK_RATE_GET;\r
325\r
326 PayloadLength = sizeof (ClockId);\r
327\r
328 // Execute and wait for response on a SCMI channel.\r
329 Status = ScmiCommandExecute (\r
330 &Cmd,\r
331 &PayloadLength,\r
332 (UINT32**)&ClockRate\r
333 );\r
334 if (EFI_ERROR (Status)) {\r
335 return Status;\r
336 }\r
337\r
338 *Rate = ConvertTo64Bit (ClockRate->Low, ClockRate->High);\r
339\r
340 return EFI_SUCCESS;\r
341}\r
342\r
343/** Set clock rate.\r
344\r
345 @param[in] This A Pointer to SCMI_CLOCK_PROTOCOL Instance.\r
346 @param[in] ClockId Identifier for the clock device.\r
347 @param[in] Rate Clock rate.\r
348\r
349 @retval EFI_SUCCESS Clock rate set success.\r
350 @retval EFI_DEVICE_ERROR SCP returns an SCMI error.\r
351 @retval !(EFI_SUCCESS) Other errors.\r
352**/\r
353STATIC\r
354EFI_STATUS\r
355ClockRateSet (\r
356 IN SCMI_CLOCK_PROTOCOL *This,\r
357 IN UINT32 ClockId,\r
358 IN UINT64 Rate\r
359 )\r
360{\r
361 EFI_STATUS Status;\r
362 CLOCK_RATE_SET_ATTRIBUTES *ClockRateSetAttributes;\r
363 SCMI_COMMAND Cmd;\r
364 UINT32 PayloadLength;\r
365\r
366 Status = ScmiCommandGetPayload ((UINT32**)&ClockRateSetAttributes);\r
367 if (EFI_ERROR (Status)) {\r
368 return Status;\r
369 }\r
370\r
371 // Fill arguments for clock protocol command.\r
372 ClockRateSetAttributes->ClockId = ClockId;\r
373 ClockRateSetAttributes->Flags = CLOCK_SET_DEFAULT_FLAGS;\r
374 ClockRateSetAttributes->Rate.Low = (UINT32)Rate;\r
375 ClockRateSetAttributes->Rate.High = (UINT32)(Rate >> 32);\r
376\r
377 Cmd.ProtocolId = SCMI_PROTOCOL_ID_CLOCK;\r
378 Cmd.MessageId = SCMI_MESSAGE_ID_CLOCK_RATE_SET;\r
379\r
380 PayloadLength = sizeof (CLOCK_RATE_SET_ATTRIBUTES);\r
381\r
382 // Execute and wait for response on a SCMI channel.\r
383 Status = ScmiCommandExecute (\r
384 &Cmd,\r
385 &PayloadLength,\r
386 NULL\r
387 );\r
388\r
389 return Status;\r
390}\r
391\r
559a07d8
JB
392/** Enable/Disable specified clock.\r
393\r
394 @param[in] This A Pointer to SCMI_CLOCK_PROTOCOL Instance.\r
395 @param[in] ClockId Identifier for the clock device.\r
396 @param[in] Enable TRUE to enable, FALSE to disable.\r
397\r
398 @retval EFI_SUCCESS Clock enable/disable successful.\r
399 @retval EFI_DEVICE_ERROR SCP returns an SCMI error.\r
400 @retval !(EFI_SUCCESS) Other errors.\r
401**/\r
402STATIC\r
403EFI_STATUS\r
404ClockEnable (\r
405 IN SCMI_CLOCK2_PROTOCOL *This,\r
406 IN UINT32 ClockId,\r
407 IN BOOLEAN Enable\r
408 )\r
409{\r
410 EFI_STATUS Status;\r
411 CLOCK_CONFIG_SET_ATTRIBUTES *ClockConfigSetAttributes;\r
412 SCMI_COMMAND Cmd;\r
413 UINT32 PayloadLength;\r
414\r
415 Status = ScmiCommandGetPayload ((UINT32**)&ClockConfigSetAttributes);\r
416 if (EFI_ERROR (Status)) {\r
417 return Status;\r
418 }\r
419\r
420 // Fill arguments for clock protocol command.\r
421 ClockConfigSetAttributes->ClockId = ClockId;\r
422 ClockConfigSetAttributes->Attributes = Enable ? BIT0 : 0;\r
423\r
424 Cmd.ProtocolId = SCMI_PROTOCOL_ID_CLOCK;\r
425 Cmd.MessageId = SCMI_MESSAGE_ID_CLOCK_CONFIG_SET;\r
426\r
427 PayloadLength = sizeof (CLOCK_CONFIG_SET_ATTRIBUTES);\r
428\r
429 // Execute and wait for response on a SCMI channel.\r
430 Status = ScmiCommandExecute (\r
431 &Cmd,\r
432 &PayloadLength,\r
433 NULL\r
434 );\r
435\r
436 return Status;\r
437}\r
438\r
4f2494cf
GP
439// Instance of the SCMI clock management protocol.\r
440STATIC CONST SCMI_CLOCK_PROTOCOL ScmiClockProtocol = {\r
441 ClockGetVersion,\r
442 ClockGetTotalClocks,\r
443 ClockGetClockAttributes,\r
444 ClockDescribeRates,\r
445 ClockRateGet,\r
446 ClockRateSet\r
447 };\r
448\r
559a07d8
JB
449// Instance of the SCMI clock management protocol.\r
450STATIC CONST SCMI_CLOCK2_PROTOCOL ScmiClock2Protocol = {\r
451 (SCMI_CLOCK2_GET_VERSION)ClockGetVersion,\r
452 (SCMI_CLOCK2_GET_TOTAL_CLOCKS)ClockGetTotalClocks,\r
453 (SCMI_CLOCK2_GET_CLOCK_ATTRIBUTES)ClockGetClockAttributes,\r
454 (SCMI_CLOCK2_DESCRIBE_RATES)ClockDescribeRates,\r
455 (SCMI_CLOCK2_RATE_GET)ClockRateGet,\r
456 (SCMI_CLOCK2_RATE_SET)ClockRateSet,\r
457 SCMI_CLOCK2_PROTOCOL_VERSION,\r
458 ClockEnable\r
459 };\r
460\r
4f2494cf
GP
461/** Initialize clock management protocol and install protocol on a given handle.\r
462\r
463 @param[in] Handle Handle to install clock management protocol.\r
464\r
465 @retval EFI_SUCCESS Clock protocol interface installed successfully.\r
466**/\r
467EFI_STATUS\r
468ScmiClockProtocolInit (\r
469 IN EFI_HANDLE* Handle\r
470 )\r
471{\r
472 return gBS->InstallMultipleProtocolInterfaces (\r
473 Handle,\r
474 &gArmScmiClockProtocolGuid,\r
475 &ScmiClockProtocol,\r
559a07d8
JB
476 &gArmScmiClock2ProtocolGuid,\r
477 &ScmiClock2Protocol,\r
4f2494cf
GP
478 NULL\r
479 );\r
480}\r