1a043e8c7SAdrian Chadd /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4477e3effSMichael Zhilin * Copyright (c) 2015-2016 Hiroki Mori.
5a043e8c7SAdrian Chadd * Copyright (c) 2011-2012 Stefan Bethke.
6a043e8c7SAdrian Chadd * All rights reserved.
7a043e8c7SAdrian Chadd *
8a043e8c7SAdrian Chadd * Redistribution and use in source and binary forms, with or without
9a043e8c7SAdrian Chadd * modification, are permitted provided that the following conditions
10a043e8c7SAdrian Chadd * are met:
11a043e8c7SAdrian Chadd * 1. Redistributions of source code must retain the above copyright
12a043e8c7SAdrian Chadd * notice, this list of conditions and the following disclaimer.
13a043e8c7SAdrian Chadd * 2. Redistributions in binary form must reproduce the above copyright
14a043e8c7SAdrian Chadd * notice, this list of conditions and the following disclaimer in the
15a043e8c7SAdrian Chadd * documentation and/or other materials provided with the distribution.
16a043e8c7SAdrian Chadd *
17a043e8c7SAdrian Chadd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18a043e8c7SAdrian Chadd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19a043e8c7SAdrian Chadd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20a043e8c7SAdrian Chadd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21a043e8c7SAdrian Chadd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22a043e8c7SAdrian Chadd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23a043e8c7SAdrian Chadd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24a043e8c7SAdrian Chadd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25a043e8c7SAdrian Chadd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26a043e8c7SAdrian Chadd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27a043e8c7SAdrian Chadd * SUCH DAMAGE.
28a043e8c7SAdrian Chadd */
29a043e8c7SAdrian Chadd
30477e3effSMichael Zhilin #include "opt_etherswitch.h"
31477e3effSMichael Zhilin
32a043e8c7SAdrian Chadd #include <sys/param.h>
33a043e8c7SAdrian Chadd #include <sys/bus.h>
34a043e8c7SAdrian Chadd #include <sys/errno.h>
35a043e8c7SAdrian Chadd #include <sys/kernel.h>
363d02237cSLuiz Otavio O Souza #include <sys/lock.h>
373d02237cSLuiz Otavio O Souza #include <sys/malloc.h>
38a043e8c7SAdrian Chadd #include <sys/module.h>
393d02237cSLuiz Otavio O Souza #include <sys/mutex.h>
40a043e8c7SAdrian Chadd #include <sys/socket.h>
41a043e8c7SAdrian Chadd #include <sys/sockio.h>
42a043e8c7SAdrian Chadd #include <sys/sysctl.h>
43a043e8c7SAdrian Chadd #include <sys/systm.h>
44a043e8c7SAdrian Chadd
45a043e8c7SAdrian Chadd #include <net/if.h>
463d02237cSLuiz Otavio O Souza #include <net/if_var.h>
47a043e8c7SAdrian Chadd #include <net/ethernet.h>
48a043e8c7SAdrian Chadd #include <net/if_media.h>
49a043e8c7SAdrian Chadd #include <net/if_types.h>
50a043e8c7SAdrian Chadd
51a043e8c7SAdrian Chadd #include <machine/bus.h>
52efce3748SRui Paulo #include <dev/iicbus/iic.h>
53a043e8c7SAdrian Chadd #include <dev/iicbus/iiconf.h>
54a043e8c7SAdrian Chadd #include <dev/iicbus/iicbus.h>
55a043e8c7SAdrian Chadd #include <dev/mii/mii.h>
56a043e8c7SAdrian Chadd #include <dev/mii/miivar.h>
575a4380b5SMichael Zhilin #include <dev/mdio/mdio.h>
58a043e8c7SAdrian Chadd
59a043e8c7SAdrian Chadd #include <dev/etherswitch/etherswitch.h>
60a043e8c7SAdrian Chadd #include <dev/etherswitch/rtl8366/rtl8366rbvar.h>
61a043e8c7SAdrian Chadd
625a4380b5SMichael Zhilin #include "mdio_if.h"
63a043e8c7SAdrian Chadd #include "iicbus_if.h"
64a043e8c7SAdrian Chadd #include "miibus_if.h"
65a043e8c7SAdrian Chadd #include "etherswitch_if.h"
66a043e8c7SAdrian Chadd
67a043e8c7SAdrian Chadd
68a043e8c7SAdrian Chadd struct rtl8366rb_softc {
69a043e8c7SAdrian Chadd struct mtx sc_mtx; /* serialize access to softc */
70a043e8c7SAdrian Chadd int smi_acquired; /* serialize access to SMI/I2C bus */
71a043e8c7SAdrian Chadd struct mtx callout_mtx; /* serialize callout */
72a043e8c7SAdrian Chadd device_t dev;
73477e3effSMichael Zhilin int vid[RTL8366_NUM_VLANS];
74477e3effSMichael Zhilin char *ifname[RTL8366_NUM_PHYS];
75477e3effSMichael Zhilin device_t miibus[RTL8366_NUM_PHYS];
762e6a8c1aSJustin Hibbits if_t ifp[RTL8366_NUM_PHYS];
77a043e8c7SAdrian Chadd struct callout callout_tick;
78477e3effSMichael Zhilin etherswitch_info_t info;
795a4380b5SMichael Zhilin int chip_type;
805a4380b5SMichael Zhilin int phy4cpu;
815a4380b5SMichael Zhilin int numphys;
82a043e8c7SAdrian Chadd };
83a043e8c7SAdrian Chadd
84a043e8c7SAdrian Chadd #define RTL_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
85a043e8c7SAdrian Chadd #define RTL_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
86a043e8c7SAdrian Chadd #define RTL_LOCK_ASSERT(_sc, _what) mtx_assert(&(_s)c->sc_mtx, (_what))
87a043e8c7SAdrian Chadd #define RTL_TRYLOCK(_sc) mtx_trylock(&(_sc)->sc_mtx)
88a043e8c7SAdrian Chadd
89a043e8c7SAdrian Chadd #define RTL_WAITOK 0
90a043e8c7SAdrian Chadd #define RTL_NOWAIT 1
91a043e8c7SAdrian Chadd
92a043e8c7SAdrian Chadd #define RTL_SMI_ACQUIRED 1
93a043e8c7SAdrian Chadd #define RTL_SMI_ACQUIRED_ASSERT(_sc) \
94a043e8c7SAdrian Chadd KASSERT((_sc)->smi_acquired == RTL_SMI_ACQUIRED, ("smi must be acquired @%s", __FUNCTION__))
95a043e8c7SAdrian Chadd
96a043e8c7SAdrian Chadd #if defined(DEBUG)
97a043e8c7SAdrian Chadd #define DPRINTF(dev, args...) device_printf(dev, args)
98a043e8c7SAdrian Chadd #define DEVERR(dev, err, fmt, args...) do { \
99a043e8c7SAdrian Chadd if (err != 0) device_printf(dev, fmt, err, args); \
100a043e8c7SAdrian Chadd } while (0)
101a043e8c7SAdrian Chadd #define DEBUG_INCRVAR(var) do { \
102a043e8c7SAdrian Chadd var++; \
103a043e8c7SAdrian Chadd } while (0)
104a043e8c7SAdrian Chadd
105a043e8c7SAdrian Chadd static int callout_blocked = 0;
106a043e8c7SAdrian Chadd static int iic_select_retries = 0;
107a043e8c7SAdrian Chadd static int phy_access_retries = 0;
1087029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, rtl8366rb, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
1097029da5cSPawel Biernacki "rtl8366rb");
110a043e8c7SAdrian Chadd SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, callout_blocked, CTLFLAG_RW, &callout_blocked, 0,
111a043e8c7SAdrian Chadd "number of times the callout couldn't acquire the bus");
112a043e8c7SAdrian Chadd SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, iic_select_retries, CTLFLAG_RW, &iic_select_retries, 0,
113a043e8c7SAdrian Chadd "number of times the I2C bus selection had to be retried");
114a043e8c7SAdrian Chadd SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, phy_access_retries, CTLFLAG_RW, &phy_access_retries, 0,
115a043e8c7SAdrian Chadd "number of times PHY register access had to be retried");
116a043e8c7SAdrian Chadd #else
117a043e8c7SAdrian Chadd #define DPRINTF(dev, args...)
118a043e8c7SAdrian Chadd #define DEVERR(dev, err, fmt, args...)
119a043e8c7SAdrian Chadd #define DEBUG_INCRVAR(var)
120a043e8c7SAdrian Chadd #endif
121a043e8c7SAdrian Chadd
122a043e8c7SAdrian Chadd static int smi_probe(device_t dev);
123a043e8c7SAdrian Chadd static int smi_read(device_t dev, uint16_t addr, uint16_t *data, int sleep);
124a043e8c7SAdrian Chadd static int smi_write(device_t dev, uint16_t addr, uint16_t data, int sleep);
125a043e8c7SAdrian Chadd static int smi_rmw(device_t dev, uint16_t addr, uint16_t mask, uint16_t data, int sleep);
126a043e8c7SAdrian Chadd static void rtl8366rb_tick(void *arg);
1272e6a8c1aSJustin Hibbits static int rtl8366rb_ifmedia_upd(if_t);
1282e6a8c1aSJustin Hibbits static void rtl8366rb_ifmedia_sts(if_t, struct ifmediareq *);
129a043e8c7SAdrian Chadd
130a043e8c7SAdrian Chadd static void
rtl8366rb_identify(driver_t * driver,device_t parent)131a043e8c7SAdrian Chadd rtl8366rb_identify(driver_t *driver, device_t parent)
132a043e8c7SAdrian Chadd {
133a043e8c7SAdrian Chadd device_t child;
134a043e8c7SAdrian Chadd struct iicbus_ivar *devi;
135a043e8c7SAdrian Chadd
136a043e8c7SAdrian Chadd if (device_find_child(parent, "rtl8366rb", -1) == NULL) {
137a05a6804SWarner Losh child = BUS_ADD_CHILD(parent, 0, "rtl8366rb", DEVICE_UNIT_ANY);
138a043e8c7SAdrian Chadd devi = IICBUS_IVAR(child);
139477e3effSMichael Zhilin devi->addr = RTL8366_IIC_ADDR;
140a043e8c7SAdrian Chadd }
141a043e8c7SAdrian Chadd }
142a043e8c7SAdrian Chadd
143a043e8c7SAdrian Chadd static int
rtl8366rb_probe(device_t dev)144a043e8c7SAdrian Chadd rtl8366rb_probe(device_t dev)
145a043e8c7SAdrian Chadd {
146477e3effSMichael Zhilin struct rtl8366rb_softc *sc;
147477e3effSMichael Zhilin
148477e3effSMichael Zhilin sc = device_get_softc(dev);
149477e3effSMichael Zhilin
150477e3effSMichael Zhilin bzero(sc, sizeof(*sc));
151a043e8c7SAdrian Chadd if (smi_probe(dev) != 0)
152a043e8c7SAdrian Chadd return (ENXIO);
1535a4380b5SMichael Zhilin if (sc->chip_type == RTL8366RB)
154a043e8c7SAdrian Chadd device_set_desc(dev, "RTL8366RB Ethernet Switch Controller");
155477e3effSMichael Zhilin else
156477e3effSMichael Zhilin device_set_desc(dev, "RTL8366SR Ethernet Switch Controller");
157a043e8c7SAdrian Chadd return (BUS_PROBE_DEFAULT);
158a043e8c7SAdrian Chadd }
159a043e8c7SAdrian Chadd
160a043e8c7SAdrian Chadd static void
rtl8366rb_init(device_t dev)161a043e8c7SAdrian Chadd rtl8366rb_init(device_t dev)
162a043e8c7SAdrian Chadd {
163bfae9329SLuiz Otavio O Souza struct rtl8366rb_softc *sc;
164477e3effSMichael Zhilin int i;
165477e3effSMichael Zhilin
166477e3effSMichael Zhilin sc = device_get_softc(dev);
167bfae9329SLuiz Otavio O Souza
168a043e8c7SAdrian Chadd /* Initialisation for TL-WR1043ND */
169477e3effSMichael Zhilin #ifdef RTL8366_SOFT_RESET
170477e3effSMichael Zhilin smi_rmw(dev, RTL8366_RCR,
171477e3effSMichael Zhilin RTL8366_RCR_SOFT_RESET,
172477e3effSMichael Zhilin RTL8366_RCR_SOFT_RESET, RTL_WAITOK);
173477e3effSMichael Zhilin #else
174477e3effSMichael Zhilin smi_rmw(dev, RTL8366_RCR,
175477e3effSMichael Zhilin RTL8366_RCR_HARD_RESET,
176477e3effSMichael Zhilin RTL8366_RCR_HARD_RESET, RTL_WAITOK);
177477e3effSMichael Zhilin #endif
178477e3effSMichael Zhilin /* hard reset not return ack */
179a043e8c7SAdrian Chadd DELAY(100000);
180a043e8c7SAdrian Chadd /* Enable 16 VLAN mode */
181477e3effSMichael Zhilin smi_rmw(dev, RTL8366_SGCR,
182477e3effSMichael Zhilin RTL8366_SGCR_EN_VLAN | RTL8366_SGCR_EN_VLAN_4KTB,
183477e3effSMichael Zhilin RTL8366_SGCR_EN_VLAN, RTL_WAITOK);
184bfae9329SLuiz Otavio O Souza /* Initialize our vlan table. */
185bfae9329SLuiz Otavio O Souza for (i = 0; i <= 1; i++)
186bfae9329SLuiz Otavio O Souza sc->vid[i] = (i + 1) | ETHERSWITCH_VID_VALID;
187bfae9329SLuiz Otavio O Souza /* Remove port 0 from VLAN 1. */
188477e3effSMichael Zhilin smi_rmw(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, 0),
189a043e8c7SAdrian Chadd (1 << 0), 0, RTL_WAITOK);
190bfae9329SLuiz Otavio O Souza /* Add port 0 untagged and port 5 tagged to VLAN 2. */
191477e3effSMichael Zhilin smi_rmw(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, 1),
192477e3effSMichael Zhilin ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_MEMBER_SHIFT)
193477e3effSMichael Zhilin | ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_UNTAG_SHIFT),
194477e3effSMichael Zhilin ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_MEMBER_SHIFT
195477e3effSMichael Zhilin | ((1 << 0) << RTL8366_VMCR_MU_UNTAG_SHIFT)),
196a043e8c7SAdrian Chadd RTL_WAITOK);
197bfae9329SLuiz Otavio O Souza /* Set PVID 2 for port 0. */
198477e3effSMichael Zhilin smi_rmw(dev, RTL8366_PVCR_REG(0),
199477e3effSMichael Zhilin RTL8366_PVCR_VAL(0, RTL8366_PVCR_PORT_MASK),
200477e3effSMichael Zhilin RTL8366_PVCR_VAL(0, 1), RTL_WAITOK);
201a043e8c7SAdrian Chadd }
202a043e8c7SAdrian Chadd
203a043e8c7SAdrian Chadd static int
rtl8366rb_attach(device_t dev)204a043e8c7SAdrian Chadd rtl8366rb_attach(device_t dev)
205a043e8c7SAdrian Chadd {
206a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc;
207477e3effSMichael Zhilin uint16_t rev = 0;
208a043e8c7SAdrian Chadd char name[IFNAMSIZ];
209a043e8c7SAdrian Chadd int err = 0;
210a043e8c7SAdrian Chadd int i;
211a043e8c7SAdrian Chadd
212a043e8c7SAdrian Chadd sc = device_get_softc(dev);
213477e3effSMichael Zhilin
214a043e8c7SAdrian Chadd sc->dev = dev;
215a043e8c7SAdrian Chadd mtx_init(&sc->sc_mtx, "rtl8366rb", NULL, MTX_DEF);
216a043e8c7SAdrian Chadd sc->smi_acquired = 0;
217a043e8c7SAdrian Chadd mtx_init(&sc->callout_mtx, "rtl8366rbcallout", NULL, MTX_DEF);
218a043e8c7SAdrian Chadd
219a043e8c7SAdrian Chadd rtl8366rb_init(dev);
220477e3effSMichael Zhilin smi_read(dev, RTL8366_CVCR, &rev, RTL_WAITOK);
221a043e8c7SAdrian Chadd device_printf(dev, "rev. %d\n", rev & 0x000f);
222a043e8c7SAdrian Chadd
2235a4380b5SMichael Zhilin sc->phy4cpu = 0;
2245a4380b5SMichael Zhilin (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
2255a4380b5SMichael Zhilin "phy4cpu", &sc->phy4cpu);
2265a4380b5SMichael Zhilin
2275a4380b5SMichael Zhilin sc->numphys = sc->phy4cpu ? RTL8366_NUM_PHYS - 1 : RTL8366_NUM_PHYS;
2285a4380b5SMichael Zhilin
2295a4380b5SMichael Zhilin sc->info.es_nports = sc->numphys + 1;
230477e3effSMichael Zhilin sc->info.es_nvlangroups = RTL8366_NUM_VLANS;
231477e3effSMichael Zhilin sc->info.es_vlan_caps = ETHERSWITCH_VLAN_DOT1Q;
2325a4380b5SMichael Zhilin if (sc->chip_type == RTL8366RB)
233477e3effSMichael Zhilin sprintf(sc->info.es_name, "Realtek RTL8366RB");
234477e3effSMichael Zhilin else
235477e3effSMichael Zhilin sprintf(sc->info.es_name, "Realtek RTL8366SR");
236477e3effSMichael Zhilin
237a043e8c7SAdrian Chadd /* attach miibus and phys */
238a043e8c7SAdrian Chadd /* PHYs need an interface, so we generate a dummy one */
2395a4380b5SMichael Zhilin for (i = 0; i < sc->numphys; i++) {
240a043e8c7SAdrian Chadd sc->ifp[i] = if_alloc(IFT_ETHER);
2412e6a8c1aSJustin Hibbits if_setsoftc(sc->ifp[i], sc);
2422e6a8c1aSJustin Hibbits if_setflagbits(sc->ifp[i], IFF_UP | IFF_BROADCAST | IFF_DRV_RUNNING
2432e6a8c1aSJustin Hibbits | IFF_SIMPLEX, 0);
244a043e8c7SAdrian Chadd snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(dev));
245a043e8c7SAdrian Chadd sc->ifname[i] = malloc(strlen(name)+1, M_DEVBUF, M_WAITOK);
246a043e8c7SAdrian Chadd bcopy(name, sc->ifname[i], strlen(name)+1);
247a043e8c7SAdrian Chadd if_initname(sc->ifp[i], sc->ifname[i], i);
248a043e8c7SAdrian Chadd err = mii_attach(dev, &sc->miibus[i], sc->ifp[i], rtl8366rb_ifmedia_upd, \
249a043e8c7SAdrian Chadd rtl8366rb_ifmedia_sts, BMSR_DEFCAPMASK, \
250a043e8c7SAdrian Chadd i, MII_OFFSET_ANY, 0);
251a043e8c7SAdrian Chadd if (err != 0) {
252a043e8c7SAdrian Chadd device_printf(dev, "attaching PHY %d failed\n", i);
253a043e8c7SAdrian Chadd return (err);
254a043e8c7SAdrian Chadd }
255a043e8c7SAdrian Chadd }
256a043e8c7SAdrian Chadd
257723da5d9SJohn Baldwin bus_identify_children(dev);
258a043e8c7SAdrian Chadd bus_enumerate_hinted_children(dev);
25918250ec6SJohn Baldwin bus_attach_children(dev);
260a043e8c7SAdrian Chadd
261a043e8c7SAdrian Chadd callout_init_mtx(&sc->callout_tick, &sc->callout_mtx, 0);
262a043e8c7SAdrian Chadd rtl8366rb_tick(sc);
263a043e8c7SAdrian Chadd
264a043e8c7SAdrian Chadd return (err);
265a043e8c7SAdrian Chadd }
266a043e8c7SAdrian Chadd
267a043e8c7SAdrian Chadd static int
rtl8366rb_detach(device_t dev)268a043e8c7SAdrian Chadd rtl8366rb_detach(device_t dev)
269a043e8c7SAdrian Chadd {
270477e3effSMichael Zhilin struct rtl8366rb_softc *sc;
271*aa48c1aeSJohn Baldwin int error, i;
272*aa48c1aeSJohn Baldwin
273*aa48c1aeSJohn Baldwin error = bus_generic_detach(dev);
274*aa48c1aeSJohn Baldwin if (error != 0)
275*aa48c1aeSJohn Baldwin return (error);
276a043e8c7SAdrian Chadd
277477e3effSMichael Zhilin sc = device_get_softc(dev);
278477e3effSMichael Zhilin
2795a4380b5SMichael Zhilin for (i=0; i < sc->numphys; i++) {
280a043e8c7SAdrian Chadd if (sc->ifp[i] != NULL)
281a043e8c7SAdrian Chadd if_free(sc->ifp[i]);
282a043e8c7SAdrian Chadd free(sc->ifname[i], M_DEVBUF);
283a043e8c7SAdrian Chadd }
284a043e8c7SAdrian Chadd callout_drain(&sc->callout_tick);
285a043e8c7SAdrian Chadd mtx_destroy(&sc->callout_mtx);
286a043e8c7SAdrian Chadd mtx_destroy(&sc->sc_mtx);
287a043e8c7SAdrian Chadd
288a043e8c7SAdrian Chadd return (0);
289a043e8c7SAdrian Chadd }
290a043e8c7SAdrian Chadd
291a043e8c7SAdrian Chadd static void
rtl8366rb_update_ifmedia(int portstatus,u_int * media_status,u_int * media_active)292a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(int portstatus, u_int *media_status, u_int *media_active)
293a043e8c7SAdrian Chadd {
294a043e8c7SAdrian Chadd *media_active = IFM_ETHER;
295a043e8c7SAdrian Chadd *media_status = IFM_AVALID;
296477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_LINK) != 0)
297a043e8c7SAdrian Chadd *media_status |= IFM_ACTIVE;
298a043e8c7SAdrian Chadd else {
299a043e8c7SAdrian Chadd *media_active |= IFM_NONE;
300a043e8c7SAdrian Chadd return;
301a043e8c7SAdrian Chadd }
302477e3effSMichael Zhilin switch (portstatus & RTL8366_PLSR_SPEED_MASK) {
303477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_10:
304a043e8c7SAdrian Chadd *media_active |= IFM_10_T;
305a043e8c7SAdrian Chadd break;
306477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_100:
307a043e8c7SAdrian Chadd *media_active |= IFM_100_TX;
308a043e8c7SAdrian Chadd break;
309477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_1000:
310a043e8c7SAdrian Chadd *media_active |= IFM_1000_T;
311a043e8c7SAdrian Chadd break;
312a043e8c7SAdrian Chadd }
313477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_FULLDUPLEX) != 0)
314a043e8c7SAdrian Chadd *media_active |= IFM_FDX;
315a043e8c7SAdrian Chadd else
316a043e8c7SAdrian Chadd *media_active |= IFM_HDX;
317477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_TXPAUSE) != 0)
318a043e8c7SAdrian Chadd *media_active |= IFM_ETH_TXPAUSE;
319477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_RXPAUSE) != 0)
320a043e8c7SAdrian Chadd *media_active |= IFM_ETH_RXPAUSE;
321a043e8c7SAdrian Chadd }
322a043e8c7SAdrian Chadd
323a043e8c7SAdrian Chadd static void
rtl833rb_miipollstat(struct rtl8366rb_softc * sc)324a043e8c7SAdrian Chadd rtl833rb_miipollstat(struct rtl8366rb_softc *sc)
325a043e8c7SAdrian Chadd {
326a043e8c7SAdrian Chadd int i;
327a043e8c7SAdrian Chadd struct mii_data *mii;
328a043e8c7SAdrian Chadd struct mii_softc *miisc;
329a043e8c7SAdrian Chadd uint16_t value;
330a043e8c7SAdrian Chadd int portstatus;
331a043e8c7SAdrian Chadd
3325a4380b5SMichael Zhilin for (i = 0; i < sc->numphys; i++) {
333a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[i]);
334a043e8c7SAdrian Chadd if ((i % 2) == 0) {
335477e3effSMichael Zhilin if (smi_read(sc->dev, RTL8366_PLSR_BASE + i/2, &value, RTL_NOWAIT) != 0) {
336a043e8c7SAdrian Chadd DEBUG_INCRVAR(callout_blocked);
337a043e8c7SAdrian Chadd return;
338a043e8c7SAdrian Chadd }
339a043e8c7SAdrian Chadd portstatus = value & 0xff;
340a043e8c7SAdrian Chadd } else {
341a043e8c7SAdrian Chadd portstatus = (value >> 8) & 0xff;
342a043e8c7SAdrian Chadd }
343a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(portstatus, &mii->mii_media_status, &mii->mii_media_active);
344a043e8c7SAdrian Chadd LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
345a043e8c7SAdrian Chadd if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) != miisc->mii_inst)
346a043e8c7SAdrian Chadd continue;
347a043e8c7SAdrian Chadd mii_phy_update(miisc, MII_POLLSTAT);
348a043e8c7SAdrian Chadd }
349a043e8c7SAdrian Chadd }
350a043e8c7SAdrian Chadd }
351a043e8c7SAdrian Chadd
352a043e8c7SAdrian Chadd static void
rtl8366rb_tick(void * arg)353a043e8c7SAdrian Chadd rtl8366rb_tick(void *arg)
354a043e8c7SAdrian Chadd {
355477e3effSMichael Zhilin struct rtl8366rb_softc *sc;
356477e3effSMichael Zhilin
357477e3effSMichael Zhilin sc = arg;
358a043e8c7SAdrian Chadd
359a043e8c7SAdrian Chadd rtl833rb_miipollstat(sc);
360a043e8c7SAdrian Chadd callout_reset(&sc->callout_tick, hz, rtl8366rb_tick, sc);
361a043e8c7SAdrian Chadd }
362a043e8c7SAdrian Chadd
363a043e8c7SAdrian Chadd static int
smi_probe(device_t dev)364a043e8c7SAdrian Chadd smi_probe(device_t dev)
365a043e8c7SAdrian Chadd {
366477e3effSMichael Zhilin struct rtl8366rb_softc *sc;
367a043e8c7SAdrian Chadd device_t iicbus, iicha;
368477e3effSMichael Zhilin int err, i, j;
369a043e8c7SAdrian Chadd uint16_t chipid;
370a043e8c7SAdrian Chadd char bytes[2];
371a043e8c7SAdrian Chadd int xferd;
372a043e8c7SAdrian Chadd
373477e3effSMichael Zhilin sc = device_get_softc(dev);
374477e3effSMichael Zhilin
375a043e8c7SAdrian Chadd iicbus = device_get_parent(dev);
376a043e8c7SAdrian Chadd iicha = device_get_parent(iicbus);
377477e3effSMichael Zhilin
378477e3effSMichael Zhilin for (i = 0; i < 2; ++i) {
379477e3effSMichael Zhilin iicbus_reset(iicbus, IIC_FASTEST, RTL8366_IIC_ADDR, NULL);
380477e3effSMichael Zhilin for (j=3; j--; ) {
381a043e8c7SAdrian Chadd IICBUS_STOP(iicha);
382a043e8c7SAdrian Chadd /*
383a043e8c7SAdrian Chadd * we go directly to the host adapter because iicbus.c
384a043e8c7SAdrian Chadd * only issues a stop on a bus that was successfully started.
385a043e8c7SAdrian Chadd */
386a043e8c7SAdrian Chadd }
387a043e8c7SAdrian Chadd err = iicbus_request_bus(iicbus, dev, IIC_WAIT);
388a043e8c7SAdrian Chadd if (err != 0)
389a043e8c7SAdrian Chadd goto out;
390477e3effSMichael Zhilin err = iicbus_start(iicbus, RTL8366_IIC_ADDR | RTL_IICBUS_READ, RTL_IICBUS_TIMEOUT);
391a043e8c7SAdrian Chadd if (err != 0)
392a043e8c7SAdrian Chadd goto out;
393477e3effSMichael Zhilin if (i == 0) {
394477e3effSMichael Zhilin bytes[0] = RTL8366RB_CIR & 0xff;
395477e3effSMichael Zhilin bytes[1] = (RTL8366RB_CIR >> 8) & 0xff;
396477e3effSMichael Zhilin } else {
397477e3effSMichael Zhilin bytes[0] = RTL8366SR_CIR & 0xff;
398477e3effSMichael Zhilin bytes[1] = (RTL8366SR_CIR >> 8) & 0xff;
399477e3effSMichael Zhilin }
400a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 2, &xferd, RTL_IICBUS_TIMEOUT);
401a043e8c7SAdrian Chadd if (err != 0)
402a043e8c7SAdrian Chadd goto out;
403a043e8c7SAdrian Chadd err = iicbus_read(iicbus, bytes, 2, &xferd, IIC_LAST_READ, 0);
404a043e8c7SAdrian Chadd if (err != 0)
405a043e8c7SAdrian Chadd goto out;
406a043e8c7SAdrian Chadd chipid = ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff);
407477e3effSMichael Zhilin if (i == 0 && chipid == RTL8366RB_CIR_ID8366RB) {
408a043e8c7SAdrian Chadd DPRINTF(dev, "chip id 0x%04x\n", chipid);
4095a4380b5SMichael Zhilin sc->chip_type = RTL8366RB;
410477e3effSMichael Zhilin err = 0;
411477e3effSMichael Zhilin break;
412477e3effSMichael Zhilin }
413477e3effSMichael Zhilin if (i == 1 && chipid == RTL8366SR_CIR_ID8366SR) {
414477e3effSMichael Zhilin DPRINTF(dev, "chip id 0x%04x\n", chipid);
4155a4380b5SMichael Zhilin sc->chip_type = RTL8366SR;
416477e3effSMichael Zhilin err = 0;
417477e3effSMichael Zhilin break;
418477e3effSMichael Zhilin }
419477e3effSMichael Zhilin if (i == 0) {
420477e3effSMichael Zhilin iicbus_stop(iicbus);
421477e3effSMichael Zhilin iicbus_release_bus(iicbus, dev);
422477e3effSMichael Zhilin }
423477e3effSMichael Zhilin }
424477e3effSMichael Zhilin if (i == 2)
425a043e8c7SAdrian Chadd err = ENXIO;
426a043e8c7SAdrian Chadd out:
427a043e8c7SAdrian Chadd iicbus_stop(iicbus);
428a043e8c7SAdrian Chadd iicbus_release_bus(iicbus, dev);
429a043e8c7SAdrian Chadd return (err == 0 ? 0 : ENXIO);
430a043e8c7SAdrian Chadd }
431a043e8c7SAdrian Chadd
432a043e8c7SAdrian Chadd static int
smi_acquire(struct rtl8366rb_softc * sc,int sleep)433a043e8c7SAdrian Chadd smi_acquire(struct rtl8366rb_softc *sc, int sleep)
434a043e8c7SAdrian Chadd {
435a043e8c7SAdrian Chadd int r = 0;
436a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK)
437a043e8c7SAdrian Chadd RTL_LOCK(sc);
438a043e8c7SAdrian Chadd else
439a043e8c7SAdrian Chadd if (RTL_TRYLOCK(sc) == 0)
440a043e8c7SAdrian Chadd return (EWOULDBLOCK);
441a043e8c7SAdrian Chadd if (sc->smi_acquired == RTL_SMI_ACQUIRED)
442a043e8c7SAdrian Chadd r = EBUSY;
443a043e8c7SAdrian Chadd else {
444a043e8c7SAdrian Chadd r = iicbus_request_bus(device_get_parent(sc->dev), sc->dev, \
445a043e8c7SAdrian Chadd sleep == RTL_WAITOK ? IIC_WAIT : IIC_DONTWAIT);
446a043e8c7SAdrian Chadd if (r == 0)
447a043e8c7SAdrian Chadd sc->smi_acquired = RTL_SMI_ACQUIRED;
448a043e8c7SAdrian Chadd }
449a043e8c7SAdrian Chadd RTL_UNLOCK(sc);
450a043e8c7SAdrian Chadd return (r);
451a043e8c7SAdrian Chadd }
452a043e8c7SAdrian Chadd
453a043e8c7SAdrian Chadd static int
smi_release(struct rtl8366rb_softc * sc,int sleep)454a043e8c7SAdrian Chadd smi_release(struct rtl8366rb_softc *sc, int sleep)
455a043e8c7SAdrian Chadd {
456a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK)
457a043e8c7SAdrian Chadd RTL_LOCK(sc);
458a043e8c7SAdrian Chadd else
459a043e8c7SAdrian Chadd if (RTL_TRYLOCK(sc) == 0)
460a043e8c7SAdrian Chadd return (EWOULDBLOCK);
461a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc);
462a043e8c7SAdrian Chadd iicbus_release_bus(device_get_parent(sc->dev), sc->dev);
463a043e8c7SAdrian Chadd sc->smi_acquired = 0;
464a043e8c7SAdrian Chadd RTL_UNLOCK(sc);
465a043e8c7SAdrian Chadd return (0);
466a043e8c7SAdrian Chadd }
467a043e8c7SAdrian Chadd
468a043e8c7SAdrian Chadd static int
smi_select(device_t dev,int op,int sleep)469a043e8c7SAdrian Chadd smi_select(device_t dev, int op, int sleep)
470a043e8c7SAdrian Chadd {
471477e3effSMichael Zhilin struct rtl8366rb_softc *sc;
472a043e8c7SAdrian Chadd int err, i;
473477e3effSMichael Zhilin device_t iicbus;
474477e3effSMichael Zhilin struct iicbus_ivar *devi;
475477e3effSMichael Zhilin int slave;
476477e3effSMichael Zhilin
477477e3effSMichael Zhilin sc = device_get_softc(dev);
478477e3effSMichael Zhilin
479477e3effSMichael Zhilin iicbus = device_get_parent(dev);
480477e3effSMichael Zhilin devi = IICBUS_IVAR(dev);
481477e3effSMichael Zhilin slave = devi->addr;
482a043e8c7SAdrian Chadd
483a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT((struct rtl8366rb_softc *)device_get_softc(dev));
484477e3effSMichael Zhilin
4855a4380b5SMichael Zhilin if (sc->chip_type == RTL8366SR) { // RTL8366SR work around
486477e3effSMichael Zhilin // this is same work around at probe
487477e3effSMichael Zhilin for (int i=3; i--; )
488477e3effSMichael Zhilin IICBUS_STOP(device_get_parent(device_get_parent(dev)));
489477e3effSMichael Zhilin }
490a043e8c7SAdrian Chadd /*
491a043e8c7SAdrian Chadd * The chip does not use clock stretching when it is busy,
492a043e8c7SAdrian Chadd * instead ignoring the command. Retry a few times.
493a043e8c7SAdrian Chadd */
494a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) {
495a043e8c7SAdrian Chadd err = iicbus_start(iicbus, slave | op, RTL_IICBUS_TIMEOUT);
496a043e8c7SAdrian Chadd if (err != IIC_ENOACK)
497a043e8c7SAdrian Chadd break;
498a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK) {
499a043e8c7SAdrian Chadd DEBUG_INCRVAR(iic_select_retries);
500a043e8c7SAdrian Chadd pause("smi_select", RTL_IICBUS_RETRY_SLEEP);
501a043e8c7SAdrian Chadd } else
502a043e8c7SAdrian Chadd break;
503a043e8c7SAdrian Chadd }
504a043e8c7SAdrian Chadd return (err);
505a043e8c7SAdrian Chadd }
506a043e8c7SAdrian Chadd
507a043e8c7SAdrian Chadd static int
smi_read_locked(struct rtl8366rb_softc * sc,uint16_t addr,uint16_t * data,int sleep)508a043e8c7SAdrian Chadd smi_read_locked(struct rtl8366rb_softc *sc, uint16_t addr, uint16_t *data, int sleep)
509a043e8c7SAdrian Chadd {
510a043e8c7SAdrian Chadd int err;
511477e3effSMichael Zhilin device_t iicbus;
512a043e8c7SAdrian Chadd char bytes[2];
513a043e8c7SAdrian Chadd int xferd;
514a043e8c7SAdrian Chadd
515477e3effSMichael Zhilin iicbus = device_get_parent(sc->dev);
516477e3effSMichael Zhilin
517a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc);
518a043e8c7SAdrian Chadd bytes[0] = addr & 0xff;
519a043e8c7SAdrian Chadd bytes[1] = (addr >> 8) & 0xff;
520a043e8c7SAdrian Chadd err = smi_select(sc->dev, RTL_IICBUS_READ, sleep);
521a043e8c7SAdrian Chadd if (err != 0)
522a043e8c7SAdrian Chadd goto out;
523a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 2, &xferd, RTL_IICBUS_TIMEOUT);
524a043e8c7SAdrian Chadd if (err != 0)
525a043e8c7SAdrian Chadd goto out;
526a043e8c7SAdrian Chadd err = iicbus_read(iicbus, bytes, 2, &xferd, IIC_LAST_READ, 0);
527a043e8c7SAdrian Chadd if (err != 0)
528a043e8c7SAdrian Chadd goto out;
529a043e8c7SAdrian Chadd *data = ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff);
530a043e8c7SAdrian Chadd
531a043e8c7SAdrian Chadd out:
532a043e8c7SAdrian Chadd iicbus_stop(iicbus);
533a043e8c7SAdrian Chadd return (err);
534a043e8c7SAdrian Chadd }
535a043e8c7SAdrian Chadd
536a043e8c7SAdrian Chadd static int
smi_write_locked(struct rtl8366rb_softc * sc,uint16_t addr,uint16_t data,int sleep)537a043e8c7SAdrian Chadd smi_write_locked(struct rtl8366rb_softc *sc, uint16_t addr, uint16_t data, int sleep)
538a043e8c7SAdrian Chadd {
539a043e8c7SAdrian Chadd int err;
540477e3effSMichael Zhilin device_t iicbus;
541a043e8c7SAdrian Chadd char bytes[4];
542a043e8c7SAdrian Chadd int xferd;
543a043e8c7SAdrian Chadd
544477e3effSMichael Zhilin iicbus = device_get_parent(sc->dev);
545477e3effSMichael Zhilin
546a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc);
547a043e8c7SAdrian Chadd bytes[0] = addr & 0xff;
548a043e8c7SAdrian Chadd bytes[1] = (addr >> 8) & 0xff;
549a043e8c7SAdrian Chadd bytes[2] = data & 0xff;
550a043e8c7SAdrian Chadd bytes[3] = (data >> 8) & 0xff;
551a043e8c7SAdrian Chadd
552a043e8c7SAdrian Chadd err = smi_select(sc->dev, RTL_IICBUS_WRITE, sleep);
553a043e8c7SAdrian Chadd if (err == 0)
554a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 4, &xferd, RTL_IICBUS_TIMEOUT);
555a043e8c7SAdrian Chadd iicbus_stop(iicbus);
556a043e8c7SAdrian Chadd
557a043e8c7SAdrian Chadd return (err);
558a043e8c7SAdrian Chadd }
559a043e8c7SAdrian Chadd
560a043e8c7SAdrian Chadd static int
smi_read(device_t dev,uint16_t addr,uint16_t * data,int sleep)561a043e8c7SAdrian Chadd smi_read(device_t dev, uint16_t addr, uint16_t *data, int sleep)
562a043e8c7SAdrian Chadd {
563477e3effSMichael Zhilin struct rtl8366rb_softc *sc;
564a043e8c7SAdrian Chadd int err;
565a043e8c7SAdrian Chadd
566477e3effSMichael Zhilin sc = device_get_softc(dev);
567477e3effSMichael Zhilin
568a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep);
569a043e8c7SAdrian Chadd if (err != 0)
570a043e8c7SAdrian Chadd return (EBUSY);
571a043e8c7SAdrian Chadd err = smi_read_locked(sc, addr, data, sleep);
572a043e8c7SAdrian Chadd smi_release(sc, sleep);
573a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_read()=%d: addr=%04x\n", addr);
574a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO);
575a043e8c7SAdrian Chadd }
576a043e8c7SAdrian Chadd
577a043e8c7SAdrian Chadd static int
smi_write(device_t dev,uint16_t addr,uint16_t data,int sleep)578a043e8c7SAdrian Chadd smi_write(device_t dev, uint16_t addr, uint16_t data, int sleep)
579a043e8c7SAdrian Chadd {
580477e3effSMichael Zhilin struct rtl8366rb_softc *sc;
581a043e8c7SAdrian Chadd int err;
582a043e8c7SAdrian Chadd
583477e3effSMichael Zhilin sc = device_get_softc(dev);
584477e3effSMichael Zhilin
585a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep);
586a043e8c7SAdrian Chadd if (err != 0)
587a043e8c7SAdrian Chadd return (EBUSY);
588a043e8c7SAdrian Chadd err = smi_write_locked(sc, addr, data, sleep);
589a043e8c7SAdrian Chadd smi_release(sc, sleep);
590a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_write()=%d: addr=%04x\n", addr);
591a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO);
592a043e8c7SAdrian Chadd }
593a043e8c7SAdrian Chadd
594a043e8c7SAdrian Chadd static int
smi_rmw(device_t dev,uint16_t addr,uint16_t mask,uint16_t data,int sleep)595a043e8c7SAdrian Chadd smi_rmw(device_t dev, uint16_t addr, uint16_t mask, uint16_t data, int sleep)
596a043e8c7SAdrian Chadd {
597477e3effSMichael Zhilin struct rtl8366rb_softc *sc;
598a043e8c7SAdrian Chadd int err;
599a043e8c7SAdrian Chadd uint16_t oldv, newv;
600a043e8c7SAdrian Chadd
601477e3effSMichael Zhilin sc = device_get_softc(dev);
602477e3effSMichael Zhilin
603a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep);
604a043e8c7SAdrian Chadd if (err != 0)
605a043e8c7SAdrian Chadd return (EBUSY);
606a043e8c7SAdrian Chadd if (err == 0) {
607a043e8c7SAdrian Chadd err = smi_read_locked(sc, addr, &oldv, sleep);
608a043e8c7SAdrian Chadd if (err == 0) {
609a043e8c7SAdrian Chadd newv = oldv & ~mask;
610a043e8c7SAdrian Chadd newv |= data & mask;
611a043e8c7SAdrian Chadd if (newv != oldv)
612a043e8c7SAdrian Chadd err = smi_write_locked(sc, addr, newv, sleep);
613a043e8c7SAdrian Chadd }
614a043e8c7SAdrian Chadd }
615a043e8c7SAdrian Chadd smi_release(sc, sleep);
616a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_rmw()=%d: addr=%04x\n", addr);
617a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO);
618a043e8c7SAdrian Chadd }
619a043e8c7SAdrian Chadd
620a043e8c7SAdrian Chadd static etherswitch_info_t *
rtl_getinfo(device_t dev)621a043e8c7SAdrian Chadd rtl_getinfo(device_t dev)
622a043e8c7SAdrian Chadd {
623477e3effSMichael Zhilin struct rtl8366rb_softc *sc;
624477e3effSMichael Zhilin
625477e3effSMichael Zhilin sc = device_get_softc(dev);
626477e3effSMichael Zhilin
627477e3effSMichael Zhilin return (&sc->info);
628a043e8c7SAdrian Chadd }
629a043e8c7SAdrian Chadd
630a043e8c7SAdrian Chadd static int
rtl_readreg(device_t dev,int reg)631a043e8c7SAdrian Chadd rtl_readreg(device_t dev, int reg)
632a043e8c7SAdrian Chadd {
633477e3effSMichael Zhilin uint16_t data;
634477e3effSMichael Zhilin
635477e3effSMichael Zhilin data = 0;
636a043e8c7SAdrian Chadd
637a043e8c7SAdrian Chadd smi_read(dev, reg, &data, RTL_WAITOK);
638a043e8c7SAdrian Chadd return (data);
639a043e8c7SAdrian Chadd }
640a043e8c7SAdrian Chadd
641a043e8c7SAdrian Chadd static int
rtl_writereg(device_t dev,int reg,int value)642a043e8c7SAdrian Chadd rtl_writereg(device_t dev, int reg, int value)
643a043e8c7SAdrian Chadd {
644a043e8c7SAdrian Chadd return (smi_write(dev, reg, value, RTL_WAITOK));
645a043e8c7SAdrian Chadd }
646a043e8c7SAdrian Chadd
647a043e8c7SAdrian Chadd static int
rtl_getport(device_t dev,etherswitch_port_t * p)648a043e8c7SAdrian Chadd rtl_getport(device_t dev, etherswitch_port_t *p)
649a043e8c7SAdrian Chadd {
650a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc;
651a043e8c7SAdrian Chadd struct ifmedia *ifm;
652a043e8c7SAdrian Chadd struct mii_data *mii;
653477e3effSMichael Zhilin struct ifmediareq *ifmr;
654a043e8c7SAdrian Chadd uint16_t v;
655a3219359SAdrian Chadd int err, vlangroup;
656a043e8c7SAdrian Chadd
657a3219359SAdrian Chadd sc = device_get_softc(dev);
658477e3effSMichael Zhilin
659477e3effSMichael Zhilin ifmr = &p->es_ifmr;
660477e3effSMichael Zhilin
6615a4380b5SMichael Zhilin if (p->es_port < 0 || p->es_port >= (sc->numphys + 1))
662477e3effSMichael Zhilin return (ENXIO);
6635a4380b5SMichael Zhilin if (sc->phy4cpu && p->es_port == sc->numphys) {
6645a4380b5SMichael Zhilin vlangroup = RTL8366_PVCR_GET(p->es_port + 1,
6655a4380b5SMichael Zhilin rtl_readreg(dev, RTL8366_PVCR_REG(p->es_port + 1)));
6665a4380b5SMichael Zhilin } else {
667477e3effSMichael Zhilin vlangroup = RTL8366_PVCR_GET(p->es_port,
668477e3effSMichael Zhilin rtl_readreg(dev, RTL8366_PVCR_REG(p->es_port)));
6695a4380b5SMichael Zhilin }
670bfae9329SLuiz Otavio O Souza p->es_pvid = sc->vid[vlangroup] & ETHERSWITCH_VID_MASK;
671a043e8c7SAdrian Chadd
6725a4380b5SMichael Zhilin if (p->es_port < sc->numphys) {
673a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[p->es_port]);
674a043e8c7SAdrian Chadd ifm = &mii->mii_media;
675a043e8c7SAdrian Chadd err = ifmedia_ioctl(sc->ifp[p->es_port], &p->es_ifr, ifm, SIOCGIFMEDIA);
676a043e8c7SAdrian Chadd if (err)
677a043e8c7SAdrian Chadd return (err);
678a043e8c7SAdrian Chadd } else {
679a043e8c7SAdrian Chadd /* fill in fixed values for CPU port */
680dddab089SLuiz Otavio O Souza p->es_flags |= ETHERSWITCH_PORT_CPU;
681477e3effSMichael Zhilin smi_read(dev, RTL8366_PLSR_BASE + (RTL8366_NUM_PHYS)/2, &v, RTL_WAITOK);
682477e3effSMichael Zhilin v = v >> (8 * ((RTL8366_NUM_PHYS) % 2));
683a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(v, &ifmr->ifm_status, &ifmr->ifm_active);
684a043e8c7SAdrian Chadd ifmr->ifm_current = ifmr->ifm_active;
685a043e8c7SAdrian Chadd ifmr->ifm_mask = 0;
686a043e8c7SAdrian Chadd ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
68728b07d23SLuiz Otavio O Souza /* Return our static media list. */
68828b07d23SLuiz Otavio O Souza if (ifmr->ifm_count > 0) {
68928b07d23SLuiz Otavio O Souza ifmr->ifm_count = 1;
69028b07d23SLuiz Otavio O Souza ifmr->ifm_ulist[0] = IFM_MAKEWORD(IFM_ETHER, IFM_1000_T,
69128b07d23SLuiz Otavio O Souza IFM_FDX, 0);
69228b07d23SLuiz Otavio O Souza } else
69328b07d23SLuiz Otavio O Souza ifmr->ifm_count = 0;
694a043e8c7SAdrian Chadd }
695a043e8c7SAdrian Chadd return (0);
696a043e8c7SAdrian Chadd }
697a043e8c7SAdrian Chadd
698a043e8c7SAdrian Chadd static int
rtl_setport(device_t dev,etherswitch_port_t * p)699a043e8c7SAdrian Chadd rtl_setport(device_t dev, etherswitch_port_t *p)
700a043e8c7SAdrian Chadd {
701a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc;
702477e3effSMichael Zhilin int i, err, vlangroup;
703a043e8c7SAdrian Chadd struct ifmedia *ifm;
704a043e8c7SAdrian Chadd struct mii_data *mii;
7055a4380b5SMichael Zhilin int port;
706a043e8c7SAdrian Chadd
707a3219359SAdrian Chadd sc = device_get_softc(dev);
708477e3effSMichael Zhilin
7095a4380b5SMichael Zhilin if (p->es_port < 0 || p->es_port >= (sc->numphys + 1))
710477e3effSMichael Zhilin return (ENXIO);
711a3219359SAdrian Chadd vlangroup = -1;
712477e3effSMichael Zhilin for (i = 0; i < RTL8366_NUM_VLANS; i++) {
713bfae9329SLuiz Otavio O Souza if ((sc->vid[i] & ETHERSWITCH_VID_MASK) == p->es_pvid) {
714a3219359SAdrian Chadd vlangroup = i;
715a3219359SAdrian Chadd break;
716a3219359SAdrian Chadd }
717a3219359SAdrian Chadd }
718a3219359SAdrian Chadd if (vlangroup == -1)
719a3219359SAdrian Chadd return (ENXIO);
7205a4380b5SMichael Zhilin if (sc->phy4cpu && p->es_port == sc->numphys) {
7215a4380b5SMichael Zhilin port = p->es_port + 1;
7225a4380b5SMichael Zhilin } else {
7235a4380b5SMichael Zhilin port = p->es_port;
7245a4380b5SMichael Zhilin }
7255a4380b5SMichael Zhilin err = smi_rmw(dev, RTL8366_PVCR_REG(port),
7265a4380b5SMichael Zhilin RTL8366_PVCR_VAL(port, RTL8366_PVCR_PORT_MASK),
7275a4380b5SMichael Zhilin RTL8366_PVCR_VAL(port, vlangroup), RTL_WAITOK);
728a043e8c7SAdrian Chadd if (err)
729a043e8c7SAdrian Chadd return (err);
7305a4380b5SMichael Zhilin /* CPU Port */
7315a4380b5SMichael Zhilin if (p->es_port == sc->numphys)
732dddab089SLuiz Otavio O Souza return (0);
733a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[p->es_port]);
734a043e8c7SAdrian Chadd ifm = &mii->mii_media;
735a043e8c7SAdrian Chadd err = ifmedia_ioctl(sc->ifp[p->es_port], &p->es_ifr, ifm, SIOCSIFMEDIA);
736a043e8c7SAdrian Chadd return (err);
737a043e8c7SAdrian Chadd }
738a043e8c7SAdrian Chadd
739a043e8c7SAdrian Chadd static int
rtl_getvgroup(device_t dev,etherswitch_vlangroup_t * vg)740a043e8c7SAdrian Chadd rtl_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
741a043e8c7SAdrian Chadd {
742bfae9329SLuiz Otavio O Souza struct rtl8366rb_softc *sc;
743a043e8c7SAdrian Chadd uint16_t vmcr[3];
744a043e8c7SAdrian Chadd int i;
7455a4380b5SMichael Zhilin int member, untagged;
746a043e8c7SAdrian Chadd
747bfae9329SLuiz Otavio O Souza sc = device_get_softc(dev);
748477e3effSMichael Zhilin
749477e3effSMichael Zhilin for (i=0; i<RTL8366_VMCR_MULT; i++)
750477e3effSMichael Zhilin vmcr[i] = rtl_readreg(dev, RTL8366_VMCR(i, vg->es_vlangroup));
751477e3effSMichael Zhilin
752bfae9329SLuiz Otavio O Souza vg->es_vid = sc->vid[vg->es_vlangroup];
7535a4380b5SMichael Zhilin member = RTL8366_VMCR_MEMBER(vmcr);
7545a4380b5SMichael Zhilin untagged = RTL8366_VMCR_UNTAG(vmcr);
7555a4380b5SMichael Zhilin if (sc->phy4cpu) {
7565a4380b5SMichael Zhilin vg->es_member_ports = ((member & 0x20) >> 1) | (member & 0x0f);
7575a4380b5SMichael Zhilin vg->es_untagged_ports = ((untagged & 0x20) >> 1) | (untagged & 0x0f);
7585a4380b5SMichael Zhilin } else {
7595a4380b5SMichael Zhilin vg->es_member_ports = member;
7605a4380b5SMichael Zhilin vg->es_untagged_ports = untagged;
7615a4380b5SMichael Zhilin }
762477e3effSMichael Zhilin vg->es_fid = RTL8366_VMCR_FID(vmcr);
763a043e8c7SAdrian Chadd return (0);
764a043e8c7SAdrian Chadd }
765a043e8c7SAdrian Chadd
766a043e8c7SAdrian Chadd static int
rtl_setvgroup(device_t dev,etherswitch_vlangroup_t * vg)767a043e8c7SAdrian Chadd rtl_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
768a043e8c7SAdrian Chadd {
769a3219359SAdrian Chadd struct rtl8366rb_softc *sc;
770477e3effSMichael Zhilin int g;
7715a4380b5SMichael Zhilin int member, untagged;
772a043e8c7SAdrian Chadd
773a3219359SAdrian Chadd sc = device_get_softc(dev);
774477e3effSMichael Zhilin
775477e3effSMichael Zhilin g = vg->es_vlangroup;
776477e3effSMichael Zhilin
777a3219359SAdrian Chadd sc->vid[g] = vg->es_vid;
778bfae9329SLuiz Otavio O Souza /* VLAN group disabled ? */
779bfae9329SLuiz Otavio O Souza if (vg->es_member_ports == 0 && vg->es_untagged_ports == 0 && vg->es_vid == 0)
780bfae9329SLuiz Otavio O Souza return (0);
781bfae9329SLuiz Otavio O Souza sc->vid[g] |= ETHERSWITCH_VID_VALID;
782477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_DOT1Q_REG, g),
783477e3effSMichael Zhilin (vg->es_vid << RTL8366_VMCR_DOT1Q_VID_SHIFT) & RTL8366_VMCR_DOT1Q_VID_MASK);
7845a4380b5SMichael Zhilin if (sc->phy4cpu) {
7855a4380b5SMichael Zhilin /* add space at phy4 */
7865a4380b5SMichael Zhilin member = (vg->es_member_ports & 0x0f) |
7875a4380b5SMichael Zhilin ((vg->es_member_ports & 0x10) << 1);
7885a4380b5SMichael Zhilin untagged = (vg->es_untagged_ports & 0x0f) |
7895a4380b5SMichael Zhilin ((vg->es_untagged_ports & 0x10) << 1);
7905a4380b5SMichael Zhilin } else {
7915a4380b5SMichael Zhilin member = vg->es_member_ports;
7925a4380b5SMichael Zhilin untagged = vg->es_untagged_ports;
7935a4380b5SMichael Zhilin }
7945a4380b5SMichael Zhilin if (sc->chip_type == RTL8366RB) {
795477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, g),
7965a4380b5SMichael Zhilin ((member << RTL8366_VMCR_MU_MEMBER_SHIFT) & RTL8366_VMCR_MU_MEMBER_MASK) |
7975a4380b5SMichael Zhilin ((untagged << RTL8366_VMCR_MU_UNTAG_SHIFT) & RTL8366_VMCR_MU_UNTAG_MASK));
798477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_FID_REG, g),
799a043e8c7SAdrian Chadd vg->es_fid);
800477e3effSMichael Zhilin } else {
801477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, g),
8025a4380b5SMichael Zhilin ((member << RTL8366_VMCR_MU_MEMBER_SHIFT) & RTL8366_VMCR_MU_MEMBER_MASK) |
8035a4380b5SMichael Zhilin ((untagged << RTL8366_VMCR_MU_UNTAG_SHIFT) & RTL8366_VMCR_MU_UNTAG_MASK) |
804477e3effSMichael Zhilin ((vg->es_fid << RTL8366_VMCR_FID_FID_SHIFT) & RTL8366_VMCR_FID_FID_MASK));
805477e3effSMichael Zhilin }
806a043e8c7SAdrian Chadd return (0);
807a043e8c7SAdrian Chadd }
808a043e8c7SAdrian Chadd
809a043e8c7SAdrian Chadd static int
rtl_getconf(device_t dev,etherswitch_conf_t * conf)810bfae9329SLuiz Otavio O Souza rtl_getconf(device_t dev, etherswitch_conf_t *conf)
811bfae9329SLuiz Otavio O Souza {
812bfae9329SLuiz Otavio O Souza
813bfae9329SLuiz Otavio O Souza /* Return the VLAN mode. */
814bfae9329SLuiz Otavio O Souza conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
815bfae9329SLuiz Otavio O Souza conf->vlan_mode = ETHERSWITCH_VLAN_DOT1Q;
816bfae9329SLuiz Otavio O Souza
817bfae9329SLuiz Otavio O Souza return (0);
818bfae9329SLuiz Otavio O Souza }
819bfae9329SLuiz Otavio O Souza
820bfae9329SLuiz Otavio O Souza static int
rtl_readphy(device_t dev,int phy,int reg)821a043e8c7SAdrian Chadd rtl_readphy(device_t dev, int phy, int reg)
822a043e8c7SAdrian Chadd {
823477e3effSMichael Zhilin struct rtl8366rb_softc *sc;
824477e3effSMichael Zhilin uint16_t data;
825a043e8c7SAdrian Chadd int err, i, sleep;
826a043e8c7SAdrian Chadd
827477e3effSMichael Zhilin sc = device_get_softc(dev);
828477e3effSMichael Zhilin
829477e3effSMichael Zhilin data = 0;
830477e3effSMichael Zhilin
831477e3effSMichael Zhilin if (phy < 0 || phy >= RTL8366_NUM_PHYS)
832a043e8c7SAdrian Chadd return (ENXIO);
833477e3effSMichael Zhilin if (reg < 0 || reg >= RTL8366_NUM_PHY_REG)
834a043e8c7SAdrian Chadd return (ENXIO);
835a043e8c7SAdrian Chadd sleep = RTL_WAITOK;
836a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep);
837a043e8c7SAdrian Chadd if (err != 0)
838a043e8c7SAdrian Chadd return (EBUSY);
839a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) {
840477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PACR, RTL8366_PACR_READ, sleep);
841a043e8c7SAdrian Chadd if (err == 0)
842477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PHYREG(phy, 0, reg), 0, sleep);
843a043e8c7SAdrian Chadd if (err == 0) {
844477e3effSMichael Zhilin err = smi_read_locked(sc, RTL8366_PADR, &data, sleep);
845a043e8c7SAdrian Chadd break;
846a043e8c7SAdrian Chadd }
847a043e8c7SAdrian Chadd DEBUG_INCRVAR(phy_access_retries);
848a043e8c7SAdrian Chadd DPRINTF(dev, "rtl_readphy(): chip not responsive, retrying %d more times\n", i);
849a043e8c7SAdrian Chadd pause("rtl_readphy", RTL_IICBUS_RETRY_SLEEP);
850a043e8c7SAdrian Chadd }
851a043e8c7SAdrian Chadd smi_release(sc, sleep);
852a043e8c7SAdrian Chadd DEVERR(dev, err, "rtl_readphy()=%d: phy=%d.%02x\n", phy, reg);
853a043e8c7SAdrian Chadd return (data);
854a043e8c7SAdrian Chadd }
855a043e8c7SAdrian Chadd
856a043e8c7SAdrian Chadd static int
rtl_writephy(device_t dev,int phy,int reg,int data)857a043e8c7SAdrian Chadd rtl_writephy(device_t dev, int phy, int reg, int data)
858a043e8c7SAdrian Chadd {
859477e3effSMichael Zhilin struct rtl8366rb_softc *sc;
860a043e8c7SAdrian Chadd int err, i, sleep;
861a043e8c7SAdrian Chadd
862477e3effSMichael Zhilin sc = device_get_softc(dev);
863477e3effSMichael Zhilin
864477e3effSMichael Zhilin if (phy < 0 || phy >= RTL8366_NUM_PHYS)
865a043e8c7SAdrian Chadd return (ENXIO);
866477e3effSMichael Zhilin if (reg < 0 || reg >= RTL8366_NUM_PHY_REG)
867a043e8c7SAdrian Chadd return (ENXIO);
868a043e8c7SAdrian Chadd sleep = RTL_WAITOK;
869a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep);
870a043e8c7SAdrian Chadd if (err != 0)
871a043e8c7SAdrian Chadd return (EBUSY);
872a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) {
873477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PACR, RTL8366_PACR_WRITE, sleep);
874a043e8c7SAdrian Chadd if (err == 0)
875477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PHYREG(phy, 0, reg), data, sleep);
876a043e8c7SAdrian Chadd if (err == 0) {
877a043e8c7SAdrian Chadd break;
878a043e8c7SAdrian Chadd }
879a043e8c7SAdrian Chadd DEBUG_INCRVAR(phy_access_retries);
880a043e8c7SAdrian Chadd DPRINTF(dev, "rtl_writephy(): chip not responsive, retrying %d more tiems\n", i);
881a043e8c7SAdrian Chadd pause("rtl_writephy", RTL_IICBUS_RETRY_SLEEP);
882a043e8c7SAdrian Chadd }
883a043e8c7SAdrian Chadd smi_release(sc, sleep);
884a043e8c7SAdrian Chadd DEVERR(dev, err, "rtl_writephy()=%d: phy=%d.%02x\n", phy, reg);
885a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO);
886a043e8c7SAdrian Chadd }
887a043e8c7SAdrian Chadd
888a043e8c7SAdrian Chadd static int
rtl8366rb_ifmedia_upd(if_t ifp)8892e6a8c1aSJustin Hibbits rtl8366rb_ifmedia_upd(if_t ifp)
890a043e8c7SAdrian Chadd {
891477e3effSMichael Zhilin struct rtl8366rb_softc *sc;
892477e3effSMichael Zhilin struct mii_data *mii;
893477e3effSMichael Zhilin
8942e6a8c1aSJustin Hibbits sc = if_getsoftc(ifp);
8952e6a8c1aSJustin Hibbits mii = device_get_softc(sc->miibus[if_getdunit(ifp)]);
896a043e8c7SAdrian Chadd
897a043e8c7SAdrian Chadd mii_mediachg(mii);
898a043e8c7SAdrian Chadd return (0);
899a043e8c7SAdrian Chadd }
900a043e8c7SAdrian Chadd
901a043e8c7SAdrian Chadd static void
rtl8366rb_ifmedia_sts(if_t ifp,struct ifmediareq * ifmr)9022e6a8c1aSJustin Hibbits rtl8366rb_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
903a043e8c7SAdrian Chadd {
904477e3effSMichael Zhilin struct rtl8366rb_softc *sc;
905477e3effSMichael Zhilin struct mii_data *mii;
906477e3effSMichael Zhilin
9072e6a8c1aSJustin Hibbits sc = if_getsoftc(ifp);
9082e6a8c1aSJustin Hibbits mii = device_get_softc(sc->miibus[if_getdunit(ifp)]);
909a043e8c7SAdrian Chadd
910a043e8c7SAdrian Chadd mii_pollstat(mii);
911a043e8c7SAdrian Chadd ifmr->ifm_active = mii->mii_media_active;
912a043e8c7SAdrian Chadd ifmr->ifm_status = mii->mii_media_status;
913a043e8c7SAdrian Chadd }
914a043e8c7SAdrian Chadd
915a043e8c7SAdrian Chadd
916a043e8c7SAdrian Chadd static device_method_t rtl8366rb_methods[] = {
917a043e8c7SAdrian Chadd /* Device interface */
918a043e8c7SAdrian Chadd DEVMETHOD(device_identify, rtl8366rb_identify),
919a043e8c7SAdrian Chadd DEVMETHOD(device_probe, rtl8366rb_probe),
920a043e8c7SAdrian Chadd DEVMETHOD(device_attach, rtl8366rb_attach),
921a043e8c7SAdrian Chadd DEVMETHOD(device_detach, rtl8366rb_detach),
922a043e8c7SAdrian Chadd
923a043e8c7SAdrian Chadd /* bus interface */
924a043e8c7SAdrian Chadd DEVMETHOD(bus_add_child, device_add_child_ordered),
925a043e8c7SAdrian Chadd
926a043e8c7SAdrian Chadd /* MII interface */
927a043e8c7SAdrian Chadd DEVMETHOD(miibus_readreg, rtl_readphy),
928a043e8c7SAdrian Chadd DEVMETHOD(miibus_writereg, rtl_writephy),
929a043e8c7SAdrian Chadd
9305a4380b5SMichael Zhilin /* MDIO interface */
9315a4380b5SMichael Zhilin DEVMETHOD(mdio_readreg, rtl_readphy),
9325a4380b5SMichael Zhilin DEVMETHOD(mdio_writereg, rtl_writephy),
9335a4380b5SMichael Zhilin
934a043e8c7SAdrian Chadd /* etherswitch interface */
935bfae9329SLuiz Otavio O Souza DEVMETHOD(etherswitch_getconf, rtl_getconf),
936a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getinfo, rtl_getinfo),
937a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_readreg, rtl_readreg),
938a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_writereg, rtl_writereg),
939a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_readphyreg, rtl_readphy),
940a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_writephyreg, rtl_writephy),
941a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getport, rtl_getport),
942a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_setport, rtl_setport),
943a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getvgroup, rtl_getvgroup),
944a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_setvgroup, rtl_setvgroup),
945a043e8c7SAdrian Chadd
946a043e8c7SAdrian Chadd DEVMETHOD_END
947a043e8c7SAdrian Chadd };
948a043e8c7SAdrian Chadd
949a043e8c7SAdrian Chadd DEFINE_CLASS_0(rtl8366rb, rtl8366rb_driver, rtl8366rb_methods,
950a043e8c7SAdrian Chadd sizeof(struct rtl8366rb_softc));
951a043e8c7SAdrian Chadd
95242726c2fSJohn Baldwin DRIVER_MODULE(rtl8366rb, iicbus, rtl8366rb_driver, 0, 0);
9533e38757dSJohn Baldwin DRIVER_MODULE(miibus, rtl8366rb, miibus_driver, 0, 0);
9548933f7d6SJohn Baldwin DRIVER_MODULE(mdio, rtl8366rb, mdio_driver, 0, 0);
955829a13faSJohn Baldwin DRIVER_MODULE(etherswitch, rtl8366rb, etherswitch_driver, 0, 0);
956a043e8c7SAdrian Chadd MODULE_VERSION(rtl8366rb, 1);
957a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, iicbus, 1, 1, 1); /* XXX which versions? */
958a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, miibus, 1, 1, 1); /* XXX which versions? */
959a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, etherswitch, 1, 1, 1); /* XXX which versions? */
960