1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
4 #include <fsl_mc_sys.h>
5 #include <fsl_mc_cmd.h>
7 #include <fsl_dprtc_cmd.h>
14 * dprtc_open() - Open a control session for the specified object.
15 * @mc_io: Pointer to MC portal's I/O object
16 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
17 * @dprtc_id: DPRTC unique ID
18 * @token: Returned token; use in subsequent API calls
20 * This function can be used to open a control session for an
21 * already created object; an object may have been declared in
22 * the DPL or by calling the dprtc_create function.
23 * This function returns a unique authentication token,
24 * associated with the specific object ID and the specific MC
25 * portal; this token must be used in all subsequent commands for
26 * this specific object
28 * Return: '0' on Success; Error code otherwise.
30 int dprtc_open(struct fsl_mc_io *mc_io,
35 struct dprtc_cmd_open *cmd_params;
36 struct mc_command cmd = { 0 };
40 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_OPEN,
43 cmd_params = (struct dprtc_cmd_open *)cmd.params;
44 cmd_params->dprtc_id = cpu_to_le32(dprtc_id);
46 /* send command to mc*/
47 err = mc_send_command(mc_io, &cmd);
51 /* retrieve response parameters */
52 *token = mc_cmd_hdr_read_token(&cmd);
58 * dprtc_close() - Close the control session of the object
59 * @mc_io: Pointer to MC portal's I/O object
60 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
61 * @token: Token of DPRTC object
63 * After this function is called, no further operations are
64 * allowed on the object without opening a new control session.
66 * Return: '0' on Success; Error code otherwise.
68 int dprtc_close(struct fsl_mc_io *mc_io,
72 struct mc_command cmd = { 0 };
75 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_CLOSE, cmd_flags,
78 /* send command to mc*/
79 return mc_send_command(mc_io, &cmd);
83 * dprtc_create() - Create the DPRTC object.
84 * @mc_io: Pointer to MC portal's I/O object
85 * @dprc_token: Parent container token; '0' for default container
86 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
87 * @cfg: Configuration structure
88 * @obj_id: Returned object id
90 * Create the DPRTC object, allocate required resources and
91 * perform required initialization.
93 * The function accepts an authentication token of a parent
94 * container that this object should be assigned to. The token
95 * can be '0' so the object will be assigned to the default container.
96 * The newly created object can be opened with the returned
97 * object id and using the container's associated tokens and MC portals.
99 * Return: '0' on Success; Error code otherwise.
101 int dprtc_create(struct fsl_mc_io *mc_io,
104 const struct dprtc_cfg *cfg,
107 struct mc_command cmd = { 0 };
110 (void)(cfg); /* unused */
112 /* prepare command */
113 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_CREATE,
117 /* send command to mc*/
118 err = mc_send_command(mc_io, &cmd);
122 /* retrieve response parameters */
123 *obj_id = mc_cmd_read_object_id(&cmd);
129 * dprtc_destroy() - Destroy the DPRTC object and release all its resources.
130 * @mc_io: Pointer to MC portal's I/O object
131 * @dprc_token: Parent container token; '0' for default container
132 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
133 * @object_id: The object id; it must be a valid id within the container that
134 * created this object;
136 * The function accepts the authentication token of the parent container that
137 * created the object (not the one that currently owns the object). The object
138 * is searched within parent using the provided 'object_id'.
139 * All tokens to the object must be closed before calling destroy.
141 * Return: '0' on Success; error code otherwise.
143 int dprtc_destroy(struct fsl_mc_io *mc_io,
148 struct dprtc_cmd_destroy *cmd_params;
149 struct mc_command cmd = { 0 };
151 /* prepare command */
152 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_DESTROY,
155 cmd_params = (struct dprtc_cmd_destroy *)cmd.params;
156 cmd_params->object_id = cpu_to_le32(object_id);
158 /* send command to mc*/
159 return mc_send_command(mc_io, &cmd);
163 * dprtc_enable() - Enable the DPRTC.
164 * @mc_io: Pointer to MC portal's I/O object
165 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
166 * @token: Token of DPRTC object
168 * Return: '0' on Success; Error code otherwise.
170 int dprtc_enable(struct fsl_mc_io *mc_io,
174 struct mc_command cmd = { 0 };
176 /* prepare command */
177 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_ENABLE, cmd_flags,
180 /* send command to mc*/
181 return mc_send_command(mc_io, &cmd);
185 * dprtc_disable() - Disable the DPRTC.
186 * @mc_io: Pointer to MC portal's I/O object
187 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
188 * @token: Token of DPRTC object
190 * Return: '0' on Success; Error code otherwise.
192 int dprtc_disable(struct fsl_mc_io *mc_io,
196 struct mc_command cmd = { 0 };
198 /* prepare command */
199 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_DISABLE,
203 /* send command to mc*/
204 return mc_send_command(mc_io, &cmd);
208 * dprtc_is_enabled() - Check if the DPRTC is enabled.
209 * @mc_io: Pointer to MC portal's I/O object
210 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
211 * @token: Token of DPRTC object
212 * @en: Returns '1' if object is enabled; '0' otherwise
214 * Return: '0' on Success; Error code otherwise.
216 int dprtc_is_enabled(struct fsl_mc_io *mc_io,
221 struct dprtc_rsp_is_enabled *rsp_params;
222 struct mc_command cmd = { 0 };
225 /* prepare command */
226 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_IS_ENABLED, cmd_flags,
229 /* send command to mc*/
230 err = mc_send_command(mc_io, &cmd);
234 /* retrieve response parameters */
235 rsp_params = (struct dprtc_rsp_is_enabled *)cmd.params;
236 *en = dprtc_get_field(rsp_params->en, ENABLE);
242 * dprtc_reset() - Reset the DPRTC, returns the object to initial state.
243 * @mc_io: Pointer to MC portal's I/O object
244 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
245 * @token: Token of DPRTC object
247 * Return: '0' on Success; Error code otherwise.
249 int dprtc_reset(struct fsl_mc_io *mc_io,
253 struct mc_command cmd = { 0 };
255 /* prepare command */
256 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_RESET,
260 /* send command to mc*/
261 return mc_send_command(mc_io, &cmd);
265 * dprtc_get_attributes - Retrieve DPRTC attributes.
267 * @mc_io: Pointer to MC portal's I/O object
268 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
269 * @token: Token of DPRTC object
270 * @attr: Returned object's attributes
272 * Return: '0' on Success; Error code otherwise.
274 int dprtc_get_attributes(struct fsl_mc_io *mc_io,
277 struct dprtc_attr *attr)
279 struct dprtc_rsp_get_attributes *rsp_params;
280 struct mc_command cmd = { 0 };
283 /* prepare command */
284 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_ATTR,
288 /* send command to mc*/
289 err = mc_send_command(mc_io, &cmd);
293 /* retrieve response parameters */
294 rsp_params = (struct dprtc_rsp_get_attributes *)cmd.params;
295 attr->id = le32_to_cpu(rsp_params->id);
296 attr->paddr = le32_to_cpu(rsp_params->paddr);
297 attr->little_endian =
298 dprtc_get_field(rsp_params->little_endian, ENDIANNESS);
303 * dprtc_set_clock_offset() - Sets the clock's offset
304 * (usually relative to another clock).
306 * @mc_io: Pointer to MC portal's I/O object
307 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
308 * @token: Token of DPRTC object
309 * @offset: New clock offset (in nanoseconds).
311 * Return: '0' on Success; Error code otherwise.
313 int dprtc_set_clock_offset(struct fsl_mc_io *mc_io,
318 struct dprtc_cmd_set_clock_offset *cmd_params;
319 struct mc_command cmd = { 0 };
321 /* prepare command */
322 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_CLOCK_OFFSET,
325 cmd_params = (struct dprtc_cmd_set_clock_offset *)cmd.params;
326 cmd_params->offset = cpu_to_le64(offset);
328 /* send command to mc*/
329 return mc_send_command(mc_io, &cmd);
333 * dprtc_set_freq_compensation() - Sets a new frequency compensation value.
335 * @mc_io: Pointer to MC portal's I/O object
336 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
337 * @token: Token of DPRTC object
338 * @freq_compensation: The new frequency compensation value to set.
340 * Return: '0' on Success; Error code otherwise.
342 int dprtc_set_freq_compensation(struct fsl_mc_io *mc_io,
345 uint32_t freq_compensation)
347 struct dprtc_get_freq_compensation *cmd_params;
348 struct mc_command cmd = { 0 };
350 /* prepare command */
351 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_FREQ_COMPENSATION,
354 cmd_params = (struct dprtc_get_freq_compensation *)cmd.params;
355 cmd_params->freq_compensation = cpu_to_le32(freq_compensation);
357 /* send command to mc*/
358 return mc_send_command(mc_io, &cmd);
362 * dprtc_get_freq_compensation() - Retrieves the frequency compensation value
364 * @mc_io: Pointer to MC portal's I/O object
365 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
366 * @token: Token of DPRTC object
367 * @freq_compensation: Frequency compensation value
369 * Return: '0' on Success; Error code otherwise.
371 int dprtc_get_freq_compensation(struct fsl_mc_io *mc_io,
374 uint32_t *freq_compensation)
376 struct dprtc_get_freq_compensation *rsp_params;
377 struct mc_command cmd = { 0 };
380 /* prepare command */
381 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_FREQ_COMPENSATION,
385 /* send command to mc*/
386 err = mc_send_command(mc_io, &cmd);
390 /* retrieve response parameters */
391 rsp_params = (struct dprtc_get_freq_compensation *)cmd.params;
392 *freq_compensation = le32_to_cpu(rsp_params->freq_compensation);
398 * dprtc_get_time() - Returns the current RTC time.
400 * @mc_io: Pointer to MC portal's I/O object
401 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
402 * @token: Token of DPRTC object
403 * @time: Current RTC time.
405 * Return: '0' on Success; Error code otherwise.
407 int dprtc_get_time(struct fsl_mc_io *mc_io,
412 struct dprtc_time *rsp_params;
413 struct mc_command cmd = { 0 };
416 /* prepare command */
417 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_TIME,
421 /* send command to mc*/
422 err = mc_send_command(mc_io, &cmd);
426 /* retrieve response parameters */
427 rsp_params = (struct dprtc_time *)cmd.params;
428 *time = le64_to_cpu(rsp_params->time);
434 * dprtc_set_time() - Updates current RTC time.
436 * @mc_io: Pointer to MC portal's I/O object
437 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
438 * @token: Token of DPRTC object
439 * @time: New RTC time.
441 * Return: '0' on Success; Error code otherwise.
443 int dprtc_set_time(struct fsl_mc_io *mc_io,
448 struct dprtc_time *cmd_params;
449 struct mc_command cmd = { 0 };
451 /* prepare command */
452 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_TIME,
455 cmd_params = (struct dprtc_time *)cmd.params;
456 cmd_params->time = cpu_to_le64(time);
458 /* send command to mc*/
459 return mc_send_command(mc_io, &cmd);
463 * dprtc_set_alarm() - Defines and sets alarm.
465 * @mc_io: Pointer to MC portal's I/O object
466 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
467 * @token: Token of DPRTC object
468 * @time: In nanoseconds, the time when the alarm
469 * should go off - must be a multiple of
472 * Return: '0' on Success; Error code otherwise.
474 int dprtc_set_alarm(struct fsl_mc_io *mc_io,
476 uint16_t token, uint64_t time)
478 struct dprtc_time *cmd_params;
479 struct mc_command cmd = { 0 };
481 /* prepare command */
482 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_ALARM,
485 cmd_params = (struct dprtc_time *)cmd.params;
486 cmd_params->time = cpu_to_le64(time);
488 /* send command to mc*/
489 return mc_send_command(mc_io, &cmd);
493 * dprtc_get_api_version() - Get Data Path Real Time Counter API version
494 * @mc_io: Pointer to MC portal's I/O object
495 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
496 * @major_ver: Major version of data path real time counter API
497 * @minor_ver: Minor version of data path real time counter API
499 * Return: '0' on Success; Error code otherwise.
501 int dprtc_get_api_version(struct fsl_mc_io *mc_io,
506 struct dprtc_rsp_get_api_version *rsp_params;
507 struct mc_command cmd = { 0 };
510 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_API_VERSION,
514 err = mc_send_command(mc_io, &cmd);
518 rsp_params = (struct dprtc_rsp_get_api_version *)cmd.params;
519 *major_ver = le16_to_cpu(rsp_params->major);
520 *minor_ver = le16_to_cpu(rsp_params->minor);