1/* Copyright 2013-2014 Freescale Semiconductor Inc.
2*
3* Redistribution and use in source and binary forms, with or without
4* modification, are permitted provided that the following conditions are met:
5* * Redistributions of source code must retain the above copyright
6* notice, this list of conditions and the following disclaimer.
7* * Redistributions in binary form must reproduce the above copyright
8* notice, this list of conditions and the following disclaimer in the
9* documentation and/or other materials provided with the distribution.
10* * Neither the name of the above-listed copyright holders nor the
11* names of any contributors may be used to endorse or promote products
12* derived from this software without specific prior written permission.
13*
14*
15* ALTERNATIVELY, this software may be distributed under the terms of the
16* GNU General Public License ("GPL") as published by the Free Software
17* Foundation, either version 2 of that License or (at your option) any
18* later version.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*/
32#include "../include/mc-sys.h"
33#include "../include/mc-cmd.h"
34#include "../include/dpbp.h"
35#include "../include/dpbp-cmd.h"
36
37int dpbp_open(struct fsl_mc_io *mc_io, int dpbp_id, uint16_t *token)
38{
39	struct mc_command cmd = { 0 };
40	int err;
41
42	/* prepare command */
43	cmd.header = mc_encode_cmd_header(DPBP_CMDID_OPEN,
44					  MC_CMD_PRI_LOW, 0);
45	cmd.params[0] |= mc_enc(0, 32, dpbp_id);
46
47	/* send command to mc*/
48	err = mc_send_command(mc_io, &cmd);
49	if (err)
50		return err;
51
52	/* retrieve response parameters */
53	*token = MC_CMD_HDR_READ_TOKEN(cmd.header);
54
55	return err;
56}
57EXPORT_SYMBOL(dpbp_open);
58
59int dpbp_close(struct fsl_mc_io *mc_io, uint16_t token)
60{
61	struct mc_command cmd = { 0 };
62
63	/* prepare command */
64	cmd.header = mc_encode_cmd_header(DPBP_CMDID_CLOSE, MC_CMD_PRI_HIGH,
65					  token);
66
67	/* send command to mc*/
68	return mc_send_command(mc_io, &cmd);
69}
70EXPORT_SYMBOL(dpbp_close);
71
72int dpbp_create(struct fsl_mc_io *mc_io,
73		const struct dpbp_cfg *cfg,
74		uint16_t *token)
75{
76	struct mc_command cmd = { 0 };
77	int err;
78
79	(void)(cfg); /* unused */
80
81	/* prepare command */
82	cmd.header = mc_encode_cmd_header(DPBP_CMDID_CREATE,
83					  MC_CMD_PRI_LOW, 0);
84
85	/* send command to mc*/
86	err = mc_send_command(mc_io, &cmd);
87	if (err)
88		return err;
89
90	/* retrieve response parameters */
91	*token = MC_CMD_HDR_READ_TOKEN(cmd.header);
92
93	return 0;
94}
95
96int dpbp_destroy(struct fsl_mc_io *mc_io, uint16_t token)
97{
98	struct mc_command cmd = { 0 };
99
100	/* prepare command */
101	cmd.header = mc_encode_cmd_header(DPBP_CMDID_DESTROY,
102					  MC_CMD_PRI_LOW, token);
103
104	/* send command to mc*/
105	return mc_send_command(mc_io, &cmd);
106}
107
108int dpbp_enable(struct fsl_mc_io *mc_io, uint16_t token)
109{
110	struct mc_command cmd = { 0 };
111
112	/* prepare command */
113	cmd.header = mc_encode_cmd_header(DPBP_CMDID_ENABLE, MC_CMD_PRI_LOW,
114					  token);
115
116	/* send command to mc*/
117	return mc_send_command(mc_io, &cmd);
118}
119EXPORT_SYMBOL(dpbp_enable);
120
121int dpbp_disable(struct fsl_mc_io *mc_io, uint16_t token)
122{
123	struct mc_command cmd = { 0 };
124
125	/* prepare command */
126	cmd.header = mc_encode_cmd_header(DPBP_CMDID_DISABLE,
127					  MC_CMD_PRI_LOW, token);
128
129	/* send command to mc*/
130	return mc_send_command(mc_io, &cmd);
131}
132EXPORT_SYMBOL(dpbp_disable);
133
134int dpbp_is_enabled(struct fsl_mc_io *mc_io, uint16_t token, int *en)
135{
136	struct mc_command cmd = { 0 };
137	int err;
138	/* prepare command */
139	cmd.header = mc_encode_cmd_header(DPBP_CMDID_IS_ENABLED, MC_CMD_PRI_LOW,
140					  token);
141
142	/* send command to mc*/
143	err = mc_send_command(mc_io, &cmd);
144	if (err)
145		return err;
146
147	/* retrieve response parameters */
148	*en = (int)mc_dec(cmd.params[0], 0, 1);
149
150	return 0;
151}
152
153int dpbp_reset(struct fsl_mc_io *mc_io, uint16_t token)
154{
155	struct mc_command cmd = { 0 };
156
157	/* prepare command */
158	cmd.header = mc_encode_cmd_header(DPBP_CMDID_RESET,
159					  MC_CMD_PRI_LOW, token);
160
161	/* send command to mc*/
162	return mc_send_command(mc_io, &cmd);
163}
164
165int dpbp_set_irq(struct fsl_mc_io *mc_io,
166		 uint16_t token,
167		 uint8_t irq_index,
168		 uint64_t irq_paddr,
169		 uint32_t irq_val,
170		 int user_irq_id)
171{
172	struct mc_command cmd = { 0 };
173
174	/* prepare command */
175	cmd.header = mc_encode_cmd_header(DPBP_CMDID_SET_IRQ,
176					  MC_CMD_PRI_LOW, token);
177	cmd.params[0] |= mc_enc(0, 8, irq_index);
178	cmd.params[0] |= mc_enc(32, 32, irq_val);
179	cmd.params[1] |= mc_enc(0, 64, irq_paddr);
180	cmd.params[2] |= mc_enc(0, 32, user_irq_id);
181
182	/* send command to mc*/
183	return mc_send_command(mc_io, &cmd);
184}
185
186int dpbp_get_irq(struct fsl_mc_io *mc_io,
187		 uint16_t token,
188		 uint8_t irq_index,
189		 int *type,
190		 uint64_t *irq_paddr,
191		 uint32_t *irq_val,
192		 int *user_irq_id)
193{
194	struct mc_command cmd = { 0 };
195	int err;
196
197	/* prepare command */
198	cmd.header = mc_encode_cmd_header(DPBP_CMDID_GET_IRQ,
199					  MC_CMD_PRI_LOW, token);
200	cmd.params[0] |= mc_enc(32, 8, irq_index);
201
202	/* send command to mc*/
203	err = mc_send_command(mc_io, &cmd);
204	if (err)
205		return err;
206
207	/* retrieve response parameters */
208	*irq_val = (uint32_t)mc_dec(cmd.params[0], 0, 32);
209	*irq_paddr = (uint64_t)mc_dec(cmd.params[1], 0, 64);
210	*user_irq_id = (int)mc_dec(cmd.params[2], 0, 32);
211	*type = (int)mc_dec(cmd.params[2], 32, 32);
212	return 0;
213}
214
215int dpbp_set_irq_enable(struct fsl_mc_io *mc_io,
216			uint16_t token,
217			uint8_t irq_index,
218			uint8_t en)
219{
220	struct mc_command cmd = { 0 };
221
222	/* prepare command */
223	cmd.header = mc_encode_cmd_header(DPBP_CMDID_SET_IRQ_ENABLE,
224					  MC_CMD_PRI_LOW, token);
225	cmd.params[0] |= mc_enc(0, 8, en);
226	cmd.params[0] |= mc_enc(32, 8, irq_index);
227
228	/* send command to mc*/
229	return mc_send_command(mc_io, &cmd);
230}
231
232int dpbp_get_irq_enable(struct fsl_mc_io *mc_io,
233			uint16_t token,
234			uint8_t irq_index,
235			uint8_t *en)
236{
237	struct mc_command cmd = { 0 };
238	int err;
239
240	/* prepare command */
241	cmd.header = mc_encode_cmd_header(DPBP_CMDID_GET_IRQ_ENABLE,
242					  MC_CMD_PRI_LOW, token);
243	cmd.params[0] |= mc_enc(32, 8, irq_index);
244
245	/* send command to mc*/
246	err = mc_send_command(mc_io, &cmd);
247	if (err)
248		return err;
249
250	/* retrieve response parameters */
251	*en = (uint8_t)mc_dec(cmd.params[0], 0, 8);
252	return 0;
253}
254
255int dpbp_set_irq_mask(struct fsl_mc_io *mc_io,
256		      uint16_t token,
257		      uint8_t irq_index,
258		      uint32_t mask)
259{
260	struct mc_command cmd = { 0 };
261
262	/* prepare command */
263	cmd.header = mc_encode_cmd_header(DPBP_CMDID_SET_IRQ_MASK,
264					  MC_CMD_PRI_LOW, token);
265	cmd.params[0] |= mc_enc(0, 32, mask);
266	cmd.params[0] |= mc_enc(32, 8, irq_index);
267
268	/* send command to mc*/
269	return mc_send_command(mc_io, &cmd);
270}
271
272int dpbp_get_irq_mask(struct fsl_mc_io *mc_io,
273		      uint16_t token,
274		      uint8_t irq_index,
275		      uint32_t *mask)
276{
277	struct mc_command cmd = { 0 };
278	int err;
279
280	/* prepare command */
281	cmd.header = mc_encode_cmd_header(DPBP_CMDID_GET_IRQ_MASK,
282					  MC_CMD_PRI_LOW, token);
283	cmd.params[0] |= mc_enc(32, 8, irq_index);
284
285	/* send command to mc*/
286	err = mc_send_command(mc_io, &cmd);
287	if (err)
288		return err;
289
290	/* retrieve response parameters */
291	*mask = (uint32_t)mc_dec(cmd.params[0], 0, 32);
292	return 0;
293}
294
295int dpbp_get_irq_status(struct fsl_mc_io *mc_io,
296			uint16_t token,
297			uint8_t irq_index,
298			uint32_t *status)
299{
300	struct mc_command cmd = { 0 };
301	int err;
302
303	/* prepare command */
304	cmd.header = mc_encode_cmd_header(DPBP_CMDID_GET_IRQ_STATUS,
305					  MC_CMD_PRI_LOW, token);
306	cmd.params[0] |= mc_enc(32, 8, irq_index);
307
308	/* send command to mc*/
309	err = mc_send_command(mc_io, &cmd);
310	if (err)
311		return err;
312
313	/* retrieve response parameters */
314	*status = (uint32_t)mc_dec(cmd.params[0], 0, 32);
315	return 0;
316}
317
318int dpbp_clear_irq_status(struct fsl_mc_io *mc_io,
319			  uint16_t token,
320			  uint8_t irq_index,
321			  uint32_t status)
322{
323	struct mc_command cmd = { 0 };
324
325	/* prepare command */
326	cmd.header = mc_encode_cmd_header(DPBP_CMDID_CLEAR_IRQ_STATUS,
327					  MC_CMD_PRI_LOW, token);
328	cmd.params[0] |= mc_enc(0, 32, status);
329	cmd.params[0] |= mc_enc(32, 8, irq_index);
330
331	/* send command to mc*/
332	return mc_send_command(mc_io, &cmd);
333}
334
335int dpbp_get_attributes(struct fsl_mc_io *mc_io,
336			uint16_t token,
337			struct dpbp_attr *attr)
338{
339	struct mc_command cmd = { 0 };
340	int err;
341
342	/* prepare command */
343	cmd.header = mc_encode_cmd_header(DPBP_CMDID_GET_ATTR,
344					  MC_CMD_PRI_LOW, token);
345
346	/* send command to mc*/
347	err = mc_send_command(mc_io, &cmd);
348	if (err)
349		return err;
350
351	/* retrieve response parameters */
352	attr->bpid = (uint16_t)mc_dec(cmd.params[0], 16, 16);
353	attr->id = (int)mc_dec(cmd.params[0], 32, 32);
354	attr->version.major = (uint16_t)mc_dec(cmd.params[1], 0, 16);
355	attr->version.minor = (uint16_t)mc_dec(cmd.params[1], 16, 16);
356	return 0;
357}
358EXPORT_SYMBOL(dpbp_get_attributes);
359