xref: /freebsd/sys/dev/mdio/mdio.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
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$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
33 
34 #include <dev/mdio/mdio.h>
35 
36 #include "mdio_if.h"
37 
38 static void
39 mdio_identify(driver_t *driver, device_t parent)
40 {
41 
42 	if (device_find_child(parent, mdio_driver.name, -1) == NULL)
43 		BUS_ADD_CHILD(parent, 0, mdio_driver.name, -1);
44 }
45 
46 static int
47 mdio_probe(device_t dev)
48 {
49 
50 	device_set_desc(dev, "MDIO");
51 
52 	return (BUS_PROBE_SPECIFIC);
53 }
54 
55 static int
56 mdio_attach(device_t dev)
57 {
58 
59 	bus_generic_probe(dev);
60 	bus_enumerate_hinted_children(dev);
61 	return (bus_generic_attach(dev));
62 }
63 
64 static int
65 mdio_detach(device_t dev)
66 {
67 
68 	bus_generic_detach(dev);
69 	return (0);
70 }
71 
72 static int
73 mdio_readreg(device_t dev, int phy, int reg)
74 {
75 
76 	return (MDIO_READREG(device_get_parent(dev), phy, reg));
77 }
78 
79 static int
80 mdio_writereg(device_t dev, int phy, int reg, int val)
81 {
82 
83 	return (MDIO_WRITEREG(device_get_parent(dev), phy, reg, val));
84 }
85 
86 static int
87 mdio_readextreg(device_t dev, int phy, int devad, int reg)
88 {
89 
90 	return (MDIO_READEXTREG(device_get_parent(dev), phy, devad, reg));
91 }
92 
93 static int
94 mdio_writeextreg(device_t dev, int phy, int devad, int reg,
95     int val)
96 {
97 
98 	return (MDIO_WRITEEXTREG(device_get_parent(dev), phy, devad, reg, val));
99 }
100 
101 static void
102 mdio_hinted_child(device_t dev, const char *name, int unit)
103 {
104 
105 	device_add_child(dev, name, unit);
106 }
107 
108 static device_method_t mdio_methods[] = {
109 	/* device interface */
110 	DEVMETHOD(device_identify,	mdio_identify),
111 	DEVMETHOD(device_probe,		mdio_probe),
112 	DEVMETHOD(device_attach,	mdio_attach),
113 	DEVMETHOD(device_detach,	mdio_detach),
114 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
115 
116 	/* bus interface */
117 	DEVMETHOD(bus_add_child,	device_add_child_ordered),
118 	DEVMETHOD(bus_hinted_child,	mdio_hinted_child),
119 
120 	/* MDIO access */
121 	DEVMETHOD(mdio_readreg,		mdio_readreg),
122 	DEVMETHOD(mdio_writereg,	mdio_writereg),
123 	DEVMETHOD(mdio_readextreg,	mdio_readextreg),
124 	DEVMETHOD(mdio_writeextreg,	mdio_writeextreg),
125 
126 	DEVMETHOD_END
127 };
128 
129 driver_t mdio_driver = {
130 	"mdio",
131 	mdio_methods,
132 	0
133 };
134 
135 devclass_t mdio_devclass;
136 
137 MODULE_VERSION(mdio, 1);
138