xref: /openbmc/linux/drivers/soc/fsl/dpio/dpio.c (revision 2cf0b6fe)
1c89105c9SRoy Pledge // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2c89105c9SRoy Pledge /*
3c89105c9SRoy Pledge  * Copyright 2013-2016 Freescale Semiconductor Inc.
4c89105c9SRoy Pledge  * Copyright 2016 NXP
5c89105c9SRoy Pledge  *
6c89105c9SRoy Pledge  */
7c89105c9SRoy Pledge #include <linux/kernel.h>
8c89105c9SRoy Pledge #include <linux/fsl/mc.h>
9c89105c9SRoy Pledge 
10c89105c9SRoy Pledge #include "dpio.h"
11c89105c9SRoy Pledge #include "dpio-cmd.h"
12c89105c9SRoy Pledge 
13c89105c9SRoy Pledge /*
14c89105c9SRoy Pledge  * Data Path I/O Portal API
15c89105c9SRoy Pledge  * Contains initialization APIs and runtime control APIs for DPIO
16c89105c9SRoy Pledge  */
17c89105c9SRoy Pledge 
18c89105c9SRoy Pledge /**
19c89105c9SRoy Pledge  * dpio_open() - Open a control session for the specified object
20c89105c9SRoy Pledge  * @mc_io:	Pointer to MC portal's I/O object
21c89105c9SRoy Pledge  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
22c89105c9SRoy Pledge  * @dpio_id:	DPIO unique ID
23c89105c9SRoy Pledge  * @token:	Returned token; use in subsequent API calls
24c89105c9SRoy Pledge  *
25c89105c9SRoy Pledge  * This function can be used to open a control session for an
26c89105c9SRoy Pledge  * already created object; an object may have been declared in
27c89105c9SRoy Pledge  * the DPL or by calling the dpio_create() function.
28c89105c9SRoy Pledge  * This function returns a unique authentication token,
29c89105c9SRoy Pledge  * associated with the specific object ID and the specific MC
30c89105c9SRoy Pledge  * portal; this token must be used in all subsequent commands for
31c89105c9SRoy Pledge  * this specific object.
32c89105c9SRoy Pledge  *
33c89105c9SRoy Pledge  * Return:	'0' on Success; Error code otherwise.
34c89105c9SRoy Pledge  */
dpio_open(struct fsl_mc_io * mc_io,u32 cmd_flags,int dpio_id,u16 * token)35c89105c9SRoy Pledge int dpio_open(struct fsl_mc_io *mc_io,
36c89105c9SRoy Pledge 	      u32 cmd_flags,
37c89105c9SRoy Pledge 	      int dpio_id,
38c89105c9SRoy Pledge 	      u16 *token)
39c89105c9SRoy Pledge {
40c89105c9SRoy Pledge 	struct fsl_mc_command cmd = { 0 };
41c89105c9SRoy Pledge 	struct dpio_cmd_open *dpio_cmd;
42c89105c9SRoy Pledge 	int err;
43c89105c9SRoy Pledge 
44c89105c9SRoy Pledge 	/* prepare command */
45c89105c9SRoy Pledge 	cmd.header = mc_encode_cmd_header(DPIO_CMDID_OPEN,
46c89105c9SRoy Pledge 					  cmd_flags,
47c89105c9SRoy Pledge 					  0);
48c89105c9SRoy Pledge 	dpio_cmd = (struct dpio_cmd_open *)cmd.params;
49c89105c9SRoy Pledge 	dpio_cmd->dpio_id = cpu_to_le32(dpio_id);
50c89105c9SRoy Pledge 
51c89105c9SRoy Pledge 	err = mc_send_command(mc_io, &cmd);
52c89105c9SRoy Pledge 	if (err)
53c89105c9SRoy Pledge 		return err;
54c89105c9SRoy Pledge 
55c89105c9SRoy Pledge 	/* retrieve response parameters */
56c89105c9SRoy Pledge 	*token = mc_cmd_hdr_read_token(&cmd);
57c89105c9SRoy Pledge 
58c89105c9SRoy Pledge 	return 0;
59c89105c9SRoy Pledge }
60c89105c9SRoy Pledge 
61c89105c9SRoy Pledge /**
62c89105c9SRoy Pledge  * dpio_close() - Close the control session of the object
63c89105c9SRoy Pledge  * @mc_io:	Pointer to MC portal's I/O object
64c89105c9SRoy Pledge  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
65c89105c9SRoy Pledge  * @token:	Token of DPIO object
66c89105c9SRoy Pledge  *
67c89105c9SRoy Pledge  * Return:	'0' on Success; Error code otherwise.
68c89105c9SRoy Pledge  */
dpio_close(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token)69c89105c9SRoy Pledge int dpio_close(struct fsl_mc_io *mc_io,
70c89105c9SRoy Pledge 	       u32 cmd_flags,
71c89105c9SRoy Pledge 	       u16 token)
72c89105c9SRoy Pledge {
73c89105c9SRoy Pledge 	struct fsl_mc_command cmd = { 0 };
74c89105c9SRoy Pledge 
75c89105c9SRoy Pledge 	/* prepare command */
76c89105c9SRoy Pledge 	cmd.header = mc_encode_cmd_header(DPIO_CMDID_CLOSE,
77c89105c9SRoy Pledge 					  cmd_flags,
78c89105c9SRoy Pledge 					  token);
79c89105c9SRoy Pledge 
80c89105c9SRoy Pledge 	return mc_send_command(mc_io, &cmd);
81c89105c9SRoy Pledge }
82c89105c9SRoy Pledge 
83c89105c9SRoy Pledge /**
84c89105c9SRoy Pledge  * dpio_enable() - Enable the DPIO, allow I/O portal operations.
85c89105c9SRoy Pledge  * @mc_io:	Pointer to MC portal's I/O object
86c89105c9SRoy Pledge  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
87c89105c9SRoy Pledge  * @token:	Token of DPIO object
88c89105c9SRoy Pledge  *
89c89105c9SRoy Pledge  * Return:	'0' on Success; Error code otherwise
90c89105c9SRoy Pledge  */
dpio_enable(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token)91c89105c9SRoy Pledge int dpio_enable(struct fsl_mc_io *mc_io,
92c89105c9SRoy Pledge 		u32 cmd_flags,
93c89105c9SRoy Pledge 		u16 token)
94c89105c9SRoy Pledge {
95c89105c9SRoy Pledge 	struct fsl_mc_command cmd = { 0 };
96c89105c9SRoy Pledge 
97c89105c9SRoy Pledge 	/* prepare command */
98c89105c9SRoy Pledge 	cmd.header = mc_encode_cmd_header(DPIO_CMDID_ENABLE,
99c89105c9SRoy Pledge 					  cmd_flags,
100c89105c9SRoy Pledge 					  token);
101c89105c9SRoy Pledge 
102c89105c9SRoy Pledge 	return mc_send_command(mc_io, &cmd);
103c89105c9SRoy Pledge }
104c89105c9SRoy Pledge 
105c89105c9SRoy Pledge /**
106c89105c9SRoy Pledge  * dpio_disable() - Disable the DPIO, stop any I/O portal operation.
107c89105c9SRoy Pledge  * @mc_io:	Pointer to MC portal's I/O object
108c89105c9SRoy Pledge  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
109c89105c9SRoy Pledge  * @token:	Token of DPIO object
110c89105c9SRoy Pledge  *
111c89105c9SRoy Pledge  * Return:	'0' on Success; Error code otherwise
112c89105c9SRoy Pledge  */
dpio_disable(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token)113c89105c9SRoy Pledge int dpio_disable(struct fsl_mc_io *mc_io,
114c89105c9SRoy Pledge 		 u32 cmd_flags,
115c89105c9SRoy Pledge 		 u16 token)
116c89105c9SRoy Pledge {
117c89105c9SRoy Pledge 	struct fsl_mc_command cmd = { 0 };
118c89105c9SRoy Pledge 
119c89105c9SRoy Pledge 	/* prepare command */
120c89105c9SRoy Pledge 	cmd.header = mc_encode_cmd_header(DPIO_CMDID_DISABLE,
121c89105c9SRoy Pledge 					  cmd_flags,
122c89105c9SRoy Pledge 					  token);
123c89105c9SRoy Pledge 
124c89105c9SRoy Pledge 	return mc_send_command(mc_io, &cmd);
125c89105c9SRoy Pledge }
126c89105c9SRoy Pledge 
127c89105c9SRoy Pledge /**
128c89105c9SRoy Pledge  * dpio_get_attributes() - Retrieve DPIO attributes
129c89105c9SRoy Pledge  * @mc_io:	Pointer to MC portal's I/O object
130c89105c9SRoy Pledge  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
131c89105c9SRoy Pledge  * @token:	Token of DPIO object
132c89105c9SRoy Pledge  * @attr:	Returned object's attributes
133c89105c9SRoy Pledge  *
134c89105c9SRoy Pledge  * Return:	'0' on Success; Error code otherwise
135c89105c9SRoy Pledge  */
dpio_get_attributes(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,struct dpio_attr * attr)136c89105c9SRoy Pledge int dpio_get_attributes(struct fsl_mc_io *mc_io,
137c89105c9SRoy Pledge 			u32 cmd_flags,
138c89105c9SRoy Pledge 			u16 token,
139c89105c9SRoy Pledge 			struct dpio_attr *attr)
140c89105c9SRoy Pledge {
141c89105c9SRoy Pledge 	struct fsl_mc_command cmd = { 0 };
142c89105c9SRoy Pledge 	struct dpio_rsp_get_attr *dpio_rsp;
143c89105c9SRoy Pledge 	int err;
144c89105c9SRoy Pledge 
145c89105c9SRoy Pledge 	/* prepare command */
146c89105c9SRoy Pledge 	cmd.header = mc_encode_cmd_header(DPIO_CMDID_GET_ATTR,
147c89105c9SRoy Pledge 					  cmd_flags,
148c89105c9SRoy Pledge 					  token);
149c89105c9SRoy Pledge 
150c89105c9SRoy Pledge 	err = mc_send_command(mc_io, &cmd);
151c89105c9SRoy Pledge 	if (err)
152c89105c9SRoy Pledge 		return err;
153c89105c9SRoy Pledge 
154c89105c9SRoy Pledge 	/* retrieve response parameters */
155c89105c9SRoy Pledge 	dpio_rsp = (struct dpio_rsp_get_attr *)cmd.params;
156c89105c9SRoy Pledge 	attr->id = le32_to_cpu(dpio_rsp->id);
157c89105c9SRoy Pledge 	attr->qbman_portal_id = le16_to_cpu(dpio_rsp->qbman_portal_id);
158c89105c9SRoy Pledge 	attr->num_priorities = dpio_rsp->num_priorities;
159c89105c9SRoy Pledge 	attr->channel_mode = dpio_rsp->channel_mode & DPIO_CHANNEL_MODE_MASK;
160c89105c9SRoy Pledge 	attr->qbman_portal_ce_offset =
161c89105c9SRoy Pledge 		le64_to_cpu(dpio_rsp->qbman_portal_ce_addr);
162c89105c9SRoy Pledge 	attr->qbman_portal_ci_offset =
163c89105c9SRoy Pledge 		le64_to_cpu(dpio_rsp->qbman_portal_ci_addr);
164c89105c9SRoy Pledge 	attr->qbman_version = le32_to_cpu(dpio_rsp->qbman_version);
165*2cf0b6feSIoana Ciornei 	attr->clk = le32_to_cpu(dpio_rsp->clk);
166c89105c9SRoy Pledge 
167c89105c9SRoy Pledge 	return 0;
168c89105c9SRoy Pledge }
169c89105c9SRoy Pledge 
dpio_set_stashing_destination(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,u8 sdest)17051da14e9SIoana Ciornei int dpio_set_stashing_destination(struct fsl_mc_io *mc_io,
17151da14e9SIoana Ciornei 				  u32 cmd_flags,
17251da14e9SIoana Ciornei 				  u16 token,
17351da14e9SIoana Ciornei 				  u8 sdest)
17451da14e9SIoana Ciornei {
17551da14e9SIoana Ciornei 	struct fsl_mc_command cmd = { 0 };
17651da14e9SIoana Ciornei 	struct dpio_stashing_dest *dpio_cmd;
17751da14e9SIoana Ciornei 
17851da14e9SIoana Ciornei 	cmd.header = mc_encode_cmd_header(DPIO_CMDID_SET_STASHING_DEST,
17951da14e9SIoana Ciornei 					  cmd_flags, token);
18051da14e9SIoana Ciornei 	dpio_cmd = (struct dpio_stashing_dest *)cmd.params;
18151da14e9SIoana Ciornei 	dpio_cmd->sdest = sdest;
18251da14e9SIoana Ciornei 
18351da14e9SIoana Ciornei 	return mc_send_command(mc_io, &cmd);
18451da14e9SIoana Ciornei }
18551da14e9SIoana Ciornei 
186c89105c9SRoy Pledge /**
187c89105c9SRoy Pledge  * dpio_get_api_version - Get Data Path I/O API version
188c89105c9SRoy Pledge  * @mc_io:	Pointer to MC portal's DPIO object
189c89105c9SRoy Pledge  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
190c89105c9SRoy Pledge  * @major_ver:	Major version of DPIO API
191c89105c9SRoy Pledge  * @minor_ver:	Minor version of DPIO API
192c89105c9SRoy Pledge  *
193c89105c9SRoy Pledge  * Return:	'0' on Success; Error code otherwise
194c89105c9SRoy Pledge  */
dpio_get_api_version(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 * major_ver,u16 * minor_ver)195c89105c9SRoy Pledge int dpio_get_api_version(struct fsl_mc_io *mc_io,
196c89105c9SRoy Pledge 			 u32 cmd_flags,
197c89105c9SRoy Pledge 			 u16 *major_ver,
198c89105c9SRoy Pledge 			 u16 *minor_ver)
199c89105c9SRoy Pledge {
200c89105c9SRoy Pledge 	struct fsl_mc_command cmd = { 0 };
201c89105c9SRoy Pledge 	int err;
202c89105c9SRoy Pledge 
203c89105c9SRoy Pledge 	/* prepare command */
204c89105c9SRoy Pledge 	cmd.header = mc_encode_cmd_header(DPIO_CMDID_GET_API_VERSION,
205c89105c9SRoy Pledge 					  cmd_flags, 0);
206c89105c9SRoy Pledge 
207c89105c9SRoy Pledge 	err = mc_send_command(mc_io, &cmd);
208c89105c9SRoy Pledge 	if (err)
209c89105c9SRoy Pledge 		return err;
210c89105c9SRoy Pledge 
211c89105c9SRoy Pledge 	/* retrieve response parameters */
212c89105c9SRoy Pledge 	mc_cmd_read_api_version(&cmd, major_ver, minor_ver);
213c89105c9SRoy Pledge 
214c89105c9SRoy Pledge 	return 0;
215c89105c9SRoy Pledge }
21611c8bac9SRoy Pledge 
21711c8bac9SRoy Pledge /**
21811c8bac9SRoy Pledge  * dpio_reset() - Reset the DPIO, returns the object to initial state.
21911c8bac9SRoy Pledge  * @mc_io:	Pointer to MC portal's I/O object
22011c8bac9SRoy Pledge  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
22111c8bac9SRoy Pledge  * @token:	Token of DPIO object
22211c8bac9SRoy Pledge  *
22311c8bac9SRoy Pledge  * Return:	'0' on Success; Error code otherwise.
22411c8bac9SRoy Pledge  */
dpio_reset(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token)22511c8bac9SRoy Pledge int dpio_reset(struct fsl_mc_io *mc_io,
22611c8bac9SRoy Pledge 	       u32 cmd_flags,
22711c8bac9SRoy Pledge 	       u16 token)
22811c8bac9SRoy Pledge {
22911c8bac9SRoy Pledge 	struct fsl_mc_command cmd = { 0 };
23011c8bac9SRoy Pledge 
23111c8bac9SRoy Pledge 	/* prepare command */
23211c8bac9SRoy Pledge 	cmd.header = mc_encode_cmd_header(DPIO_CMDID_RESET,
23311c8bac9SRoy Pledge 					  cmd_flags,
23411c8bac9SRoy Pledge 					  token);
23511c8bac9SRoy Pledge 
23611c8bac9SRoy Pledge 	/* send command to mc*/
23711c8bac9SRoy Pledge 	return mc_send_command(mc_io, &cmd);
23811c8bac9SRoy Pledge }
239