1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022 Adrian Chadd <adrian@FreeBSD.org>.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/param.h>
29#include <sys/bus.h>
30#include <sys/errno.h>
31#include <sys/kernel.h>
32#include <sys/malloc.h>
33#include <sys/module.h>
34#include <sys/socket.h>
35#include <sys/sockio.h>
36#include <sys/sysctl.h>
37#include <sys/systm.h>
38
39#include <net/if.h>
40#include <net/if_var.h>
41#include <net/if_arp.h>
42#include <net/ethernet.h>
43#include <net/if_dl.h>
44#include <net/if_media.h>
45#include <net/if_types.h>
46
47#include <machine/bus.h>
48#include <dev/iicbus/iic.h>
49#include <dev/iicbus/iiconf.h>
50#include <dev/iicbus/iicbus.h>
51#include <dev/mii/mii.h>
52#include <dev/mii/miivar.h>
53#include <dev/mdio/mdio.h>
54#include <dev/clk/clk.h>
55#include <dev/hwreset/hwreset.h>
56
57#include <dev/fdt/fdt_common.h>
58#include <dev/ofw/ofw_bus.h>
59#include <dev/ofw/ofw_bus_subr.h>
60
61#include <dev/etherswitch/etherswitch.h>
62
63#include <dev/etherswitch/ar40xx/ar40xx_var.h>
64#include <dev/etherswitch/ar40xx/ar40xx_reg.h>
65#include <dev/etherswitch/ar40xx/ar40xx_hw.h>
66#include <dev/etherswitch/ar40xx/ar40xx_hw_vtu.h>
67#include <dev/etherswitch/ar40xx/ar40xx_debug.h>
68
69#include "mdio_if.h"
70#include "miibus_if.h"
71#include "etherswitch_if.h"
72
73
74/*
75 * Perform a VTU (vlan table unit) operation.
76 */
77int
78ar40xx_hw_vtu_op(struct ar40xx_softc *sc, uint32_t op, uint32_t val)
79{
80	int ret;
81
82	AR40XX_DPRINTF(sc, AR40XX_DBG_VTU_OP,
83	    "%s: called; op=0x%08x, val=0x%08x\n",
84	    __func__, op, val);
85
86	ret = (ar40xx_hw_wait_bit(sc, AR40XX_REG_VTU_FUNC1,
87                            AR40XX_VTU_FUNC1_BUSY, 0));
88	if (ret != 0)
89		return (ret);
90
91	if ((op & AR40XX_VTU_FUNC1_OP) == AR40XX_VTU_FUNC1_OP_LOAD) {
92		AR40XX_REG_WRITE(sc, AR40XX_REG_VTU_FUNC0, val);
93		AR40XX_REG_BARRIER_WRITE(sc);
94	}
95
96	op |= AR40XX_VTU_FUNC1_BUSY;
97	AR40XX_REG_WRITE(sc, AR40XX_REG_VTU_FUNC1, op);
98	AR40XX_REG_BARRIER_WRITE(sc);
99
100	return (0);
101}
102
103/*
104 * Load in a VLAN table map / port configuration for the given
105 * vlan ID.
106 */
107int
108ar40xx_hw_vtu_load_vlan(struct ar40xx_softc *sc, uint32_t vid,
109    uint32_t port_mask, uint32_t untagged_mask)
110{
111
112	uint32_t op, val, mode;
113	int i, ret;
114
115	AR40XX_DPRINTF(sc, AR40XX_DBG_VTU_OP,
116	    "%s: called; vid=%d port_mask=0x%08x, untagged_mask=0x%08x\n",
117	    __func__, vid, port_mask, untagged_mask);
118
119	op = AR40XX_VTU_FUNC1_OP_LOAD | (vid << AR40XX_VTU_FUNC1_VID_S);
120	val = AR40XX_VTU_FUNC0_VALID | AR40XX_VTU_FUNC0_IVL;
121	for (i = 0; i < AR40XX_NUM_PORTS; i++) {
122		if ((port_mask & (1U << i)) == 0)
123			/* Not in the VLAN at all */
124			mode = AR40XX_VTU_FUNC0_EG_MODE_NOT;
125		else if (sc->sc_vlan.vlan == 0)
126			/* VLAN mode disabled; keep the provided VLAN tag */
127			mode = AR40XX_VTU_FUNC0_EG_MODE_KEEP;
128		else if (untagged_mask & (1U << i))
129			/* Port in the VLAN; is untagged */
130			mode = AR40XX_VTU_FUNC0_EG_MODE_UNTAG;
131		else
132			/* Port is in the VLAN; is tagged */
133			mode = AR40XX_VTU_FUNC0_EG_MODE_TAG;
134		val |= mode << AR40XX_VTU_FUNC0_EG_MODE_S(i);
135	}
136	ret = ar40xx_hw_vtu_op(sc, op, val);
137
138	return (ret);
139}
140
141/*
142 * Flush all VLAN port entries.
143 */
144int
145ar40xx_hw_vtu_flush(struct ar40xx_softc *sc)
146{
147	int ret;
148
149	AR40XX_DPRINTF(sc, AR40XX_DBG_VTU_OP, "%s: called\n", __func__);
150
151	ret = ar40xx_hw_vtu_op(sc, AR40XX_VTU_FUNC1_OP_FLUSH, 0);
152	return (ret);
153}
154
155/*
156 * Get the VLAN port map for the given vlan ID.
157 */
158int
159ar40xx_hw_vtu_get_vlan(struct ar40xx_softc *sc, int vid, uint32_t *ports,
160    uint32_t *untagged_ports)
161{
162	uint32_t op, reg, val;
163	int i, r;
164
165	op = AR40XX_VTU_FUNC1_OP_GET_ONE;
166
167	/* Filter out any etherswitch VID flags; only grab the VLAN ID */
168	vid &= ETHERSWITCH_VID_MASK;
169
170	/* XXX TODO: the VTU here stores egress mode - keep, tag, untagged, none */
171	op |= (vid << AR40XX_VTU_FUNC1_VID_S);
172	r = ar40xx_hw_vtu_op(sc, op, 0);
173	if (r != 0) {
174		device_printf(sc->sc_dev, "%s: %d: op failed\n", __func__, vid);
175		return (r);
176	}
177
178	AR40XX_REG_BARRIER_READ(sc);
179	reg = AR40XX_REG_READ(sc, AR40XX_REG_VTU_FUNC0);
180
181	*ports = 0;
182	for (i = 0; i < AR40XX_NUM_PORTS; i++) {
183		val = reg >> AR40XX_VTU_FUNC0_EG_MODE_S(i);
184		val = val & 0x3;
185		/* XXX KEEP (unmodified? For non-dot1q operation?) */
186		if (val == AR40XX_VTU_FUNC0_EG_MODE_TAG) {
187			*ports |= (1 << i);
188		} else if (val == AR40XX_VTU_FUNC0_EG_MODE_UNTAG) {
189			*ports |= (1 << i);
190			*untagged_ports |= (1 << i);
191		}
192	}
193
194	return (0);
195}
196
197