1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2013-2016 Freescale Semiconductor Inc.
4  * Copyright 2016-2018 NXP
5  */
6 
7 #include <linux/fsl/mc.h>
8 
9 #include "dprtc.h"
10 #include "dprtc-cmd.h"
11 
12 /**
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
18  *
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
26  *
27  * Return:	'0' on Success; Error code otherwise.
28  */
29 int dprtc_open(struct fsl_mc_io *mc_io,
30 	       u32 cmd_flags,
31 	       int dprtc_id,
32 	       u16 *token)
33 {
34 	struct dprtc_cmd_open *cmd_params;
35 	struct fsl_mc_command cmd = { 0 };
36 	int err;
37 
38 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_OPEN,
39 					  cmd_flags,
40 					  0);
41 	cmd_params = (struct dprtc_cmd_open *)cmd.params;
42 	cmd_params->dprtc_id = cpu_to_le32(dprtc_id);
43 
44 	err = mc_send_command(mc_io, &cmd);
45 	if (err)
46 		return err;
47 
48 	*token = mc_cmd_hdr_read_token(&cmd);
49 
50 	return 0;
51 }
52 
53 /**
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
58  *
59  * After this function is called, no further operations are
60  * allowed on the object without opening a new control session.
61  *
62  * Return:	'0' on Success; Error code otherwise.
63  */
64 int dprtc_close(struct fsl_mc_io *mc_io,
65 		u32 cmd_flags,
66 		u16 token)
67 {
68 	struct fsl_mc_command cmd = { 0 };
69 
70 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_CLOSE, cmd_flags,
71 					  token);
72 
73 	return mc_send_command(mc_io, &cmd);
74 }
75 
76 /**
77  * dprtc_set_irq_enable() - Set overall interrupt state.
78  * @mc_io:	Pointer to MC portal's I/O object
79  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
80  * @token:	Token of DPRTC object
81  * @irq_index:	The interrupt index to configure
82  * @en:		Interrupt state - enable = 1, disable = 0
83  *
84  * Allows GPP software to control when interrupts are generated.
85  * Each interrupt can have up to 32 causes.  The enable/disable control's the
86  * overall interrupt state. if the interrupt is disabled no causes will cause
87  * an interrupt.
88  *
89  * Return:	'0' on Success; Error code otherwise.
90  */
91 int dprtc_set_irq_enable(struct fsl_mc_io *mc_io,
92 			 u32 cmd_flags,
93 			 u16 token,
94 			 u8 irq_index,
95 			 u8 en)
96 {
97 	struct dprtc_cmd_set_irq_enable *cmd_params;
98 	struct fsl_mc_command cmd = { 0 };
99 
100 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_IRQ_ENABLE,
101 					  cmd_flags,
102 					  token);
103 	cmd_params = (struct dprtc_cmd_set_irq_enable *)cmd.params;
104 	cmd_params->irq_index = irq_index;
105 	cmd_params->en = en;
106 
107 	return mc_send_command(mc_io, &cmd);
108 }
109 
110 /**
111  * dprtc_get_irq_enable() - Get overall interrupt state
112  * @mc_io:	Pointer to MC portal's I/O object
113  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
114  * @token:	Token of DPRTC object
115  * @irq_index:	The interrupt index to configure
116  * @en:		Returned interrupt state - enable = 1, disable = 0
117  *
118  * Return:	'0' on Success; Error code otherwise.
119  */
120 int dprtc_get_irq_enable(struct fsl_mc_io *mc_io,
121 			 u32 cmd_flags,
122 			 u16 token,
123 			 u8 irq_index,
124 			 u8 *en)
125 {
126 	struct dprtc_rsp_get_irq_enable *rsp_params;
127 	struct dprtc_cmd_get_irq *cmd_params;
128 	struct fsl_mc_command cmd = { 0 };
129 	int err;
130 
131 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_IRQ_ENABLE,
132 					  cmd_flags,
133 					  token);
134 	cmd_params = (struct dprtc_cmd_get_irq *)cmd.params;
135 	cmd_params->irq_index = irq_index;
136 
137 	err = mc_send_command(mc_io, &cmd);
138 	if (err)
139 		return err;
140 
141 	rsp_params = (struct dprtc_rsp_get_irq_enable *)cmd.params;
142 	*en = rsp_params->en;
143 
144 	return 0;
145 }
146 
147 /**
148  * dprtc_set_irq_mask() - Set interrupt mask.
149  * @mc_io:	Pointer to MC portal's I/O object
150  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
151  * @token:	Token of DPRTC object
152  * @irq_index:	The interrupt index to configure
153  * @mask:	Event mask to trigger interrupt;
154  *		each bit:
155  *			0 = ignore event
156  *			1 = consider event for asserting IRQ
157  *
158  * Every interrupt can have up to 32 causes and the interrupt model supports
159  * masking/unmasking each cause independently
160  *
161  * Return:	'0' on Success; Error code otherwise.
162  */
163 int dprtc_set_irq_mask(struct fsl_mc_io *mc_io,
164 		       u32 cmd_flags,
165 		       u16 token,
166 		       u8 irq_index,
167 		       u32 mask)
168 {
169 	struct dprtc_cmd_set_irq_mask *cmd_params;
170 	struct fsl_mc_command cmd = { 0 };
171 
172 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_SET_IRQ_MASK,
173 					  cmd_flags,
174 					  token);
175 	cmd_params = (struct dprtc_cmd_set_irq_mask *)cmd.params;
176 	cmd_params->mask = cpu_to_le32(mask);
177 	cmd_params->irq_index = irq_index;
178 
179 	return mc_send_command(mc_io, &cmd);
180 }
181 
182 /**
183  * dprtc_get_irq_mask() - Get interrupt mask.
184  * @mc_io:	Pointer to MC portal's I/O object
185  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
186  * @token:	Token of DPRTC object
187  * @irq_index:	The interrupt index to configure
188  * @mask:	Returned event mask to trigger interrupt
189  *
190  * Every interrupt can have up to 32 causes and the interrupt model supports
191  * masking/unmasking each cause independently
192  *
193  * Return:	'0' on Success; Error code otherwise.
194  */
195 int dprtc_get_irq_mask(struct fsl_mc_io *mc_io,
196 		       u32 cmd_flags,
197 		       u16 token,
198 		       u8 irq_index,
199 		       u32 *mask)
200 {
201 	struct dprtc_rsp_get_irq_mask *rsp_params;
202 	struct dprtc_cmd_get_irq *cmd_params;
203 	struct fsl_mc_command cmd = { 0 };
204 	int err;
205 
206 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_IRQ_MASK,
207 					  cmd_flags,
208 					  token);
209 	cmd_params = (struct dprtc_cmd_get_irq *)cmd.params;
210 	cmd_params->irq_index = irq_index;
211 
212 	err = mc_send_command(mc_io, &cmd);
213 	if (err)
214 		return err;
215 
216 	rsp_params = (struct dprtc_rsp_get_irq_mask *)cmd.params;
217 	*mask = le32_to_cpu(rsp_params->mask);
218 
219 	return 0;
220 }
221 
222 /**
223  * dprtc_get_irq_status() - Get the current status of any pending interrupts.
224  *
225  * @mc_io:	Pointer to MC portal's I/O object
226  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
227  * @token:	Token of DPRTC object
228  * @irq_index:	The interrupt index to configure
229  * @status:	Returned interrupts status - one bit per cause:
230  *			0 = no interrupt pending
231  *			1 = interrupt pending
232  *
233  * Return:	'0' on Success; Error code otherwise.
234  */
235 int dprtc_get_irq_status(struct fsl_mc_io *mc_io,
236 			 u32 cmd_flags,
237 			 u16 token,
238 			 u8 irq_index,
239 			 u32 *status)
240 {
241 	struct dprtc_cmd_get_irq_status *cmd_params;
242 	struct dprtc_rsp_get_irq_status *rsp_params;
243 	struct fsl_mc_command cmd = { 0 };
244 	int err;
245 
246 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_GET_IRQ_STATUS,
247 					  cmd_flags,
248 					  token);
249 	cmd_params = (struct dprtc_cmd_get_irq_status *)cmd.params;
250 	cmd_params->status = cpu_to_le32(*status);
251 	cmd_params->irq_index = irq_index;
252 
253 	err = mc_send_command(mc_io, &cmd);
254 	if (err)
255 		return err;
256 
257 	rsp_params = (struct dprtc_rsp_get_irq_status *)cmd.params;
258 	*status = le32_to_cpu(rsp_params->status);
259 
260 	return 0;
261 }
262 
263 /**
264  * dprtc_clear_irq_status() - Clear a pending interrupt's status
265  *
266  * @mc_io:	Pointer to MC portal's I/O object
267  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
268  * @token:	Token of DPRTC object
269  * @irq_index:	The interrupt index to configure
270  * @status:	Bits to clear (W1C) - one bit per cause:
271  *			0 = don't change
272  *			1 = clear status bit
273  *
274  * Return:	'0' on Success; Error code otherwise.
275  */
276 int dprtc_clear_irq_status(struct fsl_mc_io *mc_io,
277 			   u32 cmd_flags,
278 			   u16 token,
279 			   u8 irq_index,
280 			   u32 status)
281 {
282 	struct dprtc_cmd_clear_irq_status *cmd_params;
283 	struct fsl_mc_command cmd = { 0 };
284 
285 	cmd.header = mc_encode_cmd_header(DPRTC_CMDID_CLEAR_IRQ_STATUS,
286 					  cmd_flags,
287 					  token);
288 	cmd_params = (struct dprtc_cmd_clear_irq_status *)cmd.params;
289 	cmd_params->irq_index = irq_index;
290 	cmd_params->status = cpu_to_le32(status);
291 
292 	return mc_send_command(mc_io, &cmd);
293 }
294