xref: /openbmc/u-boot/drivers/net/fsl-mc/dpmac.c (revision 193f6fb9)
1 /*
2  * Freescale Layerscape MC I/O wrapper
3  *
4  * Copyright (C) 2015-2016 Freescale Semiconductor, Inc.
5  * Copyright 2017 NXP
6  * Author: Prabhakar Kushwaha <prabhakar@freescale.com>
7  *
8  * SPDX-License-Identifier:	GPL-2.0+
9  */
10 
11 #include <fsl-mc/fsl_mc_sys.h>
12 #include <fsl-mc/fsl_mc_cmd.h>
13 #include <fsl-mc/fsl_dpmac.h>
14 
15 int dpmac_open(struct fsl_mc_io *mc_io,
16 	       uint32_t cmd_flags,
17 	       int dpmac_id,
18 	       uint16_t *token)
19 {
20 	struct mc_command cmd = { 0 };
21 	int err;
22 
23 	/* prepare command */
24 	cmd.header = mc_encode_cmd_header(DPMAC_CMDID_OPEN,
25 					  cmd_flags,
26 					  0);
27 	DPMAC_CMD_OPEN(cmd, dpmac_id);
28 
29 	/* send command to mc*/
30 	err = mc_send_command(mc_io, &cmd);
31 	if (err)
32 		return err;
33 
34 	/* retrieve response parameters */
35 	*token = MC_CMD_HDR_READ_TOKEN(cmd.header);
36 
37 	return err;
38 }
39 
40 int dpmac_close(struct fsl_mc_io *mc_io,
41 		uint32_t cmd_flags,
42 		uint16_t token)
43 {
44 	struct mc_command cmd = { 0 };
45 
46 	/* prepare command */
47 	cmd.header = mc_encode_cmd_header(DPMAC_CMDID_CLOSE, cmd_flags,
48 					  token);
49 
50 	/* send command to mc*/
51 	return mc_send_command(mc_io, &cmd);
52 }
53 
54 int dpmac_create(struct fsl_mc_io *mc_io,
55 		 uint16_t dprc_token,
56 		 uint32_t cmd_flags,
57 		 const struct dpmac_cfg *cfg,
58 		 uint32_t *obj_id)
59 {
60 	struct mc_command cmd = { 0 };
61 	int err;
62 
63 	/* prepare command */
64 	cmd.header = mc_encode_cmd_header(DPMAC_CMDID_CREATE,
65 					  cmd_flags,
66 					  dprc_token);
67 	DPMAC_CMD_CREATE(cmd, cfg);
68 
69 	/* send command to mc*/
70 	err = mc_send_command(mc_io, &cmd);
71 	if (err)
72 		return err;
73 
74 	/* retrieve response parameters */
75 	MC_CMD_READ_OBJ_ID(cmd, *obj_id);
76 
77 	return 0;
78 }
79 
80 int dpmac_destroy(struct fsl_mc_io *mc_io,
81 		  uint16_t dprc_token,
82 		  uint32_t cmd_flags,
83 		  uint32_t obj_id)
84 {
85 	struct mc_command cmd = { 0 };
86 
87 	/* prepare command */
88 	cmd.header = mc_encode_cmd_header(DPMAC_CMDID_DESTROY,
89 					  cmd_flags,
90 					  dprc_token);
91 
92 	/* set object id to destroy */
93 	CMD_DESTROY_SET_OBJ_ID_PARAM0(cmd, obj_id);
94 
95 	/* send command to mc*/
96 	return mc_send_command(mc_io, &cmd);
97 }
98 
99 int dpmac_get_attributes(struct fsl_mc_io *mc_io,
100 			 uint32_t cmd_flags,
101 			 uint16_t token,
102 			 struct dpmac_attr *attr)
103 {
104 	struct mc_command cmd = { 0 };
105 	int err;
106 
107 	/* prepare command */
108 	cmd.header = mc_encode_cmd_header(DPMAC_CMDID_GET_ATTR,
109 					  cmd_flags,
110 					  token);
111 
112 	/* send command to mc*/
113 	err = mc_send_command(mc_io, &cmd);
114 	if (err)
115 		return err;
116 
117 	/* retrieve response parameters */
118 	DPMAC_RSP_GET_ATTRIBUTES(cmd, attr);
119 
120 	return 0;
121 }
122 
123 int dpmac_mdio_read(struct fsl_mc_io *mc_io,
124 		    uint32_t cmd_flags,
125 		    uint16_t token,
126 		    struct dpmac_mdio_cfg *cfg)
127 {
128 	struct mc_command cmd = { 0 };
129 	int err;
130 
131 	/* prepare command */
132 	cmd.header = mc_encode_cmd_header(DPMAC_CMDID_MDIO_READ,
133 					  cmd_flags,
134 					  token);
135 	DPMAC_CMD_MDIO_READ(cmd, cfg);
136 
137 	/* send command to mc*/
138 	err = mc_send_command(mc_io, &cmd);
139 	if (err)
140 		return err;
141 
142 	/* retrieve response parameters */
143 	DPMAC_RSP_MDIO_READ(cmd, cfg->data);
144 
145 	return 0;
146 }
147 
148 int dpmac_mdio_write(struct fsl_mc_io *mc_io,
149 		     uint32_t cmd_flags,
150 		     uint16_t token,
151 		     struct dpmac_mdio_cfg *cfg)
152 {
153 	struct mc_command cmd = { 0 };
154 
155 	/* prepare command */
156 	cmd.header = mc_encode_cmd_header(DPMAC_CMDID_MDIO_WRITE,
157 					  cmd_flags,
158 					  token);
159 	DPMAC_CMD_MDIO_WRITE(cmd, cfg);
160 
161 	/* send command to mc*/
162 	return mc_send_command(mc_io, &cmd);
163 }
164 
165 int dpmac_get_link_cfg(struct fsl_mc_io *mc_io,
166 		       uint32_t cmd_flags,
167 		       uint16_t token,
168 		       struct dpmac_link_cfg *cfg)
169 {
170 	struct mc_command cmd = { 0 };
171 	int err = 0;
172 
173 	/* prepare command */
174 	cmd.header = mc_encode_cmd_header(DPMAC_CMDID_GET_LINK_CFG,
175 					  cmd_flags,
176 					  token);
177 
178 	/* send command to mc*/
179 	err = mc_send_command(mc_io, &cmd);
180 	if (err)
181 		return err;
182 
183 	DPMAC_RSP_GET_LINK_CFG(cmd, cfg);
184 
185 	return 0;
186 }
187 
188 int dpmac_set_link_state(struct fsl_mc_io *mc_io,
189 			 uint32_t cmd_flags,
190 			 uint16_t token,
191 			 struct dpmac_link_state *link_state)
192 {
193 	struct mc_command cmd = { 0 };
194 
195 	/* prepare command */
196 	cmd.header = mc_encode_cmd_header(DPMAC_CMDID_SET_LINK_STATE,
197 					  cmd_flags,
198 					  token);
199 	DPMAC_CMD_SET_LINK_STATE(cmd, link_state);
200 
201 	/* send command to mc*/
202 	return mc_send_command(mc_io, &cmd);
203 }
204 
205 int dpmac_get_counter(struct fsl_mc_io *mc_io,
206 		      uint32_t cmd_flags,
207 		      uint16_t token,
208 		      enum dpmac_counter type,
209 		      uint64_t *counter)
210 {
211 	struct mc_command cmd = { 0 };
212 	int err = 0;
213 
214 	/* prepare command */
215 	cmd.header = mc_encode_cmd_header(DPMAC_CMDID_GET_COUNTER,
216 					  cmd_flags,
217 					  token);
218 	DPMAC_CMD_GET_COUNTER(cmd, type);
219 
220 	/* send command to mc*/
221 	err = mc_send_command(mc_io, &cmd);
222 	if (err)
223 		return err;
224 
225 	DPMAC_RSP_GET_COUNTER(cmd, *counter);
226 
227 	return 0;
228 }
229 
230 int dpmac_get_api_version(struct fsl_mc_io *mc_io,
231 			 u32 cmd_flags,
232 			 u16 *major_ver,
233 			 u16 *minor_ver)
234 {
235 	struct mc_command cmd = { 0 };
236 	int err;
237 
238 	/* prepare command */
239 	cmd.header = mc_encode_cmd_header(DPMAC_CMDID_GET_API_VERSION,
240 					  cmd_flags, 0);
241 
242 	/* send command to mc */
243 	err = mc_send_command(mc_io, &cmd);
244 	if (err)
245 		return err;
246 
247 	/* retrieve response parameters */
248 	mc_cmd_read_api_version(&cmd, major_ver, minor_ver);
249 
250 	return 0;
251 }
252