1 /**
2  * @file
3  *
4  * @brief Public APIs for MDIO drivers.
5  */
6 
7 /*
8  * Copyright (c) 2021 IP-Logix Inc.
9  * Copyright 2023 NXP
10  *
11  * SPDX-License-Identifier: Apache-2.0
12  */
13 #ifndef ZEPHYR_INCLUDE_DRIVERS_MDIO_H_
14 #define ZEPHYR_INCLUDE_DRIVERS_MDIO_H_
15 
16 /**
17  * @brief MDIO Interface
18  * @defgroup mdio_interface MDIO Interface
19  * @ingroup io_interfaces
20  * @{
21  */
22 #include <zephyr/types.h>
23 #include <zephyr/device.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /**
30  * @cond INTERNAL_HIDDEN
31  *
32  * These are for internal use only, so skip these in
33  * public documentation.
34  */
35 __subsystem struct mdio_driver_api {
36 	/** Enable the MDIO bus device */
37 	void (*bus_enable)(const struct device *dev);
38 
39 	/** Disable the MDIO bus device */
40 	void (*bus_disable)(const struct device *dev);
41 
42 	/** Read data from MDIO bus */
43 	int (*read)(const struct device *dev, uint8_t prtad, uint8_t regad,
44 		    uint16_t *data);
45 
46 	/** Write data to MDIO bus */
47 	int (*write)(const struct device *dev, uint8_t prtad, uint8_t regad,
48 		     uint16_t data);
49 
50 	/** Read data from MDIO bus using Clause 45 access */
51 	int (*read_c45)(const struct device *dev, uint8_t prtad, uint8_t devad,
52 			uint16_t regad, uint16_t *data);
53 
54 	/** Write data to MDIO bus using Clause 45 access */
55 	int (*write_c45)(const struct device *dev, uint8_t prtad, uint8_t devad,
56 			 uint16_t regad, uint16_t data);
57 };
58 /**
59  * @endcond
60  */
61 
62 /**
63  * @brief      Enable MDIO bus
64  *
65  * @param[in]  dev   Pointer to the device structure for the controller
66  *
67  */
68 __syscall void mdio_bus_enable(const struct device *dev);
69 
z_impl_mdio_bus_enable(const struct device * dev)70 static inline void z_impl_mdio_bus_enable(const struct device *dev)
71 {
72 	const struct mdio_driver_api *api =
73 		(const struct mdio_driver_api *)dev->api;
74 
75 	return api->bus_enable(dev);
76 }
77 
78 /**
79  * @brief      Disable MDIO bus and tri-state drivers
80  *
81  * @param[in]  dev   Pointer to the device structure for the controller
82  *
83  */
84 __syscall void mdio_bus_disable(const struct device *dev);
85 
z_impl_mdio_bus_disable(const struct device * dev)86 static inline void z_impl_mdio_bus_disable(const struct device *dev)
87 {
88 	const struct mdio_driver_api *api =
89 		(const struct mdio_driver_api *)dev->api;
90 
91 	return api->bus_disable(dev);
92 }
93 
94 /**
95  * @brief      Read from MDIO Bus
96  *
97  * This routine provides a generic interface to perform a read on the
98  * MDIO bus.
99  *
100  * @param[in]  dev         Pointer to the device structure for the controller
101  * @param[in]  prtad       Port address
102  * @param[in]  regad       Register address
103  * @param      data        Pointer to receive read data
104  *
105  * @retval 0 If successful.
106  * @retval -EIO General input / output error.
107  * @retval -ETIMEDOUT If transaction timedout on the bus
108  * @retval -ENOSYS if read is not supported
109  */
110 __syscall int mdio_read(const struct device *dev, uint8_t prtad, uint8_t regad,
111 			uint16_t *data);
112 
z_impl_mdio_read(const struct device * dev,uint8_t prtad,uint8_t regad,uint16_t * data)113 static inline int z_impl_mdio_read(const struct device *dev, uint8_t prtad,
114 				   uint8_t regad, uint16_t *data)
115 {
116 	const struct mdio_driver_api *api =
117 		(const struct mdio_driver_api *)dev->api;
118 
119 	if (api->read == NULL) {
120 		return -ENOSYS;
121 	}
122 
123 	return api->read(dev, prtad, regad, data);
124 }
125 
126 
127 /**
128  * @brief      Write to MDIO bus
129  *
130  * This routine provides a generic interface to perform a write on the
131  * MDIO bus.
132  *
133  * @param[in]  dev         Pointer to the device structure for the controller
134  * @param[in]  prtad       Port address
135  * @param[in]  regad       Register address
136  * @param[in]  data        Data to write
137  *
138  * @retval 0 If successful.
139  * @retval -EIO General input / output error.
140  * @retval -ETIMEDOUT If transaction timedout on the bus
141  * @retval -ENOSYS if write is not supported
142  */
143 __syscall int mdio_write(const struct device *dev, uint8_t prtad, uint8_t regad,
144 			 uint16_t data);
145 
z_impl_mdio_write(const struct device * dev,uint8_t prtad,uint8_t regad,uint16_t data)146 static inline int z_impl_mdio_write(const struct device *dev, uint8_t prtad,
147 				    uint8_t regad, uint16_t data)
148 {
149 	const struct mdio_driver_api *api =
150 		(const struct mdio_driver_api *)dev->api;
151 
152 	if (api->write == NULL) {
153 		return -ENOSYS;
154 	}
155 
156 	return api->write(dev, prtad, regad, data);
157 }
158 
159 /**
160  * @brief      Read from MDIO Bus using Clause 45 access
161  *
162  * This routine provides an interface to perform a read on the MDIO bus using
163  * IEEE 802.3 Clause 45 access.
164  *
165  * @param[in]  dev         Pointer to the device structure for the controller
166  * @param[in]  prtad       Port address
167  * @param[in]  devad       Device address
168  * @param[in]  regad       Register address
169  * @param      data        Pointer to receive read data
170  *
171  * @retval 0 If successful.
172  * @retval -EIO General input / output error.
173  * @retval -ETIMEDOUT If transaction timedout on the bus
174  * @retval -ENOSYS if write using Clause 45 access is not supported
175  */
176 __syscall int mdio_read_c45(const struct device *dev, uint8_t prtad,
177 			    uint8_t devad, uint16_t regad, uint16_t *data);
178 
z_impl_mdio_read_c45(const struct device * dev,uint8_t prtad,uint8_t devad,uint16_t regad,uint16_t * data)179 static inline int z_impl_mdio_read_c45(const struct device *dev, uint8_t prtad,
180 				       uint8_t devad, uint16_t regad,
181 				       uint16_t *data)
182 {
183 	const struct mdio_driver_api *api =
184 		(const struct mdio_driver_api *)dev->api;
185 
186 	if (api->read_c45 == NULL) {
187 		return -ENOSYS;
188 	}
189 
190 	return api->read_c45(dev, prtad, devad, regad, data);
191 }
192 
193 /**
194  * @brief      Write to MDIO bus using Clause 45 access
195  *
196  * This routine provides an interface to perform a write on the MDIO bus using
197  * IEEE 802.3 Clause 45 access.
198  *
199  * @param[in]  dev         Pointer to the device structure for the controller
200  * @param[in]  prtad       Port address
201  * @param[in]  devad       Device address
202  * @param[in]  regad       Register address
203  * @param[in]  data        Data to write
204  *
205  * @retval 0 If successful.
206  * @retval -EIO General input / output error.
207  * @retval -ETIMEDOUT If transaction timedout on the bus
208  * @retval -ENOSYS if write using Clause 45 access is not supported
209  */
210 __syscall int mdio_write_c45(const struct device *dev, uint8_t prtad,
211 			     uint8_t devad, uint16_t regad, uint16_t data);
212 
z_impl_mdio_write_c45(const struct device * dev,uint8_t prtad,uint8_t devad,uint16_t regad,uint16_t data)213 static inline int z_impl_mdio_write_c45(const struct device *dev, uint8_t prtad,
214 					uint8_t devad, uint16_t regad,
215 					uint16_t data)
216 {
217 	const struct mdio_driver_api *api =
218 		(const struct mdio_driver_api *)dev->api;
219 
220 	if (api->write_c45 == NULL) {
221 		return -ENOSYS;
222 	}
223 
224 	return api->write_c45(dev, prtad, devad, regad, data);
225 }
226 
227 #ifdef __cplusplus
228 }
229 #endif
230 
231 /**
232  * @}
233  */
234 
235 #include <syscalls/mdio.h>
236 
237 #endif /* ZEPHYR_INCLUDE_DRIVERS_MDIO_H_ */
238