blob: 2b50ffc3acc70b168380bb3417159376bf90df28 [file] [log] [blame]
Alex Marginean0daa53a2019-06-03 19:12:28 +03001// 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 Marginean3336ef62019-07-12 10:13:52 +030012#define SANDBOX_PHY_REG_CNT 2
Alex Marginean0daa53a2019-06-03 19:12:28 +030013
14struct mdio_sandbox_priv {
15 int enabled;
Alex Marginean3336ef62019-07-12 10:13:52 +030016 u16 reg[SANDBOX_PHY_REG_CNT];
Alex Marginean0daa53a2019-06-03 19:12:28 +030017};
18
19static 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;
Heinrich Schuchardt9e951182019-07-30 23:49:00 +020030 if (reg < 0 || reg >= SANDBOX_PHY_REG_CNT)
Alex Marginean0daa53a2019-06-03 19:12:28 +030031 return -ENODEV;
32
Alex Marginean3336ef62019-07-12 10:13:52 +030033 return priv->reg[reg];
Alex Marginean0daa53a2019-06-03 19:12:28 +030034}
35
36static 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;
Heinrich Schuchardt9e951182019-07-30 23:49:00 +020048 if (reg < 0 || reg >= SANDBOX_PHY_REG_CNT)
Alex Marginean0daa53a2019-06-03 19:12:28 +030049 return -ENODEV;
50
Alex Marginean3336ef62019-07-12 10:13:52 +030051 priv->reg[reg] = val;
Alex Marginean0daa53a2019-06-03 19:12:28 +030052
53 return 0;
54}
55
56static int mdio_sandbox_reset(struct udevice *dev)
57{
58 struct mdio_sandbox_priv *priv = dev_get_priv(dev);
Alex Marginean3336ef62019-07-12 10:13:52 +030059 int i;
Alex Marginean0daa53a2019-06-03 19:12:28 +030060
Alex Marginean3336ef62019-07-12 10:13:52 +030061 for (i = 0; i < SANDBOX_PHY_REG_CNT; i++)
62 priv->reg[i] = 0;
Alex Marginean0daa53a2019-06-03 19:12:28 +030063
64 return 0;
65}
66
67static const struct mdio_ops mdio_sandbox_ops = {
68 .read = mdio_sandbox_read,
69 .write = mdio_sandbox_write,
70 .reset = mdio_sandbox_reset,
71};
72
73static 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
82static const struct udevice_id mdio_sandbox_ids[] = {
83 { .compatible = "sandbox,mdio" },
84 { }
85};
86
87U_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,
Simon Glass8a2b47f2020-12-03 16:55:17 -070093 .priv_auto = sizeof(struct mdio_sandbox_priv),
Alex Marginean0daa53a2019-06-03 19:12:28 +030094};