Alex Marginean | 0daa53a | 2019-06-03 19:12:28 +0300 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2019 |
| 4 | * Alex Marginean, NXP |
| 5 | */ |
| 6 | |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
| 9 | #include <miiphy.h> |
| 10 | |
| 11 | #define SANDBOX_PHY_ADDR 5 |
| 12 | #define SANDBOX_PHY_REG 0 |
| 13 | |
| 14 | struct mdio_sandbox_priv { |
| 15 | int enabled; |
| 16 | u16 reg; |
| 17 | }; |
| 18 | |
| 19 | static int mdio_sandbox_read(struct udevice *dev, int addr, int devad, int reg) |
| 20 | { |
| 21 | struct mdio_sandbox_priv *priv = dev_get_priv(dev); |
| 22 | |
| 23 | if (!priv->enabled) |
| 24 | return -ENODEV; |
| 25 | |
| 26 | if (addr != SANDBOX_PHY_ADDR) |
| 27 | return -ENODEV; |
| 28 | if (devad != MDIO_DEVAD_NONE) |
| 29 | return -ENODEV; |
| 30 | if (reg != SANDBOX_PHY_REG) |
| 31 | return -ENODEV; |
| 32 | |
| 33 | return priv->reg; |
| 34 | } |
| 35 | |
| 36 | static int mdio_sandbox_write(struct udevice *dev, int addr, int devad, int reg, |
| 37 | u16 val) |
| 38 | { |
| 39 | struct mdio_sandbox_priv *priv = dev_get_priv(dev); |
| 40 | |
| 41 | if (!priv->enabled) |
| 42 | return -ENODEV; |
| 43 | |
| 44 | if (addr != SANDBOX_PHY_ADDR) |
| 45 | return -ENODEV; |
| 46 | if (devad != MDIO_DEVAD_NONE) |
| 47 | return -ENODEV; |
| 48 | if (reg != SANDBOX_PHY_REG) |
| 49 | return -ENODEV; |
| 50 | |
| 51 | priv->reg = val; |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int mdio_sandbox_reset(struct udevice *dev) |
| 57 | { |
| 58 | struct mdio_sandbox_priv *priv = dev_get_priv(dev); |
| 59 | |
| 60 | priv->reg = 0; |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static const struct mdio_ops mdio_sandbox_ops = { |
| 66 | .read = mdio_sandbox_read, |
| 67 | .write = mdio_sandbox_write, |
| 68 | .reset = mdio_sandbox_reset, |
| 69 | }; |
| 70 | |
| 71 | static int mdio_sandbox_probe(struct udevice *dev) |
| 72 | { |
| 73 | struct mdio_sandbox_priv *priv = dev_get_priv(dev); |
| 74 | |
| 75 | priv->enabled = 1; |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static const struct udevice_id mdio_sandbox_ids[] = { |
| 81 | { .compatible = "sandbox,mdio" }, |
| 82 | { } |
| 83 | }; |
| 84 | |
| 85 | U_BOOT_DRIVER(mdio_sandbox) = { |
| 86 | .name = "mdio_sandbox", |
| 87 | .id = UCLASS_MDIO, |
| 88 | .of_match = mdio_sandbox_ids, |
| 89 | .probe = mdio_sandbox_probe, |
| 90 | .ops = &mdio_sandbox_ops, |
| 91 | .priv_auto_alloc_size = sizeof(struct mdio_sandbox_priv), |
| 92 | }; |