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 |
Alex Marginean | 3336ef6 | 2019-07-12 10:13:52 +0300 | [diff] [blame] | 12 | #define SANDBOX_PHY_REG_CNT 2 |
Alex Marginean | 0daa53a | 2019-06-03 19:12:28 +0300 | [diff] [blame] | 13 | |
| 14 | struct mdio_sandbox_priv { |
| 15 | int enabled; |
Alex Marginean | 3336ef6 | 2019-07-12 10:13:52 +0300 | [diff] [blame] | 16 | u16 reg[SANDBOX_PHY_REG_CNT]; |
Alex Marginean | 0daa53a | 2019-06-03 19:12:28 +0300 | [diff] [blame] | 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; |
Alex Marginean | 3336ef6 | 2019-07-12 10:13:52 +0300 | [diff] [blame] | 30 | if (reg < 0 || reg > SANDBOX_PHY_REG_CNT) |
Alex Marginean | 0daa53a | 2019-06-03 19:12:28 +0300 | [diff] [blame] | 31 | return -ENODEV; |
| 32 | |
Alex Marginean | 3336ef6 | 2019-07-12 10:13:52 +0300 | [diff] [blame] | 33 | return priv->reg[reg]; |
Alex Marginean | 0daa53a | 2019-06-03 19:12:28 +0300 | [diff] [blame] | 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; |
Alex Marginean | 3336ef6 | 2019-07-12 10:13:52 +0300 | [diff] [blame] | 48 | if (reg < 0 || reg > SANDBOX_PHY_REG_CNT) |
Alex Marginean | 0daa53a | 2019-06-03 19:12:28 +0300 | [diff] [blame] | 49 | return -ENODEV; |
| 50 | |
Alex Marginean | 3336ef6 | 2019-07-12 10:13:52 +0300 | [diff] [blame] | 51 | priv->reg[reg] = val; |
Alex Marginean | 0daa53a | 2019-06-03 19:12:28 +0300 | [diff] [blame] | 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); |
Alex Marginean | 3336ef6 | 2019-07-12 10:13:52 +0300 | [diff] [blame] | 59 | int i; |
Alex Marginean | 0daa53a | 2019-06-03 19:12:28 +0300 | [diff] [blame] | 60 | |
Alex Marginean | 3336ef6 | 2019-07-12 10:13:52 +0300 | [diff] [blame] | 61 | for (i = 0; i < SANDBOX_PHY_REG_CNT; i++) |
| 62 | priv->reg[i] = 0; |
Alex Marginean | 0daa53a | 2019-06-03 19:12:28 +0300 | [diff] [blame] | 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static const struct mdio_ops mdio_sandbox_ops = { |
| 68 | .read = mdio_sandbox_read, |
| 69 | .write = mdio_sandbox_write, |
| 70 | .reset = mdio_sandbox_reset, |
| 71 | }; |
| 72 | |
| 73 | static int mdio_sandbox_probe(struct udevice *dev) |
| 74 | { |
| 75 | struct mdio_sandbox_priv *priv = dev_get_priv(dev); |
| 76 | |
| 77 | priv->enabled = 1; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static const struct udevice_id mdio_sandbox_ids[] = { |
| 83 | { .compatible = "sandbox,mdio" }, |
| 84 | { } |
| 85 | }; |
| 86 | |
| 87 | U_BOOT_DRIVER(mdio_sandbox) = { |
| 88 | .name = "mdio_sandbox", |
| 89 | .id = UCLASS_MDIO, |
| 90 | .of_match = mdio_sandbox_ids, |
| 91 | .probe = mdio_sandbox_probe, |
| 92 | .ops = &mdio_sandbox_ops, |
| 93 | .priv_auto_alloc_size = sizeof(struct mdio_sandbox_priv), |
| 94 | }; |