171e8eac4SAdrian Chadd /*-
271e8eac4SAdrian Chadd * Copyright (c) 2011-2012 Stefan Bethke.
371e8eac4SAdrian Chadd * All rights reserved.
471e8eac4SAdrian Chadd *
571e8eac4SAdrian Chadd * Redistribution and use in source and binary forms, with or without
671e8eac4SAdrian Chadd * modification, are permitted provided that the following conditions
771e8eac4SAdrian Chadd * are met:
871e8eac4SAdrian Chadd * 1. Redistributions of source code must retain the above copyright
971e8eac4SAdrian Chadd * notice, this list of conditions and the following disclaimer.
1071e8eac4SAdrian Chadd * 2. Redistributions in binary form must reproduce the above copyright
1171e8eac4SAdrian Chadd * notice, this list of conditions and the following disclaimer in the
1271e8eac4SAdrian Chadd * documentation and/or other materials provided with the distribution.
1371e8eac4SAdrian Chadd *
1471e8eac4SAdrian Chadd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1571e8eac4SAdrian Chadd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1671e8eac4SAdrian Chadd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1771e8eac4SAdrian Chadd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1871e8eac4SAdrian Chadd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1971e8eac4SAdrian Chadd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2071e8eac4SAdrian Chadd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2171e8eac4SAdrian Chadd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2271e8eac4SAdrian Chadd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2371e8eac4SAdrian Chadd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2471e8eac4SAdrian Chadd * SUCH DAMAGE.
2571e8eac4SAdrian Chadd */
2671e8eac4SAdrian Chadd
2771e8eac4SAdrian Chadd #include <sys/param.h>
2871e8eac4SAdrian Chadd #include <sys/bus.h>
2971e8eac4SAdrian Chadd #include <sys/systm.h>
3071e8eac4SAdrian Chadd #include <sys/module.h>
3171e8eac4SAdrian Chadd
3271e8eac4SAdrian Chadd #include <dev/mdio/mdio.h>
3371e8eac4SAdrian Chadd
3471e8eac4SAdrian Chadd #include "mdio_if.h"
3571e8eac4SAdrian Chadd
3671e8eac4SAdrian Chadd static void
mdio_identify(driver_t * driver,device_t parent)3771e8eac4SAdrian Chadd mdio_identify(driver_t *driver, device_t parent)
3871e8eac4SAdrian Chadd {
3971e8eac4SAdrian Chadd
4071e8eac4SAdrian Chadd if (device_find_child(parent, mdio_driver.name, -1) == NULL)
41a05a6804SWarner Losh BUS_ADD_CHILD(parent, 0, mdio_driver.name, DEVICE_UNIT_ANY);
4271e8eac4SAdrian Chadd }
4371e8eac4SAdrian Chadd
4471e8eac4SAdrian Chadd static int
mdio_probe(device_t dev)4571e8eac4SAdrian Chadd mdio_probe(device_t dev)
4671e8eac4SAdrian Chadd {
4771e8eac4SAdrian Chadd
4871e8eac4SAdrian Chadd device_set_desc(dev, "MDIO");
4971e8eac4SAdrian Chadd
5071e8eac4SAdrian Chadd return (BUS_PROBE_SPECIFIC);
5171e8eac4SAdrian Chadd }
5271e8eac4SAdrian Chadd
5371e8eac4SAdrian Chadd static int
mdio_attach(device_t dev)5471e8eac4SAdrian Chadd mdio_attach(device_t dev)
5571e8eac4SAdrian Chadd {
5671e8eac4SAdrian Chadd
57723da5d9SJohn Baldwin bus_identify_children(dev);
5871e8eac4SAdrian Chadd bus_enumerate_hinted_children(dev);
59*18250ec6SJohn Baldwin bus_attach_children(dev);
60*18250ec6SJohn Baldwin return (0);
6171e8eac4SAdrian Chadd }
6271e8eac4SAdrian Chadd
6371e8eac4SAdrian Chadd static int
mdio_readreg(device_t dev,int phy,int reg)6471e8eac4SAdrian Chadd mdio_readreg(device_t dev, int phy, int reg)
6571e8eac4SAdrian Chadd {
6671e8eac4SAdrian Chadd
6771e8eac4SAdrian Chadd return (MDIO_READREG(device_get_parent(dev), phy, reg));
6871e8eac4SAdrian Chadd }
6971e8eac4SAdrian Chadd
7071e8eac4SAdrian Chadd static int
mdio_writereg(device_t dev,int phy,int reg,int val)7171e8eac4SAdrian Chadd mdio_writereg(device_t dev, int phy, int reg, int val)
7271e8eac4SAdrian Chadd {
7371e8eac4SAdrian Chadd
7471e8eac4SAdrian Chadd return (MDIO_WRITEREG(device_get_parent(dev), phy, reg, val));
7571e8eac4SAdrian Chadd }
7671e8eac4SAdrian Chadd
77e2db1d1fSAdrian Chadd static int
mdio_readextreg(device_t dev,int phy,int devad,int reg)78e2db1d1fSAdrian Chadd mdio_readextreg(device_t dev, int phy, int devad, int reg)
79e2db1d1fSAdrian Chadd {
80e2db1d1fSAdrian Chadd
81e2db1d1fSAdrian Chadd return (MDIO_READEXTREG(device_get_parent(dev), phy, devad, reg));
82e2db1d1fSAdrian Chadd }
83e2db1d1fSAdrian Chadd
84e2db1d1fSAdrian Chadd static int
mdio_writeextreg(device_t dev,int phy,int devad,int reg,int val)85e2db1d1fSAdrian Chadd mdio_writeextreg(device_t dev, int phy, int devad, int reg,
86e2db1d1fSAdrian Chadd int val)
87e2db1d1fSAdrian Chadd {
88e2db1d1fSAdrian Chadd
89e2db1d1fSAdrian Chadd return (MDIO_WRITEEXTREG(device_get_parent(dev), phy, devad, reg, val));
90e2db1d1fSAdrian Chadd }
91e2db1d1fSAdrian Chadd
9271e8eac4SAdrian Chadd static void
mdio_hinted_child(device_t dev,const char * name,int unit)9371e8eac4SAdrian Chadd mdio_hinted_child(device_t dev, const char *name, int unit)
9471e8eac4SAdrian Chadd {
9571e8eac4SAdrian Chadd
9671e8eac4SAdrian Chadd device_add_child(dev, name, unit);
9771e8eac4SAdrian Chadd }
9871e8eac4SAdrian Chadd
9971e8eac4SAdrian Chadd static device_method_t mdio_methods[] = {
10071e8eac4SAdrian Chadd /* device interface */
10171e8eac4SAdrian Chadd DEVMETHOD(device_identify, mdio_identify),
10271e8eac4SAdrian Chadd DEVMETHOD(device_probe, mdio_probe),
10371e8eac4SAdrian Chadd DEVMETHOD(device_attach, mdio_attach),
104d62d10ebSJohn Baldwin DEVMETHOD(device_detach, bus_generic_detach),
10571e8eac4SAdrian Chadd DEVMETHOD(device_shutdown, bus_generic_shutdown),
10671e8eac4SAdrian Chadd
10771e8eac4SAdrian Chadd /* bus interface */
10871e8eac4SAdrian Chadd DEVMETHOD(bus_add_child, device_add_child_ordered),
10971e8eac4SAdrian Chadd DEVMETHOD(bus_hinted_child, mdio_hinted_child),
11071e8eac4SAdrian Chadd
11171e8eac4SAdrian Chadd /* MDIO access */
11271e8eac4SAdrian Chadd DEVMETHOD(mdio_readreg, mdio_readreg),
11371e8eac4SAdrian Chadd DEVMETHOD(mdio_writereg, mdio_writereg),
114e2db1d1fSAdrian Chadd DEVMETHOD(mdio_readextreg, mdio_readextreg),
115e2db1d1fSAdrian Chadd DEVMETHOD(mdio_writeextreg, mdio_writeextreg),
11671e8eac4SAdrian Chadd
11771e8eac4SAdrian Chadd DEVMETHOD_END
11871e8eac4SAdrian Chadd };
11971e8eac4SAdrian Chadd
12071e8eac4SAdrian Chadd driver_t mdio_driver = {
12171e8eac4SAdrian Chadd "mdio",
12271e8eac4SAdrian Chadd mdio_methods,
12371e8eac4SAdrian Chadd 0
12471e8eac4SAdrian Chadd };
12571e8eac4SAdrian Chadd
12671e8eac4SAdrian Chadd MODULE_VERSION(mdio, 1);
127