1 From 579f1f6767b1008c6e5ccc2b029acbb56002ed8b Mon Sep 17 00:00:00 2001
2 From: Biwen Li <biwen.li@nxp.com>
3 Date: Tue, 30 Oct 2018 18:26:20 +0800
4 Subject: [PATCH 11/40] dpaa2-rtc: support layerscape
5 This is an integrated patch of dpaa2-rtc for layerscape
7 Signed-off-by: Catalin Horghidan <catalin.horghidan@nxp.com>
8 Signed-off-by: Yangbo Lu <yangbo.lu@nxp.com>
9 Signed-off-by: Biwen Li <biwen.li@nxp.com>
11 drivers/staging/fsl-dpaa2/rtc/Makefile | 10 +
12 drivers/staging/fsl-dpaa2/rtc/dprtc-cmd.h | 160 +++++
13 drivers/staging/fsl-dpaa2/rtc/dprtc.c | 746 ++++++++++++++++++++++
14 drivers/staging/fsl-dpaa2/rtc/dprtc.h | 172 +++++
15 drivers/staging/fsl-dpaa2/rtc/rtc.c | 242 +++++++
16 5 files changed, 1330 insertions(+)
17 create mode 100644 drivers/staging/fsl-dpaa2/rtc/Makefile
18 create mode 100644 drivers/staging/fsl-dpaa2/rtc/dprtc-cmd.h
19 create mode 100644 drivers/staging/fsl-dpaa2/rtc/dprtc.c
20 create mode 100644 drivers/staging/fsl-dpaa2/rtc/dprtc.h
21 create mode 100644 drivers/staging/fsl-dpaa2/rtc/rtc.c
24 +++ b/drivers/staging/fsl-dpaa2/rtc/Makefile
27 +obj-$(CONFIG_PTP_1588_CLOCK_DPAA2) += dpaa2-rtc.o
29 +dpaa2-rtc-objs := rtc.o dprtc.o
32 + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
35 + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
37 +++ b/drivers/staging/fsl-dpaa2/rtc/dprtc-cmd.h
39 +/* Copyright 2013-2016 Freescale Semiconductor Inc.
41 + * Redistribution and use in source and binary forms, with or without
42 + * modification, are permitted provided that the following conditions are met:
43 + * * Redistributions of source code must retain the above copyright
44 + * notice, this list of conditions and the following disclaimer.
45 + * * Redistributions in binary form must reproduce the above copyright
46 + * notice, this list of conditions and the following disclaimer in the
47 + * documentation and/or other materials provided with the distribution.
48 + * * Neither the name of the above-listed copyright holders nor the
49 + * names of any contributors may be used to endorse or promote products
50 + * derived from this software without specific prior written permission.
53 + * ALTERNATIVELY, this software may be distributed under the terms of the
54 + * GNU General Public License ("GPL") as published by the Free Software
55 + * Foundation, either version 2 of that License or (at your option) any
58 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
59 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
62 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68 + * POSSIBILITY OF SUCH DAMAGE.
70 +#ifndef _FSL_DPRTC_CMD_H
71 +#define _FSL_DPRTC_CMD_H
74 +#define DPRTC_VER_MAJOR 2
75 +#define DPRTC_VER_MINOR 0
77 +/* Command versioning */
78 +#define DPRTC_CMD_BASE_VERSION 1
79 +#define DPRTC_CMD_ID_OFFSET 4
81 +#define DPRTC_CMD(id) (((id) << DPRTC_CMD_ID_OFFSET) | DPRTC_CMD_BASE_VERSION)
84 +#define DPRTC_CMDID_CLOSE DPRTC_CMD(0x800)
85 +#define DPRTC_CMDID_OPEN DPRTC_CMD(0x810)
86 +#define DPRTC_CMDID_CREATE DPRTC_CMD(0x910)
87 +#define DPRTC_CMDID_DESTROY DPRTC_CMD(0x990)
88 +#define DPRTC_CMDID_GET_API_VERSION DPRTC_CMD(0xa10)
90 +#define DPRTC_CMDID_ENABLE DPRTC_CMD(0x002)
91 +#define DPRTC_CMDID_DISABLE DPRTC_CMD(0x003)
92 +#define DPRTC_CMDID_GET_ATTR DPRTC_CMD(0x004)
93 +#define DPRTC_CMDID_RESET DPRTC_CMD(0x005)
94 +#define DPRTC_CMDID_IS_ENABLED DPRTC_CMD(0x006)
96 +#define DPRTC_CMDID_SET_IRQ_ENABLE DPRTC_CMD(0x012)
97 +#define DPRTC_CMDID_GET_IRQ_ENABLE DPRTC_CMD(0x013)
98 +#define DPRTC_CMDID_SET_IRQ_MASK DPRTC_CMD(0x014)
99 +#define DPRTC_CMDID_GET_IRQ_MASK DPRTC_CMD(0x015)
100 +#define DPRTC_CMDID_GET_IRQ_STATUS DPRTC_CMD(0x016)
101 +#define DPRTC_CMDID_CLEAR_IRQ_STATUS DPRTC_CMD(0x017)
103 +#define DPRTC_CMDID_SET_CLOCK_OFFSET DPRTC_CMD(0x1d0)
104 +#define DPRTC_CMDID_SET_FREQ_COMPENSATION DPRTC_CMD(0x1d1)
105 +#define DPRTC_CMDID_GET_FREQ_COMPENSATION DPRTC_CMD(0x1d2)
106 +#define DPRTC_CMDID_GET_TIME DPRTC_CMD(0x1d3)
107 +#define DPRTC_CMDID_SET_TIME DPRTC_CMD(0x1d4)
108 +#define DPRTC_CMDID_SET_ALARM DPRTC_CMD(0x1d5)
109 +#define DPRTC_CMDID_SET_PERIODIC_PULSE DPRTC_CMD(0x1d6)
110 +#define DPRTC_CMDID_CLEAR_PERIODIC_PULSE DPRTC_CMD(0x1d7)
111 +#define DPRTC_CMDID_SET_EXT_TRIGGER DPRTC_CMD(0x1d8)
112 +#define DPRTC_CMDID_CLEAR_EXT_TRIGGER DPRTC_CMD(0x1d9)
113 +#define DPRTC_CMDID_GET_EXT_TRIGGER_TIMESTAMP DPRTC_CMD(0x1dA)
115 +/* Macros for accessing command fields smaller than 1byte */
116 +#define DPRTC_MASK(field) \
117 + GENMASK(DPRTC_##field##_SHIFT + DPRTC_##field##_SIZE - 1, \
118 + DPRTC_##field##_SHIFT)
119 +#define dprtc_get_field(var, field) \
120 + (((var) & DPRTC_MASK(field)) >> DPRTC_##field##_SHIFT)
122 +#pragma pack(push, 1)
123 +struct dprtc_cmd_open {
127 +struct dprtc_cmd_destroy {
128 + uint32_t object_id;
131 +#define DPRTC_ENABLE_SHIFT 0
132 +#define DPRTC_ENABLE_SIZE 1
134 +struct dprtc_rsp_is_enabled {
138 +struct dprtc_cmd_get_irq {
143 +struct dprtc_cmd_set_irq_enable {
149 +struct dprtc_rsp_get_irq_enable {
153 +struct dprtc_cmd_set_irq_mask {
158 +struct dprtc_rsp_get_irq_mask {
162 +struct dprtc_cmd_get_irq_status {
167 +struct dprtc_rsp_get_irq_status {
171 +struct dprtc_cmd_clear_irq_status {
176 +struct dprtc_rsp_get_attributes {
181 +struct dprtc_cmd_set_clock_offset {
185 +struct dprtc_get_freq_compensation {
186 + uint32_t freq_compensation;
193 +struct dprtc_rsp_get_api_version {
198 +#endif /* _FSL_DPRTC_CMD_H */
200 +++ b/drivers/staging/fsl-dpaa2/rtc/dprtc.c
202 +/* Copyright 2013-2016 Freescale Semiconductor Inc.
204 + * Redistribution and use in source and binary forms, with or without
205 + * modification, are permitted provided that the following conditions are met:
206 + * * Redistributions of source code must retain the above copyright
207 + * notice, this list of conditions and the following disclaimer.
208 + * * Redistributions in binary form must reproduce the above copyright
209 + * notice, this list of conditions and the following disclaimer in the
210 + * documentation and/or other materials provided with the distribution.
211 + * * Neither the name of the above-listed copyright holders nor the
212 + * names of any contributors may be used to endorse or promote products
213 + * derived from this software without specific prior written permission.
216 + * ALTERNATIVELY, this software may be distributed under the terms of the
217 + * GNU General Public License ("GPL") as published by the Free Software
218 + * Foundation, either version 2 of that License or (at your option) any
221 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
222 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
223 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
225 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
226 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
227 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
228 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
229 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
230 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
231 + * POSSIBILITY OF SUCH DAMAGE.
233 +#include <linux/fsl/mc.h>
236 +#include "dprtc-cmd.h"
239 + * dprtc_open() - Open a control session for the specified object.
240 + * @mc_io: Pointer to MC portal's I/O object
241 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
242 + * @dprtc_id: DPRTC unique ID
243 + * @token: Returned token; use in subsequent API calls
245 + * This function can be used to open a control session for an
246 + * already created object; an object may have been declared in
247 + * the DPL or by calling the dprtc_create function.
248 + * This function returns a unique authentication token,
249 + * associated with the specific object ID and the specific MC
250 + * portal; this token must be used in all subsequent commands for
251 + * this specific object
253 + * Return: '0' on Success; Error code otherwise.
255 +int dprtc_open(struct fsl_mc_io *mc_io,
256 + uint32_t cmd_flags,
260 + struct dprtc_cmd_open *cmd_params;
261 + struct fsl_mc_command cmd = { 0 };
264 + /* prepare command */
265 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_OPEN,
268 + cmd_params = (struct dprtc_cmd_open *)cmd.params;
269 + cmd_params->dprtc_id = cpu_to_le32(dprtc_id);
271 + /* send command to mc*/
272 + err = mc_send_command(mc_io, &cmd);
276 + /* retrieve response parameters */
277 + *token = mc_cmd_hdr_read_token(&cmd);
283 + * dprtc_close() - Close the control session of the object
284 + * @mc_io: Pointer to MC portal's I/O object
285 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
286 + * @token: Token of DPRTC object
288 + * After this function is called, no further operations are
289 + * allowed on the object without opening a new control session.
291 + * Return: '0' on Success; Error code otherwise.
293 +int dprtc_close(struct fsl_mc_io *mc_io,
294 + uint32_t cmd_flags,
297 + struct fsl_mc_command cmd = { 0 };
299 + /* prepare command */
300 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_CLOSE, cmd_flags,
303 + /* send command to mc*/
304 + return mc_send_command(mc_io, &cmd);
308 + * dprtc_create() - Create the DPRTC object.
309 + * @mc_io: Pointer to MC portal's I/O object
310 + * @dprc_token: Parent container token; '0' for default container
311 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
312 + * @cfg: Configuration structure
313 + * @obj_id: Returned object id
315 + * Create the DPRTC object, allocate required resources and
316 + * perform required initialization.
318 + * The function accepts an authentication token of a parent
319 + * container that this object should be assigned to. The token
320 + * can be '0' so the object will be assigned to the default container.
321 + * The newly created object can be opened with the returned
322 + * object id and using the container's associated tokens and MC portals.
324 + * Return: '0' on Success; Error code otherwise.
326 +int dprtc_create(struct fsl_mc_io *mc_io,
327 + uint16_t dprc_token,
328 + uint32_t cmd_flags,
329 + const struct dprtc_cfg *cfg,
332 + struct fsl_mc_command cmd = { 0 };
335 + (void)(cfg); /* unused */
337 + /* prepare command */
338 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_CREATE,
342 + /* send command to mc*/
343 + err = mc_send_command(mc_io, &cmd);
347 + /* retrieve response parameters */
348 + *obj_id = mc_cmd_read_object_id(&cmd);
354 + * dprtc_destroy() - Destroy the DPRTC object and release all its resources.
355 + * @mc_io: Pointer to MC portal's I/O object
356 + * @dprc_token: Parent container token; '0' for default container
357 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
358 + * @object_id: The object id; it must be a valid id within the container that
359 + * created this object;
361 + * The function accepts the authentication token of the parent container that
362 + * created the object (not the one that currently owns the object). The object
363 + * is searched within parent using the provided 'object_id'.
364 + * All tokens to the object must be closed before calling destroy.
366 + * Return: '0' on Success; error code otherwise.
368 +int dprtc_destroy(struct fsl_mc_io *mc_io,
369 + uint16_t dprc_token,
370 + uint32_t cmd_flags,
371 + uint32_t object_id)
373 + struct dprtc_cmd_destroy *cmd_params;
374 + struct fsl_mc_command cmd = { 0 };
376 + /* prepare command */
377 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_DESTROY,
380 + cmd_params = (struct dprtc_cmd_destroy *)cmd.params;
381 + cmd_params->object_id = cpu_to_le32(object_id);
383 + /* send command to mc*/
384 + return mc_send_command(mc_io, &cmd);
387 +int dprtc_enable(struct fsl_mc_io *mc_io,
388 + uint32_t cmd_flags,
391 + struct fsl_mc_command cmd = { 0 };
393 + /* prepare command */
394 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_ENABLE, cmd_flags,
397 + /* send command to mc*/
398 + return mc_send_command(mc_io, &cmd);
401 +int dprtc_disable(struct fsl_mc_io *mc_io,
402 + uint32_t cmd_flags,
405 + struct fsl_mc_command cmd = { 0 };
407 + /* prepare command */
408 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_DISABLE,
412 + /* send command to mc*/
413 + return mc_send_command(mc_io, &cmd);
416 +int dprtc_is_enabled(struct fsl_mc_io *mc_io,
417 + uint32_t cmd_flags,
421 + struct dprtc_rsp_is_enabled *rsp_params;
422 + struct fsl_mc_command cmd = { 0 };
425 + /* prepare command */
426 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_IS_ENABLED, cmd_flags,
429 + /* send command to mc*/
430 + err = mc_send_command(mc_io, &cmd);
434 + /* retrieve response parameters */
435 + rsp_params = (struct dprtc_rsp_is_enabled *)cmd.params;
436 + *en = dprtc_get_field(rsp_params->en, ENABLE);
441 +int dprtc_reset(struct fsl_mc_io *mc_io,
442 + uint32_t cmd_flags,
445 + struct fsl_mc_command cmd = { 0 };
447 + /* prepare command */
448 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_RESET,
452 + /* send command to mc*/
453 + return mc_send_command(mc_io, &cmd);
457 + * dprtc_set_irq_enable() - Set overall interrupt state.
458 + * @mc_io: Pointer to MC portal's I/O object
459 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
460 + * @token: Token of DPRTC object
461 + * @irq_index: The interrupt index to configure
462 + * @en: Interrupt state - enable = 1, disable = 0
464 + * Allows GPP software to control when interrupts are generated.
465 + * Each interrupt can have up to 32 causes. The enable/disable control's the
466 + * overall interrupt state. if the interrupt is disabled no causes will cause
469 + * Return: '0' on Success; Error code otherwise.
471 +int dprtc_set_irq_enable(struct fsl_mc_io *mc_io,
472 + uint32_t cmd_flags,
477 + struct dprtc_cmd_set_irq_enable *cmd_params;
478 + struct fsl_mc_command cmd = { 0 };
480 + /* prepare command */
481 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_IRQ_ENABLE,
484 + cmd_params = (struct dprtc_cmd_set_irq_enable *)cmd.params;
485 + cmd_params->irq_index = irq_index;
486 + cmd_params->en = en;
488 + /* send command to mc*/
489 + return mc_send_command(mc_io, &cmd);
493 + * dprtc_get_irq_enable() - Get overall interrupt state
494 + * @mc_io: Pointer to MC portal's I/O object
495 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
496 + * @token: Token of DPRTC object
497 + * @irq_index: The interrupt index to configure
498 + * @en: Returned interrupt state - enable = 1, disable = 0
500 + * Return: '0' on Success; Error code otherwise.
502 +int dprtc_get_irq_enable(struct fsl_mc_io *mc_io,
503 + uint32_t cmd_flags,
508 + struct dprtc_rsp_get_irq_enable *rsp_params;
509 + struct dprtc_cmd_get_irq *cmd_params;
510 + struct fsl_mc_command cmd = { 0 };
513 + /* prepare command */
514 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_IRQ_ENABLE,
517 + cmd_params = (struct dprtc_cmd_get_irq *)cmd.params;
518 + cmd_params->irq_index = irq_index;
520 + /* send command to mc*/
521 + err = mc_send_command(mc_io, &cmd);
525 + /* retrieve response parameters */
526 + rsp_params = (struct dprtc_rsp_get_irq_enable *)cmd.params;
527 + *en = rsp_params->en;
533 + * dprtc_set_irq_mask() - Set interrupt mask.
534 + * @mc_io: Pointer to MC portal's I/O object
535 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
536 + * @token: Token of DPRTC object
537 + * @irq_index: The interrupt index to configure
538 + * @mask: Event mask to trigger interrupt;
541 + * 1 = consider event for asserting IRQ
543 + * Every interrupt can have up to 32 causes and the interrupt model supports
544 + * masking/unmasking each cause independently
546 + * Return: '0' on Success; Error code otherwise.
548 +int dprtc_set_irq_mask(struct fsl_mc_io *mc_io,
549 + uint32_t cmd_flags,
554 + struct dprtc_cmd_set_irq_mask *cmd_params;
555 + struct fsl_mc_command cmd = { 0 };
557 + /* prepare command */
558 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_IRQ_MASK,
561 + cmd_params = (struct dprtc_cmd_set_irq_mask *)cmd.params;
562 + cmd_params->mask = cpu_to_le32(mask);
563 + cmd_params->irq_index = irq_index;
565 + /* send command to mc*/
566 + return mc_send_command(mc_io, &cmd);
570 + * dprtc_get_irq_mask() - Get interrupt mask.
571 + * @mc_io: Pointer to MC portal's I/O object
572 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
573 + * @token: Token of DPRTC object
574 + * @irq_index: The interrupt index to configure
575 + * @mask: Returned event mask to trigger interrupt
577 + * Every interrupt can have up to 32 causes and the interrupt model supports
578 + * masking/unmasking each cause independently
580 + * Return: '0' on Success; Error code otherwise.
582 +int dprtc_get_irq_mask(struct fsl_mc_io *mc_io,
583 + uint32_t cmd_flags,
588 + struct dprtc_rsp_get_irq_mask *rsp_params;
589 + struct dprtc_cmd_get_irq *cmd_params;
590 + struct fsl_mc_command cmd = { 0 };
593 + /* prepare command */
594 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_IRQ_MASK,
597 + cmd_params = (struct dprtc_cmd_get_irq *)cmd.params;
598 + cmd_params->irq_index = irq_index;
600 + /* send command to mc*/
601 + err = mc_send_command(mc_io, &cmd);
605 + /* retrieve response parameters */
606 + rsp_params = (struct dprtc_rsp_get_irq_mask *)cmd.params;
607 + *mask = le32_to_cpu(rsp_params->mask);
613 + * dprtc_get_irq_status() - Get the current status of any pending interrupts.
615 + * @mc_io: Pointer to MC portal's I/O object
616 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
617 + * @token: Token of DPRTC object
618 + * @irq_index: The interrupt index to configure
619 + * @status: Returned interrupts status - one bit per cause:
620 + * 0 = no interrupt pending
621 + * 1 = interrupt pending
623 + * Return: '0' on Success; Error code otherwise.
625 +int dprtc_get_irq_status(struct fsl_mc_io *mc_io,
626 + uint32_t cmd_flags,
631 + struct dprtc_cmd_get_irq_status *cmd_params;
632 + struct dprtc_rsp_get_irq_status *rsp_params;
633 + struct fsl_mc_command cmd = { 0 };
636 + /* prepare command */
637 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_IRQ_STATUS,
640 + cmd_params = (struct dprtc_cmd_get_irq_status *)cmd.params;
641 + cmd_params->status = cpu_to_le32(*status);
642 + cmd_params->irq_index = irq_index;
644 + /* send command to mc*/
645 + err = mc_send_command(mc_io, &cmd);
649 + /* retrieve response parameters */
650 + rsp_params = (struct dprtc_rsp_get_irq_status *)cmd.params;
651 + *status = rsp_params->status;
657 + * dprtc_clear_irq_status() - Clear a pending interrupt's status
659 + * @mc_io: Pointer to MC portal's I/O object
660 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
661 + * @token: Token of DPRTC object
662 + * @irq_index: The interrupt index to configure
663 + * @status: Bits to clear (W1C) - one bit per cause:
665 + * 1 = clear status bit
667 + * Return: '0' on Success; Error code otherwise.
669 +int dprtc_clear_irq_status(struct fsl_mc_io *mc_io,
670 + uint32_t cmd_flags,
675 + struct dprtc_cmd_clear_irq_status *cmd_params;
676 + struct fsl_mc_command cmd = { 0 };
678 + /* prepare command */
679 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_CLEAR_IRQ_STATUS,
682 + cmd_params = (struct dprtc_cmd_clear_irq_status *)cmd.params;
683 + cmd_params->irq_index = irq_index;
684 + cmd_params->status = cpu_to_le32(status);
686 + /* send command to mc*/
687 + return mc_send_command(mc_io, &cmd);
691 + * dprtc_get_attributes - Retrieve DPRTC attributes.
693 + * @mc_io: Pointer to MC portal's I/O object
694 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
695 + * @token: Token of DPRTC object
696 + * @attr: Returned object's attributes
698 + * Return: '0' on Success; Error code otherwise.
700 +int dprtc_get_attributes(struct fsl_mc_io *mc_io,
701 + uint32_t cmd_flags,
703 + struct dprtc_attr *attr)
705 + struct dprtc_rsp_get_attributes *rsp_params;
706 + struct fsl_mc_command cmd = { 0 };
709 + /* prepare command */
710 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_ATTR,
714 + /* send command to mc*/
715 + err = mc_send_command(mc_io, &cmd);
719 + /* retrieve response parameters */
720 + rsp_params = (struct dprtc_rsp_get_attributes *)cmd.params;
721 + attr->id = le32_to_cpu(rsp_params->id);
727 + * dprtc_set_clock_offset() - Sets the clock's offset
728 + * (usually relative to another clock).
730 + * @mc_io: Pointer to MC portal's I/O object
731 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
732 + * @token: Token of DPRTC object
733 + * @offset: New clock offset (in nanoseconds).
735 + * Return: '0' on Success; Error code otherwise.
737 +int dprtc_set_clock_offset(struct fsl_mc_io *mc_io,
738 + uint32_t cmd_flags,
742 + struct dprtc_cmd_set_clock_offset *cmd_params;
743 + struct fsl_mc_command cmd = { 0 };
745 + /* prepare command */
746 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_CLOCK_OFFSET,
749 + cmd_params = (struct dprtc_cmd_set_clock_offset *)cmd.params;
750 + cmd_params->offset = cpu_to_le64(offset);
752 + /* send command to mc*/
753 + return mc_send_command(mc_io, &cmd);
757 + * dprtc_set_freq_compensation() - Sets a new frequency compensation value.
759 + * @mc_io: Pointer to MC portal's I/O object
760 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
761 + * @token: Token of DPRTC object
762 + * @freq_compensation: The new frequency compensation value to set.
764 + * Return: '0' on Success; Error code otherwise.
766 +int dprtc_set_freq_compensation(struct fsl_mc_io *mc_io,
767 + uint32_t cmd_flags,
769 + uint32_t freq_compensation)
771 + struct dprtc_get_freq_compensation *cmd_params;
772 + struct fsl_mc_command cmd = { 0 };
774 + /* prepare command */
775 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_FREQ_COMPENSATION,
778 + cmd_params = (struct dprtc_get_freq_compensation *)cmd.params;
779 + cmd_params->freq_compensation = cpu_to_le32(freq_compensation);
781 + /* send command to mc*/
782 + return mc_send_command(mc_io, &cmd);
786 + * dprtc_get_freq_compensation() - Retrieves the frequency compensation value
788 + * @mc_io: Pointer to MC portal's I/O object
789 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
790 + * @token: Token of DPRTC object
791 + * @freq_compensation: Frequency compensation value
793 + * Return: '0' on Success; Error code otherwise.
795 +int dprtc_get_freq_compensation(struct fsl_mc_io *mc_io,
796 + uint32_t cmd_flags,
798 + uint32_t *freq_compensation)
800 + struct dprtc_get_freq_compensation *rsp_params;
801 + struct fsl_mc_command cmd = { 0 };
804 + /* prepare command */
805 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_FREQ_COMPENSATION,
809 + /* send command to mc*/
810 + err = mc_send_command(mc_io, &cmd);
814 + /* retrieve response parameters */
815 + rsp_params = (struct dprtc_get_freq_compensation *)cmd.params;
816 + *freq_compensation = le32_to_cpu(rsp_params->freq_compensation);
822 + * dprtc_get_time() - Returns the current RTC time.
824 + * @mc_io: Pointer to MC portal's I/O object
825 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
826 + * @token: Token of DPRTC object
827 + * @time: Current RTC time.
829 + * Return: '0' on Success; Error code otherwise.
831 +int dprtc_get_time(struct fsl_mc_io *mc_io,
832 + uint32_t cmd_flags,
836 + struct dprtc_time *rsp_params;
837 + struct fsl_mc_command cmd = { 0 };
840 + /* prepare command */
841 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_TIME,
845 + /* send command to mc*/
846 + err = mc_send_command(mc_io, &cmd);
850 + /* retrieve response parameters */
851 + rsp_params = (struct dprtc_time *)cmd.params;
852 + *time = le64_to_cpu(rsp_params->time);
858 + * dprtc_set_time() - Updates current RTC time.
860 + * @mc_io: Pointer to MC portal's I/O object
861 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
862 + * @token: Token of DPRTC object
863 + * @time: New RTC time.
865 + * Return: '0' on Success; Error code otherwise.
867 +int dprtc_set_time(struct fsl_mc_io *mc_io,
868 + uint32_t cmd_flags,
872 + struct dprtc_time *cmd_params;
873 + struct fsl_mc_command cmd = { 0 };
875 + /* prepare command */
876 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_TIME,
879 + cmd_params = (struct dprtc_time *)cmd.params;
880 + cmd_params->time = cpu_to_le64(time);
882 + /* send command to mc*/
883 + return mc_send_command(mc_io, &cmd);
887 + * dprtc_set_alarm() - Defines and sets alarm.
889 + * @mc_io: Pointer to MC portal's I/O object
890 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
891 + * @token: Token of DPRTC object
892 + * @time: In nanoseconds, the time when the alarm
893 + * should go off - must be a multiple of
896 + * Return: '0' on Success; Error code otherwise.
898 +int dprtc_set_alarm(struct fsl_mc_io *mc_io,
899 + uint32_t cmd_flags,
900 + uint16_t token, uint64_t time)
902 + struct dprtc_time *cmd_params;
903 + struct fsl_mc_command cmd = { 0 };
905 + /* prepare command */
906 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_ALARM,
909 + cmd_params = (struct dprtc_time *)cmd.params;
910 + cmd_params->time = cpu_to_le64(time);
912 + /* send command to mc*/
913 + return mc_send_command(mc_io, &cmd);
917 + * dprtc_get_api_version() - Get Data Path Real Time Counter API version
918 + * @mc_io: Pointer to MC portal's I/O object
919 + * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
920 + * @major_ver: Major version of data path real time counter API
921 + * @minor_ver: Minor version of data path real time counter API
923 + * Return: '0' on Success; Error code otherwise.
925 +int dprtc_get_api_version(struct fsl_mc_io *mc_io,
926 + uint32_t cmd_flags,
927 + uint16_t *major_ver,
928 + uint16_t *minor_ver)
930 + struct dprtc_rsp_get_api_version *rsp_params;
931 + struct fsl_mc_command cmd = { 0 };
934 + cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_API_VERSION,
938 + err = mc_send_command(mc_io, &cmd);
942 + rsp_params = (struct dprtc_rsp_get_api_version *)cmd.params;
943 + *major_ver = le16_to_cpu(rsp_params->major);
944 + *minor_ver = le16_to_cpu(rsp_params->minor);
949 +++ b/drivers/staging/fsl-dpaa2/rtc/dprtc.h
951 +/* Copyright 2013-2016 Freescale Semiconductor Inc.
953 + * Redistribution and use in source and binary forms, with or without
954 + * modification, are permitted provided that the following conditions are met:
955 + * * Redistributions of source code must retain the above copyright
956 + * notice, this list of conditions and the following disclaimer.
957 + * * Redistributions in binary form must reproduce the above copyright
958 + * notice, this list of conditions and the following disclaimer in the
959 + * documentation and/or other materials provided with the distribution.
960 + * * Neither the name of the above-listed copyright holders nor the
961 + * names of any contributors may be used to endorse or promote products
962 + * derived from this software without specific prior written permission.
965 + * ALTERNATIVELY, this software may be distributed under the terms of the
966 + * GNU General Public License ("GPL") as published by the Free Software
967 + * Foundation, either version 2 of that License or (at your option) any
970 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
971 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
972 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
973 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
974 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
975 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
976 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
977 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
978 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
979 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
980 + * POSSIBILITY OF SUCH DAMAGE.
982 +#ifndef __FSL_DPRTC_H
983 +#define __FSL_DPRTC_H
985 +/* Data Path Real Time Counter API
986 + * Contains initialization APIs and runtime control APIs for RTC
994 +#define DPRTC_MAX_IRQ_NUM 1
995 +#define DPRTC_IRQ_INDEX 0
998 + * Interrupt event masks:
1002 + * Interrupt event mask indicating alarm event had occurred
1004 +#define DPRTC_EVENT_ALARM 0x40000000
1006 + * Interrupt event mask indicating periodic pulse event had occurred
1008 +#define DPRTC_EVENT_PPS 0x08000000
1010 +int dprtc_open(struct fsl_mc_io *mc_io,
1011 + uint32_t cmd_flags,
1015 +int dprtc_close(struct fsl_mc_io *mc_io,
1016 + uint32_t cmd_flags,
1020 + * struct dprtc_cfg - Structure representing DPRTC configuration
1021 + * @options: place holder
1027 +int dprtc_create(struct fsl_mc_io *mc_io,
1028 + uint16_t dprc_token,
1029 + uint32_t cmd_flags,
1030 + const struct dprtc_cfg *cfg,
1031 + uint32_t *obj_id);
1033 +int dprtc_destroy(struct fsl_mc_io *mc_io,
1034 + uint16_t dprc_token,
1035 + uint32_t cmd_flags,
1036 + uint32_t object_id);
1038 +int dprtc_set_clock_offset(struct fsl_mc_io *mc_io,
1039 + uint32_t cmd_flags,
1043 +int dprtc_set_freq_compensation(struct fsl_mc_io *mc_io,
1044 + uint32_t cmd_flags,
1046 + uint32_t freq_compensation);
1048 +int dprtc_get_freq_compensation(struct fsl_mc_io *mc_io,
1049 + uint32_t cmd_flags,
1051 + uint32_t *freq_compensation);
1053 +int dprtc_get_time(struct fsl_mc_io *mc_io,
1054 + uint32_t cmd_flags,
1058 +int dprtc_set_time(struct fsl_mc_io *mc_io,
1059 + uint32_t cmd_flags,
1063 +int dprtc_set_alarm(struct fsl_mc_io *mc_io,
1064 + uint32_t cmd_flags,
1068 +int dprtc_set_irq_enable(struct fsl_mc_io *mc_io,
1069 + uint32_t cmd_flags,
1071 + uint8_t irq_index,
1074 +int dprtc_get_irq_enable(struct fsl_mc_io *mc_io,
1075 + uint32_t cmd_flags,
1077 + uint8_t irq_index,
1080 +int dprtc_set_irq_mask(struct fsl_mc_io *mc_io,
1081 + uint32_t cmd_flags,
1083 + uint8_t irq_index,
1086 +int dprtc_get_irq_mask(struct fsl_mc_io *mc_io,
1087 + uint32_t cmd_flags,
1089 + uint8_t irq_index,
1092 +int dprtc_get_irq_status(struct fsl_mc_io *mc_io,
1093 + uint32_t cmd_flags,
1095 + uint8_t irq_index,
1096 + uint32_t *status);
1098 +int dprtc_clear_irq_status(struct fsl_mc_io *mc_io,
1099 + uint32_t cmd_flags,
1101 + uint8_t irq_index,
1105 + * struct dprtc_attr - Structure representing DPRTC attributes
1106 + * @id: DPRTC object ID
1108 +struct dprtc_attr {
1112 +int dprtc_get_attributes(struct fsl_mc_io *mc_io,
1113 + uint32_t cmd_flags,
1115 + struct dprtc_attr *attr);
1117 +int dprtc_get_api_version(struct fsl_mc_io *mc_io,
1118 + uint32_t cmd_flags,
1119 + uint16_t *major_ver,
1120 + uint16_t *minor_ver);
1122 +#endif /* __FSL_DPRTC_H */
1124 +++ b/drivers/staging/fsl-dpaa2/rtc/rtc.c
1126 +/* Copyright 2013-2015 Freescale Semiconductor Inc.
1128 + * Redistribution and use in source and binary forms, with or without
1129 + * modification, are permitted provided that the following conditions are met:
1130 + * * Redistributions of source code must retain the above copyright
1131 + * notice, this list of conditions and the following disclaimer.
1132 + * * Redistributions in binary form must reproduce the above copyright
1133 + * notice, this list of conditions and the following disclaimer in the
1134 + * documentation and/or other materials provided with the distribution.
1135 + * * Neither the name of the above-listed copyright holders nor the
1136 + * names of any contributors may be used to endorse or promote products
1137 + * derived from this software without specific prior written permission.
1140 + * ALTERNATIVELY, this software may be distributed under the terms of the
1141 + * GNU General Public License ("GPL") as published by the Free Software
1142 + * Foundation, either version 2 of that License or (at your option) any
1145 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1146 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1147 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1148 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
1149 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1150 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1151 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1152 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1153 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1154 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1155 + * POSSIBILITY OF SUCH DAMAGE.
1158 +#include <linux/module.h>
1159 +#include <linux/ptp_clock_kernel.h>
1161 +#include <linux/fsl/mc.h>
1164 +#include "dprtc-cmd.h"
1168 +struct ptp_clock *clock;
1169 +struct fsl_mc_device *rtc_mc_dev;
1170 +u32 freqCompensation;
1172 +/* PTP clock operations */
1173 +static int ptp_dpaa2_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1176 + u32 diff, tmr_add;
1179 + struct fsl_mc_device *mc_dev = rtc_mc_dev;
1180 + struct device *dev = &mc_dev->dev;
1187 + tmr_add = freqCompensation;
1190 + diff = div_u64(adj, 1000000000ULL);
1192 + tmr_add = neg_adj ? tmr_add - diff : tmr_add + diff;
1194 + err = dprtc_set_freq_compensation(mc_dev->mc_io, 0,
1195 + mc_dev->mc_handle, tmr_add);
1197 + dev_err(dev, "dprtc_set_freq_compensation err %d\n", err);
1201 +static int ptp_dpaa2_adjtime(struct ptp_clock_info *ptp, s64 delta)
1205 + struct fsl_mc_device *mc_dev = rtc_mc_dev;
1206 + struct device *dev = &mc_dev->dev;
1208 + err = dprtc_get_time(mc_dev->mc_io, 0, mc_dev->mc_handle, &now);
1210 + dev_err(dev, "dprtc_get_time err %d\n", err);
1216 + err = dprtc_set_time(mc_dev->mc_io, 0, mc_dev->mc_handle, now);
1218 + dev_err(dev, "dprtc_set_time err %d\n", err);
1224 +static int ptp_dpaa2_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
1229 + struct fsl_mc_device *mc_dev = rtc_mc_dev;
1230 + struct device *dev = &mc_dev->dev;
1232 + err = dprtc_get_time(mc_dev->mc_io, 0, mc_dev->mc_handle, &ns);
1234 + dev_err(dev, "dprtc_get_time err %d\n", err);
1238 + ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
1239 + ts->tv_nsec = remainder;
1243 +static int ptp_dpaa2_settime(struct ptp_clock_info *ptp,
1244 + const struct timespec *ts)
1248 + struct fsl_mc_device *mc_dev = rtc_mc_dev;
1249 + struct device *dev = &mc_dev->dev;
1251 + ns = ts->tv_sec * 1000000000ULL;
1252 + ns += ts->tv_nsec;
1254 + err = dprtc_set_time(mc_dev->mc_io, 0, mc_dev->mc_handle, ns);
1256 + dev_err(dev, "dprtc_set_time err %d\n", err);
1260 +static struct ptp_clock_info ptp_dpaa2_caps = {
1261 + .owner = THIS_MODULE,
1262 + .name = "dpaa2 clock",
1263 + .max_adj = 512000,
1265 + .n_ext_ts = N_EXT_TS,
1269 + .adjfreq = ptp_dpaa2_adjfreq,
1270 + .adjtime = ptp_dpaa2_adjtime,
1271 + .gettime64 = ptp_dpaa2_gettime,
1272 + .settime64 = ptp_dpaa2_settime,
1275 +static int rtc_probe(struct fsl_mc_device *mc_dev)
1277 + struct device *dev;
1279 + int dpaa2_phc_index;
1285 + dev = &mc_dev->dev;
1287 + err = fsl_mc_portal_allocate(mc_dev, 0, &mc_dev->mc_io);
1288 + if (unlikely(err)) {
1289 + dev_err(dev, "fsl_mc_portal_allocate err %d\n", err);
1292 + if (!mc_dev->mc_io) {
1294 + "fsl_mc_portal_allocate returned null handle but no error\n");
1299 + err = dprtc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
1300 + &mc_dev->mc_handle);
1302 + dev_err(dev, "dprtc_open err %d\n", err);
1303 + goto err_free_mcp;
1305 + if (!mc_dev->mc_handle) {
1306 + dev_err(dev, "dprtc_open returned null handle but no error\n");
1308 + goto err_free_mcp;
1311 + rtc_mc_dev = mc_dev;
1313 + err = dprtc_get_freq_compensation(mc_dev->mc_io, 0,
1314 + mc_dev->mc_handle, &tmr_add);
1316 + dev_err(dev, "dprtc_get_freq_compensation err %d\n", err);
1319 + freqCompensation = tmr_add;
1321 + clock = ptp_clock_register(&ptp_dpaa2_caps, dev);
1322 + if (IS_ERR(clock)) {
1323 + err = PTR_ERR(clock);
1326 + dpaa2_phc_index = ptp_clock_index(clock);
1330 + dprtc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
1332 + fsl_mc_portal_free(mc_dev->mc_io);
1337 +static int rtc_remove(struct fsl_mc_device *mc_dev)
1339 + ptp_clock_unregister(clock);
1340 + dprtc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
1341 + fsl_mc_portal_free(mc_dev->mc_io);
1346 +static const struct fsl_mc_device_id rtc_match_id_table[] = {
1348 + .vendor = FSL_MC_VENDOR_FREESCALE,
1349 + .obj_type = "dprtc",
1354 +static struct fsl_mc_driver rtc_drv = {
1356 + .name = KBUILD_MODNAME,
1357 + .owner = THIS_MODULE,
1359 + .probe = rtc_probe,
1360 + .remove = rtc_remove,
1361 + .match_id_table = rtc_match_id_table,
1364 +module_fsl_mc_driver(rtc_drv);
1366 +MODULE_LICENSE("GPL");
1367 +MODULE_DESCRIPTION("DPAA2 RTC (PTP 1588 clock) driver (prototype)");