1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2013-2016 Freescale Semiconductor Inc.
4 * Copyright 2016-2018 NXP
7 #include <linux/fsl/mc.h>
10 #include "dprtc-cmd.h"
13 * dprtc_open() - Open a control session for the specified object.
14 * @mc_io: Pointer to MC portal's I/O object
15 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
16 * @dprtc_id: DPRTC unique ID
17 * @token: Returned token; use in subsequent API calls
19 * This function can be used to open a control session for an
20 * already created object; an object may have been declared in
21 * the DPL or by calling the dprtc_create function.
22 * This function returns a unique authentication token,
23 * associated with the specific object ID and the specific MC
24 * portal; this token must be used in all subsequent commands for
25 * this specific object
27 * Return: '0' on Success; Error code otherwise.
29 int dprtc_open(struct fsl_mc_io *mc_io,
34 struct dprtc_cmd_open *cmd_params;
35 struct fsl_mc_command cmd = { 0 };
38 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_OPEN,
41 cmd_params = (struct dprtc_cmd_open *)cmd.params;
42 cmd_params->dprtc_id = cpu_to_le32(dprtc_id);
44 err = mc_send_command(mc_io, &cmd);
48 *token = mc_cmd_hdr_read_token(&cmd);
54 * dprtc_close() - Close the control session of the object
55 * @mc_io: Pointer to MC portal's I/O object
56 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
57 * @token: Token of DPRTC object
59 * After this function is called, no further operations are
60 * allowed on the object without opening a new control session.
62 * Return: '0' on Success; Error code otherwise.
64 int dprtc_close(struct fsl_mc_io *mc_io,
68 struct fsl_mc_command cmd = { 0 };
70 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_CLOSE, cmd_flags,
73 return mc_send_command(mc_io, &cmd);
77 * dprtc_create() - Create the DPRTC object.
78 * @mc_io: Pointer to MC portal's I/O object
79 * @dprc_token: Parent container token; '0' for default container
80 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
81 * @cfg: Configuration structure
82 * @obj_id: Returned object id
84 * Create the DPRTC object, allocate required resources and
85 * perform required initialization.
87 * The function accepts an authentication token of a parent
88 * container that this object should be assigned to. The token
89 * can be '0' so the object will be assigned to the default container.
90 * The newly created object can be opened with the returned
91 * object id and using the container's associated tokens and MC portals.
93 * Return: '0' on Success; Error code otherwise.
95 int dprtc_create(struct fsl_mc_io *mc_io,
98 const struct dprtc_cfg *cfg,
101 struct fsl_mc_command cmd = { 0 };
104 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_CREATE,
108 err = mc_send_command(mc_io, &cmd);
112 *obj_id = mc_cmd_read_object_id(&cmd);
118 * dprtc_destroy() - Destroy the DPRTC object and release all its resources.
119 * @mc_io: Pointer to MC portal's I/O object
120 * @dprc_token: Parent container token; '0' for default container
121 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
122 * @object_id: The object id; it must be a valid id within the container that
123 * created this object;
125 * The function accepts the authentication token of the parent container that
126 * created the object (not the one that currently owns the object). The object
127 * is searched within parent using the provided 'object_id'.
128 * All tokens to the object must be closed before calling destroy.
130 * Return: '0' on Success; error code otherwise.
132 int dprtc_destroy(struct fsl_mc_io *mc_io,
137 struct dprtc_cmd_destroy *cmd_params;
138 struct fsl_mc_command cmd = { 0 };
140 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_DESTROY,
143 cmd_params = (struct dprtc_cmd_destroy *)cmd.params;
144 cmd_params->object_id = cpu_to_le32(object_id);
146 return mc_send_command(mc_io, &cmd);
150 * dprtc_enable() - Enable the DPRTC.
151 * @mc_io: Pointer to MC portal's I/O object
152 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
153 * @token: Token of DPRTC object
155 * Return: '0' on Success; Error code otherwise.
157 int dprtc_enable(struct fsl_mc_io *mc_io,
161 struct fsl_mc_command cmd = { 0 };
163 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_ENABLE, cmd_flags,
166 return mc_send_command(mc_io, &cmd);
170 * dprtc_disable() - Disable the DPRTC.
171 * @mc_io: Pointer to MC portal's I/O object
172 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
173 * @token: Token of DPRTC object
175 * Return: '0' on Success; Error code otherwise.
177 int dprtc_disable(struct fsl_mc_io *mc_io,
181 struct fsl_mc_command cmd = { 0 };
183 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_DISABLE,
187 return mc_send_command(mc_io, &cmd);
191 * dprtc_is_enabled() - Check if the DPRTC is enabled.
192 * @mc_io: Pointer to MC portal's I/O object
193 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
194 * @token: Token of DPRTC object
195 * @en: Returns '1' if object is enabled; '0' otherwise
197 * Return: '0' on Success; Error code otherwise.
199 int dprtc_is_enabled(struct fsl_mc_io *mc_io,
204 struct dprtc_rsp_is_enabled *rsp_params;
205 struct fsl_mc_command cmd = { 0 };
208 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_IS_ENABLED, cmd_flags,
211 err = mc_send_command(mc_io, &cmd);
215 rsp_params = (struct dprtc_rsp_is_enabled *)cmd.params;
216 *en = dprtc_get_field(rsp_params->en, ENABLE);
222 * dprtc_reset() - Reset the DPRTC, returns the object to initial state.
223 * @mc_io: Pointer to MC portal's I/O object
224 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
225 * @token: Token of DPRTC object
227 * Return: '0' on Success; Error code otherwise.
229 int dprtc_reset(struct fsl_mc_io *mc_io,
233 struct fsl_mc_command cmd = { 0 };
235 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_RESET,
239 return mc_send_command(mc_io, &cmd);
243 * dprtc_set_irq_enable() - Set overall interrupt state.
244 * @mc_io: Pointer to MC portal's I/O object
245 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
246 * @token: Token of DPRTC object
247 * @irq_index: The interrupt index to configure
248 * @en: Interrupt state - enable = 1, disable = 0
250 * Allows GPP software to control when interrupts are generated.
251 * Each interrupt can have up to 32 causes. The enable/disable control's the
252 * overall interrupt state. if the interrupt is disabled no causes will cause
255 * Return: '0' on Success; Error code otherwise.
257 int dprtc_set_irq_enable(struct fsl_mc_io *mc_io,
263 struct dprtc_cmd_set_irq_enable *cmd_params;
264 struct fsl_mc_command cmd = { 0 };
266 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_IRQ_ENABLE,
269 cmd_params = (struct dprtc_cmd_set_irq_enable *)cmd.params;
270 cmd_params->irq_index = irq_index;
273 return mc_send_command(mc_io, &cmd);
277 * dprtc_get_irq_enable() - Get overall interrupt state
278 * @mc_io: Pointer to MC portal's I/O object
279 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
280 * @token: Token of DPRTC object
281 * @irq_index: The interrupt index to configure
282 * @en: Returned interrupt state - enable = 1, disable = 0
284 * Return: '0' on Success; Error code otherwise.
286 int dprtc_get_irq_enable(struct fsl_mc_io *mc_io,
292 struct dprtc_rsp_get_irq_enable *rsp_params;
293 struct dprtc_cmd_get_irq *cmd_params;
294 struct fsl_mc_command cmd = { 0 };
297 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_IRQ_ENABLE,
300 cmd_params = (struct dprtc_cmd_get_irq *)cmd.params;
301 cmd_params->irq_index = irq_index;
303 err = mc_send_command(mc_io, &cmd);
307 rsp_params = (struct dprtc_rsp_get_irq_enable *)cmd.params;
308 *en = rsp_params->en;
314 * dprtc_set_irq_mask() - Set interrupt mask.
315 * @mc_io: Pointer to MC portal's I/O object
316 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
317 * @token: Token of DPRTC object
318 * @irq_index: The interrupt index to configure
319 * @mask: Event mask to trigger interrupt;
322 * 1 = consider event for asserting IRQ
324 * Every interrupt can have up to 32 causes and the interrupt model supports
325 * masking/unmasking each cause independently
327 * Return: '0' on Success; Error code otherwise.
329 int dprtc_set_irq_mask(struct fsl_mc_io *mc_io,
335 struct dprtc_cmd_set_irq_mask *cmd_params;
336 struct fsl_mc_command cmd = { 0 };
338 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_IRQ_MASK,
341 cmd_params = (struct dprtc_cmd_set_irq_mask *)cmd.params;
342 cmd_params->mask = cpu_to_le32(mask);
343 cmd_params->irq_index = irq_index;
345 return mc_send_command(mc_io, &cmd);
349 * dprtc_get_irq_mask() - Get interrupt mask.
350 * @mc_io: Pointer to MC portal's I/O object
351 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
352 * @token: Token of DPRTC object
353 * @irq_index: The interrupt index to configure
354 * @mask: Returned event mask to trigger interrupt
356 * Every interrupt can have up to 32 causes and the interrupt model supports
357 * masking/unmasking each cause independently
359 * Return: '0' on Success; Error code otherwise.
361 int dprtc_get_irq_mask(struct fsl_mc_io *mc_io,
367 struct dprtc_rsp_get_irq_mask *rsp_params;
368 struct dprtc_cmd_get_irq *cmd_params;
369 struct fsl_mc_command cmd = { 0 };
372 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_IRQ_MASK,
375 cmd_params = (struct dprtc_cmd_get_irq *)cmd.params;
376 cmd_params->irq_index = irq_index;
378 err = mc_send_command(mc_io, &cmd);
382 rsp_params = (struct dprtc_rsp_get_irq_mask *)cmd.params;
383 *mask = le32_to_cpu(rsp_params->mask);
389 * dprtc_get_irq_status() - Get the current status of any pending interrupts.
391 * @mc_io: Pointer to MC portal's I/O object
392 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
393 * @token: Token of DPRTC object
394 * @irq_index: The interrupt index to configure
395 * @status: Returned interrupts status - one bit per cause:
396 * 0 = no interrupt pending
397 * 1 = interrupt pending
399 * Return: '0' on Success; Error code otherwise.
401 int dprtc_get_irq_status(struct fsl_mc_io *mc_io,
407 struct dprtc_cmd_get_irq_status *cmd_params;
408 struct dprtc_rsp_get_irq_status *rsp_params;
409 struct fsl_mc_command cmd = { 0 };
412 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_IRQ_STATUS,
415 cmd_params = (struct dprtc_cmd_get_irq_status *)cmd.params;
416 cmd_params->status = cpu_to_le32(*status);
417 cmd_params->irq_index = irq_index;
419 err = mc_send_command(mc_io, &cmd);
423 rsp_params = (struct dprtc_rsp_get_irq_status *)cmd.params;
424 *status = le32_to_cpu(rsp_params->status);
430 * dprtc_clear_irq_status() - Clear a pending interrupt's status
432 * @mc_io: Pointer to MC portal's I/O object
433 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
434 * @token: Token of DPRTC object
435 * @irq_index: The interrupt index to configure
436 * @status: Bits to clear (W1C) - one bit per cause:
438 * 1 = clear status bit
440 * Return: '0' on Success; Error code otherwise.
442 int dprtc_clear_irq_status(struct fsl_mc_io *mc_io,
448 struct dprtc_cmd_clear_irq_status *cmd_params;
449 struct fsl_mc_command cmd = { 0 };
451 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_CLEAR_IRQ_STATUS,
454 cmd_params = (struct dprtc_cmd_clear_irq_status *)cmd.params;
455 cmd_params->irq_index = irq_index;
456 cmd_params->status = cpu_to_le32(status);
458 return mc_send_command(mc_io, &cmd);
462 * dprtc_get_attributes - Retrieve DPRTC attributes.
464 * @mc_io: Pointer to MC portal's I/O object
465 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
466 * @token: Token of DPRTC object
467 * @attr: Returned object's attributes
469 * Return: '0' on Success; Error code otherwise.
471 int dprtc_get_attributes(struct fsl_mc_io *mc_io,
474 struct dprtc_attr *attr)
476 struct dprtc_rsp_get_attributes *rsp_params;
477 struct fsl_mc_command cmd = { 0 };
480 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_ATTR,
484 err = mc_send_command(mc_io, &cmd);
488 rsp_params = (struct dprtc_rsp_get_attributes *)cmd.params;
489 attr->id = le32_to_cpu(rsp_params->id);
495 * dprtc_set_clock_offset() - Sets the clock's offset
496 * (usually relative to another clock).
498 * @mc_io: Pointer to MC portal's I/O object
499 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
500 * @token: Token of DPRTC object
501 * @offset: New clock offset (in nanoseconds).
503 * Return: '0' on Success; Error code otherwise.
505 int dprtc_set_clock_offset(struct fsl_mc_io *mc_io,
510 struct dprtc_cmd_set_clock_offset *cmd_params;
511 struct fsl_mc_command cmd = { 0 };
513 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_CLOCK_OFFSET,
516 cmd_params = (struct dprtc_cmd_set_clock_offset *)cmd.params;
517 cmd_params->offset = cpu_to_le64(offset);
519 return mc_send_command(mc_io, &cmd);
523 * dprtc_set_freq_compensation() - Sets a new frequency compensation value.
525 * @mc_io: Pointer to MC portal's I/O object
526 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
527 * @token: Token of DPRTC object
528 * @freq_compensation: The new frequency compensation value to set.
530 * Return: '0' on Success; Error code otherwise.
532 int dprtc_set_freq_compensation(struct fsl_mc_io *mc_io,
535 u32 freq_compensation)
537 struct dprtc_get_freq_compensation *cmd_params;
538 struct fsl_mc_command cmd = { 0 };
540 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_FREQ_COMPENSATION,
543 cmd_params = (struct dprtc_get_freq_compensation *)cmd.params;
544 cmd_params->freq_compensation = cpu_to_le32(freq_compensation);
546 return mc_send_command(mc_io, &cmd);
550 * dprtc_get_freq_compensation() - Retrieves the frequency compensation value
552 * @mc_io: Pointer to MC portal's I/O object
553 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
554 * @token: Token of DPRTC object
555 * @freq_compensation: Frequency compensation value
557 * Return: '0' on Success; Error code otherwise.
559 int dprtc_get_freq_compensation(struct fsl_mc_io *mc_io,
562 u32 *freq_compensation)
564 struct dprtc_get_freq_compensation *rsp_params;
565 struct fsl_mc_command cmd = { 0 };
568 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_FREQ_COMPENSATION,
572 err = mc_send_command(mc_io, &cmd);
576 rsp_params = (struct dprtc_get_freq_compensation *)cmd.params;
577 *freq_compensation = le32_to_cpu(rsp_params->freq_compensation);
583 * dprtc_get_time() - Returns the current RTC time.
585 * @mc_io: Pointer to MC portal's I/O object
586 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
587 * @token: Token of DPRTC object
588 * @time: Current RTC time.
590 * Return: '0' on Success; Error code otherwise.
592 int dprtc_get_time(struct fsl_mc_io *mc_io,
597 struct dprtc_time *rsp_params;
598 struct fsl_mc_command cmd = { 0 };
601 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_TIME,
605 err = mc_send_command(mc_io, &cmd);
609 rsp_params = (struct dprtc_time *)cmd.params;
610 *time = le64_to_cpu(rsp_params->time);
616 * dprtc_set_time() - Updates current RTC time.
618 * @mc_io: Pointer to MC portal's I/O object
619 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
620 * @token: Token of DPRTC object
621 * @time: New RTC time.
623 * Return: '0' on Success; Error code otherwise.
625 int dprtc_set_time(struct fsl_mc_io *mc_io,
630 struct dprtc_time *cmd_params;
631 struct fsl_mc_command cmd = { 0 };
633 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_TIME,
636 cmd_params = (struct dprtc_time *)cmd.params;
637 cmd_params->time = cpu_to_le64(time);
639 return mc_send_command(mc_io, &cmd);
643 * dprtc_set_alarm() - Defines and sets alarm.
645 * @mc_io: Pointer to MC portal's I/O object
646 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
647 * @token: Token of DPRTC object
648 * @time: In nanoseconds, the time when the alarm
649 * should go off - must be a multiple of
652 * Return: '0' on Success; Error code otherwise.
654 int dprtc_set_alarm(struct fsl_mc_io *mc_io,
656 u16 token, uint64_t time)
658 struct dprtc_time *cmd_params;
659 struct fsl_mc_command cmd = { 0 };
661 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_ALARM,
664 cmd_params = (struct dprtc_time *)cmd.params;
665 cmd_params->time = cpu_to_le64(time);
667 return mc_send_command(mc_io, &cmd);
671 * dprtc_get_api_version() - Get Data Path Real Time Counter API version
672 * @mc_io: Pointer to MC portal's I/O object
673 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
674 * @major_ver: Major version of data path real time counter API
675 * @minor_ver: Minor version of data path real time counter API
677 * Return: '0' on Success; Error code otherwise.
679 int dprtc_get_api_version(struct fsl_mc_io *mc_io,
684 struct dprtc_rsp_get_api_version *rsp_params;
685 struct fsl_mc_command cmd = { 0 };
688 cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_API_VERSION,
692 err = mc_send_command(mc_io, &cmd);
696 rsp_params = (struct dprtc_rsp_get_api_version *)cmd.params;
697 *major_ver = le16_to_cpu(rsp_params->major);
698 *minor_ver = le16_to_cpu(rsp_params->minor);