1248dd603SAdrian Chadd /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4248dd603SAdrian Chadd * Copyright (c) 2013 Luiz Otavio O Souza.
5248dd603SAdrian Chadd * Copyright (c) 2011-2012 Stefan Bethke.
6248dd603SAdrian Chadd * Copyright (c) 2012 Adrian Chadd.
7248dd603SAdrian Chadd * All rights reserved.
8248dd603SAdrian Chadd *
9248dd603SAdrian Chadd * Redistribution and use in source and binary forms, with or without
10248dd603SAdrian Chadd * modification, are permitted provided that the following conditions
11248dd603SAdrian Chadd * are met:
12248dd603SAdrian Chadd * 1. Redistributions of source code must retain the above copyright
13248dd603SAdrian Chadd * notice, this list of conditions and the following disclaimer.
14248dd603SAdrian Chadd * 2. Redistributions in binary form must reproduce the above copyright
15248dd603SAdrian Chadd * notice, this list of conditions and the following disclaimer in the
16248dd603SAdrian Chadd * documentation and/or other materials provided with the distribution.
17248dd603SAdrian Chadd *
18248dd603SAdrian Chadd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19248dd603SAdrian Chadd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20248dd603SAdrian Chadd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21248dd603SAdrian Chadd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22248dd603SAdrian Chadd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23248dd603SAdrian Chadd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24248dd603SAdrian Chadd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25248dd603SAdrian Chadd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26248dd603SAdrian Chadd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27248dd603SAdrian Chadd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28248dd603SAdrian Chadd * SUCH DAMAGE.
29248dd603SAdrian Chadd */
30248dd603SAdrian Chadd
31248dd603SAdrian Chadd #include <sys/param.h>
32248dd603SAdrian Chadd #include <sys/bus.h>
33248dd603SAdrian Chadd #include <sys/errno.h>
34248dd603SAdrian Chadd #include <sys/kernel.h>
354170452dSChristian Brueffer #include <sys/lock.h>
364170452dSChristian Brueffer #include <sys/mutex.h>
37248dd603SAdrian Chadd #include <sys/systm.h>
38248dd603SAdrian Chadd #include <sys/socket.h>
39248dd603SAdrian Chadd
40248dd603SAdrian Chadd #include <net/if.h>
41248dd603SAdrian Chadd
42248dd603SAdrian Chadd #include <dev/mii/mii.h>
43248dd603SAdrian Chadd
44248dd603SAdrian Chadd #include <dev/etherswitch/etherswitch.h>
45248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_phy.h>
46248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_reg.h>
47248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_var.h>
48248dd603SAdrian Chadd
49248dd603SAdrian Chadd #include "mdio_if.h"
50248dd603SAdrian Chadd #include "miibus_if.h"
51248dd603SAdrian Chadd #include "etherswitch_if.h"
52248dd603SAdrian Chadd
53248dd603SAdrian Chadd int
ip17x_readphy(device_t dev,int phy,int reg)54248dd603SAdrian Chadd ip17x_readphy(device_t dev, int phy, int reg)
55248dd603SAdrian Chadd {
56248dd603SAdrian Chadd struct ip17x_softc *sc;
57248dd603SAdrian Chadd int data;
58248dd603SAdrian Chadd
59248dd603SAdrian Chadd sc = device_get_softc(dev);
60248dd603SAdrian Chadd IP17X_LOCK_ASSERT(sc, MA_NOTOWNED);
61248dd603SAdrian Chadd
62248dd603SAdrian Chadd if (phy < 0 || phy >= 32)
63248dd603SAdrian Chadd return (ENXIO);
64248dd603SAdrian Chadd if (reg < 0 || reg >= 32)
65248dd603SAdrian Chadd return (ENXIO);
66248dd603SAdrian Chadd
67248dd603SAdrian Chadd IP17X_LOCK(sc);
68248dd603SAdrian Chadd data = MDIO_READREG(device_get_parent(dev), phy, reg);
69248dd603SAdrian Chadd IP17X_UNLOCK(sc);
70248dd603SAdrian Chadd
71248dd603SAdrian Chadd return (data);
72248dd603SAdrian Chadd }
73248dd603SAdrian Chadd
74248dd603SAdrian Chadd int
ip17x_writephy(device_t dev,int phy,int reg,int data)75248dd603SAdrian Chadd ip17x_writephy(device_t dev, int phy, int reg, int data)
76248dd603SAdrian Chadd {
77248dd603SAdrian Chadd struct ip17x_softc *sc;
78248dd603SAdrian Chadd int err;
79248dd603SAdrian Chadd
80248dd603SAdrian Chadd sc = device_get_softc(dev);
81248dd603SAdrian Chadd IP17X_LOCK_ASSERT(sc, MA_NOTOWNED);
82248dd603SAdrian Chadd
83248dd603SAdrian Chadd if (phy < 0 || phy >= 32)
84248dd603SAdrian Chadd return (ENXIO);
85248dd603SAdrian Chadd if (reg < 0 || reg >= 32)
86248dd603SAdrian Chadd return (ENXIO);
87248dd603SAdrian Chadd
88248dd603SAdrian Chadd IP17X_LOCK(sc);
89248dd603SAdrian Chadd err = MDIO_WRITEREG(device_get_parent(dev), phy, reg, data);
90248dd603SAdrian Chadd IP17X_UNLOCK(sc);
91248dd603SAdrian Chadd
92248dd603SAdrian Chadd return (err);
93248dd603SAdrian Chadd }
94248dd603SAdrian Chadd
95248dd603SAdrian Chadd int
ip17x_updatephy(device_t dev,int phy,int reg,int mask,int value)96248dd603SAdrian Chadd ip17x_updatephy(device_t dev, int phy, int reg, int mask, int value)
97248dd603SAdrian Chadd {
98248dd603SAdrian Chadd int val;
99248dd603SAdrian Chadd
100248dd603SAdrian Chadd val = ip17x_readphy(dev, phy, reg);
101248dd603SAdrian Chadd val &= ~mask;
102248dd603SAdrian Chadd val |= value;
103248dd603SAdrian Chadd return (ip17x_writephy(dev, phy, reg, val));
104248dd603SAdrian Chadd }
105