blob: df053f5381422aeb692d5ef6c64590d345be365e [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;
Alex Marginean3336ef62019-07-12 10:13:52 +030030 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;
Alex Marginean3336ef62019-07-12 10:13:52 +030048 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,
93 .priv_auto_alloc_size = sizeof(struct mdio_sandbox_priv),
94};