xref: /openbmc/u-boot/drivers/phy/sandbox-phy.c (revision fbe44dd1)
1 /*
2  * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
3  * Written by Jean-Jacques Hiblot  <jjhiblot@ti.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <generic-phy.h>
11 
12 DECLARE_GLOBAL_DATA_PTR;
13 
14 struct sandbox_phy_priv {
15 	bool initialized;
16 	bool on;
17 	bool broken;
18 };
19 
20 static int sandbox_phy_power_on(struct phy *phy)
21 {
22 	struct sandbox_phy_priv *priv = dev_get_priv(phy->dev);
23 
24 	if (!priv->initialized)
25 		return -EIO;
26 
27 	if (priv->broken)
28 		return -EIO;
29 
30 	priv->on = true;
31 
32 	return 0;
33 }
34 
35 static int sandbox_phy_power_off(struct phy *phy)
36 {
37 	struct sandbox_phy_priv *priv = dev_get_priv(phy->dev);
38 
39 	if (!priv->initialized)
40 		return -EIO;
41 
42 	if (priv->broken)
43 		return -EIO;
44 
45 	/*
46 	 * for validation purpose, let's says that power off
47 	 * works only for PHY 0
48 	 */
49 	if (phy->id)
50 		return -EIO;
51 
52 	priv->on = false;
53 
54 	return 0;
55 }
56 
57 static int sandbox_phy_init(struct phy *phy)
58 {
59 	struct sandbox_phy_priv *priv = dev_get_priv(phy->dev);
60 
61 	priv->initialized = true;
62 	priv->on = true;
63 
64 	return 0;
65 }
66 
67 static int sandbox_phy_exit(struct phy *phy)
68 {
69 	struct sandbox_phy_priv *priv = dev_get_priv(phy->dev);
70 
71 	priv->initialized = false;
72 	priv->on = false;
73 
74 	return 0;
75 }
76 
77 static int sandbox_phy_probe(struct udevice *dev)
78 {
79 	struct sandbox_phy_priv *priv = dev_get_priv(dev);
80 
81 	priv->initialized = false;
82 	priv->on = false;
83 	priv->broken = fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
84 				       "broken");
85 
86 	return 0;
87 }
88 
89 static struct phy_ops sandbox_phy_ops = {
90 	.power_on = sandbox_phy_power_on,
91 	.power_off = sandbox_phy_power_off,
92 	.init = sandbox_phy_init,
93 	.exit = sandbox_phy_exit,
94 };
95 
96 static const struct udevice_id sandbox_phy_ids[] = {
97 	{ .compatible = "sandbox,phy" },
98 	{ }
99 };
100 
101 U_BOOT_DRIVER(phy_sandbox) = {
102 	.name		= "phy_sandbox",
103 	.id		= UCLASS_PHY,
104 	.of_match	= sandbox_phy_ids,
105 	.ops		= &sandbox_phy_ops,
106 	.probe		= sandbox_phy_probe,
107 	.priv_auto_alloc_size = sizeof(struct sandbox_phy_priv),
108 };
109