1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <assert.h>
4 #include <console/console.h>
5 #include <device/device.h>
6 #include <device/mdio.h>
7 #include <stddef.h>
8
dev_get_mdio_ops(struct device * dev)9 const struct mdio_bus_operations *dev_get_mdio_ops(struct device *dev)
10 {
11 if (!dev || !dev->ops || !dev->ops->ops_mdio) {
12 printk(BIOS_ERR, "Could not get MDIO operations.\n");
13 return NULL;
14 }
15
16 return dev->ops->ops_mdio;
17 }
18
mdio_read(struct device * dev,uint8_t offset)19 uint16_t mdio_read(struct device *dev, uint8_t offset)
20 {
21 const struct mdio_bus_operations *mdio_ops;
22 struct device *parent = dev->upstream->dev;
23
24 assert(dev->path.type == DEVICE_PATH_MDIO);
25 mdio_ops = dev_get_mdio_ops(parent);
26 if (!mdio_ops)
27 return 0;
28 return mdio_ops->read(parent, dev->path.mdio.addr, offset);
29 }
mdio_write(struct device * dev,uint8_t offset,uint16_t val)30 void mdio_write(struct device *dev, uint8_t offset, uint16_t val)
31 {
32 const struct mdio_bus_operations *mdio_ops;
33 struct device *parent = dev->upstream->dev;
34
35 assert(dev->path.type == DEVICE_PATH_MDIO);
36 mdio_ops = dev_get_mdio_ops(parent);
37 if (!mdio_ops)
38 return;
39 mdio_ops->write(parent, dev->path.mdio.addr, offset, val);
40 }
41