mdio.c revision 234861
1/*-
2 * Copyright (c) 2011-2012 Stefan Bethke.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/etherswitch/mdio.c 234861 2012-05-01 06:11:38Z adrian $
27 */
28
29#include <sys/param.h>
30#include <sys/bus.h>
31#include <sys/systm.h>
32
33#include <dev/etherswitch/mdio.h>
34
35#include "mdio_if.h"
36
37static void
38mdio_identify(driver_t *driver, device_t parent)
39{
40
41	if (device_find_child(parent, mdio_driver.name, -1) == NULL)
42		BUS_ADD_CHILD(parent, 0, mdio_driver.name, -1);
43}
44
45static int
46mdio_probe(device_t dev)
47{
48
49	device_set_desc(dev, "MDIO");
50
51	return (BUS_PROBE_SPECIFIC);
52}
53
54static int
55mdio_attach(device_t dev)
56{
57
58	bus_generic_probe(dev);
59	bus_enumerate_hinted_children(dev);
60	return (bus_generic_attach(dev));
61}
62
63static int
64mdio_detach(device_t dev)
65{
66
67	bus_generic_detach(dev);
68	return (0);
69}
70
71static int
72mdio_readreg(device_t dev, int phy, int reg)
73{
74
75	return (MDIO_READREG(device_get_parent(dev), phy, reg));
76}
77
78static int
79mdio_writereg(device_t dev, int phy, int reg, int val)
80{
81
82	return (MDIO_WRITEREG(device_get_parent(dev), phy, reg, val));
83}
84
85static void
86mdio_hinted_child(device_t dev, const char *name, int unit)
87{
88
89	device_add_child(dev, name, unit);
90}
91
92static device_method_t mdio_methods[] = {
93	/* device interface */
94	DEVMETHOD(device_identify,	mdio_identify),
95	DEVMETHOD(device_probe,		mdio_probe),
96	DEVMETHOD(device_attach,	mdio_attach),
97	DEVMETHOD(device_detach,	mdio_detach),
98	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
99
100	/* bus interface */
101	DEVMETHOD(bus_add_child,	device_add_child_ordered),
102	DEVMETHOD(bus_hinted_child,	mdio_hinted_child),
103
104	/* MDIO access */
105	DEVMETHOD(mdio_readreg,		mdio_readreg),
106	DEVMETHOD(mdio_writereg,	mdio_writereg),
107
108	DEVMETHOD_END
109};
110
111driver_t mdio_driver = {
112	"mdio",
113	mdio_methods,
114	0
115};
116
117devclass_t mdio_devclass;
118