2 * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include "opt_platform.h"
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/systm.h>
36 #include <dev/ofw/ofw_bus.h>
37 #include <dev/ofw/ofw_bus_subr.h>
40 #include <dev/extres/phy/phy.h>
45 device_t consumer_dev; /* consumer device*/
46 device_t provider_dev; /* provider device*/
47 uintptr_t phy_id; /* phy id */
50 MALLOC_DEFINE(M_PHY, "phy", "Phy framework");
53 phy_init(device_t consumer, phy_t phy)
56 return (PHY_INIT(phy->provider_dev, phy->phy_id, true));
60 phy_deinit(device_t consumer, phy_t phy)
63 return (PHY_INIT(phy->provider_dev, phy->phy_id, false));
68 phy_enable(device_t consumer, phy_t phy)
71 return (PHY_ENABLE(phy->provider_dev, phy->phy_id, true));
75 phy_disable(device_t consumer, phy_t phy)
78 return (PHY_ENABLE(phy->provider_dev, phy->phy_id, false));
82 phy_status(device_t consumer, phy_t phy, int *value)
85 return (PHY_STATUS(phy->provider_dev, phy->phy_id, value));
89 phy_get_by_id(device_t consumer_dev, device_t provider_dev, intptr_t id,
95 phy = malloc(sizeof(struct phy), M_PHY,
97 phy->consumer_dev = consumer_dev;
98 phy->provider_dev = provider_dev;
105 phy_release(phy_t phy)
112 int phy_default_map(device_t provider, phandle_t xref, int ncells,
113 pcell_t *cells, intptr_t *id)
118 else if (ncells == 1)
127 phy_get_by_ofw_idx(device_t consumer_dev, phandle_t cnode, int idx, phy_t *phy)
136 cnode = ofw_bus_get_node(consumer_dev);
138 device_printf(consumer_dev,
139 "%s called on not ofw based device\n", __func__);
142 rv = ofw_bus_parse_xref_list_alloc(cnode, "phys", "#phy-cells", idx,
143 &xnode, &ncells, &cells);
147 /* Tranlate provider to device. */
148 phydev = OF_device_from_xref(xnode);
149 if (phydev == NULL) {
153 /* Map phy to number. */
154 rv = PHY_MAP(phydev, xnode, ncells, cells, &id);
159 return (phy_get_by_id(consumer_dev, phydev, id, phy));
163 phy_get_by_ofw_name(device_t consumer_dev, phandle_t cnode, char *name,
169 cnode = ofw_bus_get_node(consumer_dev);
171 device_printf(consumer_dev,
172 "%s called on not ofw based device\n", __func__);
175 rv = ofw_bus_find_string_index(cnode, "phy-names", name, &idx);
178 return (phy_get_by_ofw_idx(consumer_dev, cnode, idx, phy));
182 phy_get_by_ofw_property(device_t consumer_dev, phandle_t cnode, char *name,
191 cnode = ofw_bus_get_node(consumer_dev);
193 device_printf(consumer_dev,
194 "%s called on not ofw based device\n", __func__);
197 ncells = OF_getencprop_alloc(cnode, name, sizeof(pcell_t),
202 /* Tranlate provider to device. */
203 phydev = OF_device_from_xref(cells[0]);
204 if (phydev == NULL) {
208 /* Map phy to number. */
209 rv = PHY_MAP(phydev, cells[0], ncells - 1 , cells + 1, &id);
214 return (phy_get_by_id(consumer_dev, phydev, id, phy));
218 phy_register_provider(device_t provider_dev)
220 phandle_t xref, node;
222 node = ofw_bus_get_node(provider_dev);
224 panic("%s called on not ofw based device.\n", __func__);
226 xref = OF_xref_from_node(node);
227 OF_device_register_xref(xref, provider_dev);
231 phy_unregister_provider(device_t provider_dev)
235 xref = OF_xref_from_device(provider_dev);
236 OF_device_register_xref(xref, NULL);