Skip to content

Commit 419b103

Browse files
arvinfnashif
authored andcommitted
drivers: Add mdio API
This commit adds support for MDIO bus. The bus is used by Ethernet MACs to communicate with PHYs. Signed-off-by: Arvin Farahmand <[email protected]>
1 parent f0e1823 commit 419b103

File tree

9 files changed

+243
-0
lines changed

9 files changed

+243
-0
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
/drivers/ethernet/*stm32* @Nukersson @lochej
223223
/drivers/ethernet/*w5500* @parthitce
224224
/drivers/ethernet/*xlnx_gem* @ibirnbaum
225+
/drivers/mdio/ @rlubos @tbursztyka @arvinf
225226
/drivers/flash/ @nashif @nvlsianpu
226227
/drivers/flash/*b91* @yurvyn
227228
/drivers/flash/*nrf* @nvlsianpu

doc/reference/peripherals/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Peripherals
3232
sensor.rst
3333
spi.rst
3434
uart.rst
35+
mdio.rst
3536
watchdog.rst
3637
video.rst
3738
espi.rst

doc/reference/peripherals/mdio.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.. _mdio_api:
2+
3+
MDIO
4+
####
5+
6+
Overview
7+
********
8+
9+
MDIO is a bus that is commonly used to communicate with ethernet PHY devices.
10+
Many ethernet MAC controllers also provide hardware to communicate over MDIO
11+
bus with a peripheral device.
12+
13+
This API is intended to be used primarily by PHY drivers but can also be
14+
used by user firmware.
15+
16+
API Reference
17+
*************
18+
19+
.. doxygengroup:: mdio_interface
20+
:project: Zephyr

drivers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_subdirectory_ifdef(CONFIG_GPIO gpio)
2020
add_subdirectory_ifdef(CONFIG_EC_HOST_CMD_PERIPH ec_host_cmd_periph)
2121
add_subdirectory_ifdef(CONFIG_I2C i2c)
2222
add_subdirectory_ifdef(CONFIG_I2S i2s)
23+
add_subdirectory_ifdef(CONFIG_MDIO mdio)
2324
add_subdirectory_ifdef(CONFIG_IEEE802154 ieee802154)
2425
add_subdirectory_ifdef(CONFIG_IPM ipm)
2526
add_subdirectory_ifdef(CONFIG_LED led)

drivers/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ source "drivers/ec_host_cmd_periph/Kconfig"
1919

2020
source "drivers/ethernet/Kconfig"
2121

22+
source "drivers/mdio/Kconfig"
23+
2224
source "drivers/net/Kconfig"
2325

2426
source "drivers/serial/Kconfig"

drivers/mdio/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
zephyr_library()

drivers/mdio/Kconfig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# MDIO configuration options
2+
3+
# Copyright (c) 2021 IP-Logix Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
#
7+
# MDIO options
8+
#
9+
menuconfig MDIO
10+
bool "MDIO Drivers"
11+
help
12+
Enable MDIO Driver Configuration
13+
14+
if MDIO
15+
16+
# Include these first so that any properties (e.g. defaults) below can be
17+
# overridden (by defining symbols in multiple locations)
18+
19+
config MDIO_INIT_PRIORITY
20+
int "Init priority"
21+
default 60
22+
help
23+
MDIO device driver initialization priority.
24+
25+
26+
module = MDIO
27+
module-str = mdio
28+
source "subsys/logging/Kconfig.template.log_config"
29+
30+
endif # MDIO
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2021 IP-Logix Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Common fields for MDIO controllers
5+
6+
include: base.yaml
7+
8+
bus: mdio
9+
10+
properties:
11+
label:
12+
required: true
13+
protocol:
14+
required: false
15+
type: string
16+
description: |
17+
MDIO bus framing protocol to use for communication. Most devices
18+
support clause 22.
19+
20+
- clause 22: IEEE802.3 clause 22 frame format
21+
- clause 45: IEEE802.3 clause 45 frame format
22+
- micrel SMI: Micrel Serial Management Interface frame format
23+
enum:
24+
- "clause 22"
25+
- "clause 45"
26+
- "micrel SMI"
27+
default: "clause 22"

include/drivers/mdio.h

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/**
2+
* @file
3+
*
4+
* @brief Public APIs for MDIO drivers.
5+
*/
6+
7+
/*
8+
* Copyright (c) 2021 IP-Logix Inc.
9+
*
10+
* SPDX-License-Identifier: Apache-2.0
11+
*/
12+
#ifndef ZEPHYR_INCLUDE_DRIVERS_MDIO_H_
13+
#define ZEPHYR_INCLUDE_DRIVERS_MDIO_H_
14+
15+
/**
16+
* @brief MDIO Interface
17+
* @defgroup mdio_interface MDIO Interface
18+
* @ingroup io_interfaces
19+
* @{
20+
*/
21+
#include <zephyr/types.h>
22+
#include <device.h>
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
28+
/**
29+
* @cond INTERNAL_HIDDEN
30+
*
31+
* These are for internal use only, so skip these in
32+
* public documentation.
33+
*/
34+
35+
/** Order of items in this enum must match the `protocol` dts binding */
36+
enum MDIO_PROTOCOL {
37+
CLAUSE_22 = 0,
38+
CLAUSE_45 = 1,
39+
MICREL_SMI = 2,
40+
};
41+
42+
__subsystem struct mdio_driver_api {
43+
/** Enable the MDIO bus device */
44+
void (*bus_enable)(const struct device *dev);
45+
46+
/** Disable the MDIO bus device */
47+
void (*bus_disable)(const struct device *dev);
48+
49+
/** Read data from MDIO bus */
50+
int (*read)(const struct device *dev, uint8_t prtad, uint8_t devad,
51+
uint16_t *data);
52+
53+
/** Write data to MDIO bus */
54+
int (*write)(const struct device *dev, uint8_t prtad, uint8_t devad,
55+
uint16_t data);
56+
};
57+
/**
58+
* @endcond
59+
*/
60+
61+
/**
62+
* @brief Enable MDIO bus
63+
*
64+
* @param[in] dev Pointer to the device structure for the controller
65+
*
66+
*/
67+
__syscall void mdio_bus_enable(const struct device *dev);
68+
69+
static inline void z_impl_mdio_bus_enable(const struct device *dev)
70+
{
71+
const struct mdio_driver_api *api =
72+
(const struct mdio_driver_api *)dev->api;
73+
74+
return api->bus_enable(dev);
75+
}
76+
77+
/**
78+
* @brief Disable MDIO bus and tri-state drivers
79+
*
80+
* @param[in] dev Pointer to the device structure for the controller
81+
*
82+
*/
83+
__syscall void mdio_bus_disable(const struct device *dev);
84+
85+
static inline void z_impl_mdio_bus_disable(const struct device *dev)
86+
{
87+
const struct mdio_driver_api *api =
88+
(const struct mdio_driver_api *)dev->api;
89+
90+
return api->bus_disable(dev);
91+
}
92+
93+
/**
94+
* @brief Read from MDIO Bus
95+
*
96+
* This routine provides a generic interface to perform a read on the
97+
* MDIO bus.
98+
*
99+
* @param[in] dev Pointer to the device structure for the controller
100+
* @param[in] prtad Port address
101+
* @param[in] devad Device address
102+
* @param data Pointer to receive read data
103+
*
104+
* @retval 0 If successful.
105+
* @retval -EIO General input / output error.
106+
* @retval -ETIMEDOUT If transaction timedout on the bus
107+
*/
108+
__syscall int mdio_read(const struct device *dev, uint8_t prtad, uint8_t devad,
109+
uint16_t *data);
110+
111+
static inline int z_impl_mdio_read(const struct device *dev, uint8_t prtad,
112+
uint8_t devad, uint16_t *data)
113+
{
114+
const struct mdio_driver_api *api =
115+
(const struct mdio_driver_api *)dev->api;
116+
117+
return api->read(dev, prtad, devad, data);
118+
}
119+
120+
121+
/**
122+
* @brief Write to MDIO bus
123+
*
124+
* This routine provides a generic interface to perform a write on the
125+
* MDIO bus.
126+
*
127+
* @param[in] dev Pointer to the device structure for the controller
128+
* @param[in] prtad Port address
129+
* @param[in] devad Device address
130+
* @param[in] data Data to write
131+
*
132+
* @retval 0 If successful.
133+
* @retval -EIO General input / output error.
134+
* @retval -ETIMEDOUT If transaction timedout on the bus
135+
*/
136+
__syscall int mdio_write(const struct device *dev, uint8_t prtad, uint8_t devad,
137+
uint16_t data);
138+
139+
static inline int z_impl_mdio_write(const struct device *dev, uint8_t prtad,
140+
uint8_t devad, uint16_t data)
141+
{
142+
const struct mdio_driver_api *api =
143+
(const struct mdio_driver_api *)dev->api;
144+
145+
return api->write(dev, prtad, devad, data);
146+
}
147+
148+
#ifdef __cplusplus
149+
}
150+
#endif
151+
152+
/**
153+
* @}
154+
*/
155+
156+
#include <syscalls/mdio.h>
157+
158+
#endif /* ZEPHYR_INCLUDE_DRIVERS_MDIO_H_ */

0 commit comments

Comments
 (0)